]> Git Repo - qemu.git/blame - hw/intc/xive.c
ppc/xive: introduce a simplified XIVE presenter
[qemu.git] / hw / intc / xive.c
CommitLineData
02e3ff54
CLG
1/*
2 * QEMU PowerPC XIVE interrupt controller model
3 *
4 * Copyright (c) 2017-2018, IBM Corporation.
5 *
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
8 */
9
10#include "qemu/osdep.h"
11#include "qemu/log.h"
12#include "qapi/error.h"
13#include "target/ppc/cpu.h"
14#include "sysemu/cpus.h"
15#include "sysemu/dma.h"
16#include "hw/qdev-properties.h"
17#include "monitor/monitor.h"
18#include "hw/ppc/xive.h"
207d9fe9
CLG
19#include "hw/ppc/xive_regs.h"
20
21/*
22 * XIVE Thread Interrupt Management context
23 */
24
25static uint64_t xive_tctx_accept(XiveTCTX *tctx, uint8_t ring)
26{
27 return 0;
28}
29
30static void xive_tctx_set_cppr(XiveTCTX *tctx, uint8_t ring, uint8_t cppr)
31{
32 if (cppr > XIVE_PRIORITY_MAX) {
33 cppr = 0xff;
34 }
35
36 tctx->regs[ring + TM_CPPR] = cppr;
37}
38
39/*
40 * XIVE Thread Interrupt Management Area (TIMA)
41 */
42
43/*
44 * Define an access map for each page of the TIMA that we will use in
45 * the memory region ops to filter values when doing loads and stores
46 * of raw registers values
47 *
48 * Registers accessibility bits :
49 *
50 * 0x0 - no access
51 * 0x1 - write only
52 * 0x2 - read only
53 * 0x3 - read/write
54 */
55
56static const uint8_t xive_tm_hw_view[] = {
57 /* QW-0 User */ 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0,
58 /* QW-1 OS */ 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 0, 0, 0,
59 /* QW-2 POOL */ 0, 0, 3, 3, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0,
60 /* QW-3 PHYS */ 3, 3, 3, 3, 0, 3, 0, 3, 3, 0, 0, 3, 3, 3, 3, 0,
61};
62
63static const uint8_t xive_tm_hv_view[] = {
64 /* QW-0 User */ 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0,
65 /* QW-1 OS */ 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 0, 0, 0,
66 /* QW-2 POOL */ 0, 0, 3, 3, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0,
67 /* QW-3 PHYS */ 3, 3, 3, 3, 0, 3, 0, 3, 3, 0, 0, 3, 0, 0, 0, 0,
68};
69
70static const uint8_t xive_tm_os_view[] = {
71 /* QW-0 User */ 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0,
72 /* QW-1 OS */ 2, 3, 2, 2, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0,
73 /* QW-2 POOL */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 /* QW-3 PHYS */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75};
76
77static const uint8_t xive_tm_user_view[] = {
78 /* QW-0 User */ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 /* QW-1 OS */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 /* QW-2 POOL */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 /* QW-3 PHYS */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82};
83
84/*
85 * Overall TIMA access map for the thread interrupt management context
86 * registers
87 */
88static const uint8_t *xive_tm_views[] = {
89 [XIVE_TM_HW_PAGE] = xive_tm_hw_view,
90 [XIVE_TM_HV_PAGE] = xive_tm_hv_view,
91 [XIVE_TM_OS_PAGE] = xive_tm_os_view,
92 [XIVE_TM_USER_PAGE] = xive_tm_user_view,
93};
94
95/*
96 * Computes a register access mask for a given offset in the TIMA
97 */
98static uint64_t xive_tm_mask(hwaddr offset, unsigned size, bool write)
99{
100 uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
101 uint8_t reg_offset = offset & 0x3F;
102 uint8_t reg_mask = write ? 0x1 : 0x2;
103 uint64_t mask = 0x0;
104 int i;
105
106 for (i = 0; i < size; i++) {
107 if (xive_tm_views[page_offset][reg_offset + i] & reg_mask) {
108 mask |= (uint64_t) 0xff << (8 * (size - i - 1));
109 }
110 }
111
112 return mask;
113}
114
115static void xive_tm_raw_write(XiveTCTX *tctx, hwaddr offset, uint64_t value,
116 unsigned size)
117{
118 uint8_t ring_offset = offset & 0x30;
119 uint8_t reg_offset = offset & 0x3F;
120 uint64_t mask = xive_tm_mask(offset, size, true);
121 int i;
122
123 /*
124 * Only 4 or 8 bytes stores are allowed and the User ring is
125 * excluded
126 */
127 if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
128 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA @%"
129 HWADDR_PRIx"\n", offset);
130 return;
131 }
132
133 /*
134 * Use the register offset for the raw values and filter out
135 * reserved values
136 */
137 for (i = 0; i < size; i++) {
138 uint8_t byte_mask = (mask >> (8 * (size - i - 1)));
139 if (byte_mask) {
140 tctx->regs[reg_offset + i] = (value >> (8 * (size - i - 1))) &
141 byte_mask;
142 }
143 }
144}
145
146static uint64_t xive_tm_raw_read(XiveTCTX *tctx, hwaddr offset, unsigned size)
147{
148 uint8_t ring_offset = offset & 0x30;
149 uint8_t reg_offset = offset & 0x3F;
150 uint64_t mask = xive_tm_mask(offset, size, false);
151 uint64_t ret;
152 int i;
153
154 /*
155 * Only 4 or 8 bytes loads are allowed and the User ring is
156 * excluded
157 */
158 if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
159 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access at TIMA @%"
160 HWADDR_PRIx"\n", offset);
161 return -1;
162 }
163
164 /* Use the register offset for the raw values */
165 ret = 0;
166 for (i = 0; i < size; i++) {
167 ret |= (uint64_t) tctx->regs[reg_offset + i] << (8 * (size - i - 1));
168 }
169
170 /* filter out reserved values */
171 return ret & mask;
172}
173
174/*
175 * The TM context is mapped twice within each page. Stores and loads
176 * to the first mapping below 2K write and read the specified values
177 * without modification. The second mapping above 2K performs specific
178 * state changes (side effects) in addition to setting/returning the
179 * interrupt management area context of the processor thread.
180 */
181static uint64_t xive_tm_ack_os_reg(XiveTCTX *tctx, hwaddr offset, unsigned size)
182{
183 return xive_tctx_accept(tctx, TM_QW1_OS);
184}
185
186static void xive_tm_set_os_cppr(XiveTCTX *tctx, hwaddr offset,
187 uint64_t value, unsigned size)
188{
189 xive_tctx_set_cppr(tctx, TM_QW1_OS, value & 0xff);
190}
191
192/*
193 * Define a mapping of "special" operations depending on the TIMA page
194 * offset and the size of the operation.
195 */
196typedef struct XiveTmOp {
197 uint8_t page_offset;
198 uint32_t op_offset;
199 unsigned size;
200 void (*write_handler)(XiveTCTX *tctx, hwaddr offset, uint64_t value,
201 unsigned size);
202 uint64_t (*read_handler)(XiveTCTX *tctx, hwaddr offset, unsigned size);
203} XiveTmOp;
204
205static const XiveTmOp xive_tm_operations[] = {
206 /*
207 * MMIOs below 2K : raw values and special operations without side
208 * effects
209 */
210 { XIVE_TM_OS_PAGE, TM_QW1_OS + TM_CPPR, 1, xive_tm_set_os_cppr, NULL },
211
212 /* MMIOs above 2K : special operations with side effects */
213 { XIVE_TM_OS_PAGE, TM_SPC_ACK_OS_REG, 2, NULL, xive_tm_ack_os_reg },
214};
215
216static const XiveTmOp *xive_tm_find_op(hwaddr offset, unsigned size, bool write)
217{
218 uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
219 uint32_t op_offset = offset & 0xFFF;
220 int i;
221
222 for (i = 0; i < ARRAY_SIZE(xive_tm_operations); i++) {
223 const XiveTmOp *xto = &xive_tm_operations[i];
224
225 /* Accesses done from a more privileged TIMA page is allowed */
226 if (xto->page_offset >= page_offset &&
227 xto->op_offset == op_offset &&
228 xto->size == size &&
229 ((write && xto->write_handler) || (!write && xto->read_handler))) {
230 return xto;
231 }
232 }
233 return NULL;
234}
235
236/*
237 * TIMA MMIO handlers
238 */
239static void xive_tm_write(void *opaque, hwaddr offset,
240 uint64_t value, unsigned size)
241{
242 PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
243 XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
244 const XiveTmOp *xto;
245
246 /*
247 * TODO: check V bit in Q[0-3]W2, check PTER bit associated with CPU
248 */
249
250 /*
251 * First, check for special operations in the 2K region
252 */
253 if (offset & 0x800) {
254 xto = xive_tm_find_op(offset, size, true);
255 if (!xto) {
256 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA"
257 "@%"HWADDR_PRIx"\n", offset);
258 } else {
259 xto->write_handler(tctx, offset, value, size);
260 }
261 return;
262 }
263
264 /*
265 * Then, for special operations in the region below 2K.
266 */
267 xto = xive_tm_find_op(offset, size, true);
268 if (xto) {
269 xto->write_handler(tctx, offset, value, size);
270 return;
271 }
272
273 /*
274 * Finish with raw access to the register values
275 */
276 xive_tm_raw_write(tctx, offset, value, size);
277}
278
279static uint64_t xive_tm_read(void *opaque, hwaddr offset, unsigned size)
280{
281 PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
282 XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
283 const XiveTmOp *xto;
284
285 /*
286 * TODO: check V bit in Q[0-3]W2, check PTER bit associated with CPU
287 */
288
289 /*
290 * First, check for special operations in the 2K region
291 */
292 if (offset & 0x800) {
293 xto = xive_tm_find_op(offset, size, false);
294 if (!xto) {
295 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access to TIMA"
296 "@%"HWADDR_PRIx"\n", offset);
297 return -1;
298 }
299 return xto->read_handler(tctx, offset, size);
300 }
301
302 /*
303 * Then, for special operations in the region below 2K.
304 */
305 xto = xive_tm_find_op(offset, size, false);
306 if (xto) {
307 return xto->read_handler(tctx, offset, size);
308 }
309
310 /*
311 * Finish with raw access to the register values
312 */
313 return xive_tm_raw_read(tctx, offset, size);
314}
315
316const MemoryRegionOps xive_tm_ops = {
317 .read = xive_tm_read,
318 .write = xive_tm_write,
319 .endianness = DEVICE_BIG_ENDIAN,
320 .valid = {
321 .min_access_size = 1,
322 .max_access_size = 8,
323 },
324 .impl = {
325 .min_access_size = 1,
326 .max_access_size = 8,
327 },
328};
329
330static inline uint32_t xive_tctx_word2(uint8_t *ring)
331{
332 return *((uint32_t *) &ring[TM_WORD2]);
333}
334
335static char *xive_tctx_ring_print(uint8_t *ring)
336{
337 uint32_t w2 = xive_tctx_word2(ring);
338
339 return g_strdup_printf("%02x %02x %02x %02x %02x "
340 "%02x %02x %02x %08x",
341 ring[TM_NSR], ring[TM_CPPR], ring[TM_IPB], ring[TM_LSMFB],
342 ring[TM_ACK_CNT], ring[TM_INC], ring[TM_AGE], ring[TM_PIPR],
343 be32_to_cpu(w2));
344}
345
346static const char * const xive_tctx_ring_names[] = {
347 "USER", "OS", "POOL", "PHYS",
348};
349
350void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon)
351{
352 int cpu_index = tctx->cs ? tctx->cs->cpu_index : -1;
353 int i;
354
355 monitor_printf(mon, "CPU[%04x]: QW NSR CPPR IPB LSMFB ACK# INC AGE PIPR"
356 " W2\n", cpu_index);
357
358 for (i = 0; i < XIVE_TM_RING_COUNT; i++) {
359 char *s = xive_tctx_ring_print(&tctx->regs[i * XIVE_TM_RING_SIZE]);
360 monitor_printf(mon, "CPU[%04x]: %4s %s\n", cpu_index,
361 xive_tctx_ring_names[i], s);
362 g_free(s);
363 }
364}
365
366static void xive_tctx_reset(void *dev)
367{
368 XiveTCTX *tctx = XIVE_TCTX(dev);
369
370 memset(tctx->regs, 0, sizeof(tctx->regs));
371
372 /* Set some defaults */
373 tctx->regs[TM_QW1_OS + TM_LSMFB] = 0xFF;
374 tctx->regs[TM_QW1_OS + TM_ACK_CNT] = 0xFF;
375 tctx->regs[TM_QW1_OS + TM_AGE] = 0xFF;
376}
377
378static void xive_tctx_realize(DeviceState *dev, Error **errp)
379{
380 XiveTCTX *tctx = XIVE_TCTX(dev);
381 PowerPCCPU *cpu;
382 CPUPPCState *env;
383 Object *obj;
384 Error *local_err = NULL;
385
386 obj = object_property_get_link(OBJECT(dev), "cpu", &local_err);
387 if (!obj) {
388 error_propagate(errp, local_err);
389 error_prepend(errp, "required link 'cpu' not found: ");
390 return;
391 }
392
393 cpu = POWERPC_CPU(obj);
394 tctx->cs = CPU(obj);
395
396 env = &cpu->env;
397 switch (PPC_INPUT(env)) {
398 case PPC_FLAGS_INPUT_POWER7:
399 tctx->output = env->irq_inputs[POWER7_INPUT_INT];
400 break;
401
402 default:
403 error_setg(errp, "XIVE interrupt controller does not support "
404 "this CPU bus model");
405 return;
406 }
407
408 qemu_register_reset(xive_tctx_reset, dev);
409}
410
411static void xive_tctx_unrealize(DeviceState *dev, Error **errp)
412{
413 qemu_unregister_reset(xive_tctx_reset, dev);
414}
415
416static const VMStateDescription vmstate_xive_tctx = {
417 .name = TYPE_XIVE_TCTX,
418 .version_id = 1,
419 .minimum_version_id = 1,
420 .fields = (VMStateField[]) {
421 VMSTATE_BUFFER(regs, XiveTCTX),
422 VMSTATE_END_OF_LIST()
423 },
424};
425
426static void xive_tctx_class_init(ObjectClass *klass, void *data)
427{
428 DeviceClass *dc = DEVICE_CLASS(klass);
429
430 dc->desc = "XIVE Interrupt Thread Context";
431 dc->realize = xive_tctx_realize;
432 dc->unrealize = xive_tctx_unrealize;
433 dc->vmsd = &vmstate_xive_tctx;
434}
435
436static const TypeInfo xive_tctx_info = {
437 .name = TYPE_XIVE_TCTX,
438 .parent = TYPE_DEVICE,
439 .instance_size = sizeof(XiveTCTX),
440 .class_init = xive_tctx_class_init,
441};
02e3ff54
CLG
442
443/*
444 * XIVE ESB helpers
445 */
446
447static uint8_t xive_esb_set(uint8_t *pq, uint8_t value)
448{
449 uint8_t old_pq = *pq & 0x3;
450
451 *pq &= ~0x3;
452 *pq |= value & 0x3;
453
454 return old_pq;
455}
456
457static bool xive_esb_trigger(uint8_t *pq)
458{
459 uint8_t old_pq = *pq & 0x3;
460
461 switch (old_pq) {
462 case XIVE_ESB_RESET:
463 xive_esb_set(pq, XIVE_ESB_PENDING);
464 return true;
465 case XIVE_ESB_PENDING:
466 case XIVE_ESB_QUEUED:
467 xive_esb_set(pq, XIVE_ESB_QUEUED);
468 return false;
469 case XIVE_ESB_OFF:
470 xive_esb_set(pq, XIVE_ESB_OFF);
471 return false;
472 default:
473 g_assert_not_reached();
474 }
475}
476
477static bool xive_esb_eoi(uint8_t *pq)
478{
479 uint8_t old_pq = *pq & 0x3;
480
481 switch (old_pq) {
482 case XIVE_ESB_RESET:
483 case XIVE_ESB_PENDING:
484 xive_esb_set(pq, XIVE_ESB_RESET);
485 return false;
486 case XIVE_ESB_QUEUED:
487 xive_esb_set(pq, XIVE_ESB_PENDING);
488 return true;
489 case XIVE_ESB_OFF:
490 xive_esb_set(pq, XIVE_ESB_OFF);
491 return false;
492 default:
493 g_assert_not_reached();
494 }
495}
496
497/*
498 * XIVE Interrupt Source (or IVSE)
499 */
500
501uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno)
502{
503 assert(srcno < xsrc->nr_irqs);
504
505 return xsrc->status[srcno] & 0x3;
506}
507
508uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
509{
510 assert(srcno < xsrc->nr_irqs);
511
512 return xive_esb_set(&xsrc->status[srcno], pq);
513}
514
5fd9ef18
CLG
515/*
516 * Returns whether the event notification should be forwarded.
517 */
518static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t srcno)
519{
520 uint8_t old_pq = xive_source_esb_get(xsrc, srcno);
521
522 xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
523
524 switch (old_pq) {
525 case XIVE_ESB_RESET:
526 xive_source_esb_set(xsrc, srcno, XIVE_ESB_PENDING);
527 return true;
528 default:
529 return false;
530 }
531}
532
02e3ff54
CLG
533/*
534 * Returns whether the event notification should be forwarded.
535 */
536static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
537{
5fd9ef18
CLG
538 bool ret;
539
02e3ff54
CLG
540 assert(srcno < xsrc->nr_irqs);
541
5fd9ef18
CLG
542 ret = xive_esb_trigger(&xsrc->status[srcno]);
543
544 if (xive_source_irq_is_lsi(xsrc, srcno) &&
545 xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
546 qemu_log_mask(LOG_GUEST_ERROR,
547 "XIVE: queued an event on LSI IRQ %d\n", srcno);
548 }
549
550 return ret;
02e3ff54
CLG
551}
552
553/*
554 * Returns whether the event notification should be forwarded.
555 */
556static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
557{
5fd9ef18
CLG
558 bool ret;
559
02e3ff54
CLG
560 assert(srcno < xsrc->nr_irqs);
561
5fd9ef18
CLG
562 ret = xive_esb_eoi(&xsrc->status[srcno]);
563
564 /*
565 * LSI sources do not set the Q bit but they can still be
566 * asserted, in which case we should forward a new event
567 * notification
568 */
569 if (xive_source_irq_is_lsi(xsrc, srcno) &&
570 xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
571 ret = xive_source_lsi_trigger(xsrc, srcno);
572 }
573
574 return ret;
02e3ff54
CLG
575}
576
577/*
578 * Forward the source event notification to the Router
579 */
580static void xive_source_notify(XiveSource *xsrc, int srcno)
581{
5e79b155 582 XiveNotifierClass *xnc = XIVE_NOTIFIER_GET_CLASS(xsrc->xive);
02e3ff54 583
5e79b155
CLG
584 if (xnc->notify) {
585 xnc->notify(xsrc->xive, srcno);
586 }
02e3ff54
CLG
587}
588
589/*
590 * In a two pages ESB MMIO setting, even page is the trigger page, odd
591 * page is for management
592 */
593static inline bool addr_is_even(hwaddr addr, uint32_t shift)
594{
595 return !((addr >> shift) & 1);
596}
597
598static inline bool xive_source_is_trigger_page(XiveSource *xsrc, hwaddr addr)
599{
600 return xive_source_esb_has_2page(xsrc) &&
601 addr_is_even(addr, xsrc->esb_shift - 1);
602}
603
604/*
605 * ESB MMIO loads
606 * Trigger page Management/EOI page
607 *
608 * ESB MMIO setting 2 pages 1 or 2 pages
609 *
610 * 0x000 .. 0x3FF -1 EOI and return 0|1
611 * 0x400 .. 0x7FF -1 EOI and return 0|1
612 * 0x800 .. 0xBFF -1 return PQ
613 * 0xC00 .. 0xCFF -1 return PQ and atomically PQ=00
614 * 0xD00 .. 0xDFF -1 return PQ and atomically PQ=01
615 * 0xE00 .. 0xDFF -1 return PQ and atomically PQ=10
616 * 0xF00 .. 0xDFF -1 return PQ and atomically PQ=11
617 */
618static uint64_t xive_source_esb_read(void *opaque, hwaddr addr, unsigned size)
619{
620 XiveSource *xsrc = XIVE_SOURCE(opaque);
621 uint32_t offset = addr & 0xFFF;
622 uint32_t srcno = addr >> xsrc->esb_shift;
623 uint64_t ret = -1;
624
625 /* In a two pages ESB MMIO setting, trigger page should not be read */
626 if (xive_source_is_trigger_page(xsrc, addr)) {
627 qemu_log_mask(LOG_GUEST_ERROR,
628 "XIVE: invalid load on IRQ %d trigger page at "
629 "0x%"HWADDR_PRIx"\n", srcno, addr);
630 return -1;
631 }
632
633 switch (offset) {
634 case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
635 ret = xive_source_esb_eoi(xsrc, srcno);
636
637 /* Forward the source event notification for routing */
638 if (ret) {
639 xive_source_notify(xsrc, srcno);
640 }
641 break;
642
643 case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
644 ret = xive_source_esb_get(xsrc, srcno);
645 break;
646
647 case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
648 case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
649 case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
650 case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
651 ret = xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
652 break;
653 default:
654 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB load addr %x\n",
655 offset);
656 }
657
658 return ret;
659}
660
661/*
662 * ESB MMIO stores
663 * Trigger page Management/EOI page
664 *
665 * ESB MMIO setting 2 pages 1 or 2 pages
666 *
667 * 0x000 .. 0x3FF Trigger Trigger
668 * 0x400 .. 0x7FF Trigger EOI
669 * 0x800 .. 0xBFF Trigger undefined
670 * 0xC00 .. 0xCFF Trigger PQ=00
671 * 0xD00 .. 0xDFF Trigger PQ=01
672 * 0xE00 .. 0xDFF Trigger PQ=10
673 * 0xF00 .. 0xDFF Trigger PQ=11
674 */
675static void xive_source_esb_write(void *opaque, hwaddr addr,
676 uint64_t value, unsigned size)
677{
678 XiveSource *xsrc = XIVE_SOURCE(opaque);
679 uint32_t offset = addr & 0xFFF;
680 uint32_t srcno = addr >> xsrc->esb_shift;
681 bool notify = false;
682
683 /* In a two pages ESB MMIO setting, trigger page only triggers */
684 if (xive_source_is_trigger_page(xsrc, addr)) {
685 notify = xive_source_esb_trigger(xsrc, srcno);
686 goto out;
687 }
688
689 switch (offset) {
690 case 0 ... 0x3FF:
691 notify = xive_source_esb_trigger(xsrc, srcno);
692 break;
693
694 case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
695 if (!(xsrc->esb_flags & XIVE_SRC_STORE_EOI)) {
696 qemu_log_mask(LOG_GUEST_ERROR,
697 "XIVE: invalid Store EOI for IRQ %d\n", srcno);
698 return;
699 }
700
701 notify = xive_source_esb_eoi(xsrc, srcno);
702 break;
703
704 case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
705 case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
706 case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
707 case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
708 xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
709 break;
710
711 default:
712 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr %x\n",
713 offset);
714 return;
715 }
716
717out:
718 /* Forward the source event notification for routing */
719 if (notify) {
720 xive_source_notify(xsrc, srcno);
721 }
722}
723
724static const MemoryRegionOps xive_source_esb_ops = {
725 .read = xive_source_esb_read,
726 .write = xive_source_esb_write,
727 .endianness = DEVICE_BIG_ENDIAN,
728 .valid = {
729 .min_access_size = 8,
730 .max_access_size = 8,
731 },
732 .impl = {
733 .min_access_size = 8,
734 .max_access_size = 8,
735 },
736};
737
738static void xive_source_set_irq(void *opaque, int srcno, int val)
739{
740 XiveSource *xsrc = XIVE_SOURCE(opaque);
741 bool notify = false;
742
5fd9ef18
CLG
743 if (xive_source_irq_is_lsi(xsrc, srcno)) {
744 if (val) {
745 notify = xive_source_lsi_trigger(xsrc, srcno);
746 } else {
747 xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
748 }
749 } else {
750 if (val) {
751 notify = xive_source_esb_trigger(xsrc, srcno);
752 }
02e3ff54
CLG
753 }
754
755 /* Forward the source event notification for routing */
756 if (notify) {
757 xive_source_notify(xsrc, srcno);
758 }
759}
760
761void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
762{
763 int i;
764
765 for (i = 0; i < xsrc->nr_irqs; i++) {
766 uint8_t pq = xive_source_esb_get(xsrc, i);
767
768 if (pq == XIVE_ESB_OFF) {
769 continue;
770 }
771
5fd9ef18
CLG
772 monitor_printf(mon, " %08x %s %c%c%c\n", i + offset,
773 xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
02e3ff54 774 pq & XIVE_ESB_VAL_P ? 'P' : '-',
5fd9ef18
CLG
775 pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
776 xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
02e3ff54
CLG
777 }
778}
779
780static void xive_source_reset(void *dev)
781{
782 XiveSource *xsrc = XIVE_SOURCE(dev);
783
5fd9ef18
CLG
784 /* Do not clear the LSI bitmap */
785
02e3ff54
CLG
786 /* PQs are initialized to 0b01 (Q=1) which corresponds to "ints off" */
787 memset(xsrc->status, XIVE_ESB_OFF, xsrc->nr_irqs);
788}
789
790static void xive_source_realize(DeviceState *dev, Error **errp)
791{
792 XiveSource *xsrc = XIVE_SOURCE(dev);
5e79b155
CLG
793 Object *obj;
794 Error *local_err = NULL;
795
796 obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
797 if (!obj) {
798 error_propagate(errp, local_err);
799 error_prepend(errp, "required link 'xive' not found: ");
800 return;
801 }
802
803 xsrc->xive = XIVE_NOTIFIER(obj);
02e3ff54
CLG
804
805 if (!xsrc->nr_irqs) {
806 error_setg(errp, "Number of interrupt needs to be greater than 0");
807 return;
808 }
809
810 if (xsrc->esb_shift != XIVE_ESB_4K &&
811 xsrc->esb_shift != XIVE_ESB_4K_2PAGE &&
812 xsrc->esb_shift != XIVE_ESB_64K &&
813 xsrc->esb_shift != XIVE_ESB_64K_2PAGE) {
814 error_setg(errp, "Invalid ESB shift setting");
815 return;
816 }
817
818 xsrc->status = g_malloc0(xsrc->nr_irqs);
5fd9ef18 819 xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
02e3ff54
CLG
820
821 memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
822 &xive_source_esb_ops, xsrc, "xive.esb",
823 (1ull << xsrc->esb_shift) * xsrc->nr_irqs);
824
825 xsrc->qirqs = qemu_allocate_irqs(xive_source_set_irq, xsrc,
826 xsrc->nr_irqs);
827
828 qemu_register_reset(xive_source_reset, dev);
829}
830
831static const VMStateDescription vmstate_xive_source = {
832 .name = TYPE_XIVE_SOURCE,
833 .version_id = 1,
834 .minimum_version_id = 1,
835 .fields = (VMStateField[]) {
836 VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
837 VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
838 VMSTATE_END_OF_LIST()
839 },
840};
841
842/*
843 * The default XIVE interrupt source setting for the ESB MMIOs is two
844 * 64k pages without Store EOI, to be in sync with KVM.
845 */
846static Property xive_source_properties[] = {
847 DEFINE_PROP_UINT64("flags", XiveSource, esb_flags, 0),
848 DEFINE_PROP_UINT32("nr-irqs", XiveSource, nr_irqs, 0),
849 DEFINE_PROP_UINT32("shift", XiveSource, esb_shift, XIVE_ESB_64K_2PAGE),
850 DEFINE_PROP_END_OF_LIST(),
851};
852
853static void xive_source_class_init(ObjectClass *klass, void *data)
854{
855 DeviceClass *dc = DEVICE_CLASS(klass);
856
857 dc->desc = "XIVE Interrupt Source";
858 dc->props = xive_source_properties;
859 dc->realize = xive_source_realize;
860 dc->vmsd = &vmstate_xive_source;
861}
862
863static const TypeInfo xive_source_info = {
864 .name = TYPE_XIVE_SOURCE,
865 .parent = TYPE_DEVICE,
866 .instance_size = sizeof(XiveSource),
867 .class_init = xive_source_class_init,
868};
869
e4ddaac6
CLG
870/*
871 * XiveEND helpers
872 */
873
874void xive_end_queue_pic_print_info(XiveEND *end, uint32_t width, Monitor *mon)
875{
876 uint64_t qaddr_base = (uint64_t) be32_to_cpu(end->w2 & 0x0fffffff) << 32
877 | be32_to_cpu(end->w3);
878 uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
879 uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
880 uint32_t qentries = 1 << (qsize + 10);
881 int i;
882
883 /*
884 * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
885 */
886 monitor_printf(mon, " [ ");
887 qindex = (qindex - (width - 1)) & (qentries - 1);
888 for (i = 0; i < width; i++) {
889 uint64_t qaddr = qaddr_base + (qindex << 2);
890 uint32_t qdata = -1;
891
892 if (dma_memory_read(&address_space_memory, qaddr, &qdata,
893 sizeof(qdata))) {
894 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
895 HWADDR_PRIx "\n", qaddr);
896 return;
897 }
898 monitor_printf(mon, "%s%08x ", i == width - 1 ? "^" : "",
899 be32_to_cpu(qdata));
900 qindex = (qindex + 1) & (qentries - 1);
901 }
902}
903
904void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon)
905{
906 uint64_t qaddr_base = (uint64_t) be32_to_cpu(end->w2 & 0x0fffffff) << 32
907 | be32_to_cpu(end->w3);
908 uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
909 uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
910 uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
911 uint32_t qentries = 1 << (qsize + 10);
912
913 uint32_t nvt = xive_get_field32(END_W6_NVT_INDEX, end->w6);
914 uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
915
916 if (!xive_end_is_valid(end)) {
917 return;
918 }
919
920 monitor_printf(mon, " %08x %c%c%c%c%c prio:%d nvt:%04x eq:@%08"PRIx64
921 "% 6d/%5d ^%d", end_idx,
922 xive_end_is_valid(end) ? 'v' : '-',
923 xive_end_is_enqueue(end) ? 'q' : '-',
924 xive_end_is_notify(end) ? 'n' : '-',
925 xive_end_is_backlog(end) ? 'b' : '-',
926 xive_end_is_escalate(end) ? 'e' : '-',
927 priority, nvt, qaddr_base, qindex, qentries, qgen);
928
929 xive_end_queue_pic_print_info(end, 6, mon);
930 monitor_printf(mon, "]\n");
931}
932
933static void xive_end_enqueue(XiveEND *end, uint32_t data)
934{
935 uint64_t qaddr_base = (uint64_t) be32_to_cpu(end->w2 & 0x0fffffff) << 32
936 | be32_to_cpu(end->w3);
937 uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
938 uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
939 uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
940
941 uint64_t qaddr = qaddr_base + (qindex << 2);
942 uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
943 uint32_t qentries = 1 << (qsize + 10);
944
945 if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata))) {
946 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
947 HWADDR_PRIx "\n", qaddr);
948 return;
949 }
950
951 qindex = (qindex + 1) & (qentries - 1);
952 if (qindex == 0) {
953 qgen ^= 1;
954 end->w1 = xive_set_field32(END_W1_GENERATION, end->w1, qgen);
955 }
956 end->w1 = xive_set_field32(END_W1_PAGE_OFF, end->w1, qindex);
957}
958
7ff7ea92
CLG
959/*
960 * XIVE Router (aka. Virtualization Controller or IVRE)
961 */
962
963int xive_router_get_eas(XiveRouter *xrtr, uint8_t eas_blk, uint32_t eas_idx,
964 XiveEAS *eas)
965{
966 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
967
968 return xrc->get_eas(xrtr, eas_blk, eas_idx, eas);
969}
970
e4ddaac6
CLG
971int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
972 XiveEND *end)
973{
974 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
975
976 return xrc->get_end(xrtr, end_blk, end_idx, end);
977}
978
979int xive_router_write_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
980 XiveEND *end, uint8_t word_number)
981{
982 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
983
984 return xrc->write_end(xrtr, end_blk, end_idx, end, word_number);
985}
986
af53dbf6
CLG
987int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
988 XiveNVT *nvt)
989{
990 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
991
992 return xrc->get_nvt(xrtr, nvt_blk, nvt_idx, nvt);
993}
994
995int xive_router_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
996 XiveNVT *nvt, uint8_t word_number)
997{
998 XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
999
1000 return xrc->write_nvt(xrtr, nvt_blk, nvt_idx, nvt, word_number);
1001}
1002
1003/*
1004 * The thread context register words are in big-endian format.
1005 */
1006static int xive_presenter_tctx_match(XiveTCTX *tctx, uint8_t format,
1007 uint8_t nvt_blk, uint32_t nvt_idx,
1008 bool cam_ignore, uint32_t logic_serv)
1009{
1010 uint32_t cam = xive_nvt_cam_line(nvt_blk, nvt_idx);
1011 uint32_t qw2w2 = xive_tctx_word2(&tctx->regs[TM_QW2_HV_POOL]);
1012 uint32_t qw1w2 = xive_tctx_word2(&tctx->regs[TM_QW1_OS]);
1013 uint32_t qw0w2 = xive_tctx_word2(&tctx->regs[TM_QW0_USER]);
1014
1015 /*
1016 * TODO (PowerNV): ignore mode. The low order bits of the NVT
1017 * identifier are ignored in the "CAM" match.
1018 */
1019
1020 if (format == 0) {
1021 if (cam_ignore == true) {
1022 /*
1023 * F=0 & i=1: Logical server notification (bits ignored at
1024 * the end of the NVT identifier)
1025 */
1026 qemu_log_mask(LOG_UNIMP, "XIVE: no support for LS NVT %x/%x\n",
1027 nvt_blk, nvt_idx);
1028 return -1;
1029 }
1030
1031 /* F=0 & i=0: Specific NVT notification */
1032
1033 /* TODO (PowerNV) : PHYS ring */
1034
1035 /* HV POOL ring */
1036 if ((be32_to_cpu(qw2w2) & TM_QW2W2_VP) &&
1037 cam == xive_get_field32(TM_QW2W2_POOL_CAM, qw2w2)) {
1038 return TM_QW2_HV_POOL;
1039 }
1040
1041 /* OS ring */
1042 if ((be32_to_cpu(qw1w2) & TM_QW1W2_VO) &&
1043 cam == xive_get_field32(TM_QW1W2_OS_CAM, qw1w2)) {
1044 return TM_QW1_OS;
1045 }
1046 } else {
1047 /* F=1 : User level Event-Based Branch (EBB) notification */
1048
1049 /* USER ring */
1050 if ((be32_to_cpu(qw1w2) & TM_QW1W2_VO) &&
1051 (cam == xive_get_field32(TM_QW1W2_OS_CAM, qw1w2)) &&
1052 (be32_to_cpu(qw0w2) & TM_QW0W2_VU) &&
1053 (logic_serv == xive_get_field32(TM_QW0W2_LOGIC_SERV, qw0w2))) {
1054 return TM_QW0_USER;
1055 }
1056 }
1057 return -1;
1058}
1059
1060typedef struct XiveTCTXMatch {
1061 XiveTCTX *tctx;
1062 uint8_t ring;
1063} XiveTCTXMatch;
1064
1065static bool xive_presenter_match(XiveRouter *xrtr, uint8_t format,
1066 uint8_t nvt_blk, uint32_t nvt_idx,
1067 bool cam_ignore, uint8_t priority,
1068 uint32_t logic_serv, XiveTCTXMatch *match)
1069{
1070 CPUState *cs;
1071
1072 /*
1073 * TODO (PowerNV): handle chip_id overwrite of block field for
1074 * hardwired CAM compares
1075 */
1076
1077 CPU_FOREACH(cs) {
1078 PowerPCCPU *cpu = POWERPC_CPU(cs);
1079 XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
1080 int ring;
1081
1082 /*
1083 * HW checks that the CPU is enabled in the Physical Thread
1084 * Enable Register (PTER).
1085 */
1086
1087 /*
1088 * Check the thread context CAM lines and record matches. We
1089 * will handle CPU exception delivery later
1090 */
1091 ring = xive_presenter_tctx_match(tctx, format, nvt_blk, nvt_idx,
1092 cam_ignore, logic_serv);
1093 /*
1094 * Save the context and follow on to catch duplicates, that we
1095 * don't support yet.
1096 */
1097 if (ring != -1) {
1098 if (match->tctx) {
1099 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: already found a thread "
1100 "context NVT %x/%x\n", nvt_blk, nvt_idx);
1101 return false;
1102 }
1103
1104 match->ring = ring;
1105 match->tctx = tctx;
1106 }
1107 }
1108
1109 if (!match->tctx) {
1110 qemu_log_mask(LOG_UNIMP, "XIVE: NVT %x/%x is not dispatched\n",
1111 nvt_blk, nvt_idx);
1112 return false;
1113 }
1114
1115 return true;
1116}
1117
1118/*
1119 * This is our simple Xive Presenter Engine model. It is merged in the
1120 * Router as it does not require an extra object.
1121 *
1122 * It receives notification requests sent by the IVRE to find one
1123 * matching NVT (or more) dispatched on the processor threads. In case
1124 * of a single NVT notification, the process is abreviated and the
1125 * thread is signaled if a match is found. In case of a logical server
1126 * notification (bits ignored at the end of the NVT identifier), the
1127 * IVPE and IVRE select a winning thread using different filters. This
1128 * involves 2 or 3 exchanges on the PowerBus that the model does not
1129 * support.
1130 *
1131 * The parameters represent what is sent on the PowerBus
1132 */
1133static void xive_presenter_notify(XiveRouter *xrtr, uint8_t format,
1134 uint8_t nvt_blk, uint32_t nvt_idx,
1135 bool cam_ignore, uint8_t priority,
1136 uint32_t logic_serv)
1137{
1138 XiveNVT nvt;
1139 XiveTCTXMatch match = { .tctx = NULL, .ring = 0 };
1140 bool found;
1141
1142 /* NVT cache lookup */
1143 if (xive_router_get_nvt(xrtr, nvt_blk, nvt_idx, &nvt)) {
1144 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no NVT %x/%x\n",
1145 nvt_blk, nvt_idx);
1146 return;
1147 }
1148
1149 if (!xive_nvt_is_valid(&nvt)) {
1150 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is invalid\n",
1151 nvt_blk, nvt_idx);
1152 return;
1153 }
1154
1155 found = xive_presenter_match(xrtr, format, nvt_blk, nvt_idx, cam_ignore,
1156 priority, logic_serv, &match);
1157 if (found) {
1158 return;
1159 }
1160
1161 /*
1162 * If no matching NVT is dispatched on a HW thread :
1163 * - update the NVT structure if backlog is activated
1164 * - escalate (ESe PQ bits and EAS in w4-5) if escalation is
1165 * activated
1166 */
1167}
1168
e4ddaac6
CLG
1169/*
1170 * An END trigger can come from an event trigger (IPI or HW) or from
1171 * another chip. We don't model the PowerBus but the END trigger
1172 * message has the same parameters than in the function below.
1173 */
1174static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
1175 uint32_t end_idx, uint32_t end_data)
1176{
1177 XiveEND end;
1178 uint8_t priority;
1179 uint8_t format;
1180
1181 /* END cache lookup */
1182 if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
1183 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
1184 end_idx);
1185 return;
1186 }
1187
1188 if (!xive_end_is_valid(&end)) {
1189 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
1190 end_blk, end_idx);
1191 return;
1192 }
1193
1194 if (xive_end_is_enqueue(&end)) {
1195 xive_end_enqueue(&end, end_data);
1196 /* Enqueuing event data modifies the EQ toggle and index */
1197 xive_router_write_end(xrtr, end_blk, end_idx, &end, 1);
1198 }
1199
1200 /*
1201 * The W7 format depends on the F bit in W6. It defines the type
1202 * of the notification :
1203 *
1204 * F=0 : single or multiple NVT notification
1205 * F=1 : User level Event-Based Branch (EBB) notification, no
1206 * priority
1207 */
1208 format = xive_get_field32(END_W6_FORMAT_BIT, end.w6);
1209 priority = xive_get_field32(END_W7_F0_PRIORITY, end.w7);
1210
1211 /* The END is masked */
1212 if (format == 0 && priority == 0xff) {
1213 return;
1214 }
1215
1216 /*
1217 * Check the END ESn (Event State Buffer for notification) for
1218 * even futher coalescing in the Router
1219 */
1220 if (!xive_end_is_notify(&end)) {
002686be
CLG
1221 uint8_t pq = xive_get_field32(END_W1_ESn, end.w1);
1222 bool notify = xive_esb_trigger(&pq);
1223
1224 if (pq != xive_get_field32(END_W1_ESn, end.w1)) {
1225 end.w1 = xive_set_field32(END_W1_ESn, end.w1, pq);
1226 xive_router_write_end(xrtr, end_blk, end_idx, &end, 1);
1227 }
1228
1229 /* ESn[Q]=1 : end of notification */
1230 if (!notify) {
1231 return;
1232 }
e4ddaac6
CLG
1233 }
1234
1235 /*
1236 * Follows IVPE notification
1237 */
af53dbf6
CLG
1238 xive_presenter_notify(xrtr, format,
1239 xive_get_field32(END_W6_NVT_BLOCK, end.w6),
1240 xive_get_field32(END_W6_NVT_INDEX, end.w6),
1241 xive_get_field32(END_W7_F0_IGNORE, end.w7),
1242 priority,
1243 xive_get_field32(END_W7_F1_LOG_SERVER_ID, end.w7));
1244
1245 /* TODO: Auto EOI. */
e4ddaac6
CLG
1246}
1247
7ff7ea92
CLG
1248static void xive_router_notify(XiveNotifier *xn, uint32_t lisn)
1249{
1250 XiveRouter *xrtr = XIVE_ROUTER(xn);
1251 uint8_t eas_blk = XIVE_SRCNO_BLOCK(lisn);
1252 uint32_t eas_idx = XIVE_SRCNO_INDEX(lisn);
1253 XiveEAS eas;
1254
1255 /* EAS cache lookup */
1256 if (xive_router_get_eas(xrtr, eas_blk, eas_idx, &eas)) {
1257 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
1258 return;
1259 }
1260
1261 /*
1262 * The IVRE checks the State Bit Cache at this point. We skip the
1263 * SBC lookup because the state bits of the sources are modeled
1264 * internally in QEMU.
1265 */
1266
1267 if (!xive_eas_is_valid(&eas)) {
1268 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid LISN %x\n", lisn);
1269 return;
1270 }
1271
1272 if (xive_eas_is_masked(&eas)) {
1273 /* Notification completed */
1274 return;
1275 }
e4ddaac6
CLG
1276
1277 /*
1278 * The event trigger becomes an END trigger
1279 */
1280 xive_router_end_notify(xrtr,
1281 xive_get_field64(EAS_END_BLOCK, eas.w),
1282 xive_get_field64(EAS_END_INDEX, eas.w),
1283 xive_get_field64(EAS_END_DATA, eas.w));
7ff7ea92
CLG
1284}
1285
1286static void xive_router_class_init(ObjectClass *klass, void *data)
1287{
1288 DeviceClass *dc = DEVICE_CLASS(klass);
1289 XiveNotifierClass *xnc = XIVE_NOTIFIER_CLASS(klass);
1290
1291 dc->desc = "XIVE Router Engine";
1292 xnc->notify = xive_router_notify;
1293}
1294
1295static const TypeInfo xive_router_info = {
1296 .name = TYPE_XIVE_ROUTER,
1297 .parent = TYPE_SYS_BUS_DEVICE,
1298 .abstract = true,
1299 .class_size = sizeof(XiveRouterClass),
1300 .class_init = xive_router_class_init,
1301 .interfaces = (InterfaceInfo[]) {
1302 { TYPE_XIVE_NOTIFIER },
1303 { }
1304 }
1305};
1306
1307void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
1308{
1309 if (!xive_eas_is_valid(eas)) {
1310 return;
1311 }
1312
1313 monitor_printf(mon, " %08x %s end:%02x/%04x data:%08x\n",
1314 lisn, xive_eas_is_masked(eas) ? "M" : " ",
1315 (uint8_t) xive_get_field64(EAS_END_BLOCK, eas->w),
1316 (uint32_t) xive_get_field64(EAS_END_INDEX, eas->w),
1317 (uint32_t) xive_get_field64(EAS_END_DATA, eas->w));
1318}
1319
002686be
CLG
1320/*
1321 * END ESB MMIO loads
1322 */
1323static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
1324{
1325 XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
1326 uint32_t offset = addr & 0xFFF;
1327 uint8_t end_blk;
1328 uint32_t end_idx;
1329 XiveEND end;
1330 uint32_t end_esmask;
1331 uint8_t pq;
1332 uint64_t ret = -1;
1333
1334 end_blk = xsrc->block_id;
1335 end_idx = addr >> (xsrc->esb_shift + 1);
1336
1337 if (xive_router_get_end(xsrc->xrtr, end_blk, end_idx, &end)) {
1338 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
1339 end_idx);
1340 return -1;
1341 }
1342
1343 if (!xive_end_is_valid(&end)) {
1344 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
1345 end_blk, end_idx);
1346 return -1;
1347 }
1348
1349 end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END_W1_ESn : END_W1_ESe;
1350 pq = xive_get_field32(end_esmask, end.w1);
1351
1352 switch (offset) {
1353 case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
1354 ret = xive_esb_eoi(&pq);
1355
1356 /* Forward the source event notification for routing ?? */
1357 break;
1358
1359 case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
1360 ret = pq;
1361 break;
1362
1363 case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
1364 case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
1365 case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
1366 case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
1367 ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
1368 break;
1369 default:
1370 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
1371 offset);
1372 return -1;
1373 }
1374
1375 if (pq != xive_get_field32(end_esmask, end.w1)) {
1376 end.w1 = xive_set_field32(end_esmask, end.w1, pq);
1377 xive_router_write_end(xsrc->xrtr, end_blk, end_idx, &end, 1);
1378 }
1379
1380 return ret;
1381}
1382
1383/*
1384 * END ESB MMIO stores are invalid
1385 */
1386static void xive_end_source_write(void *opaque, hwaddr addr,
1387 uint64_t value, unsigned size)
1388{
1389 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr 0x%"
1390 HWADDR_PRIx"\n", addr);
1391}
1392
1393static const MemoryRegionOps xive_end_source_ops = {
1394 .read = xive_end_source_read,
1395 .write = xive_end_source_write,
1396 .endianness = DEVICE_BIG_ENDIAN,
1397 .valid = {
1398 .min_access_size = 8,
1399 .max_access_size = 8,
1400 },
1401 .impl = {
1402 .min_access_size = 8,
1403 .max_access_size = 8,
1404 },
1405};
1406
1407static void xive_end_source_realize(DeviceState *dev, Error **errp)
1408{
1409 XiveENDSource *xsrc = XIVE_END_SOURCE(dev);
1410 Object *obj;
1411 Error *local_err = NULL;
1412
1413 obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
1414 if (!obj) {
1415 error_propagate(errp, local_err);
1416 error_prepend(errp, "required link 'xive' not found: ");
1417 return;
1418 }
1419
1420 xsrc->xrtr = XIVE_ROUTER(obj);
1421
1422 if (!xsrc->nr_ends) {
1423 error_setg(errp, "Number of interrupt needs to be greater than 0");
1424 return;
1425 }
1426
1427 if (xsrc->esb_shift != XIVE_ESB_4K &&
1428 xsrc->esb_shift != XIVE_ESB_64K) {
1429 error_setg(errp, "Invalid ESB shift setting");
1430 return;
1431 }
1432
1433 /*
1434 * Each END is assigned an even/odd pair of MMIO pages, the even page
1435 * manages the ESn field while the odd page manages the ESe field.
1436 */
1437 memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
1438 &xive_end_source_ops, xsrc, "xive.end",
1439 (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
1440}
1441
1442static Property xive_end_source_properties[] = {
1443 DEFINE_PROP_UINT8("block-id", XiveENDSource, block_id, 0),
1444 DEFINE_PROP_UINT32("nr-ends", XiveENDSource, nr_ends, 0),
1445 DEFINE_PROP_UINT32("shift", XiveENDSource, esb_shift, XIVE_ESB_64K),
1446 DEFINE_PROP_END_OF_LIST(),
1447};
1448
1449static void xive_end_source_class_init(ObjectClass *klass, void *data)
1450{
1451 DeviceClass *dc = DEVICE_CLASS(klass);
1452
1453 dc->desc = "XIVE END Source";
1454 dc->props = xive_end_source_properties;
1455 dc->realize = xive_end_source_realize;
1456}
1457
1458static const TypeInfo xive_end_source_info = {
1459 .name = TYPE_XIVE_END_SOURCE,
1460 .parent = TYPE_DEVICE,
1461 .instance_size = sizeof(XiveENDSource),
1462 .class_init = xive_end_source_class_init,
1463};
1464
5e79b155
CLG
1465/*
1466 * XIVE Fabric
1467 */
1468static const TypeInfo xive_fabric_info = {
1469 .name = TYPE_XIVE_NOTIFIER,
1470 .parent = TYPE_INTERFACE,
1471 .class_size = sizeof(XiveNotifierClass),
1472};
1473
02e3ff54
CLG
1474static void xive_register_types(void)
1475{
1476 type_register_static(&xive_source_info);
5e79b155 1477 type_register_static(&xive_fabric_info);
7ff7ea92 1478 type_register_static(&xive_router_info);
002686be 1479 type_register_static(&xive_end_source_info);
207d9fe9 1480 type_register_static(&xive_tctx_info);
02e3ff54
CLG
1481}
1482
1483type_init(xive_register_types)
This page took 0.178084 seconds and 4 git commands to generate.