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