1 /* 2 * QEMU PowerPC sPAPR IRQ interface 3 * 4 * Copyright (c) 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/error-report.h" 13 #include "qapi/error.h" 14 #include "hw/ppc/spapr.h" 15 #include "hw/ppc/spapr_xive.h" 16 #include "hw/ppc/xics.h" 17 #include "sysemu/kvm.h" 18 19 #include "trace.h" 20 21 void spapr_irq_msi_init(sPAPRMachineState *spapr, uint32_t nr_msis) 22 { 23 spapr->irq_map_nr = nr_msis; 24 spapr->irq_map = bitmap_new(spapr->irq_map_nr); 25 } 26 27 int spapr_irq_msi_alloc(sPAPRMachineState *spapr, uint32_t num, bool align, 28 Error **errp) 29 { 30 int irq; 31 32 /* 33 * The 'align_mask' parameter of bitmap_find_next_zero_area() 34 * should be one less than a power of 2; 0 means no 35 * alignment. Adapt the 'align' value of the former allocator 36 * to fit the requirements of bitmap_find_next_zero_area() 37 */ 38 align -= 1; 39 40 irq = bitmap_find_next_zero_area(spapr->irq_map, spapr->irq_map_nr, 0, num, 41 align); 42 if (irq == spapr->irq_map_nr) { 43 error_setg(errp, "can't find a free %d-IRQ block", num); 44 return -1; 45 } 46 47 bitmap_set(spapr->irq_map, irq, num); 48 49 return irq + SPAPR_IRQ_MSI; 50 } 51 52 void spapr_irq_msi_free(sPAPRMachineState *spapr, int irq, uint32_t num) 53 { 54 bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num); 55 } 56 57 void spapr_irq_msi_reset(sPAPRMachineState *spapr) 58 { 59 bitmap_clear(spapr->irq_map, 0, spapr->irq_map_nr); 60 } 61 62 63 /* 64 * XICS IRQ backend. 65 */ 66 67 static ICSState *spapr_ics_create(sPAPRMachineState *spapr, 68 const char *type_ics, 69 int nr_irqs, Error **errp) 70 { 71 Error *local_err = NULL; 72 Object *obj; 73 74 obj = object_new(type_ics); 75 object_property_add_child(OBJECT(spapr), "ics", obj, &error_abort); 76 object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr), 77 &error_abort); 78 object_property_set_int(obj, nr_irqs, "nr-irqs", &local_err); 79 if (local_err) { 80 goto error; 81 } 82 object_property_set_bool(obj, true, "realized", &local_err); 83 if (local_err) { 84 goto error; 85 } 86 87 return ICS_BASE(obj); 88 89 error: 90 error_propagate(errp, local_err); 91 return NULL; 92 } 93 94 static void spapr_irq_init_xics(sPAPRMachineState *spapr, Error **errp) 95 { 96 MachineState *machine = MACHINE(spapr); 97 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 98 int nr_irqs = smc->irq->nr_irqs; 99 Error *local_err = NULL; 100 101 if (kvm_enabled()) { 102 if (machine_kernel_irqchip_allowed(machine) && 103 !xics_kvm_init(spapr, &local_err)) { 104 spapr->icp_type = TYPE_KVM_ICP; 105 spapr->ics = spapr_ics_create(spapr, TYPE_ICS_KVM, nr_irqs, 106 &local_err); 107 } 108 if (machine_kernel_irqchip_required(machine) && !spapr->ics) { 109 error_prepend(&local_err, 110 "kernel_irqchip requested but unavailable: "); 111 goto error; 112 } 113 error_free(local_err); 114 local_err = NULL; 115 } 116 117 if (!spapr->ics) { 118 xics_spapr_init(spapr); 119 spapr->icp_type = TYPE_ICP; 120 spapr->ics = spapr_ics_create(spapr, TYPE_ICS_SIMPLE, nr_irqs, 121 &local_err); 122 } 123 124 error: 125 error_propagate(errp, local_err); 126 } 127 128 #define ICS_IRQ_FREE(ics, srcno) \ 129 (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK))) 130 131 static int spapr_irq_claim_xics(sPAPRMachineState *spapr, int irq, bool lsi, 132 Error **errp) 133 { 134 ICSState *ics = spapr->ics; 135 136 assert(ics); 137 138 if (!ics_valid_irq(ics, irq)) { 139 error_setg(errp, "IRQ %d is invalid", irq); 140 return -1; 141 } 142 143 if (!ICS_IRQ_FREE(ics, irq - ics->offset)) { 144 error_setg(errp, "IRQ %d is not free", irq); 145 return -1; 146 } 147 148 ics_set_irq_type(ics, irq - ics->offset, lsi); 149 return 0; 150 } 151 152 static void spapr_irq_free_xics(sPAPRMachineState *spapr, int irq, int num) 153 { 154 ICSState *ics = spapr->ics; 155 uint32_t srcno = irq - ics->offset; 156 int i; 157 158 if (ics_valid_irq(ics, irq)) { 159 trace_spapr_irq_free(0, irq, num); 160 for (i = srcno; i < srcno + num; ++i) { 161 if (ICS_IRQ_FREE(ics, i)) { 162 trace_spapr_irq_free_warn(0, i); 163 } 164 memset(&ics->irqs[i], 0, sizeof(ICSIRQState)); 165 } 166 } 167 } 168 169 static qemu_irq spapr_qirq_xics(sPAPRMachineState *spapr, int irq) 170 { 171 ICSState *ics = spapr->ics; 172 uint32_t srcno = irq - ics->offset; 173 174 if (ics_valid_irq(ics, irq)) { 175 return ics->qirqs[srcno]; 176 } 177 178 return NULL; 179 } 180 181 static void spapr_irq_print_info_xics(sPAPRMachineState *spapr, Monitor *mon) 182 { 183 CPUState *cs; 184 185 CPU_FOREACH(cs) { 186 PowerPCCPU *cpu = POWERPC_CPU(cs); 187 188 icp_pic_print_info(ICP(cpu->intc), mon); 189 } 190 191 ics_pic_print_info(spapr->ics, mon); 192 } 193 194 #define SPAPR_IRQ_XICS_NR_IRQS 0x1000 195 #define SPAPR_IRQ_XICS_NR_MSIS \ 196 (XICS_IRQ_BASE + SPAPR_IRQ_XICS_NR_IRQS - SPAPR_IRQ_MSI) 197 198 sPAPRIrq spapr_irq_xics = { 199 .nr_irqs = SPAPR_IRQ_XICS_NR_IRQS, 200 .nr_msis = SPAPR_IRQ_XICS_NR_MSIS, 201 202 .init = spapr_irq_init_xics, 203 .claim = spapr_irq_claim_xics, 204 .free = spapr_irq_free_xics, 205 .qirq = spapr_qirq_xics, 206 .print_info = spapr_irq_print_info_xics, 207 }; 208 209 /* 210 * XIVE IRQ backend. 211 */ 212 static void spapr_irq_init_xive(sPAPRMachineState *spapr, Error **errp) 213 { 214 MachineState *machine = MACHINE(spapr); 215 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 216 uint32_t nr_servers = spapr_max_server_number(spapr); 217 DeviceState *dev; 218 int i; 219 220 /* KVM XIVE device not yet available */ 221 if (kvm_enabled()) { 222 if (machine_kernel_irqchip_required(machine)) { 223 error_setg(errp, "kernel_irqchip requested. no KVM XIVE support"); 224 return; 225 } 226 } 227 228 dev = qdev_create(NULL, TYPE_SPAPR_XIVE); 229 qdev_prop_set_uint32(dev, "nr-irqs", smc->irq->nr_irqs); 230 /* 231 * 8 XIVE END structures per CPU. One for each available priority 232 */ 233 qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3); 234 qdev_init_nofail(dev); 235 236 spapr->xive = SPAPR_XIVE(dev); 237 238 /* Enable the CPU IPIs */ 239 for (i = 0; i < nr_servers; ++i) { 240 spapr_xive_irq_claim(spapr->xive, SPAPR_IRQ_IPI + i, false); 241 } 242 } 243 244 static int spapr_irq_claim_xive(sPAPRMachineState *spapr, int irq, bool lsi, 245 Error **errp) 246 { 247 if (!spapr_xive_irq_claim(spapr->xive, irq, lsi)) { 248 error_setg(errp, "IRQ %d is invalid", irq); 249 return -1; 250 } 251 return 0; 252 } 253 254 static void spapr_irq_free_xive(sPAPRMachineState *spapr, int irq, int num) 255 { 256 int i; 257 258 for (i = irq; i < irq + num; ++i) { 259 spapr_xive_irq_free(spapr->xive, i); 260 } 261 } 262 263 static qemu_irq spapr_qirq_xive(sPAPRMachineState *spapr, int irq) 264 { 265 return spapr_xive_qirq(spapr->xive, irq); 266 } 267 268 static void spapr_irq_print_info_xive(sPAPRMachineState *spapr, 269 Monitor *mon) 270 { 271 CPUState *cs; 272 273 CPU_FOREACH(cs) { 274 PowerPCCPU *cpu = POWERPC_CPU(cs); 275 276 xive_tctx_pic_print_info(XIVE_TCTX(cpu->intc), mon); 277 } 278 279 spapr_xive_pic_print_info(spapr->xive, mon); 280 } 281 282 /* 283 * XIVE uses the full IRQ number space. Set it to 8K to be compatible 284 * with XICS. 285 */ 286 287 #define SPAPR_IRQ_XIVE_NR_IRQS 0x2000 288 #define SPAPR_IRQ_XIVE_NR_MSIS (SPAPR_IRQ_XIVE_NR_IRQS - SPAPR_IRQ_MSI) 289 290 sPAPRIrq spapr_irq_xive = { 291 .nr_irqs = SPAPR_IRQ_XIVE_NR_IRQS, 292 .nr_msis = SPAPR_IRQ_XIVE_NR_MSIS, 293 294 .init = spapr_irq_init_xive, 295 .claim = spapr_irq_claim_xive, 296 .free = spapr_irq_free_xive, 297 .qirq = spapr_qirq_xive, 298 .print_info = spapr_irq_print_info_xive, 299 }; 300 301 /* 302 * sPAPR IRQ frontend routines for devices 303 */ 304 void spapr_irq_init(sPAPRMachineState *spapr, Error **errp) 305 { 306 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 307 308 /* Initialize the MSI IRQ allocator. */ 309 if (!SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) { 310 spapr_irq_msi_init(spapr, smc->irq->nr_msis); 311 } 312 313 smc->irq->init(spapr, errp); 314 } 315 316 int spapr_irq_claim(sPAPRMachineState *spapr, int irq, bool lsi, Error **errp) 317 { 318 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 319 320 return smc->irq->claim(spapr, irq, lsi, errp); 321 } 322 323 void spapr_irq_free(sPAPRMachineState *spapr, int irq, int num) 324 { 325 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 326 327 smc->irq->free(spapr, irq, num); 328 } 329 330 qemu_irq spapr_qirq(sPAPRMachineState *spapr, int irq) 331 { 332 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 333 334 return smc->irq->qirq(spapr, irq); 335 } 336 337 /* 338 * XICS legacy routines - to deprecate one day 339 */ 340 341 static int ics_find_free_block(ICSState *ics, int num, int alignnum) 342 { 343 int first, i; 344 345 for (first = 0; first < ics->nr_irqs; first += alignnum) { 346 if (num > (ics->nr_irqs - first)) { 347 return -1; 348 } 349 for (i = first; i < first + num; ++i) { 350 if (!ICS_IRQ_FREE(ics, i)) { 351 break; 352 } 353 } 354 if (i == (first + num)) { 355 return first; 356 } 357 } 358 359 return -1; 360 } 361 362 int spapr_irq_find(sPAPRMachineState *spapr, int num, bool align, Error **errp) 363 { 364 ICSState *ics = spapr->ics; 365 int first = -1; 366 367 assert(ics); 368 369 /* 370 * MSIMesage::data is used for storing VIRQ so 371 * it has to be aligned to num to support multiple 372 * MSI vectors. MSI-X is not affected by this. 373 * The hint is used for the first IRQ, the rest should 374 * be allocated continuously. 375 */ 376 if (align) { 377 assert((num == 1) || (num == 2) || (num == 4) || 378 (num == 8) || (num == 16) || (num == 32)); 379 first = ics_find_free_block(ics, num, num); 380 } else { 381 first = ics_find_free_block(ics, num, 1); 382 } 383 384 if (first < 0) { 385 error_setg(errp, "can't find a free %d-IRQ block", num); 386 return -1; 387 } 388 389 return first + ics->offset; 390 } 391 392 #define SPAPR_IRQ_XICS_LEGACY_NR_IRQS 0x400 393 394 sPAPRIrq spapr_irq_xics_legacy = { 395 .nr_irqs = SPAPR_IRQ_XICS_LEGACY_NR_IRQS, 396 .nr_msis = SPAPR_IRQ_XICS_LEGACY_NR_IRQS, 397 398 .init = spapr_irq_init_xics, 399 .claim = spapr_irq_claim_xics, 400 .free = spapr_irq_free_xics, 401 .qirq = spapr_qirq_xics, 402 .print_info = spapr_irq_print_info_xics, 403 }; 404