1 // SPDX-License-Identifier: GPL-2.0 2 #define pr_fmt(fmt) "zpci: " fmt 3 4 #include <linux/kernel.h> 5 #include <linux/irq.h> 6 #include <linux/kernel_stat.h> 7 #include <linux/pci.h> 8 #include <linux/msi.h> 9 #include <linux/smp.h> 10 11 #include <asm/isc.h> 12 #include <asm/airq.h> 13 #include <asm/tpi.h> 14 15 static enum {FLOATING, DIRECTED} irq_delivery; 16 17 /* 18 * summary bit vector 19 * FLOATING - summary bit per function 20 * DIRECTED - summary bit per cpu (only used in fallback path) 21 */ 22 static struct airq_iv *zpci_sbv; 23 24 /* 25 * interrupt bit vectors 26 * FLOATING - interrupt bit vector per function 27 * DIRECTED - interrupt bit vector per cpu 28 */ 29 static struct airq_iv **zpci_ibv; 30 31 /* Modify PCI: Register floating adapter interruptions */ 32 static int zpci_set_airq(struct zpci_dev *zdev) 33 { 34 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 35 struct zpci_fib fib = {0}; 36 u8 status; 37 38 fib.fmt0.isc = PCI_ISC; 39 fib.fmt0.sum = 1; /* enable summary notifications */ 40 fib.fmt0.noi = airq_iv_end(zdev->aibv); 41 fib.fmt0.aibv = virt_to_phys(zdev->aibv->vector); 42 fib.fmt0.aibvo = 0; /* each zdev has its own interrupt vector */ 43 fib.fmt0.aisb = virt_to_phys(zpci_sbv->vector) + (zdev->aisb / 64) * 8; 44 fib.fmt0.aisbo = zdev->aisb & 63; 45 fib.gd = zdev->gisa; 46 47 return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 48 } 49 50 /* Modify PCI: Unregister floating adapter interruptions */ 51 static int zpci_clear_airq(struct zpci_dev *zdev) 52 { 53 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_DEREG_INT); 54 struct zpci_fib fib = {0}; 55 u8 cc, status; 56 57 fib.gd = zdev->gisa; 58 59 cc = zpci_mod_fc(req, &fib, &status); 60 if (cc == 3 || (cc == 1 && status == 24)) 61 /* Function already gone or IRQs already deregistered. */ 62 cc = 0; 63 64 return cc ? -EIO : 0; 65 } 66 67 /* Modify PCI: Register CPU directed interruptions */ 68 static int zpci_set_directed_irq(struct zpci_dev *zdev) 69 { 70 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT_D); 71 struct zpci_fib fib = {0}; 72 u8 status; 73 74 fib.fmt = 1; 75 fib.fmt1.noi = zdev->msi_nr_irqs; 76 fib.fmt1.dibvo = zdev->msi_first_bit; 77 fib.gd = zdev->gisa; 78 79 return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 80 } 81 82 /* Modify PCI: Unregister CPU directed interruptions */ 83 static int zpci_clear_directed_irq(struct zpci_dev *zdev) 84 { 85 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_DEREG_INT_D); 86 struct zpci_fib fib = {0}; 87 u8 cc, status; 88 89 fib.fmt = 1; 90 fib.gd = zdev->gisa; 91 cc = zpci_mod_fc(req, &fib, &status); 92 if (cc == 3 || (cc == 1 && status == 24)) 93 /* Function already gone or IRQs already deregistered. */ 94 cc = 0; 95 96 return cc ? -EIO : 0; 97 } 98 99 /* Register adapter interruptions */ 100 static int zpci_set_irq(struct zpci_dev *zdev) 101 { 102 int rc; 103 104 if (irq_delivery == DIRECTED) 105 rc = zpci_set_directed_irq(zdev); 106 else 107 rc = zpci_set_airq(zdev); 108 109 return rc; 110 } 111 112 /* Clear adapter interruptions */ 113 static int zpci_clear_irq(struct zpci_dev *zdev) 114 { 115 int rc; 116 117 if (irq_delivery == DIRECTED) 118 rc = zpci_clear_directed_irq(zdev); 119 else 120 rc = zpci_clear_airq(zdev); 121 122 return rc; 123 } 124 125 static int zpci_set_irq_affinity(struct irq_data *data, const struct cpumask *dest, 126 bool force) 127 { 128 struct msi_desc *entry = irq_data_get_msi_desc(data); 129 struct msi_msg msg = entry->msg; 130 int cpu_addr = smp_cpu_get_cpu_address(cpumask_first(dest)); 131 132 msg.address_lo &= 0xff0000ff; 133 msg.address_lo |= (cpu_addr << 8); 134 pci_write_msi_msg(data->irq, &msg); 135 136 return IRQ_SET_MASK_OK; 137 } 138 139 static struct irq_chip zpci_irq_chip = { 140 .name = "PCI-MSI", 141 .irq_unmask = pci_msi_unmask_irq, 142 .irq_mask = pci_msi_mask_irq, 143 }; 144 145 static void zpci_handle_cpu_local_irq(bool rescan) 146 { 147 struct airq_iv *dibv = zpci_ibv[smp_processor_id()]; 148 union zpci_sic_iib iib = {{0}}; 149 unsigned long bit; 150 int irqs_on = 0; 151 152 for (bit = 0;;) { 153 /* Scan the directed IRQ bit vector */ 154 bit = airq_iv_scan(dibv, bit, airq_iv_end(dibv)); 155 if (bit == -1UL) { 156 if (!rescan || irqs_on++) 157 /* End of second scan with interrupts on. */ 158 break; 159 /* First scan complete, re-enable interrupts. */ 160 if (zpci_set_irq_ctrl(SIC_IRQ_MODE_D_SINGLE, PCI_ISC, &iib)) 161 break; 162 bit = 0; 163 continue; 164 } 165 inc_irq_stat(IRQIO_MSI); 166 generic_handle_irq(airq_iv_get_data(dibv, bit)); 167 } 168 } 169 170 struct cpu_irq_data { 171 call_single_data_t csd; 172 atomic_t scheduled; 173 }; 174 static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpu_irq_data, irq_data); 175 176 static void zpci_handle_remote_irq(void *data) 177 { 178 atomic_t *scheduled = data; 179 180 do { 181 zpci_handle_cpu_local_irq(false); 182 } while (atomic_dec_return(scheduled)); 183 } 184 185 static void zpci_handle_fallback_irq(void) 186 { 187 struct cpu_irq_data *cpu_data; 188 union zpci_sic_iib iib = {{0}}; 189 unsigned long cpu; 190 int irqs_on = 0; 191 192 for (cpu = 0;;) { 193 cpu = airq_iv_scan(zpci_sbv, cpu, airq_iv_end(zpci_sbv)); 194 if (cpu == -1UL) { 195 if (irqs_on++) 196 /* End of second scan with interrupts on. */ 197 break; 198 /* First scan complete, re-enable interrupts. */ 199 if (zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC, &iib)) 200 break; 201 cpu = 0; 202 continue; 203 } 204 cpu_data = &per_cpu(irq_data, cpu); 205 if (atomic_inc_return(&cpu_data->scheduled) > 1) 206 continue; 207 208 INIT_CSD(&cpu_data->csd, zpci_handle_remote_irq, &cpu_data->scheduled); 209 smp_call_function_single_async(cpu, &cpu_data->csd); 210 } 211 } 212 213 static void zpci_directed_irq_handler(struct airq_struct *airq, 214 struct tpi_info *tpi_info) 215 { 216 bool floating = !tpi_info->directed_irq; 217 218 if (floating) { 219 inc_irq_stat(IRQIO_PCF); 220 zpci_handle_fallback_irq(); 221 } else { 222 inc_irq_stat(IRQIO_PCD); 223 zpci_handle_cpu_local_irq(true); 224 } 225 } 226 227 static void zpci_floating_irq_handler(struct airq_struct *airq, 228 struct tpi_info *tpi_info) 229 { 230 union zpci_sic_iib iib = {{0}}; 231 unsigned long si, ai; 232 struct airq_iv *aibv; 233 int irqs_on = 0; 234 235 inc_irq_stat(IRQIO_PCF); 236 for (si = 0;;) { 237 /* Scan adapter summary indicator bit vector */ 238 si = airq_iv_scan(zpci_sbv, si, airq_iv_end(zpci_sbv)); 239 if (si == -1UL) { 240 if (irqs_on++) 241 /* End of second scan with interrupts on. */ 242 break; 243 /* First scan complete, re-enable interrupts. */ 244 if (zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC, &iib)) 245 break; 246 si = 0; 247 continue; 248 } 249 250 /* Scan the adapter interrupt vector for this device. */ 251 aibv = zpci_ibv[si]; 252 for (ai = 0;;) { 253 ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 254 if (ai == -1UL) 255 break; 256 inc_irq_stat(IRQIO_MSI); 257 airq_iv_lock(aibv, ai); 258 generic_handle_irq(airq_iv_get_data(aibv, ai)); 259 airq_iv_unlock(aibv, ai); 260 } 261 } 262 } 263 264 static int __alloc_airq(struct zpci_dev *zdev, int msi_vecs, 265 unsigned long *bit) 266 { 267 if (irq_delivery == DIRECTED) { 268 /* Allocate cpu vector bits */ 269 *bit = airq_iv_alloc(zpci_ibv[0], msi_vecs); 270 if (*bit == -1UL) 271 return -EIO; 272 } else { 273 /* Allocate adapter summary indicator bit */ 274 *bit = airq_iv_alloc_bit(zpci_sbv); 275 if (*bit == -1UL) 276 return -EIO; 277 zdev->aisb = *bit; 278 279 /* Create adapter interrupt vector */ 280 zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK, NULL); 281 if (!zdev->aibv) 282 return -ENOMEM; 283 284 /* Wire up shortcut pointer */ 285 zpci_ibv[*bit] = zdev->aibv; 286 /* Each function has its own interrupt vector */ 287 *bit = 0; 288 } 289 return 0; 290 } 291 292 int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 293 { 294 unsigned int hwirq, msi_vecs, irqs_per_msi, i, cpu; 295 struct zpci_dev *zdev = to_zpci(pdev); 296 struct msi_desc *msi; 297 struct msi_msg msg; 298 unsigned long bit; 299 int cpu_addr; 300 int rc, irq; 301 302 zdev->aisb = -1UL; 303 zdev->msi_first_bit = -1U; 304 305 msi_vecs = min_t(unsigned int, nvec, zdev->max_msi); 306 if (msi_vecs < nvec) { 307 pr_info("%s requested %d irqs, allocate system limit of %d", 308 pci_name(pdev), nvec, zdev->max_msi); 309 } 310 311 rc = __alloc_airq(zdev, msi_vecs, &bit); 312 if (rc < 0) 313 return rc; 314 315 /* 316 * Request MSI interrupts: 317 * When using MSI, nvec_used interrupt sources and their irq 318 * descriptors are controlled through one msi descriptor. 319 * Thus the outer loop over msi descriptors shall run only once, 320 * while two inner loops iterate over the interrupt vectors. 321 * When using MSI-X, each interrupt vector/irq descriptor 322 * is bound to exactly one msi descriptor (nvec_used is one). 323 * So the inner loops are executed once, while the outer iterates 324 * over the MSI-X descriptors. 325 */ 326 hwirq = bit; 327 msi_for_each_desc(msi, &pdev->dev, MSI_DESC_NOTASSOCIATED) { 328 if (hwirq - bit >= msi_vecs) 329 break; 330 irqs_per_msi = min_t(unsigned int, msi_vecs, msi->nvec_used); 331 irq = __irq_alloc_descs(-1, 0, irqs_per_msi, 0, THIS_MODULE, 332 (irq_delivery == DIRECTED) ? 333 msi->affinity : NULL); 334 if (irq < 0) 335 return -ENOMEM; 336 337 for (i = 0; i < irqs_per_msi; i++) { 338 rc = irq_set_msi_desc_off(irq, i, msi); 339 if (rc) 340 return rc; 341 irq_set_chip_and_handler(irq + i, &zpci_irq_chip, 342 handle_percpu_irq); 343 } 344 345 msg.data = hwirq - bit; 346 if (irq_delivery == DIRECTED) { 347 if (msi->affinity) 348 cpu = cpumask_first(&msi->affinity->mask); 349 else 350 cpu = 0; 351 cpu_addr = smp_cpu_get_cpu_address(cpu); 352 353 msg.address_lo = zdev->msi_addr & 0xff0000ff; 354 msg.address_lo |= (cpu_addr << 8); 355 356 for_each_possible_cpu(cpu) { 357 for (i = 0; i < irqs_per_msi; i++) 358 airq_iv_set_data(zpci_ibv[cpu], 359 hwirq + i, irq + i); 360 } 361 } else { 362 msg.address_lo = zdev->msi_addr & 0xffffffff; 363 for (i = 0; i < irqs_per_msi; i++) 364 airq_iv_set_data(zdev->aibv, hwirq + i, irq + i); 365 } 366 msg.address_hi = zdev->msi_addr >> 32; 367 pci_write_msi_msg(irq, &msg); 368 hwirq += irqs_per_msi; 369 } 370 371 zdev->msi_first_bit = bit; 372 zdev->msi_nr_irqs = hwirq - bit; 373 374 rc = zpci_set_irq(zdev); 375 if (rc) 376 return rc; 377 378 return (zdev->msi_nr_irqs == nvec) ? 0 : zdev->msi_nr_irqs; 379 } 380 381 void arch_teardown_msi_irqs(struct pci_dev *pdev) 382 { 383 struct zpci_dev *zdev = to_zpci(pdev); 384 struct msi_desc *msi; 385 unsigned int i; 386 int rc; 387 388 /* Disable interrupts */ 389 rc = zpci_clear_irq(zdev); 390 if (rc) 391 return; 392 393 /* Release MSI interrupts */ 394 msi_for_each_desc(msi, &pdev->dev, MSI_DESC_ASSOCIATED) { 395 for (i = 0; i < msi->nvec_used; i++) { 396 irq_set_msi_desc(msi->irq + i, NULL); 397 irq_free_desc(msi->irq + i); 398 } 399 msi->msg.address_lo = 0; 400 msi->msg.address_hi = 0; 401 msi->msg.data = 0; 402 msi->irq = 0; 403 } 404 405 if (zdev->aisb != -1UL) { 406 zpci_ibv[zdev->aisb] = NULL; 407 airq_iv_free_bit(zpci_sbv, zdev->aisb); 408 zdev->aisb = -1UL; 409 } 410 if (zdev->aibv) { 411 airq_iv_release(zdev->aibv); 412 zdev->aibv = NULL; 413 } 414 415 if ((irq_delivery == DIRECTED) && zdev->msi_first_bit != -1U) 416 airq_iv_free(zpci_ibv[0], zdev->msi_first_bit, zdev->msi_nr_irqs); 417 } 418 419 bool arch_restore_msi_irqs(struct pci_dev *pdev) 420 { 421 struct zpci_dev *zdev = to_zpci(pdev); 422 423 zpci_set_irq(zdev); 424 return true; 425 } 426 427 static struct airq_struct zpci_airq = { 428 .handler = zpci_floating_irq_handler, 429 .isc = PCI_ISC, 430 }; 431 432 static void __init cpu_enable_directed_irq(void *unused) 433 { 434 union zpci_sic_iib iib = {{0}}; 435 union zpci_sic_iib ziib = {{0}}; 436 437 iib.cdiib.dibv_addr = virt_to_phys(zpci_ibv[smp_processor_id()]->vector); 438 439 zpci_set_irq_ctrl(SIC_IRQ_MODE_SET_CPU, 0, &iib); 440 zpci_set_irq_ctrl(SIC_IRQ_MODE_D_SINGLE, PCI_ISC, &ziib); 441 } 442 443 static int __init zpci_directed_irq_init(void) 444 { 445 union zpci_sic_iib iib = {{0}}; 446 unsigned int cpu; 447 448 zpci_sbv = airq_iv_create(num_possible_cpus(), 0, NULL); 449 if (!zpci_sbv) 450 return -ENOMEM; 451 452 iib.diib.isc = PCI_ISC; 453 iib.diib.nr_cpus = num_possible_cpus(); 454 iib.diib.disb_addr = virt_to_phys(zpci_sbv->vector); 455 zpci_set_irq_ctrl(SIC_IRQ_MODE_DIRECT, 0, &iib); 456 457 zpci_ibv = kcalloc(num_possible_cpus(), sizeof(*zpci_ibv), 458 GFP_KERNEL); 459 if (!zpci_ibv) 460 return -ENOMEM; 461 462 for_each_possible_cpu(cpu) { 463 /* 464 * Per CPU IRQ vectors look the same but bit-allocation 465 * is only done on the first vector. 466 */ 467 zpci_ibv[cpu] = airq_iv_create(cache_line_size() * BITS_PER_BYTE, 468 AIRQ_IV_DATA | 469 AIRQ_IV_CACHELINE | 470 (!cpu ? AIRQ_IV_ALLOC : 0), NULL); 471 if (!zpci_ibv[cpu]) 472 return -ENOMEM; 473 } 474 on_each_cpu(cpu_enable_directed_irq, NULL, 1); 475 476 zpci_irq_chip.irq_set_affinity = zpci_set_irq_affinity; 477 478 return 0; 479 } 480 481 static int __init zpci_floating_irq_init(void) 482 { 483 zpci_ibv = kcalloc(ZPCI_NR_DEVICES, sizeof(*zpci_ibv), GFP_KERNEL); 484 if (!zpci_ibv) 485 return -ENOMEM; 486 487 zpci_sbv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC, NULL); 488 if (!zpci_sbv) 489 goto out_free; 490 491 return 0; 492 493 out_free: 494 kfree(zpci_ibv); 495 return -ENOMEM; 496 } 497 498 int __init zpci_irq_init(void) 499 { 500 union zpci_sic_iib iib = {{0}}; 501 int rc; 502 503 irq_delivery = sclp.has_dirq ? DIRECTED : FLOATING; 504 if (s390_pci_force_floating) 505 irq_delivery = FLOATING; 506 507 if (irq_delivery == DIRECTED) 508 zpci_airq.handler = zpci_directed_irq_handler; 509 510 rc = register_adapter_interrupt(&zpci_airq); 511 if (rc) 512 goto out; 513 /* Set summary to 1 to be called every time for the ISC. */ 514 *zpci_airq.lsi_ptr = 1; 515 516 switch (irq_delivery) { 517 case FLOATING: 518 rc = zpci_floating_irq_init(); 519 break; 520 case DIRECTED: 521 rc = zpci_directed_irq_init(); 522 break; 523 } 524 525 if (rc) 526 goto out_airq; 527 528 /* 529 * Enable floating IRQs (with suppression after one IRQ). When using 530 * directed IRQs this enables the fallback path. 531 */ 532 zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, PCI_ISC, &iib); 533 534 return 0; 535 out_airq: 536 unregister_adapter_interrupt(&zpci_airq); 537 out: 538 return rc; 539 } 540 541 void __init zpci_irq_exit(void) 542 { 543 unsigned int cpu; 544 545 if (irq_delivery == DIRECTED) { 546 for_each_possible_cpu(cpu) { 547 airq_iv_release(zpci_ibv[cpu]); 548 } 549 } 550 kfree(zpci_ibv); 551 if (zpci_sbv) 552 airq_iv_release(zpci_sbv); 553 unregister_adapter_interrupt(&zpci_airq); 554 } 555