2 * QEMU PowerPC XIVE2 interrupt controller model (POWER10)
4 * Copyright (c) 2019-2022, IBM Corporation..
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
12 #include "qemu/module.h"
13 #include "qapi/error.h"
14 #include "target/ppc/cpu.h"
15 #include "sysemu/cpus.h"
16 #include "sysemu/dma.h"
17 #include "hw/qdev-properties.h"
18 #include "monitor/monitor.h"
19 #include "hw/ppc/xive.h"
20 #include "hw/ppc/xive2.h"
21 #include "hw/ppc/xive2_regs.h"
23 void xive2_eas_pic_print_info(Xive2Eas
*eas
, uint32_t lisn
, Monitor
*mon
)
25 if (!xive2_eas_is_valid(eas
)) {
29 monitor_printf(mon
, " %08x %s end:%02x/%04x data:%08x\n",
30 lisn
, xive2_eas_is_masked(eas
) ? "M" : " ",
31 (uint8_t) xive_get_field64(EAS2_END_BLOCK
, eas
->w
),
32 (uint32_t) xive_get_field64(EAS2_END_INDEX
, eas
->w
),
33 (uint32_t) xive_get_field64(EAS2_END_DATA
, eas
->w
));
36 void xive2_end_queue_pic_print_info(Xive2End
*end
, uint32_t width
,
39 uint64_t qaddr_base
= xive2_end_qaddr(end
);
40 uint32_t qsize
= xive_get_field32(END2_W3_QSIZE
, end
->w3
);
41 uint32_t qindex
= xive_get_field32(END2_W1_PAGE_OFF
, end
->w1
);
42 uint32_t qentries
= 1 << (qsize
+ 10);
46 * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
48 monitor_printf(mon
, " [ ");
49 qindex
= (qindex
- (width
- 1)) & (qentries
- 1);
50 for (i
= 0; i
< width
; i
++) {
51 uint64_t qaddr
= qaddr_base
+ (qindex
<< 2);
54 if (dma_memory_read(&address_space_memory
, qaddr
, &qdata
,
55 sizeof(qdata
), MEMTXATTRS_UNSPECIFIED
)) {
56 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: failed to read EQ @0x%"
57 HWADDR_PRIx
"\n", qaddr
);
60 monitor_printf(mon
, "%s%08x ", i
== width
- 1 ? "^" : "",
62 qindex
= (qindex
+ 1) & (qentries
- 1);
64 monitor_printf(mon
, "]");
67 void xive2_end_pic_print_info(Xive2End
*end
, uint32_t end_idx
, Monitor
*mon
)
69 uint64_t qaddr_base
= xive2_end_qaddr(end
);
70 uint32_t qindex
= xive_get_field32(END2_W1_PAGE_OFF
, end
->w1
);
71 uint32_t qgen
= xive_get_field32(END2_W1_GENERATION
, end
->w1
);
72 uint32_t qsize
= xive_get_field32(END2_W3_QSIZE
, end
->w3
);
73 uint32_t qentries
= 1 << (qsize
+ 10);
75 uint32_t nvp_blk
= xive_get_field32(END2_W6_VP_BLOCK
, end
->w6
);
76 uint32_t nvp_idx
= xive_get_field32(END2_W6_VP_OFFSET
, end
->w6
);
77 uint8_t priority
= xive_get_field32(END2_W7_F0_PRIORITY
, end
->w7
);
80 if (!xive2_end_is_valid(end
)) {
84 pq
= xive_get_field32(END2_W1_ESn
, end
->w1
);
87 " %08x %c%c %c%c%c%c%c%c%c%c%c%c prio:%d nvp:%02x/%04x",
89 pq
& XIVE_ESB_VAL_P
? 'P' : '-',
90 pq
& XIVE_ESB_VAL_Q
? 'Q' : '-',
91 xive2_end_is_valid(end
) ? 'v' : '-',
92 xive2_end_is_enqueue(end
) ? 'q' : '-',
93 xive2_end_is_notify(end
) ? 'n' : '-',
94 xive2_end_is_backlog(end
) ? 'b' : '-',
95 xive2_end_is_escalate(end
) ? 'e' : '-',
96 xive2_end_is_escalate_end(end
) ? 'N' : '-',
97 xive2_end_is_uncond_escalation(end
) ? 'u' : '-',
98 xive2_end_is_silent_escalation(end
) ? 's' : '-',
99 xive2_end_is_firmware1(end
) ? 'f' : '-',
100 xive2_end_is_firmware2(end
) ? 'F' : '-',
101 priority
, nvp_blk
, nvp_idx
);
104 monitor_printf(mon
, " eq:@%08"PRIx64
"% 6d/%5d ^%d",
105 qaddr_base
, qindex
, qentries
, qgen
);
106 xive2_end_queue_pic_print_info(end
, 6, mon
);
108 monitor_printf(mon
, "\n");
111 void xive2_end_eas_pic_print_info(Xive2End
*end
, uint32_t end_idx
,
114 Xive2Eas
*eas
= (Xive2Eas
*) &end
->w4
;
117 if (!xive2_end_is_escalate(end
)) {
121 pq
= xive_get_field32(END2_W1_ESe
, end
->w1
);
123 monitor_printf(mon
, " %08x %c%c %c%c end:%02x/%04x data:%08x\n",
125 pq
& XIVE_ESB_VAL_P
? 'P' : '-',
126 pq
& XIVE_ESB_VAL_Q
? 'Q' : '-',
127 xive2_eas_is_valid(eas
) ? 'v' : ' ',
128 xive2_eas_is_masked(eas
) ? 'M' : ' ',
129 (uint8_t) xive_get_field64(EAS2_END_BLOCK
, eas
->w
),
130 (uint32_t) xive_get_field64(EAS2_END_INDEX
, eas
->w
),
131 (uint32_t) xive_get_field64(EAS2_END_DATA
, eas
->w
));
134 static void xive2_end_enqueue(Xive2End
*end
, uint32_t data
)
136 uint64_t qaddr_base
= xive2_end_qaddr(end
);
137 uint32_t qsize
= xive_get_field32(END2_W3_QSIZE
, end
->w3
);
138 uint32_t qindex
= xive_get_field32(END2_W1_PAGE_OFF
, end
->w1
);
139 uint32_t qgen
= xive_get_field32(END2_W1_GENERATION
, end
->w1
);
141 uint64_t qaddr
= qaddr_base
+ (qindex
<< 2);
142 uint32_t qdata
= cpu_to_be32((qgen
<< 31) | (data
& 0x7fffffff));
143 uint32_t qentries
= 1 << (qsize
+ 10);
145 if (dma_memory_write(&address_space_memory
, qaddr
, &qdata
, sizeof(qdata
),
146 MEMTXATTRS_UNSPECIFIED
)) {
147 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: failed to write END data @0x%"
148 HWADDR_PRIx
"\n", qaddr
);
152 qindex
= (qindex
+ 1) & (qentries
- 1);
155 end
->w1
= xive_set_field32(END2_W1_GENERATION
, end
->w1
, qgen
);
157 /* TODO(PowerNV): reset GF bit on a cache watch operation */
158 end
->w1
= xive_set_field32(END2_W1_GEN_FLIPPED
, end
->w1
, qgen
);
160 end
->w1
= xive_set_field32(END2_W1_PAGE_OFF
, end
->w1
, qindex
);
163 * XIVE Router (aka. Virtualization Controller or IVRE)
166 int xive2_router_get_eas(Xive2Router
*xrtr
, uint8_t eas_blk
, uint32_t eas_idx
,
169 Xive2RouterClass
*xrc
= XIVE2_ROUTER_GET_CLASS(xrtr
);
171 return xrc
->get_eas(xrtr
, eas_blk
, eas_idx
, eas
);
174 int xive2_router_get_end(Xive2Router
*xrtr
, uint8_t end_blk
, uint32_t end_idx
,
177 Xive2RouterClass
*xrc
= XIVE2_ROUTER_GET_CLASS(xrtr
);
179 return xrc
->get_end(xrtr
, end_blk
, end_idx
, end
);
182 int xive2_router_write_end(Xive2Router
*xrtr
, uint8_t end_blk
, uint32_t end_idx
,
183 Xive2End
*end
, uint8_t word_number
)
185 Xive2RouterClass
*xrc
= XIVE2_ROUTER_GET_CLASS(xrtr
);
187 return xrc
->write_end(xrtr
, end_blk
, end_idx
, end
, word_number
);
190 int xive2_router_get_nvp(Xive2Router
*xrtr
, uint8_t nvp_blk
, uint32_t nvp_idx
,
193 Xive2RouterClass
*xrc
= XIVE2_ROUTER_GET_CLASS(xrtr
);
195 return xrc
->get_nvp(xrtr
, nvp_blk
, nvp_idx
, nvp
);
198 int xive2_router_write_nvp(Xive2Router
*xrtr
, uint8_t nvp_blk
, uint32_t nvp_idx
,
199 Xive2Nvp
*nvp
, uint8_t word_number
)
201 Xive2RouterClass
*xrc
= XIVE2_ROUTER_GET_CLASS(xrtr
);
203 return xrc
->write_nvp(xrtr
, nvp_blk
, nvp_idx
, nvp
, word_number
);
206 static int xive2_router_get_block_id(Xive2Router
*xrtr
)
208 Xive2RouterClass
*xrc
= XIVE2_ROUTER_GET_CLASS(xrtr
);
210 return xrc
->get_block_id(xrtr
);
213 static void xive2_router_realize(DeviceState
*dev
, Error
**errp
)
215 Xive2Router
*xrtr
= XIVE2_ROUTER(dev
);
221 * Notification using the END ESe/ESn bit (Event State Buffer for
222 * escalation and notification). Profide futher coalescing in the
225 static bool xive2_router_end_es_notify(Xive2Router
*xrtr
, uint8_t end_blk
,
226 uint32_t end_idx
, Xive2End
*end
,
229 uint8_t pq
= xive_get_field32(end_esmask
, end
->w1
);
230 bool notify
= xive_esb_trigger(&pq
);
232 if (pq
!= xive_get_field32(end_esmask
, end
->w1
)) {
233 end
->w1
= xive_set_field32(end_esmask
, end
->w1
, pq
);
234 xive2_router_write_end(xrtr
, end_blk
, end_idx
, end
, 1);
237 /* ESe/n[Q]=1 : end of notification */
242 * An END trigger can come from an event trigger (IPI or HW) or from
243 * another chip. We don't model the PowerBus but the END trigger
244 * message has the same parameters than in the function below.
246 static void xive2_router_end_notify(Xive2Router
*xrtr
, uint8_t end_blk
,
247 uint32_t end_idx
, uint32_t end_data
)
257 /* END cache lookup */
258 if (xive2_router_get_end(xrtr
, end_blk
, end_idx
, &end
)) {
259 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: No END %x/%x\n", end_blk
,
264 if (!xive2_end_is_valid(&end
)) {
265 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: END %x/%x is invalid\n",
270 if (xive2_end_is_enqueue(&end
)) {
271 xive2_end_enqueue(&end
, end_data
);
272 /* Enqueuing event data modifies the EQ toggle and index */
273 xive2_router_write_end(xrtr
, end_blk
, end_idx
, &end
, 1);
277 * When the END is silent, we skip the notification part.
279 if (xive2_end_is_silent_escalation(&end
)) {
284 * The W7 format depends on the F bit in W6. It defines the type
285 * of the notification :
287 * F=0 : single or multiple NVP notification
288 * F=1 : User level Event-Based Branch (EBB) notification, no
291 format
= xive_get_field32(END2_W6_FORMAT_BIT
, end
.w6
);
292 priority
= xive_get_field32(END2_W7_F0_PRIORITY
, end
.w7
);
294 /* The END is masked */
295 if (format
== 0 && priority
== 0xff) {
300 * Check the END ESn (Event State Buffer for notification) for
301 * even futher coalescing in the Router
303 if (!xive2_end_is_notify(&end
)) {
304 /* ESn[Q]=1 : end of notification */
305 if (!xive2_router_end_es_notify(xrtr
, end_blk
, end_idx
,
306 &end
, END2_W1_ESn
)) {
312 * Follows IVPE notification
314 nvp_blk
= xive_get_field32(END2_W6_VP_BLOCK
, end
.w6
);
315 nvp_idx
= xive_get_field32(END2_W6_VP_OFFSET
, end
.w6
);
317 /* NVP cache lookup */
318 if (xive2_router_get_nvp(xrtr
, nvp_blk
, nvp_idx
, &nvp
)) {
319 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: no NVP %x/%x\n",
324 if (!xive2_nvp_is_valid(&nvp
)) {
325 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: NVP %x/%x is invalid\n",
330 found
= xive_presenter_notify(xrtr
->xfb
, format
, nvp_blk
, nvp_idx
,
331 xive_get_field32(END2_W6_IGNORE
, end
.w7
),
333 xive_get_field32(END2_W7_F1_LOG_SERVER_ID
, end
.w7
));
335 /* TODO: Auto EOI. */
342 * If no matching NVP is dispatched on a HW thread :
343 * - specific VP: update the NVP structure if backlog is activated
344 * - logical server : forward request to IVPE (not supported)
346 if (xive2_end_is_backlog(&end
)) {
350 qemu_log_mask(LOG_GUEST_ERROR
,
351 "XIVE: END %x/%x invalid config: F1 & backlog\n",
357 * Record the IPB in the associated NVP structure for later
358 * use. The presenter will resend the interrupt when the vCPU
359 * is dispatched again on a HW thread.
361 ipb
= xive_get_field32(NVP2_W2_IPB
, nvp
.w2
) |
362 xive_priority_to_ipb(priority
);
363 nvp
.w2
= xive_set_field32(NVP2_W2_IPB
, nvp
.w2
, ipb
);
364 xive2_router_write_nvp(xrtr
, nvp_blk
, nvp_idx
, &nvp
, 2);
367 * On HW, follows a "Broadcast Backlog" to IVPEs
373 * If activated, escalate notification using the ESe PQ bits and
376 if (!xive2_end_is_escalate(&end
)) {
381 * Check the END ESe (Event State Buffer for escalation) for even
382 * futher coalescing in the Router
384 if (!xive2_end_is_uncond_escalation(&end
)) {
385 /* ESe[Q]=1 : end of escalation notification */
386 if (!xive2_router_end_es_notify(xrtr
, end_blk
, end_idx
,
387 &end
, END2_W1_ESe
)) {
393 * The END trigger becomes an Escalation trigger
395 xive2_router_end_notify(xrtr
,
396 xive_get_field32(END2_W4_END_BLOCK
, end
.w4
),
397 xive_get_field32(END2_W4_ESC_END_INDEX
, end
.w4
),
398 xive_get_field32(END2_W5_ESC_END_DATA
, end
.w5
));
401 void xive2_router_notify(XiveNotifier
*xn
, uint32_t lisn
)
403 Xive2Router
*xrtr
= XIVE2_ROUTER(xn
);
404 uint8_t eas_blk
= XIVE_EAS_BLOCK(lisn
);
405 uint32_t eas_idx
= XIVE_EAS_INDEX(lisn
);
408 /* EAS cache lookup */
409 if (xive2_router_get_eas(xrtr
, eas_blk
, eas_idx
, &eas
)) {
410 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: Unknown LISN %x\n", lisn
);
414 if (!xive2_eas_is_valid(&eas
)) {
415 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: Invalid LISN %x\n", lisn
);
419 if (xive2_eas_is_masked(&eas
)) {
420 /* Notification completed */
425 * The event trigger becomes an END trigger
427 xive2_router_end_notify(xrtr
,
428 xive_get_field64(EAS2_END_BLOCK
, eas
.w
),
429 xive_get_field64(EAS2_END_INDEX
, eas
.w
),
430 xive_get_field64(EAS2_END_DATA
, eas
.w
));
433 static Property xive2_router_properties
[] = {
434 DEFINE_PROP_LINK("xive-fabric", Xive2Router
, xfb
,
435 TYPE_XIVE_FABRIC
, XiveFabric
*),
436 DEFINE_PROP_END_OF_LIST(),
439 static void xive2_router_class_init(ObjectClass
*klass
, void *data
)
441 DeviceClass
*dc
= DEVICE_CLASS(klass
);
442 XiveNotifierClass
*xnc
= XIVE_NOTIFIER_CLASS(klass
);
444 dc
->desc
= "XIVE2 Router Engine";
445 device_class_set_props(dc
, xive2_router_properties
);
446 /* Parent is SysBusDeviceClass. No need to call its realize hook */
447 dc
->realize
= xive2_router_realize
;
448 xnc
->notify
= xive2_router_notify
;
451 static const TypeInfo xive2_router_info
= {
452 .name
= TYPE_XIVE2_ROUTER
,
453 .parent
= TYPE_SYS_BUS_DEVICE
,
455 .instance_size
= sizeof(Xive2Router
),
456 .class_size
= sizeof(Xive2RouterClass
),
457 .class_init
= xive2_router_class_init
,
458 .interfaces
= (InterfaceInfo
[]) {
459 { TYPE_XIVE_NOTIFIER
},
460 { TYPE_XIVE_PRESENTER
},
465 static inline bool addr_is_even(hwaddr addr
, uint32_t shift
)
467 return !((addr
>> shift
) & 1);
470 static uint64_t xive2_end_source_read(void *opaque
, hwaddr addr
, unsigned size
)
472 Xive2EndSource
*xsrc
= XIVE2_END_SOURCE(opaque
);
473 uint32_t offset
= addr
& 0xFFF;
482 * The block id should be deduced from the load address on the END
483 * ESB MMIO but our model only supports a single block per XIVE chip.
485 end_blk
= xive2_router_get_block_id(xsrc
->xrtr
);
486 end_idx
= addr
>> (xsrc
->esb_shift
+ 1);
488 if (xive2_router_get_end(xsrc
->xrtr
, end_blk
, end_idx
, &end
)) {
489 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: No END %x/%x\n", end_blk
,
494 if (!xive2_end_is_valid(&end
)) {
495 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: END %x/%x is invalid\n",
500 end_esmask
= addr_is_even(addr
, xsrc
->esb_shift
) ? END2_W1_ESn
:
502 pq
= xive_get_field32(end_esmask
, end
.w1
);
505 case XIVE_ESB_LOAD_EOI
... XIVE_ESB_LOAD_EOI
+ 0x7FF:
506 ret
= xive_esb_eoi(&pq
);
508 /* Forward the source event notification for routing ?? */
511 case XIVE_ESB_GET
... XIVE_ESB_GET
+ 0x3FF:
515 case XIVE_ESB_SET_PQ_00
... XIVE_ESB_SET_PQ_00
+ 0x0FF:
516 case XIVE_ESB_SET_PQ_01
... XIVE_ESB_SET_PQ_01
+ 0x0FF:
517 case XIVE_ESB_SET_PQ_10
... XIVE_ESB_SET_PQ_10
+ 0x0FF:
518 case XIVE_ESB_SET_PQ_11
... XIVE_ESB_SET_PQ_11
+ 0x0FF:
519 ret
= xive_esb_set(&pq
, (offset
>> 8) & 0x3);
522 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: invalid END ESB load addr %d\n",
527 if (pq
!= xive_get_field32(end_esmask
, end
.w1
)) {
528 end
.w1
= xive_set_field32(end_esmask
, end
.w1
, pq
);
529 xive2_router_write_end(xsrc
->xrtr
, end_blk
, end_idx
, &end
, 1);
535 static void xive2_end_source_write(void *opaque
, hwaddr addr
,
536 uint64_t value
, unsigned size
)
538 Xive2EndSource
*xsrc
= XIVE2_END_SOURCE(opaque
);
539 uint32_t offset
= addr
& 0xFFF;
548 * The block id should be deduced from the load address on the END
549 * ESB MMIO but our model only supports a single block per XIVE chip.
551 end_blk
= xive2_router_get_block_id(xsrc
->xrtr
);
552 end_idx
= addr
>> (xsrc
->esb_shift
+ 1);
554 if (xive2_router_get_end(xsrc
->xrtr
, end_blk
, end_idx
, &end
)) {
555 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: No END %x/%x\n", end_blk
,
560 if (!xive2_end_is_valid(&end
)) {
561 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: END %x/%x is invalid\n",
566 end_esmask
= addr_is_even(addr
, xsrc
->esb_shift
) ? END2_W1_ESn
:
568 pq
= xive_get_field32(end_esmask
, end
.w1
);
572 notify
= xive_esb_trigger(&pq
);
575 case XIVE_ESB_STORE_EOI
... XIVE_ESB_STORE_EOI
+ 0x3FF:
576 /* TODO: can we check StoreEOI availability from the router ? */
577 notify
= xive_esb_eoi(&pq
);
581 qemu_log_mask(LOG_GUEST_ERROR
, "XIVE: invalid END ESB write addr %d\n",
586 if (pq
!= xive_get_field32(end_esmask
, end
.w1
)) {
587 end
.w1
= xive_set_field32(end_esmask
, end
.w1
, pq
);
588 xive2_router_write_end(xsrc
->xrtr
, end_blk
, end_idx
, &end
, 1);
591 /* TODO: Forward the source event notification for routing */
597 static const MemoryRegionOps xive2_end_source_ops
= {
598 .read
= xive2_end_source_read
,
599 .write
= xive2_end_source_write
,
600 .endianness
= DEVICE_BIG_ENDIAN
,
602 .min_access_size
= 8,
603 .max_access_size
= 8,
606 .min_access_size
= 8,
607 .max_access_size
= 8,
611 static void xive2_end_source_realize(DeviceState
*dev
, Error
**errp
)
613 Xive2EndSource
*xsrc
= XIVE2_END_SOURCE(dev
);
617 if (!xsrc
->nr_ends
) {
618 error_setg(errp
, "Number of interrupt needs to be greater than 0");
622 if (xsrc
->esb_shift
!= XIVE_ESB_4K
&&
623 xsrc
->esb_shift
!= XIVE_ESB_64K
) {
624 error_setg(errp
, "Invalid ESB shift setting");
629 * Each END is assigned an even/odd pair of MMIO pages, the even page
630 * manages the ESn field while the odd page manages the ESe field.
632 memory_region_init_io(&xsrc
->esb_mmio
, OBJECT(xsrc
),
633 &xive2_end_source_ops
, xsrc
, "xive.end",
634 (1ull << (xsrc
->esb_shift
+ 1)) * xsrc
->nr_ends
);
637 static Property xive2_end_source_properties
[] = {
638 DEFINE_PROP_UINT32("nr-ends", Xive2EndSource
, nr_ends
, 0),
639 DEFINE_PROP_UINT32("shift", Xive2EndSource
, esb_shift
, XIVE_ESB_64K
),
640 DEFINE_PROP_LINK("xive", Xive2EndSource
, xrtr
, TYPE_XIVE2_ROUTER
,
642 DEFINE_PROP_END_OF_LIST(),
645 static void xive2_end_source_class_init(ObjectClass
*klass
, void *data
)
647 DeviceClass
*dc
= DEVICE_CLASS(klass
);
649 dc
->desc
= "XIVE END Source";
650 device_class_set_props(dc
, xive2_end_source_properties
);
651 dc
->realize
= xive2_end_source_realize
;
654 static const TypeInfo xive2_end_source_info
= {
655 .name
= TYPE_XIVE2_END_SOURCE
,
656 .parent
= TYPE_DEVICE
,
657 .instance_size
= sizeof(Xive2EndSource
),
658 .class_init
= xive2_end_source_class_init
,
661 static void xive2_register_types(void)
663 type_register_static(&xive2_router_info
);
664 type_register_static(&xive2_end_source_info
);
667 type_init(xive2_register_types
)