1 /* 2 * QEMU PowerPC sPAPR 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 "qemu/module.h" 13 #include "qapi/error.h" 14 #include "qemu/error-report.h" 15 #include "target/ppc/cpu.h" 16 #include "sysemu/cpus.h" 17 #include "sysemu/reset.h" 18 #include "migration/vmstate.h" 19 #include "monitor/monitor.h" 20 #include "hw/ppc/fdt.h" 21 #include "hw/ppc/spapr.h" 22 #include "hw/ppc/spapr_cpu_core.h" 23 #include "hw/ppc/spapr_xive.h" 24 #include "hw/ppc/xive.h" 25 #include "hw/ppc/xive_regs.h" 26 #include "hw/qdev-properties.h" 27 28 /* 29 * XIVE Virtualization Controller BAR and Thread Managment BAR that we 30 * use for the ESB pages and the TIMA pages 31 */ 32 #define SPAPR_XIVE_VC_BASE 0x0006010000000000ull 33 #define SPAPR_XIVE_TM_BASE 0x0006030203180000ull 34 35 /* 36 * The allocation of VP blocks is a complex operation in OPAL and the 37 * VP identifiers have a relation with the number of HW chips, the 38 * size of the VP blocks, VP grouping, etc. The QEMU sPAPR XIVE 39 * controller model does not have the same constraints and can use a 40 * simple mapping scheme of the CPU vcpu_id 41 * 42 * These identifiers are never returned to the OS. 43 */ 44 45 #define SPAPR_XIVE_NVT_BASE 0x400 46 47 /* 48 * sPAPR NVT and END indexing helpers 49 */ 50 static uint32_t spapr_xive_nvt_to_target(uint8_t nvt_blk, uint32_t nvt_idx) 51 { 52 return nvt_idx - SPAPR_XIVE_NVT_BASE; 53 } 54 55 static void spapr_xive_cpu_to_nvt(PowerPCCPU *cpu, 56 uint8_t *out_nvt_blk, uint32_t *out_nvt_idx) 57 { 58 assert(cpu); 59 60 if (out_nvt_blk) { 61 *out_nvt_blk = SPAPR_XIVE_BLOCK_ID; 62 } 63 64 if (out_nvt_blk) { 65 *out_nvt_idx = SPAPR_XIVE_NVT_BASE + cpu->vcpu_id; 66 } 67 } 68 69 static int spapr_xive_target_to_nvt(uint32_t target, 70 uint8_t *out_nvt_blk, uint32_t *out_nvt_idx) 71 { 72 PowerPCCPU *cpu = spapr_find_cpu(target); 73 74 if (!cpu) { 75 return -1; 76 } 77 78 spapr_xive_cpu_to_nvt(cpu, out_nvt_blk, out_nvt_idx); 79 return 0; 80 } 81 82 /* 83 * sPAPR END indexing uses a simple mapping of the CPU vcpu_id, 8 84 * priorities per CPU 85 */ 86 int spapr_xive_end_to_target(uint8_t end_blk, uint32_t end_idx, 87 uint32_t *out_server, uint8_t *out_prio) 88 { 89 90 assert(end_blk == SPAPR_XIVE_BLOCK_ID); 91 92 if (out_server) { 93 *out_server = end_idx >> 3; 94 } 95 96 if (out_prio) { 97 *out_prio = end_idx & 0x7; 98 } 99 return 0; 100 } 101 102 static void spapr_xive_cpu_to_end(PowerPCCPU *cpu, uint8_t prio, 103 uint8_t *out_end_blk, uint32_t *out_end_idx) 104 { 105 assert(cpu); 106 107 if (out_end_blk) { 108 *out_end_blk = SPAPR_XIVE_BLOCK_ID; 109 } 110 111 if (out_end_idx) { 112 *out_end_idx = (cpu->vcpu_id << 3) + prio; 113 } 114 } 115 116 static int spapr_xive_target_to_end(uint32_t target, uint8_t prio, 117 uint8_t *out_end_blk, uint32_t *out_end_idx) 118 { 119 PowerPCCPU *cpu = spapr_find_cpu(target); 120 121 if (!cpu) { 122 return -1; 123 } 124 125 spapr_xive_cpu_to_end(cpu, prio, out_end_blk, out_end_idx); 126 return 0; 127 } 128 129 /* 130 * On sPAPR machines, use a simplified output for the XIVE END 131 * structure dumping only the information related to the OS EQ. 132 */ 133 static void spapr_xive_end_pic_print_info(SpaprXive *xive, XiveEND *end, 134 Monitor *mon) 135 { 136 uint64_t qaddr_base = xive_end_qaddr(end); 137 uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1); 138 uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1); 139 uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0); 140 uint32_t qentries = 1 << (qsize + 10); 141 uint32_t nvt = xive_get_field32(END_W6_NVT_INDEX, end->w6); 142 uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7); 143 144 monitor_printf(mon, "%3d/%d % 6d/%5d @%"PRIx64" ^%d", 145 spapr_xive_nvt_to_target(0, nvt), 146 priority, qindex, qentries, qaddr_base, qgen); 147 148 xive_end_queue_pic_print_info(end, 6, mon); 149 } 150 151 /* 152 * kvm_irqchip_in_kernel() will cause the compiler to turn this 153 * info a nop if CONFIG_KVM isn't defined. 154 */ 155 #define spapr_xive_in_kernel(xive) \ 156 (kvm_irqchip_in_kernel() && (xive)->fd != -1) 157 158 void spapr_xive_pic_print_info(SpaprXive *xive, Monitor *mon) 159 { 160 XiveSource *xsrc = &xive->source; 161 int i; 162 163 if (spapr_xive_in_kernel(xive)) { 164 Error *local_err = NULL; 165 166 kvmppc_xive_synchronize_state(xive, &local_err); 167 if (local_err) { 168 error_report_err(local_err); 169 return; 170 } 171 } 172 173 monitor_printf(mon, " LISN PQ EISN CPU/PRIO EQ\n"); 174 175 for (i = 0; i < xive->nr_irqs; i++) { 176 uint8_t pq = xive_source_esb_get(xsrc, i); 177 XiveEAS *eas = &xive->eat[i]; 178 179 if (!xive_eas_is_valid(eas)) { 180 continue; 181 } 182 183 monitor_printf(mon, " %08x %s %c%c%c %s %08x ", i, 184 xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI", 185 pq & XIVE_ESB_VAL_P ? 'P' : '-', 186 pq & XIVE_ESB_VAL_Q ? 'Q' : '-', 187 xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ', 188 xive_eas_is_masked(eas) ? "M" : " ", 189 (int) xive_get_field64(EAS_END_DATA, eas->w)); 190 191 if (!xive_eas_is_masked(eas)) { 192 uint32_t end_idx = xive_get_field64(EAS_END_INDEX, eas->w); 193 XiveEND *end; 194 195 assert(end_idx < xive->nr_ends); 196 end = &xive->endt[end_idx]; 197 198 if (xive_end_is_valid(end)) { 199 spapr_xive_end_pic_print_info(xive, end, mon); 200 } 201 } 202 monitor_printf(mon, "\n"); 203 } 204 } 205 206 void spapr_xive_mmio_set_enabled(SpaprXive *xive, bool enable) 207 { 208 memory_region_set_enabled(&xive->source.esb_mmio, enable); 209 memory_region_set_enabled(&xive->tm_mmio, enable); 210 211 /* Disable the END ESBs until a guest OS makes use of them */ 212 memory_region_set_enabled(&xive->end_source.esb_mmio, false); 213 } 214 215 static void spapr_xive_tm_write(void *opaque, hwaddr offset, 216 uint64_t value, unsigned size) 217 { 218 XiveTCTX *tctx = spapr_cpu_state(POWERPC_CPU(current_cpu))->tctx; 219 220 xive_tctx_tm_write(XIVE_PRESENTER(opaque), tctx, offset, value, size); 221 } 222 223 static uint64_t spapr_xive_tm_read(void *opaque, hwaddr offset, unsigned size) 224 { 225 XiveTCTX *tctx = spapr_cpu_state(POWERPC_CPU(current_cpu))->tctx; 226 227 return xive_tctx_tm_read(XIVE_PRESENTER(opaque), tctx, offset, size); 228 } 229 230 const MemoryRegionOps spapr_xive_tm_ops = { 231 .read = spapr_xive_tm_read, 232 .write = spapr_xive_tm_write, 233 .endianness = DEVICE_BIG_ENDIAN, 234 .valid = { 235 .min_access_size = 1, 236 .max_access_size = 8, 237 }, 238 .impl = { 239 .min_access_size = 1, 240 .max_access_size = 8, 241 }, 242 }; 243 244 static void spapr_xive_end_reset(XiveEND *end) 245 { 246 memset(end, 0, sizeof(*end)); 247 248 /* switch off the escalation and notification ESBs */ 249 end->w1 = cpu_to_be32(END_W1_ESe_Q | END_W1_ESn_Q); 250 } 251 252 static void spapr_xive_reset(void *dev) 253 { 254 SpaprXive *xive = SPAPR_XIVE(dev); 255 int i; 256 257 /* 258 * The XiveSource has its own reset handler, which mask off all 259 * IRQs (!P|Q) 260 */ 261 262 /* Mask all valid EASs in the IRQ number space. */ 263 for (i = 0; i < xive->nr_irqs; i++) { 264 XiveEAS *eas = &xive->eat[i]; 265 if (xive_eas_is_valid(eas)) { 266 eas->w = cpu_to_be64(EAS_VALID | EAS_MASKED); 267 } else { 268 eas->w = 0; 269 } 270 } 271 272 /* Clear all ENDs */ 273 for (i = 0; i < xive->nr_ends; i++) { 274 spapr_xive_end_reset(&xive->endt[i]); 275 } 276 } 277 278 static void spapr_xive_instance_init(Object *obj) 279 { 280 SpaprXive *xive = SPAPR_XIVE(obj); 281 282 object_initialize_child(obj, "source", &xive->source, TYPE_XIVE_SOURCE); 283 284 object_initialize_child(obj, "end_source", &xive->end_source, 285 TYPE_XIVE_END_SOURCE); 286 287 /* Not connected to the KVM XIVE device */ 288 xive->fd = -1; 289 } 290 291 static void spapr_xive_realize(DeviceState *dev, Error **errp) 292 { 293 SpaprXive *xive = SPAPR_XIVE(dev); 294 SpaprXiveClass *sxc = SPAPR_XIVE_GET_CLASS(xive); 295 XiveSource *xsrc = &xive->source; 296 XiveENDSource *end_xsrc = &xive->end_source; 297 Error *local_err = NULL; 298 299 /* Set by spapr_irq_init() */ 300 g_assert(xive->nr_irqs); 301 g_assert(xive->nr_ends); 302 303 sxc->parent_realize(dev, &local_err); 304 if (local_err) { 305 error_propagate(errp, local_err); 306 return; 307 } 308 309 /* 310 * Initialize the internal sources, for IPIs and virtual devices. 311 */ 312 object_property_set_int(OBJECT(xsrc), "nr-irqs", xive->nr_irqs, 313 &error_fatal); 314 object_property_set_link(OBJECT(xsrc), "xive", OBJECT(xive), &error_abort); 315 if (!qdev_realize(DEVICE(xsrc), NULL, errp)) { 316 return; 317 } 318 sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xsrc->esb_mmio); 319 320 /* 321 * Initialize the END ESB source 322 */ 323 object_property_set_int(OBJECT(end_xsrc), "nr-ends", xive->nr_irqs, 324 &error_fatal); 325 object_property_set_link(OBJECT(end_xsrc), "xive", OBJECT(xive), 326 &error_abort); 327 if (!qdev_realize(DEVICE(end_xsrc), NULL, errp)) { 328 return; 329 } 330 sysbus_init_mmio(SYS_BUS_DEVICE(xive), &end_xsrc->esb_mmio); 331 332 /* Set the mapping address of the END ESB pages after the source ESBs */ 333 xive->end_base = xive->vc_base + xive_source_esb_len(xsrc); 334 335 /* 336 * Allocate the routing tables 337 */ 338 xive->eat = g_new0(XiveEAS, xive->nr_irqs); 339 xive->endt = g_new0(XiveEND, xive->nr_ends); 340 341 xive->nodename = g_strdup_printf("interrupt-controller@%" PRIx64, 342 xive->tm_base + XIVE_TM_USER_PAGE * (1 << TM_SHIFT)); 343 344 qemu_register_reset(spapr_xive_reset, dev); 345 346 /* TIMA initialization */ 347 memory_region_init_io(&xive->tm_mmio, OBJECT(xive), &spapr_xive_tm_ops, 348 xive, "xive.tima", 4ull << TM_SHIFT); 349 sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xive->tm_mmio); 350 351 /* 352 * Map all regions. These will be enabled or disabled at reset and 353 * can also be overridden by KVM memory regions if active 354 */ 355 sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->vc_base); 356 sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1, xive->end_base); 357 sysbus_mmio_map(SYS_BUS_DEVICE(xive), 2, xive->tm_base); 358 } 359 360 static int spapr_xive_get_eas(XiveRouter *xrtr, uint8_t eas_blk, 361 uint32_t eas_idx, XiveEAS *eas) 362 { 363 SpaprXive *xive = SPAPR_XIVE(xrtr); 364 365 if (eas_idx >= xive->nr_irqs) { 366 return -1; 367 } 368 369 *eas = xive->eat[eas_idx]; 370 return 0; 371 } 372 373 static int spapr_xive_get_end(XiveRouter *xrtr, 374 uint8_t end_blk, uint32_t end_idx, XiveEND *end) 375 { 376 SpaprXive *xive = SPAPR_XIVE(xrtr); 377 378 if (end_idx >= xive->nr_ends) { 379 return -1; 380 } 381 382 memcpy(end, &xive->endt[end_idx], sizeof(XiveEND)); 383 return 0; 384 } 385 386 static int spapr_xive_write_end(XiveRouter *xrtr, uint8_t end_blk, 387 uint32_t end_idx, XiveEND *end, 388 uint8_t word_number) 389 { 390 SpaprXive *xive = SPAPR_XIVE(xrtr); 391 392 if (end_idx >= xive->nr_ends) { 393 return -1; 394 } 395 396 memcpy(&xive->endt[end_idx], end, sizeof(XiveEND)); 397 return 0; 398 } 399 400 static int spapr_xive_get_nvt(XiveRouter *xrtr, 401 uint8_t nvt_blk, uint32_t nvt_idx, XiveNVT *nvt) 402 { 403 uint32_t vcpu_id = spapr_xive_nvt_to_target(nvt_blk, nvt_idx); 404 PowerPCCPU *cpu = spapr_find_cpu(vcpu_id); 405 406 if (!cpu) { 407 /* TODO: should we assert() if we can find a NVT ? */ 408 return -1; 409 } 410 411 /* 412 * sPAPR does not maintain a NVT table. Return that the NVT is 413 * valid if we have found a matching CPU 414 */ 415 nvt->w0 = cpu_to_be32(NVT_W0_VALID); 416 return 0; 417 } 418 419 static int spapr_xive_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk, 420 uint32_t nvt_idx, XiveNVT *nvt, 421 uint8_t word_number) 422 { 423 /* 424 * We don't need to write back to the NVTs because the sPAPR 425 * machine should never hit a non-scheduled NVT. It should never 426 * get called. 427 */ 428 g_assert_not_reached(); 429 } 430 431 static int spapr_xive_match_nvt(XivePresenter *xptr, uint8_t format, 432 uint8_t nvt_blk, uint32_t nvt_idx, 433 bool cam_ignore, uint8_t priority, 434 uint32_t logic_serv, XiveTCTXMatch *match) 435 { 436 CPUState *cs; 437 int count = 0; 438 439 CPU_FOREACH(cs) { 440 PowerPCCPU *cpu = POWERPC_CPU(cs); 441 XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx; 442 int ring; 443 444 /* 445 * Skip partially initialized vCPUs. This can happen when 446 * vCPUs are hotplugged. 447 */ 448 if (!tctx) { 449 continue; 450 } 451 452 /* 453 * Check the thread context CAM lines and record matches. 454 */ 455 ring = xive_presenter_tctx_match(xptr, tctx, format, nvt_blk, nvt_idx, 456 cam_ignore, logic_serv); 457 /* 458 * Save the matching thread interrupt context and follow on to 459 * check for duplicates which are invalid. 460 */ 461 if (ring != -1) { 462 if (match->tctx) { 463 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: already found a thread " 464 "context NVT %x/%x\n", nvt_blk, nvt_idx); 465 return -1; 466 } 467 468 match->ring = ring; 469 match->tctx = tctx; 470 count++; 471 } 472 } 473 474 return count; 475 } 476 477 static uint8_t spapr_xive_get_block_id(XiveRouter *xrtr) 478 { 479 return SPAPR_XIVE_BLOCK_ID; 480 } 481 482 static const VMStateDescription vmstate_spapr_xive_end = { 483 .name = TYPE_SPAPR_XIVE "/end", 484 .version_id = 1, 485 .minimum_version_id = 1, 486 .fields = (VMStateField []) { 487 VMSTATE_UINT32(w0, XiveEND), 488 VMSTATE_UINT32(w1, XiveEND), 489 VMSTATE_UINT32(w2, XiveEND), 490 VMSTATE_UINT32(w3, XiveEND), 491 VMSTATE_UINT32(w4, XiveEND), 492 VMSTATE_UINT32(w5, XiveEND), 493 VMSTATE_UINT32(w6, XiveEND), 494 VMSTATE_UINT32(w7, XiveEND), 495 VMSTATE_END_OF_LIST() 496 }, 497 }; 498 499 static const VMStateDescription vmstate_spapr_xive_eas = { 500 .name = TYPE_SPAPR_XIVE "/eas", 501 .version_id = 1, 502 .minimum_version_id = 1, 503 .fields = (VMStateField []) { 504 VMSTATE_UINT64(w, XiveEAS), 505 VMSTATE_END_OF_LIST() 506 }, 507 }; 508 509 static int vmstate_spapr_xive_pre_save(void *opaque) 510 { 511 SpaprXive *xive = SPAPR_XIVE(opaque); 512 513 if (spapr_xive_in_kernel(xive)) { 514 return kvmppc_xive_pre_save(xive); 515 } 516 517 return 0; 518 } 519 520 /* 521 * Called by the sPAPR IRQ backend 'post_load' method at the machine 522 * level. 523 */ 524 static int spapr_xive_post_load(SpaprInterruptController *intc, int version_id) 525 { 526 SpaprXive *xive = SPAPR_XIVE(intc); 527 528 if (spapr_xive_in_kernel(xive)) { 529 return kvmppc_xive_post_load(xive, version_id); 530 } 531 532 return 0; 533 } 534 535 static const VMStateDescription vmstate_spapr_xive = { 536 .name = TYPE_SPAPR_XIVE, 537 .version_id = 1, 538 .minimum_version_id = 1, 539 .pre_save = vmstate_spapr_xive_pre_save, 540 .post_load = NULL, /* handled at the machine level */ 541 .fields = (VMStateField[]) { 542 VMSTATE_UINT32_EQUAL(nr_irqs, SpaprXive, NULL), 543 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(eat, SpaprXive, nr_irqs, 544 vmstate_spapr_xive_eas, XiveEAS), 545 VMSTATE_STRUCT_VARRAY_POINTER_UINT32(endt, SpaprXive, nr_ends, 546 vmstate_spapr_xive_end, XiveEND), 547 VMSTATE_END_OF_LIST() 548 }, 549 }; 550 551 static int spapr_xive_claim_irq(SpaprInterruptController *intc, int lisn, 552 bool lsi, Error **errp) 553 { 554 SpaprXive *xive = SPAPR_XIVE(intc); 555 XiveSource *xsrc = &xive->source; 556 557 assert(lisn < xive->nr_irqs); 558 559 if (xive_eas_is_valid(&xive->eat[lisn])) { 560 error_setg(errp, "IRQ %d is not free", lisn); 561 return -EBUSY; 562 } 563 564 /* 565 * Set default values when allocating an IRQ number 566 */ 567 xive->eat[lisn].w |= cpu_to_be64(EAS_VALID | EAS_MASKED); 568 if (lsi) { 569 xive_source_irq_set_lsi(xsrc, lisn); 570 } 571 572 if (spapr_xive_in_kernel(xive)) { 573 return kvmppc_xive_source_reset_one(xsrc, lisn, errp); 574 } 575 576 return 0; 577 } 578 579 static void spapr_xive_free_irq(SpaprInterruptController *intc, int lisn) 580 { 581 SpaprXive *xive = SPAPR_XIVE(intc); 582 assert(lisn < xive->nr_irqs); 583 584 xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID); 585 } 586 587 static Property spapr_xive_properties[] = { 588 DEFINE_PROP_UINT32("nr-irqs", SpaprXive, nr_irqs, 0), 589 DEFINE_PROP_UINT32("nr-ends", SpaprXive, nr_ends, 0), 590 DEFINE_PROP_UINT64("vc-base", SpaprXive, vc_base, SPAPR_XIVE_VC_BASE), 591 DEFINE_PROP_UINT64("tm-base", SpaprXive, tm_base, SPAPR_XIVE_TM_BASE), 592 DEFINE_PROP_UINT8("hv-prio", SpaprXive, hv_prio, 7), 593 DEFINE_PROP_END_OF_LIST(), 594 }; 595 596 static int spapr_xive_cpu_intc_create(SpaprInterruptController *intc, 597 PowerPCCPU *cpu, Error **errp) 598 { 599 SpaprXive *xive = SPAPR_XIVE(intc); 600 Object *obj; 601 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu); 602 603 obj = xive_tctx_create(OBJECT(cpu), XIVE_PRESENTER(xive), errp); 604 if (!obj) { 605 return -1; 606 } 607 608 spapr_cpu->tctx = XIVE_TCTX(obj); 609 return 0; 610 } 611 612 static void xive_tctx_set_os_cam(XiveTCTX *tctx, uint32_t os_cam) 613 { 614 uint32_t qw1w2 = cpu_to_be32(TM_QW1W2_VO | os_cam); 615 memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4); 616 } 617 618 static void spapr_xive_cpu_intc_reset(SpaprInterruptController *intc, 619 PowerPCCPU *cpu) 620 { 621 XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx; 622 uint8_t nvt_blk; 623 uint32_t nvt_idx; 624 625 xive_tctx_reset(tctx); 626 627 /* 628 * When a Virtual Processor is scheduled to run on a HW thread, 629 * the hypervisor pushes its identifier in the OS CAM line. 630 * Emulate the same behavior under QEMU. 631 */ 632 spapr_xive_cpu_to_nvt(cpu, &nvt_blk, &nvt_idx); 633 634 xive_tctx_set_os_cam(tctx, xive_nvt_cam_line(nvt_blk, nvt_idx)); 635 } 636 637 static void spapr_xive_cpu_intc_destroy(SpaprInterruptController *intc, 638 PowerPCCPU *cpu) 639 { 640 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu); 641 642 xive_tctx_destroy(spapr_cpu->tctx); 643 spapr_cpu->tctx = NULL; 644 } 645 646 static void spapr_xive_set_irq(SpaprInterruptController *intc, int irq, int val) 647 { 648 SpaprXive *xive = SPAPR_XIVE(intc); 649 650 if (spapr_xive_in_kernel(xive)) { 651 kvmppc_xive_source_set_irq(&xive->source, irq, val); 652 } else { 653 xive_source_set_irq(&xive->source, irq, val); 654 } 655 } 656 657 static void spapr_xive_print_info(SpaprInterruptController *intc, Monitor *mon) 658 { 659 SpaprXive *xive = SPAPR_XIVE(intc); 660 CPUState *cs; 661 662 CPU_FOREACH(cs) { 663 PowerPCCPU *cpu = POWERPC_CPU(cs); 664 665 xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, mon); 666 } 667 668 spapr_xive_pic_print_info(xive, mon); 669 } 670 671 static void spapr_xive_dt(SpaprInterruptController *intc, uint32_t nr_servers, 672 void *fdt, uint32_t phandle) 673 { 674 SpaprXive *xive = SPAPR_XIVE(intc); 675 int node; 676 uint64_t timas[2 * 2]; 677 /* Interrupt number ranges for the IPIs */ 678 uint32_t lisn_ranges[] = { 679 cpu_to_be32(SPAPR_IRQ_IPI), 680 cpu_to_be32(SPAPR_IRQ_IPI + nr_servers), 681 }; 682 /* 683 * EQ size - the sizes of pages supported by the system 4K, 64K, 684 * 2M, 16M. We only advertise 64K for the moment. 685 */ 686 uint32_t eq_sizes[] = { 687 cpu_to_be32(16), /* 64K */ 688 }; 689 /* 690 * QEMU/KVM only needs to define a single range to reserve the 691 * escalation priority. A priority bitmask would have been more 692 * appropriate. 693 */ 694 uint32_t plat_res_int_priorities[] = { 695 cpu_to_be32(xive->hv_prio), /* start */ 696 cpu_to_be32(0xff - xive->hv_prio), /* count */ 697 }; 698 699 /* Thread Interrupt Management Area : User (ring 3) and OS (ring 2) */ 700 timas[0] = cpu_to_be64(xive->tm_base + 701 XIVE_TM_USER_PAGE * (1ull << TM_SHIFT)); 702 timas[1] = cpu_to_be64(1ull << TM_SHIFT); 703 timas[2] = cpu_to_be64(xive->tm_base + 704 XIVE_TM_OS_PAGE * (1ull << TM_SHIFT)); 705 timas[3] = cpu_to_be64(1ull << TM_SHIFT); 706 707 _FDT(node = fdt_add_subnode(fdt, 0, xive->nodename)); 708 709 _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe")); 710 _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas))); 711 712 _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe")); 713 _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes, 714 sizeof(eq_sizes))); 715 _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges, 716 sizeof(lisn_ranges))); 717 718 /* For Linux to link the LSIs to the interrupt controller. */ 719 _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0)); 720 _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2)); 721 722 /* For SLOF */ 723 _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle)); 724 _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle)); 725 726 /* 727 * The "ibm,plat-res-int-priorities" property defines the priority 728 * ranges reserved by the hypervisor 729 */ 730 _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities", 731 plat_res_int_priorities, sizeof(plat_res_int_priorities))); 732 } 733 734 static int spapr_xive_activate(SpaprInterruptController *intc, 735 uint32_t nr_servers, Error **errp) 736 { 737 SpaprXive *xive = SPAPR_XIVE(intc); 738 739 if (kvm_enabled()) { 740 int rc = spapr_irq_init_kvm(kvmppc_xive_connect, intc, nr_servers, 741 errp); 742 if (rc < 0) { 743 return rc; 744 } 745 } 746 747 /* Activate the XIVE MMIOs */ 748 spapr_xive_mmio_set_enabled(xive, true); 749 750 return 0; 751 } 752 753 static void spapr_xive_deactivate(SpaprInterruptController *intc) 754 { 755 SpaprXive *xive = SPAPR_XIVE(intc); 756 757 spapr_xive_mmio_set_enabled(xive, false); 758 759 if (spapr_xive_in_kernel(xive)) { 760 kvmppc_xive_disconnect(intc); 761 } 762 } 763 764 static bool spapr_xive_in_kernel_xptr(const XivePresenter *xptr) 765 { 766 return spapr_xive_in_kernel(SPAPR_XIVE(xptr)); 767 } 768 769 static void spapr_xive_class_init(ObjectClass *klass, void *data) 770 { 771 DeviceClass *dc = DEVICE_CLASS(klass); 772 XiveRouterClass *xrc = XIVE_ROUTER_CLASS(klass); 773 SpaprInterruptControllerClass *sicc = SPAPR_INTC_CLASS(klass); 774 XivePresenterClass *xpc = XIVE_PRESENTER_CLASS(klass); 775 SpaprXiveClass *sxc = SPAPR_XIVE_CLASS(klass); 776 777 dc->desc = "sPAPR XIVE Interrupt Controller"; 778 device_class_set_props(dc, spapr_xive_properties); 779 device_class_set_parent_realize(dc, spapr_xive_realize, 780 &sxc->parent_realize); 781 dc->vmsd = &vmstate_spapr_xive; 782 783 xrc->get_eas = spapr_xive_get_eas; 784 xrc->get_end = spapr_xive_get_end; 785 xrc->write_end = spapr_xive_write_end; 786 xrc->get_nvt = spapr_xive_get_nvt; 787 xrc->write_nvt = spapr_xive_write_nvt; 788 xrc->get_block_id = spapr_xive_get_block_id; 789 790 sicc->activate = spapr_xive_activate; 791 sicc->deactivate = spapr_xive_deactivate; 792 sicc->cpu_intc_create = spapr_xive_cpu_intc_create; 793 sicc->cpu_intc_reset = spapr_xive_cpu_intc_reset; 794 sicc->cpu_intc_destroy = spapr_xive_cpu_intc_destroy; 795 sicc->claim_irq = spapr_xive_claim_irq; 796 sicc->free_irq = spapr_xive_free_irq; 797 sicc->set_irq = spapr_xive_set_irq; 798 sicc->print_info = spapr_xive_print_info; 799 sicc->dt = spapr_xive_dt; 800 sicc->post_load = spapr_xive_post_load; 801 802 xpc->match_nvt = spapr_xive_match_nvt; 803 xpc->in_kernel = spapr_xive_in_kernel_xptr; 804 } 805 806 static const TypeInfo spapr_xive_info = { 807 .name = TYPE_SPAPR_XIVE, 808 .parent = TYPE_XIVE_ROUTER, 809 .instance_init = spapr_xive_instance_init, 810 .instance_size = sizeof(SpaprXive), 811 .class_init = spapr_xive_class_init, 812 .class_size = sizeof(SpaprXiveClass), 813 .interfaces = (InterfaceInfo[]) { 814 { TYPE_SPAPR_INTC }, 815 { } 816 }, 817 }; 818 819 static void spapr_xive_register_types(void) 820 { 821 type_register_static(&spapr_xive_info); 822 } 823 824 type_init(spapr_xive_register_types) 825 826 /* 827 * XIVE hcalls 828 * 829 * The terminology used by the XIVE hcalls is the following : 830 * 831 * TARGET vCPU number 832 * EQ Event Queue assigned by OS to receive event data 833 * ESB page for source interrupt management 834 * LISN Logical Interrupt Source Number identifying a source in the 835 * machine 836 * EISN Effective Interrupt Source Number used by guest OS to 837 * identify source in the guest 838 * 839 * The EAS, END, NVT structures are not exposed. 840 */ 841 842 /* 843 * On POWER9, the KVM XIVE device uses priority 7 for the escalation 844 * interrupts. So we only allow the guest to use priorities [0..6]. 845 */ 846 static bool spapr_xive_priority_is_reserved(SpaprXive *xive, uint8_t priority) 847 { 848 return priority >= xive->hv_prio; 849 } 850 851 /* 852 * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical 853 * real address of the MMIO page through which the Event State Buffer 854 * entry associated with the value of the "lisn" parameter is managed. 855 * 856 * Parameters: 857 * Input 858 * - R4: "flags" 859 * Bits 0-63 reserved 860 * - R5: "lisn" is per "interrupts", "interrupt-map", or 861 * "ibm,xive-lisn-ranges" properties, or as returned by the 862 * ibm,query-interrupt-source-number RTAS call, or as returned 863 * by the H_ALLOCATE_VAS_WINDOW hcall 864 * 865 * Output 866 * - R4: "flags" 867 * Bits 0-59: Reserved 868 * Bit 60: H_INT_ESB must be used for Event State Buffer 869 * management 870 * Bit 61: 1 == LSI 0 == MSI 871 * Bit 62: the full function page supports trigger 872 * Bit 63: Store EOI Supported 873 * - R5: Logical Real address of full function Event State Buffer 874 * management page, -1 if H_INT_ESB hcall flag is set to 1. 875 * - R6: Logical Real Address of trigger only Event State Buffer 876 * management page or -1. 877 * - R7: Power of 2 page size for the ESB management pages returned in 878 * R5 and R6. 879 */ 880 881 #define SPAPR_XIVE_SRC_H_INT_ESB PPC_BIT(60) /* ESB manage with H_INT_ESB */ 882 #define SPAPR_XIVE_SRC_LSI PPC_BIT(61) /* Virtual LSI type */ 883 #define SPAPR_XIVE_SRC_TRIGGER PPC_BIT(62) /* Trigger and management 884 on same page */ 885 #define SPAPR_XIVE_SRC_STORE_EOI PPC_BIT(63) /* Store EOI support */ 886 887 static target_ulong h_int_get_source_info(PowerPCCPU *cpu, 888 SpaprMachineState *spapr, 889 target_ulong opcode, 890 target_ulong *args) 891 { 892 SpaprXive *xive = spapr->xive; 893 XiveSource *xsrc = &xive->source; 894 target_ulong flags = args[0]; 895 target_ulong lisn = args[1]; 896 897 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 898 return H_FUNCTION; 899 } 900 901 if (flags) { 902 return H_PARAMETER; 903 } 904 905 if (lisn >= xive->nr_irqs) { 906 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n", 907 lisn); 908 return H_P2; 909 } 910 911 if (!xive_eas_is_valid(&xive->eat[lisn])) { 912 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n", 913 lisn); 914 return H_P2; 915 } 916 917 /* 918 * All sources are emulated under the main XIVE object and share 919 * the same characteristics. 920 */ 921 args[0] = 0; 922 if (!xive_source_esb_has_2page(xsrc)) { 923 args[0] |= SPAPR_XIVE_SRC_TRIGGER; 924 } 925 if (xsrc->esb_flags & XIVE_SRC_STORE_EOI) { 926 args[0] |= SPAPR_XIVE_SRC_STORE_EOI; 927 } 928 929 /* 930 * Force the use of the H_INT_ESB hcall in case of an LSI 931 * interrupt. This is necessary under KVM to re-trigger the 932 * interrupt if the level is still asserted 933 */ 934 if (xive_source_irq_is_lsi(xsrc, lisn)) { 935 args[0] |= SPAPR_XIVE_SRC_H_INT_ESB | SPAPR_XIVE_SRC_LSI; 936 } 937 938 if (!(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) { 939 args[1] = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn); 940 } else { 941 args[1] = -1; 942 } 943 944 if (xive_source_esb_has_2page(xsrc) && 945 !(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) { 946 args[2] = xive->vc_base + xive_source_esb_page(xsrc, lisn); 947 } else { 948 args[2] = -1; 949 } 950 951 if (xive_source_esb_has_2page(xsrc)) { 952 args[3] = xsrc->esb_shift - 1; 953 } else { 954 args[3] = xsrc->esb_shift; 955 } 956 957 return H_SUCCESS; 958 } 959 960 /* 961 * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical 962 * Interrupt Source to a target. The Logical Interrupt Source is 963 * designated with the "lisn" parameter and the target is designated 964 * with the "target" and "priority" parameters. Upon return from the 965 * hcall(), no additional interrupts will be directed to the old EQ. 966 * 967 * Parameters: 968 * Input: 969 * - R4: "flags" 970 * Bits 0-61: Reserved 971 * Bit 62: set the "eisn" in the EAS 972 * Bit 63: masks the interrupt source in the hardware interrupt 973 * control structure. An interrupt masked by this mechanism will 974 * be dropped, but it's source state bits will still be 975 * set. There is no race-free way of unmasking and restoring the 976 * source. Thus this should only be used in interrupts that are 977 * also masked at the source, and only in cases where the 978 * interrupt is not meant to be used for a large amount of time 979 * because no valid target exists for it for example 980 * - R5: "lisn" is per "interrupts", "interrupt-map", or 981 * "ibm,xive-lisn-ranges" properties, or as returned by the 982 * ibm,query-interrupt-source-number RTAS call, or as returned by 983 * the H_ALLOCATE_VAS_WINDOW hcall 984 * - R6: "target" is per "ibm,ppc-interrupt-server#s" or 985 * "ibm,ppc-interrupt-gserver#s" 986 * - R7: "priority" is a valid priority not in 987 * "ibm,plat-res-int-priorities" 988 * - R8: "eisn" is the guest EISN associated with the "lisn" 989 * 990 * Output: 991 * - None 992 */ 993 994 #define SPAPR_XIVE_SRC_SET_EISN PPC_BIT(62) 995 #define SPAPR_XIVE_SRC_MASK PPC_BIT(63) 996 997 static target_ulong h_int_set_source_config(PowerPCCPU *cpu, 998 SpaprMachineState *spapr, 999 target_ulong opcode, 1000 target_ulong *args) 1001 { 1002 SpaprXive *xive = spapr->xive; 1003 XiveEAS eas, new_eas; 1004 target_ulong flags = args[0]; 1005 target_ulong lisn = args[1]; 1006 target_ulong target = args[2]; 1007 target_ulong priority = args[3]; 1008 target_ulong eisn = args[4]; 1009 uint8_t end_blk; 1010 uint32_t end_idx; 1011 1012 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1013 return H_FUNCTION; 1014 } 1015 1016 if (flags & ~(SPAPR_XIVE_SRC_SET_EISN | SPAPR_XIVE_SRC_MASK)) { 1017 return H_PARAMETER; 1018 } 1019 1020 if (lisn >= xive->nr_irqs) { 1021 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n", 1022 lisn); 1023 return H_P2; 1024 } 1025 1026 eas = xive->eat[lisn]; 1027 if (!xive_eas_is_valid(&eas)) { 1028 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n", 1029 lisn); 1030 return H_P2; 1031 } 1032 1033 /* priority 0xff is used to reset the EAS */ 1034 if (priority == 0xff) { 1035 new_eas.w = cpu_to_be64(EAS_VALID | EAS_MASKED); 1036 goto out; 1037 } 1038 1039 if (flags & SPAPR_XIVE_SRC_MASK) { 1040 new_eas.w = eas.w | cpu_to_be64(EAS_MASKED); 1041 } else { 1042 new_eas.w = eas.w & cpu_to_be64(~EAS_MASKED); 1043 } 1044 1045 if (spapr_xive_priority_is_reserved(xive, priority)) { 1046 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld 1047 " is reserved\n", priority); 1048 return H_P4; 1049 } 1050 1051 /* 1052 * Validate that "target" is part of the list of threads allocated 1053 * to the partition. For that, find the END corresponding to the 1054 * target. 1055 */ 1056 if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) { 1057 return H_P3; 1058 } 1059 1060 new_eas.w = xive_set_field64(EAS_END_BLOCK, new_eas.w, end_blk); 1061 new_eas.w = xive_set_field64(EAS_END_INDEX, new_eas.w, end_idx); 1062 1063 if (flags & SPAPR_XIVE_SRC_SET_EISN) { 1064 new_eas.w = xive_set_field64(EAS_END_DATA, new_eas.w, eisn); 1065 } 1066 1067 if (spapr_xive_in_kernel(xive)) { 1068 Error *local_err = NULL; 1069 1070 kvmppc_xive_set_source_config(xive, lisn, &new_eas, &local_err); 1071 if (local_err) { 1072 error_report_err(local_err); 1073 return H_HARDWARE; 1074 } 1075 } 1076 1077 out: 1078 xive->eat[lisn] = new_eas; 1079 return H_SUCCESS; 1080 } 1081 1082 /* 1083 * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which 1084 * target/priority pair is assigned to the specified Logical Interrupt 1085 * Source. 1086 * 1087 * Parameters: 1088 * Input: 1089 * - R4: "flags" 1090 * Bits 0-63 Reserved 1091 * - R5: "lisn" is per "interrupts", "interrupt-map", or 1092 * "ibm,xive-lisn-ranges" properties, or as returned by the 1093 * ibm,query-interrupt-source-number RTAS call, or as 1094 * returned by the H_ALLOCATE_VAS_WINDOW hcall 1095 * 1096 * Output: 1097 * - R4: Target to which the specified Logical Interrupt Source is 1098 * assigned 1099 * - R5: Priority to which the specified Logical Interrupt Source is 1100 * assigned 1101 * - R6: EISN for the specified Logical Interrupt Source (this will be 1102 * equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG) 1103 */ 1104 static target_ulong h_int_get_source_config(PowerPCCPU *cpu, 1105 SpaprMachineState *spapr, 1106 target_ulong opcode, 1107 target_ulong *args) 1108 { 1109 SpaprXive *xive = spapr->xive; 1110 target_ulong flags = args[0]; 1111 target_ulong lisn = args[1]; 1112 XiveEAS eas; 1113 XiveEND *end; 1114 uint8_t nvt_blk; 1115 uint32_t end_idx, nvt_idx; 1116 1117 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1118 return H_FUNCTION; 1119 } 1120 1121 if (flags) { 1122 return H_PARAMETER; 1123 } 1124 1125 if (lisn >= xive->nr_irqs) { 1126 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n", 1127 lisn); 1128 return H_P2; 1129 } 1130 1131 eas = xive->eat[lisn]; 1132 if (!xive_eas_is_valid(&eas)) { 1133 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n", 1134 lisn); 1135 return H_P2; 1136 } 1137 1138 /* EAS_END_BLOCK is unused on sPAPR */ 1139 end_idx = xive_get_field64(EAS_END_INDEX, eas.w); 1140 1141 assert(end_idx < xive->nr_ends); 1142 end = &xive->endt[end_idx]; 1143 1144 nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6); 1145 nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6); 1146 args[0] = spapr_xive_nvt_to_target(nvt_blk, nvt_idx); 1147 1148 if (xive_eas_is_masked(&eas)) { 1149 args[1] = 0xff; 1150 } else { 1151 args[1] = xive_get_field32(END_W7_F0_PRIORITY, end->w7); 1152 } 1153 1154 args[2] = xive_get_field64(EAS_END_DATA, eas.w); 1155 1156 return H_SUCCESS; 1157 } 1158 1159 /* 1160 * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real 1161 * address of the notification management page associated with the 1162 * specified target and priority. 1163 * 1164 * Parameters: 1165 * Input: 1166 * - R4: "flags" 1167 * Bits 0-63 Reserved 1168 * - R5: "target" is per "ibm,ppc-interrupt-server#s" or 1169 * "ibm,ppc-interrupt-gserver#s" 1170 * - R6: "priority" is a valid priority not in 1171 * "ibm,plat-res-int-priorities" 1172 * 1173 * Output: 1174 * - R4: Logical real address of notification page 1175 * - R5: Power of 2 page size of the notification page 1176 */ 1177 static target_ulong h_int_get_queue_info(PowerPCCPU *cpu, 1178 SpaprMachineState *spapr, 1179 target_ulong opcode, 1180 target_ulong *args) 1181 { 1182 SpaprXive *xive = spapr->xive; 1183 XiveENDSource *end_xsrc = &xive->end_source; 1184 target_ulong flags = args[0]; 1185 target_ulong target = args[1]; 1186 target_ulong priority = args[2]; 1187 XiveEND *end; 1188 uint8_t end_blk; 1189 uint32_t end_idx; 1190 1191 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1192 return H_FUNCTION; 1193 } 1194 1195 if (flags) { 1196 return H_PARAMETER; 1197 } 1198 1199 /* 1200 * H_STATE should be returned if a H_INT_RESET is in progress. 1201 * This is not needed when running the emulation under QEMU 1202 */ 1203 1204 if (spapr_xive_priority_is_reserved(xive, priority)) { 1205 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld 1206 " is reserved\n", priority); 1207 return H_P3; 1208 } 1209 1210 /* 1211 * Validate that "target" is part of the list of threads allocated 1212 * to the partition. For that, find the END corresponding to the 1213 * target. 1214 */ 1215 if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) { 1216 return H_P2; 1217 } 1218 1219 assert(end_idx < xive->nr_ends); 1220 end = &xive->endt[end_idx]; 1221 1222 args[0] = xive->end_base + (1ull << (end_xsrc->esb_shift + 1)) * end_idx; 1223 if (xive_end_is_enqueue(end)) { 1224 args[1] = xive_get_field32(END_W0_QSIZE, end->w0) + 12; 1225 } else { 1226 args[1] = 0; 1227 } 1228 1229 return H_SUCCESS; 1230 } 1231 1232 /* 1233 * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for 1234 * a given "target" and "priority". It is also used to set the 1235 * notification config associated with the EQ. An EQ size of 0 is 1236 * used to reset the EQ config for a given target and priority. If 1237 * resetting the EQ config, the END associated with the given "target" 1238 * and "priority" will be changed to disable queueing. 1239 * 1240 * Upon return from the hcall(), no additional interrupts will be 1241 * directed to the old EQ (if one was set). The old EQ (if one was 1242 * set) should be investigated for interrupts that occurred prior to 1243 * or during the hcall(). 1244 * 1245 * Parameters: 1246 * Input: 1247 * - R4: "flags" 1248 * Bits 0-62: Reserved 1249 * Bit 63: Unconditional Notify (n) per the XIVE spec 1250 * - R5: "target" is per "ibm,ppc-interrupt-server#s" or 1251 * "ibm,ppc-interrupt-gserver#s" 1252 * - R6: "priority" is a valid priority not in 1253 * "ibm,plat-res-int-priorities" 1254 * - R7: "eventQueue": The logical real address of the start of the EQ 1255 * - R8: "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes" 1256 * 1257 * Output: 1258 * - None 1259 */ 1260 1261 #define SPAPR_XIVE_END_ALWAYS_NOTIFY PPC_BIT(63) 1262 1263 static target_ulong h_int_set_queue_config(PowerPCCPU *cpu, 1264 SpaprMachineState *spapr, 1265 target_ulong opcode, 1266 target_ulong *args) 1267 { 1268 SpaprXive *xive = spapr->xive; 1269 target_ulong flags = args[0]; 1270 target_ulong target = args[1]; 1271 target_ulong priority = args[2]; 1272 target_ulong qpage = args[3]; 1273 target_ulong qsize = args[4]; 1274 XiveEND end; 1275 uint8_t end_blk, nvt_blk; 1276 uint32_t end_idx, nvt_idx; 1277 1278 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1279 return H_FUNCTION; 1280 } 1281 1282 if (flags & ~SPAPR_XIVE_END_ALWAYS_NOTIFY) { 1283 return H_PARAMETER; 1284 } 1285 1286 /* 1287 * H_STATE should be returned if a H_INT_RESET is in progress. 1288 * This is not needed when running the emulation under QEMU 1289 */ 1290 1291 if (spapr_xive_priority_is_reserved(xive, priority)) { 1292 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld 1293 " is reserved\n", priority); 1294 return H_P3; 1295 } 1296 1297 /* 1298 * Validate that "target" is part of the list of threads allocated 1299 * to the partition. For that, find the END corresponding to the 1300 * target. 1301 */ 1302 1303 if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) { 1304 return H_P2; 1305 } 1306 1307 assert(end_idx < xive->nr_ends); 1308 memcpy(&end, &xive->endt[end_idx], sizeof(XiveEND)); 1309 1310 switch (qsize) { 1311 case 12: 1312 case 16: 1313 case 21: 1314 case 24: 1315 if (!QEMU_IS_ALIGNED(qpage, 1ul << qsize)) { 1316 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: EQ @0x%" HWADDR_PRIx 1317 " is not naturally aligned with %" HWADDR_PRIx "\n", 1318 qpage, (hwaddr)1 << qsize); 1319 return H_P4; 1320 } 1321 end.w2 = cpu_to_be32((qpage >> 32) & 0x0fffffff); 1322 end.w3 = cpu_to_be32(qpage & 0xffffffff); 1323 end.w0 |= cpu_to_be32(END_W0_ENQUEUE); 1324 end.w0 = xive_set_field32(END_W0_QSIZE, end.w0, qsize - 12); 1325 break; 1326 case 0: 1327 /* reset queue and disable queueing */ 1328 spapr_xive_end_reset(&end); 1329 goto out; 1330 1331 default: 1332 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid EQ size %"PRIx64"\n", 1333 qsize); 1334 return H_P5; 1335 } 1336 1337 if (qsize) { 1338 hwaddr plen = 1 << qsize; 1339 void *eq; 1340 1341 /* 1342 * Validate the guest EQ. We should also check that the queue 1343 * has been zeroed by the OS. 1344 */ 1345 eq = address_space_map(CPU(cpu)->as, qpage, &plen, true, 1346 MEMTXATTRS_UNSPECIFIED); 1347 if (plen != 1 << qsize) { 1348 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to map EQ @0x%" 1349 HWADDR_PRIx "\n", qpage); 1350 return H_P4; 1351 } 1352 address_space_unmap(CPU(cpu)->as, eq, plen, true, plen); 1353 } 1354 1355 /* "target" should have been validated above */ 1356 if (spapr_xive_target_to_nvt(target, &nvt_blk, &nvt_idx)) { 1357 g_assert_not_reached(); 1358 } 1359 1360 /* 1361 * Ensure the priority and target are correctly set (they will not 1362 * be right after allocation) 1363 */ 1364 end.w6 = xive_set_field32(END_W6_NVT_BLOCK, 0ul, nvt_blk) | 1365 xive_set_field32(END_W6_NVT_INDEX, 0ul, nvt_idx); 1366 end.w7 = xive_set_field32(END_W7_F0_PRIORITY, 0ul, priority); 1367 1368 if (flags & SPAPR_XIVE_END_ALWAYS_NOTIFY) { 1369 end.w0 |= cpu_to_be32(END_W0_UCOND_NOTIFY); 1370 } else { 1371 end.w0 &= cpu_to_be32((uint32_t)~END_W0_UCOND_NOTIFY); 1372 } 1373 1374 /* 1375 * The generation bit for the END starts at 1 and The END page 1376 * offset counter starts at 0. 1377 */ 1378 end.w1 = cpu_to_be32(END_W1_GENERATION) | 1379 xive_set_field32(END_W1_PAGE_OFF, 0ul, 0ul); 1380 end.w0 |= cpu_to_be32(END_W0_VALID); 1381 1382 /* 1383 * TODO: issue syncs required to ensure all in-flight interrupts 1384 * are complete on the old END 1385 */ 1386 1387 out: 1388 if (spapr_xive_in_kernel(xive)) { 1389 Error *local_err = NULL; 1390 1391 kvmppc_xive_set_queue_config(xive, end_blk, end_idx, &end, &local_err); 1392 if (local_err) { 1393 error_report_err(local_err); 1394 return H_HARDWARE; 1395 } 1396 } 1397 1398 /* Update END */ 1399 memcpy(&xive->endt[end_idx], &end, sizeof(XiveEND)); 1400 return H_SUCCESS; 1401 } 1402 1403 /* 1404 * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given 1405 * target and priority. 1406 * 1407 * Parameters: 1408 * Input: 1409 * - R4: "flags" 1410 * Bits 0-62: Reserved 1411 * Bit 63: Debug: Return debug data 1412 * - R5: "target" is per "ibm,ppc-interrupt-server#s" or 1413 * "ibm,ppc-interrupt-gserver#s" 1414 * - R6: "priority" is a valid priority not in 1415 * "ibm,plat-res-int-priorities" 1416 * 1417 * Output: 1418 * - R4: "flags": 1419 * Bits 0-61: Reserved 1420 * Bit 62: The value of Event Queue Generation Number (g) per 1421 * the XIVE spec if "Debug" = 1 1422 * Bit 63: The value of Unconditional Notify (n) per the XIVE spec 1423 * - R5: The logical real address of the start of the EQ 1424 * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes" 1425 * - R7: The value of Event Queue Offset Counter per XIVE spec 1426 * if "Debug" = 1, else 0 1427 * 1428 */ 1429 1430 #define SPAPR_XIVE_END_DEBUG PPC_BIT(63) 1431 1432 static target_ulong h_int_get_queue_config(PowerPCCPU *cpu, 1433 SpaprMachineState *spapr, 1434 target_ulong opcode, 1435 target_ulong *args) 1436 { 1437 SpaprXive *xive = spapr->xive; 1438 target_ulong flags = args[0]; 1439 target_ulong target = args[1]; 1440 target_ulong priority = args[2]; 1441 XiveEND *end; 1442 uint8_t end_blk; 1443 uint32_t end_idx; 1444 1445 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1446 return H_FUNCTION; 1447 } 1448 1449 if (flags & ~SPAPR_XIVE_END_DEBUG) { 1450 return H_PARAMETER; 1451 } 1452 1453 /* 1454 * H_STATE should be returned if a H_INT_RESET is in progress. 1455 * This is not needed when running the emulation under QEMU 1456 */ 1457 1458 if (spapr_xive_priority_is_reserved(xive, priority)) { 1459 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld 1460 " is reserved\n", priority); 1461 return H_P3; 1462 } 1463 1464 /* 1465 * Validate that "target" is part of the list of threads allocated 1466 * to the partition. For that, find the END corresponding to the 1467 * target. 1468 */ 1469 if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) { 1470 return H_P2; 1471 } 1472 1473 assert(end_idx < xive->nr_ends); 1474 end = &xive->endt[end_idx]; 1475 1476 args[0] = 0; 1477 if (xive_end_is_notify(end)) { 1478 args[0] |= SPAPR_XIVE_END_ALWAYS_NOTIFY; 1479 } 1480 1481 if (xive_end_is_enqueue(end)) { 1482 args[1] = xive_end_qaddr(end); 1483 args[2] = xive_get_field32(END_W0_QSIZE, end->w0) + 12; 1484 } else { 1485 args[1] = 0; 1486 args[2] = 0; 1487 } 1488 1489 if (spapr_xive_in_kernel(xive)) { 1490 Error *local_err = NULL; 1491 1492 kvmppc_xive_get_queue_config(xive, end_blk, end_idx, end, &local_err); 1493 if (local_err) { 1494 error_report_err(local_err); 1495 return H_HARDWARE; 1496 } 1497 } 1498 1499 /* TODO: do we need any locking on the END ? */ 1500 if (flags & SPAPR_XIVE_END_DEBUG) { 1501 /* Load the event queue generation number into the return flags */ 1502 args[0] |= (uint64_t)xive_get_field32(END_W1_GENERATION, end->w1) << 62; 1503 1504 /* Load R7 with the event queue offset counter */ 1505 args[3] = xive_get_field32(END_W1_PAGE_OFF, end->w1); 1506 } else { 1507 args[3] = 0; 1508 } 1509 1510 return H_SUCCESS; 1511 } 1512 1513 /* 1514 * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the 1515 * reporting cache line pair for the calling thread. The reporting 1516 * cache lines will contain the OS interrupt context when the OS 1517 * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS 1518 * interrupt. The reporting cache lines can be reset by inputting -1 1519 * in "reportingLine". Issuing the CI store byte without reporting 1520 * cache lines registered will result in the data not being accessible 1521 * to the OS. 1522 * 1523 * Parameters: 1524 * Input: 1525 * - R4: "flags" 1526 * Bits 0-63: Reserved 1527 * - R5: "reportingLine": The logical real address of the reporting cache 1528 * line pair 1529 * 1530 * Output: 1531 * - None 1532 */ 1533 static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu, 1534 SpaprMachineState *spapr, 1535 target_ulong opcode, 1536 target_ulong *args) 1537 { 1538 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1539 return H_FUNCTION; 1540 } 1541 1542 /* 1543 * H_STATE should be returned if a H_INT_RESET is in progress. 1544 * This is not needed when running the emulation under QEMU 1545 */ 1546 1547 /* TODO: H_INT_SET_OS_REPORTING_LINE */ 1548 return H_FUNCTION; 1549 } 1550 1551 /* 1552 * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical 1553 * real address of the reporting cache line pair set for the input 1554 * "target". If no reporting cache line pair has been set, -1 is 1555 * returned. 1556 * 1557 * Parameters: 1558 * Input: 1559 * - R4: "flags" 1560 * Bits 0-63: Reserved 1561 * - R5: "target" is per "ibm,ppc-interrupt-server#s" or 1562 * "ibm,ppc-interrupt-gserver#s" 1563 * - R6: "reportingLine": The logical real address of the reporting 1564 * cache line pair 1565 * 1566 * Output: 1567 * - R4: The logical real address of the reporting line if set, else -1 1568 */ 1569 static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu, 1570 SpaprMachineState *spapr, 1571 target_ulong opcode, 1572 target_ulong *args) 1573 { 1574 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1575 return H_FUNCTION; 1576 } 1577 1578 /* 1579 * H_STATE should be returned if a H_INT_RESET is in progress. 1580 * This is not needed when running the emulation under QEMU 1581 */ 1582 1583 /* TODO: H_INT_GET_OS_REPORTING_LINE */ 1584 return H_FUNCTION; 1585 } 1586 1587 /* 1588 * The H_INT_ESB hcall() is used to issue a load or store to the ESB 1589 * page for the input "lisn". This hcall is only supported for LISNs 1590 * that have the ESB hcall flag set to 1 when returned from hcall() 1591 * H_INT_GET_SOURCE_INFO. 1592 * 1593 * Parameters: 1594 * Input: 1595 * - R4: "flags" 1596 * Bits 0-62: Reserved 1597 * bit 63: Store: Store=1, store operation, else load operation 1598 * - R5: "lisn" is per "interrupts", "interrupt-map", or 1599 * "ibm,xive-lisn-ranges" properties, or as returned by the 1600 * ibm,query-interrupt-source-number RTAS call, or as 1601 * returned by the H_ALLOCATE_VAS_WINDOW hcall 1602 * - R6: "esbOffset" is the offset into the ESB page for the load or 1603 * store operation 1604 * - R7: "storeData" is the data to write for a store operation 1605 * 1606 * Output: 1607 * - R4: The value of the load if load operation, else -1 1608 */ 1609 1610 #define SPAPR_XIVE_ESB_STORE PPC_BIT(63) 1611 1612 static target_ulong h_int_esb(PowerPCCPU *cpu, 1613 SpaprMachineState *spapr, 1614 target_ulong opcode, 1615 target_ulong *args) 1616 { 1617 SpaprXive *xive = spapr->xive; 1618 XiveEAS eas; 1619 target_ulong flags = args[0]; 1620 target_ulong lisn = args[1]; 1621 target_ulong offset = args[2]; 1622 target_ulong data = args[3]; 1623 hwaddr mmio_addr; 1624 XiveSource *xsrc = &xive->source; 1625 1626 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1627 return H_FUNCTION; 1628 } 1629 1630 if (flags & ~SPAPR_XIVE_ESB_STORE) { 1631 return H_PARAMETER; 1632 } 1633 1634 if (lisn >= xive->nr_irqs) { 1635 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n", 1636 lisn); 1637 return H_P2; 1638 } 1639 1640 eas = xive->eat[lisn]; 1641 if (!xive_eas_is_valid(&eas)) { 1642 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n", 1643 lisn); 1644 return H_P2; 1645 } 1646 1647 if (offset > (1ull << xsrc->esb_shift)) { 1648 return H_P3; 1649 } 1650 1651 if (spapr_xive_in_kernel(xive)) { 1652 args[0] = kvmppc_xive_esb_rw(xsrc, lisn, offset, data, 1653 flags & SPAPR_XIVE_ESB_STORE); 1654 } else { 1655 mmio_addr = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn) + offset; 1656 1657 if (dma_memory_rw(&address_space_memory, mmio_addr, &data, 8, 1658 (flags & SPAPR_XIVE_ESB_STORE))) { 1659 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to access ESB @0x%" 1660 HWADDR_PRIx "\n", mmio_addr); 1661 return H_HARDWARE; 1662 } 1663 args[0] = (flags & SPAPR_XIVE_ESB_STORE) ? -1 : data; 1664 } 1665 return H_SUCCESS; 1666 } 1667 1668 /* 1669 * The H_INT_SYNC hcall() is used to issue hardware syncs that will 1670 * ensure any in flight events for the input lisn are in the event 1671 * queue. 1672 * 1673 * Parameters: 1674 * Input: 1675 * - R4: "flags" 1676 * Bits 0-63: Reserved 1677 * - R5: "lisn" is per "interrupts", "interrupt-map", or 1678 * "ibm,xive-lisn-ranges" properties, or as returned by the 1679 * ibm,query-interrupt-source-number RTAS call, or as 1680 * returned by the H_ALLOCATE_VAS_WINDOW hcall 1681 * 1682 * Output: 1683 * - None 1684 */ 1685 static target_ulong h_int_sync(PowerPCCPU *cpu, 1686 SpaprMachineState *spapr, 1687 target_ulong opcode, 1688 target_ulong *args) 1689 { 1690 SpaprXive *xive = spapr->xive; 1691 XiveEAS eas; 1692 target_ulong flags = args[0]; 1693 target_ulong lisn = args[1]; 1694 1695 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1696 return H_FUNCTION; 1697 } 1698 1699 if (flags) { 1700 return H_PARAMETER; 1701 } 1702 1703 if (lisn >= xive->nr_irqs) { 1704 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n", 1705 lisn); 1706 return H_P2; 1707 } 1708 1709 eas = xive->eat[lisn]; 1710 if (!xive_eas_is_valid(&eas)) { 1711 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n", 1712 lisn); 1713 return H_P2; 1714 } 1715 1716 /* 1717 * H_STATE should be returned if a H_INT_RESET is in progress. 1718 * This is not needed when running the emulation under QEMU 1719 */ 1720 1721 /* 1722 * This is not real hardware. Nothing to be done unless when 1723 * under KVM 1724 */ 1725 1726 if (spapr_xive_in_kernel(xive)) { 1727 Error *local_err = NULL; 1728 1729 kvmppc_xive_sync_source(xive, lisn, &local_err); 1730 if (local_err) { 1731 error_report_err(local_err); 1732 return H_HARDWARE; 1733 } 1734 } 1735 return H_SUCCESS; 1736 } 1737 1738 /* 1739 * The H_INT_RESET hcall() is used to reset all of the partition's 1740 * interrupt exploitation structures to their initial state. This 1741 * means losing all previously set interrupt state set via 1742 * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG. 1743 * 1744 * Parameters: 1745 * Input: 1746 * - R4: "flags" 1747 * Bits 0-63: Reserved 1748 * 1749 * Output: 1750 * - None 1751 */ 1752 static target_ulong h_int_reset(PowerPCCPU *cpu, 1753 SpaprMachineState *spapr, 1754 target_ulong opcode, 1755 target_ulong *args) 1756 { 1757 SpaprXive *xive = spapr->xive; 1758 target_ulong flags = args[0]; 1759 1760 if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) { 1761 return H_FUNCTION; 1762 } 1763 1764 if (flags) { 1765 return H_PARAMETER; 1766 } 1767 1768 device_legacy_reset(DEVICE(xive)); 1769 1770 if (spapr_xive_in_kernel(xive)) { 1771 Error *local_err = NULL; 1772 1773 kvmppc_xive_reset(xive, &local_err); 1774 if (local_err) { 1775 error_report_err(local_err); 1776 return H_HARDWARE; 1777 } 1778 } 1779 return H_SUCCESS; 1780 } 1781 1782 void spapr_xive_hcall_init(SpaprMachineState *spapr) 1783 { 1784 spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info); 1785 spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config); 1786 spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config); 1787 spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info); 1788 spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config); 1789 spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config); 1790 spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE, 1791 h_int_set_os_reporting_line); 1792 spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE, 1793 h_int_get_os_reporting_line); 1794 spapr_register_hypercall(H_INT_ESB, h_int_esb); 1795 spapr_register_hypercall(H_INT_SYNC, h_int_sync); 1796 spapr_register_hypercall(H_INT_RESET, h_int_reset); 1797 } 1798