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