1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * apic_introp.c: 30 * Has code for Advanced DDI interrupt framework support. 31 */ 32 33 #include <sys/cpuvar.h> 34 #include <sys/psm.h> 35 #include <sys/archsystm.h> 36 #include <sys/apic.h> 37 #include <sys/sunddi.h> 38 #include <sys/ddi_impldefs.h> 39 #include <sys/mach_intr.h> 40 #include <sys/sysmacros.h> 41 #include <sys/trap.h> 42 #include <sys/pci.h> 43 #include <sys/pci_intr_lib.h> 44 45 extern struct av_head autovect[]; 46 47 /* 48 * Local Function Prototypes 49 */ 50 apic_irq_t *apic_find_irq(dev_info_t *, struct intrspec *, int); 51 52 /* 53 * MSI support flag: 54 * reflects whether MSI is supported at APIC level 55 * it can also be patched through /etc/system 56 * 57 * 0 = default value - don't know and need to call apic_check_msi_support() 58 * to find out then set it accordingly 59 * 1 = supported 60 * -1 = not supported 61 */ 62 int apic_support_msi = 0; 63 64 /* Multiple vector support for MSI */ 65 int apic_multi_msi_enable = 1; 66 int apic_multi_msi_max = 2; 67 68 /* Maximum no. of MSI-X vectors supported */ 69 int apic_msix_enable = 1; 70 int apic_msix_max = 2; 71 72 /* 73 * apic_pci_msi_enable_vector: 74 * Set the address/data fields in the MSI/X capability structure 75 * XXX: MSI-X support 76 */ 77 /* ARGSUSED */ 78 void 79 apic_pci_msi_enable_vector(dev_info_t *dip, int type, int inum, int vector, 80 int count, int target_apic_id) 81 { 82 uint64_t msi_addr, msi_data; 83 ushort_t msi_ctrl; 84 int cap_ptr = i_ddi_get_msi_msix_cap_ptr(dip); 85 ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(dip); 86 87 DDI_INTR_IMPLDBG((CE_CONT, "apic_pci_msi_enable_vector: dip=0x%p\n" 88 "\tdriver = %s, inum=0x%x vector=0x%x apicid=0x%x\n", (void *)dip, 89 ddi_driver_name(dip), inum, vector, target_apic_id)); 90 91 ASSERT((handle != NULL) && (cap_ptr != 0)); 92 93 /* MSI Address */ 94 msi_addr = (MSI_ADDR_HDR | (target_apic_id << MSI_ADDR_DEST_SHIFT)); 95 msi_addr |= ((MSI_ADDR_RH_FIXED << MSI_ADDR_RH_SHIFT) | 96 (MSI_ADDR_DM_PHYSICAL << MSI_ADDR_DM_SHIFT)); 97 98 /* MSI Data: MSI is edge triggered according to spec */ 99 msi_data = ((MSI_DATA_TM_EDGE << MSI_DATA_TM_SHIFT) | vector); 100 101 DDI_INTR_IMPLDBG((CE_CONT, "apic_pci_msi_enable_vector: addr=0x%lx " 102 "data=0x%lx\n", (long)msi_addr, (long)msi_data)); 103 104 if (type == DDI_INTR_TYPE_MSI) { 105 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL); 106 107 /* Set the bits to inform how many MSIs are enabled */ 108 msi_ctrl |= ((highbit(count) -1) << PCI_MSI_MME_SHIFT); 109 pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl); 110 111 pci_config_put32(handle, 112 cap_ptr + PCI_MSI_ADDR_OFFSET, msi_addr); 113 114 if (msi_ctrl & PCI_MSI_64BIT_MASK) { 115 pci_config_put32(handle, 116 cap_ptr + PCI_MSI_ADDR_OFFSET + 4, msi_addr >> 32); 117 pci_config_put16(handle, 118 cap_ptr + PCI_MSI_64BIT_DATA, msi_data); 119 } else { 120 pci_config_put16(handle, 121 cap_ptr + PCI_MSI_32BIT_DATA, msi_data); 122 } 123 124 } else if (type == DDI_INTR_TYPE_MSIX) { 125 uintptr_t off; 126 ddi_intr_msix_t *msix_p = i_ddi_get_msix(dip); 127 128 /* Offset into the "inum"th entry in the MSI-X table */ 129 off = (uintptr_t)msix_p->msix_tbl_addr + 130 (inum * PCI_MSIX_VECTOR_SIZE); 131 132 ddi_put32(msix_p->msix_tbl_hdl, 133 (uint32_t *)(off + PCI_MSIX_DATA_OFFSET), msi_data); 134 ddi_put64(msix_p->msix_tbl_hdl, 135 (uint64_t *)(off + PCI_MSIX_LOWER_ADDR_OFFSET), msi_addr); 136 } 137 } 138 139 140 /* 141 * This function returns the no. of vectors available for the pri. 142 * dip is not used at this moment. If we really don't need that, 143 * it will be removed. 144 */ 145 /*ARGSUSED*/ 146 int 147 apic_navail_vector(dev_info_t *dip, int pri) 148 { 149 int lowest, highest, i, navail, count; 150 151 DDI_INTR_IMPLDBG((CE_CONT, "apic_navail_vector: dip: %p, pri: %x\n", 152 (void *)dip, pri)); 153 154 highest = apic_ipltopri[pri] + APIC_VECTOR_MASK; 155 lowest = apic_ipltopri[pri - 1] + APIC_VECTOR_PER_IPL; 156 navail = count = 0; 157 158 /* It has to be contiguous */ 159 for (i = lowest; i < highest; i++) { 160 count = 0; 161 while ((apic_vector_to_irq[i] == APIC_RESV_IRQ) && 162 (i < highest)) { 163 if (APIC_CHECK_RESERVE_VECTORS(i)) 164 break; 165 count++; 166 i++; 167 } 168 if (count > navail) 169 navail = count; 170 } 171 return (navail); 172 } 173 174 /* 175 * Finds "count" contiguous MSI vectors starting at the proper alignment 176 * at "pri". 177 * Caller needs to make sure that count has to be power of 2 and should not 178 * be < 1. 179 */ 180 uchar_t 181 apic_find_multi_vectors(int pri, int count) 182 { 183 int lowest, highest, i, navail, start, msibits; 184 185 DDI_INTR_IMPLDBG((CE_CONT, "apic_find_mult: pri: %x, count: %x\n", 186 pri, count)); 187 188 highest = apic_ipltopri[pri] + APIC_VECTOR_MASK; 189 lowest = apic_ipltopri[pri - 1] + APIC_VECTOR_PER_IPL; 190 navail = 0; 191 192 /* 193 * msibits is the no. of lower order message data bits for the 194 * allocated MSI vectors and is used to calculate the aligned 195 * starting vector 196 */ 197 msibits = count - 1; 198 199 /* It has to be contiguous */ 200 for (i = lowest; i < highest; i++) { 201 navail = 0; 202 203 /* 204 * starting vector has to be aligned accordingly for 205 * multiple MSIs 206 */ 207 if (msibits) 208 i = (i + msibits) & ~msibits; 209 start = i; 210 while ((apic_vector_to_irq[i] == APIC_RESV_IRQ) && 211 (i < highest)) { 212 if (APIC_CHECK_RESERVE_VECTORS(i)) 213 break; 214 navail++; 215 if (navail >= count) 216 return (start); 217 i++; 218 } 219 } 220 return (0); 221 } 222 223 224 /* 225 * It finds the apic_irq_t associates with the dip, ispec and type. 226 */ 227 apic_irq_t * 228 apic_find_irq(dev_info_t *dip, struct intrspec *ispec, int type) 229 { 230 apic_irq_t *irqp; 231 int i; 232 233 DDI_INTR_IMPLDBG((CE_CONT, "apic_find_irq: dip=0x%p vec=0x%x " 234 "ipl=0x%x type=0x%x\n", (void *)dip, ispec->intrspec_vec, 235 ispec->intrspec_pri, type)); 236 237 for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) { 238 if ((irqp = apic_irq_table[i]) == NULL) 239 continue; 240 if ((irqp->airq_dip == dip) && 241 (irqp->airq_origirq == ispec->intrspec_vec) && 242 (irqp->airq_ipl == ispec->intrspec_pri)) { 243 if (type == DDI_INTR_TYPE_MSI) { 244 if (irqp->airq_mps_intr_index == MSI_INDEX) 245 return (irqp); 246 } else if (type == DDI_INTR_TYPE_MSIX) { 247 if (irqp->airq_mps_intr_index == MSIX_INDEX) 248 return (irqp); 249 } else 250 return (irqp); 251 } 252 } 253 DDI_INTR_IMPLDBG((CE_CONT, "apic_find_irq: return NULL\n")); 254 return (NULL); 255 } 256 257 258 #if !defined(__xpv) 259 260 /* 261 * This function will return the pending bit of the irqp. 262 * It either comes from the IRR register of the APIC or the RDT 263 * entry of the I/O APIC. 264 * For the IRR to work, it needs to be to its binding CPU 265 */ 266 static int 267 apic_get_pending(apic_irq_t *irqp, int type) 268 { 269 int bit, index, irr, pending; 270 int intin_no; 271 int apic_ix; 272 273 DDI_INTR_IMPLDBG((CE_CONT, "apic_get_pending: irqp: %p, cpuid: %x " 274 "type: %x\n", (void *)irqp, irqp->airq_cpu & ~IRQ_USER_BOUND, 275 type)); 276 277 /* need to get on the bound cpu */ 278 mutex_enter(&cpu_lock); 279 affinity_set(irqp->airq_cpu & ~IRQ_USER_BOUND); 280 281 index = irqp->airq_vector / 32; 282 bit = irqp->airq_vector % 32; 283 irr = apicadr[APIC_IRR_REG + index]; 284 285 affinity_clear(); 286 mutex_exit(&cpu_lock); 287 288 pending = (irr & (1 << bit)) ? 1 : 0; 289 if (!pending && (type == DDI_INTR_TYPE_FIXED)) { 290 /* check I/O APIC for fixed interrupt */ 291 intin_no = irqp->airq_intin_no; 292 apic_ix = irqp->airq_ioapicindex; 293 pending = (READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no) & 294 AV_PENDING) ? 1 : 0; 295 } 296 return (pending); 297 } 298 299 300 /* 301 * This function will clear the mask for the interrupt on the I/O APIC 302 */ 303 static void 304 apic_clear_mask(apic_irq_t *irqp) 305 { 306 int intin_no; 307 ulong_t iflag; 308 int32_t rdt_entry; 309 int apic_ix; 310 311 DDI_INTR_IMPLDBG((CE_CONT, "apic_clear_mask: irqp: %p\n", 312 (void *)irqp)); 313 314 intin_no = irqp->airq_intin_no; 315 apic_ix = irqp->airq_ioapicindex; 316 317 iflag = intr_clear(); 318 lock_set(&apic_ioapic_lock); 319 320 rdt_entry = READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no); 321 322 /* clear mask */ 323 WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no, 324 ((~AV_MASK) & rdt_entry)); 325 326 lock_clear(&apic_ioapic_lock); 327 intr_restore(iflag); 328 } 329 330 331 /* 332 * This function will mask the interrupt on the I/O APIC 333 */ 334 static void 335 apic_set_mask(apic_irq_t *irqp) 336 { 337 int intin_no; 338 int apic_ix; 339 ulong_t iflag; 340 int32_t rdt_entry; 341 342 DDI_INTR_IMPLDBG((CE_CONT, "apic_set_mask: irqp: %p\n", (void *)irqp)); 343 344 intin_no = irqp->airq_intin_no; 345 apic_ix = irqp->airq_ioapicindex; 346 347 iflag = intr_clear(); 348 349 lock_set(&apic_ioapic_lock); 350 351 rdt_entry = READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no); 352 353 /* mask it */ 354 WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no, 355 (AV_MASK | rdt_entry)); 356 357 lock_clear(&apic_ioapic_lock); 358 intr_restore(iflag); 359 } 360 361 #endif /* ! __xpv */ 362 363 void 364 apic_free_vectors(dev_info_t *dip, int inum, int count, int pri, int type) 365 { 366 int i; 367 apic_irq_t *irqptr; 368 struct intrspec ispec; 369 370 DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: dip: %p inum: %x " 371 "count: %x pri: %x type: %x\n", 372 (void *)dip, inum, count, pri, type)); 373 374 /* for MSI/X only */ 375 if (!DDI_INTR_IS_MSI_OR_MSIX(type)) 376 return; 377 378 for (i = 0; i < count; i++) { 379 DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: inum=0x%x " 380 "pri=0x%x count=0x%x\n", inum, pri, count)); 381 ispec.intrspec_vec = inum + i; 382 ispec.intrspec_pri = pri; 383 if ((irqptr = apic_find_irq(dip, &ispec, type)) == NULL) { 384 DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: " 385 "dip=0x%p inum=0x%x pri=0x%x apic_find_irq() " 386 "failed\n", (void *)dip, inum, pri)); 387 continue; 388 } 389 irqptr->airq_mps_intr_index = FREE_INDEX; 390 apic_vector_to_irq[irqptr->airq_vector] = APIC_RESV_IRQ; 391 } 392 } 393 394 395 /* 396 * check whether the system supports MSI 397 * 398 * If PCI-E capability is found, then this must be a PCI-E system. 399 * Since MSI is required for PCI-E system, it returns PSM_SUCCESS 400 * to indicate this system supports MSI. 401 */ 402 int 403 apic_check_msi_support() 404 { 405 dev_info_t *cdip; 406 char dev_type[16]; 407 int dev_len; 408 409 DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support:\n")); 410 411 /* 412 * check whether the first level children of root_node have 413 * PCI-E capability 414 */ 415 for (cdip = ddi_get_child(ddi_root_node()); cdip != NULL; 416 cdip = ddi_get_next_sibling(cdip)) { 417 418 DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support: cdip: 0x%p," 419 " driver: %s, binding: %s, nodename: %s\n", (void *)cdip, 420 ddi_driver_name(cdip), ddi_binding_name(cdip), 421 ddi_node_name(cdip))); 422 dev_len = sizeof (dev_type); 423 if (ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS, 424 "device_type", (caddr_t)dev_type, &dev_len) 425 != DDI_PROP_SUCCESS) 426 continue; 427 if (strcmp(dev_type, "pciex") == 0) 428 return (PSM_SUCCESS); 429 } 430 431 /* MSI is not supported on this system */ 432 DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support: no 'pciex' " 433 "device_type found\n")); 434 return (PSM_FAILURE); 435 } 436 437 /* 438 * apic_pci_msi_unconfigure: 439 * 440 * This and next two interfaces are copied from pci_intr_lib.c 441 * Do ensure that these two files stay in sync. 442 * These needed to be copied over here to avoid a deadlock situation on 443 * certain mp systems that use MSI interrupts. 444 * 445 * IMPORTANT regards next three interfaces: 446 * i) are called only for MSI/X interrupts. 447 * ii) called with interrupts disabled, and must not block 448 */ 449 void 450 apic_pci_msi_unconfigure(dev_info_t *rdip, int type, int inum) 451 { 452 ushort_t msi_ctrl; 453 int cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip); 454 ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(rdip); 455 456 ASSERT((handle != NULL) && (cap_ptr != 0)); 457 458 if (type == DDI_INTR_TYPE_MSI) { 459 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL); 460 msi_ctrl &= (~PCI_MSI_MME_MASK); 461 pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl); 462 pci_config_put32(handle, cap_ptr + PCI_MSI_ADDR_OFFSET, 0); 463 464 if (msi_ctrl & PCI_MSI_64BIT_MASK) { 465 pci_config_put16(handle, 466 cap_ptr + PCI_MSI_64BIT_DATA, 0); 467 pci_config_put32(handle, 468 cap_ptr + PCI_MSI_ADDR_OFFSET + 4, 0); 469 } else { 470 pci_config_put16(handle, 471 cap_ptr + PCI_MSI_32BIT_DATA, 0); 472 } 473 474 } else if (type == DDI_INTR_TYPE_MSIX) { 475 uintptr_t off; 476 uint32_t mask; 477 ddi_intr_msix_t *msix_p = i_ddi_get_msix(rdip); 478 479 /* Offset into "inum"th entry in the MSI-X table & mask it */ 480 off = (uintptr_t)msix_p->msix_tbl_addr + (inum * 481 PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET; 482 483 mask = ddi_get32(msix_p->msix_tbl_hdl, (uint32_t *)off); 484 485 ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, (mask | 1)); 486 487 /* Offset into the "inum"th entry in the MSI-X table */ 488 off = (uintptr_t)msix_p->msix_tbl_addr + 489 (inum * PCI_MSIX_VECTOR_SIZE); 490 491 /* Reset the "data" and "addr" bits */ 492 ddi_put32(msix_p->msix_tbl_hdl, 493 (uint32_t *)(off + PCI_MSIX_DATA_OFFSET), 0); 494 ddi_put64(msix_p->msix_tbl_hdl, (uint64_t *)off, 0); 495 } 496 } 497 498 499 /* 500 * apic_pci_msi_enable_mode: 501 */ 502 void 503 apic_pci_msi_enable_mode(dev_info_t *rdip, int type, int inum) 504 { 505 ushort_t msi_ctrl; 506 int cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip); 507 ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(rdip); 508 509 ASSERT((handle != NULL) && (cap_ptr != 0)); 510 511 if (type == DDI_INTR_TYPE_MSI) { 512 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL); 513 if ((msi_ctrl & PCI_MSI_ENABLE_BIT)) 514 return; 515 516 msi_ctrl |= PCI_MSI_ENABLE_BIT; 517 pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl); 518 519 } else if (type == DDI_INTR_TYPE_MSIX) { 520 uintptr_t off; 521 uint32_t mask; 522 ddi_intr_msix_t *msix_p; 523 524 msix_p = i_ddi_get_msix(rdip); 525 526 /* Offset into "inum"th entry in the MSI-X table & clear mask */ 527 off = (uintptr_t)msix_p->msix_tbl_addr + (inum * 528 PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET; 529 530 mask = ddi_get32(msix_p->msix_tbl_hdl, (uint32_t *)off); 531 532 ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, (mask & ~1)); 533 534 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSIX_CTRL); 535 536 if (!(msi_ctrl & PCI_MSIX_ENABLE_BIT)) { 537 msi_ctrl |= PCI_MSIX_ENABLE_BIT; 538 pci_config_put16(handle, cap_ptr + PCI_MSIX_CTRL, 539 msi_ctrl); 540 } 541 } 542 } 543 544 /* 545 * apic_pci_msi_disable_mode: 546 */ 547 void 548 apic_pci_msi_disable_mode(dev_info_t *rdip, int type) 549 { 550 ushort_t msi_ctrl; 551 int cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip); 552 ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(rdip); 553 554 ASSERT((handle != NULL) && (cap_ptr != 0)); 555 556 if (type == DDI_INTR_TYPE_MSI) { 557 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL); 558 if (!(msi_ctrl & PCI_MSI_ENABLE_BIT)) 559 return; 560 561 msi_ctrl &= ~PCI_MSI_ENABLE_BIT; /* MSI disable */ 562 pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl); 563 564 } else if (type == DDI_INTR_TYPE_MSIX) { 565 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSIX_CTRL); 566 if (msi_ctrl & PCI_MSIX_ENABLE_BIT) { 567 msi_ctrl &= ~PCI_MSIX_ENABLE_BIT; 568 pci_config_put16(handle, cap_ptr + PCI_MSIX_CTRL, 569 msi_ctrl); 570 } 571 } 572 } 573 574 #if !defined(__xpv) 575 576 static int 577 apic_set_cpu(uint32_t vector, int cpu, int *result) 578 { 579 apic_irq_t *irqp; 580 int iflag; 581 int ret; 582 583 DDI_INTR_IMPLDBG((CE_CONT, "APIC_SET_CPU\n")); 584 585 /* Convert the vector to the irq using vector_to_irq table. */ 586 mutex_enter(&airq_mutex); 587 irqp = apic_irq_table[apic_vector_to_irq[vector]]; 588 mutex_exit(&airq_mutex); 589 590 if (irqp == NULL) { 591 *result = ENXIO; 592 return (PSM_FAILURE); 593 } 594 595 /* Fail if this is an MSI intr and is part of a group. */ 596 if ((irqp->airq_mps_intr_index == MSI_INDEX) && 597 (irqp->airq_intin_no > 1)) { 598 *result = ENXIO; 599 return (PSM_FAILURE); 600 } 601 602 iflag = intr_clear(); 603 lock_set(&apic_ioapic_lock); 604 605 ret = apic_rebind_all(irqp, cpu); 606 607 lock_clear(&apic_ioapic_lock); 608 intr_restore(iflag); 609 610 if (ret) { 611 *result = EIO; 612 return (PSM_FAILURE); 613 } 614 *result = 0; 615 return (PSM_SUCCESS); 616 } 617 618 static int 619 apic_grp_set_cpu(uint32_t vector, int new_cpu, int *result) 620 { 621 dev_info_t *orig_dip; 622 uchar_t orig_cpu; 623 int iflag; 624 apic_irq_t *irqps[PCI_MSI_MAX_INTRS]; 625 int i; 626 int cap_ptr; 627 int msi_mask_off; 628 ushort_t msi_ctrl; 629 uint32_t msi_pvm; 630 ddi_acc_handle_t handle; 631 int num_vectors = 0; 632 633 DDI_INTR_IMPLDBG((CE_CONT, "APIC_GRP_SET_CPU\n")); 634 635 /* 636 * Take mutex to insure that table doesn't change out from underneath 637 * us while we're playing with it. 638 */ 639 mutex_enter(&airq_mutex); 640 irqps[0] = apic_irq_table[apic_vector_to_irq[vector]]; 641 orig_cpu = irqps[0]->airq_temp_cpu; 642 orig_dip = irqps[0]->airq_dip; 643 num_vectors = irqps[0]->airq_intin_no; 644 645 /* A "group" of 1 */ 646 if (num_vectors == 1) { 647 mutex_exit(&airq_mutex); 648 return (apic_set_cpu(vector, new_cpu, result)); 649 } 650 651 *result = ENXIO; 652 653 if (irqps[0]->airq_mps_intr_index != MSI_INDEX) { 654 mutex_exit(&airq_mutex); 655 DDI_INTR_IMPLDBG((CE_CONT, "set_grp: intr not MSI\n")); 656 goto set_grp_intr_done; 657 } 658 if ((num_vectors < 1) || ((num_vectors - 1) & vector)) { 659 mutex_exit(&airq_mutex); 660 DDI_INTR_IMPLDBG((CE_CONT, 661 "set_grp: base vec not part of a grp or not aligned: " 662 "vec:0x%x, num_vec:0x%x\n", vector, num_vectors)); 663 goto set_grp_intr_done; 664 } 665 DDI_INTR_IMPLDBG((CE_CONT, "set_grp: num intrs in grp: %d\n", 666 num_vectors)); 667 668 ASSERT((num_vectors + vector) < APIC_MAX_VECTOR); 669 670 *result = EIO; 671 672 /* 673 * All IRQ entries in the table for the given device will be not 674 * shared. Since they are not shared, the dip in the table will 675 * be true to the device of interest. 676 */ 677 for (i = 1; i < num_vectors; i++) { 678 irqps[i] = apic_irq_table[apic_vector_to_irq[vector + i]]; 679 if (irqps[i] == NULL) { 680 mutex_exit(&airq_mutex); 681 goto set_grp_intr_done; 682 } 683 #ifdef DEBUG 684 /* Sanity check: CPU and dip is the same for all entries. */ 685 if ((irqps[i]->airq_dip != orig_dip) || 686 (irqps[i]->airq_temp_cpu != orig_cpu)) { 687 mutex_exit(&airq_mutex); 688 DDI_INTR_IMPLDBG((CE_CONT, 689 "set_grp: cpu or dip for vec 0x%x difft than for " 690 "vec 0x%x\n", vector, vector + i)); 691 DDI_INTR_IMPLDBG((CE_CONT, 692 " cpu: %d vs %d, dip: 0x%p vs 0x%p\n", orig_cpu, 693 irqps[i]->airq_temp_cpu, (void *)orig_dip, 694 (void *)irqps[i]->airq_dip)); 695 goto set_grp_intr_done; 696 } 697 #endif /* DEBUG */ 698 } 699 mutex_exit(&airq_mutex); 700 701 cap_ptr = i_ddi_get_msi_msix_cap_ptr(orig_dip); 702 handle = i_ddi_get_pci_config_handle(orig_dip); 703 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL); 704 705 /* MSI Per vector masking is supported. */ 706 if (msi_ctrl & PCI_MSI_PVM_MASK) { 707 if (msi_ctrl & PCI_MSI_64BIT_MASK) 708 msi_mask_off = cap_ptr + PCI_MSI_64BIT_MASKBITS; 709 else 710 msi_mask_off = cap_ptr + PCI_MSI_32BIT_MASK; 711 msi_pvm = pci_config_get32(handle, msi_mask_off); 712 pci_config_put32(handle, msi_mask_off, (uint32_t)-1); 713 DDI_INTR_IMPLDBG((CE_CONT, 714 "set_grp: pvm supported. Mask set to 0x%x\n", 715 pci_config_get32(handle, msi_mask_off))); 716 } 717 718 iflag = intr_clear(); 719 lock_set(&apic_ioapic_lock); 720 721 /* 722 * Do the first rebind and check for errors. Apic_rebind_all returns 723 * an error if the CPU is not accepting interrupts. If the first one 724 * succeeds they all will. 725 */ 726 if (apic_rebind_all(irqps[0], new_cpu)) 727 (void) apic_rebind_all(irqps[0], orig_cpu); 728 else { 729 for (i = 1; i < num_vectors; i++) 730 (void) apic_rebind_all(irqps[i], new_cpu); 731 *result = 0; /* SUCCESS */ 732 } 733 734 lock_clear(&apic_ioapic_lock); 735 intr_restore(iflag); 736 737 /* Reenable vectors if per vector masking is supported. */ 738 if (msi_ctrl & PCI_MSI_PVM_MASK) { 739 pci_config_put32(handle, msi_mask_off, msi_pvm); 740 DDI_INTR_IMPLDBG((CE_CONT, 741 "set_grp: pvm supported. Mask restored to 0x%x\n", 742 pci_config_get32(handle, msi_mask_off))); 743 } 744 745 set_grp_intr_done: 746 if (*result != 0) 747 return (PSM_FAILURE); 748 749 return (PSM_SUCCESS); 750 } 751 752 #endif /* !__xpv */ 753 754 int 755 apic_get_vector_intr_info(int vecirq, apic_get_intr_t *intr_params_p) 756 { 757 struct autovec *av_dev; 758 uchar_t irqno; 759 int i; 760 apic_irq_t *irq_p; 761 762 /* Sanity check the vector/irq argument. */ 763 ASSERT((vecirq >= 0) || (vecirq <= APIC_MAX_VECTOR)); 764 765 mutex_enter(&airq_mutex); 766 767 /* 768 * Convert the vecirq arg to an irq using vector_to_irq table 769 * if the arg is a vector. Pass thru if already an irq. 770 */ 771 if ((intr_params_p->avgi_req_flags & PSMGI_INTRBY_FLAGS) == 772 PSMGI_INTRBY_VEC) 773 irqno = apic_vector_to_irq[vecirq]; 774 else 775 irqno = vecirq; 776 777 irq_p = apic_irq_table[irqno]; 778 779 if ((irq_p == NULL) || 780 (irq_p->airq_temp_cpu == IRQ_UNBOUND) || 781 (irq_p->airq_temp_cpu == IRQ_UNINIT)) { 782 mutex_exit(&airq_mutex); 783 return (PSM_FAILURE); 784 } 785 786 if (intr_params_p->avgi_req_flags & PSMGI_REQ_CPUID) { 787 788 /* Get the (temp) cpu from apic_irq table, indexed by irq. */ 789 intr_params_p->avgi_cpu_id = irq_p->airq_temp_cpu; 790 791 /* Return user bound info for intrd. */ 792 if (intr_params_p->avgi_cpu_id & IRQ_USER_BOUND) { 793 intr_params_p->avgi_cpu_id &= ~IRQ_USER_BOUND; 794 intr_params_p->avgi_cpu_id |= PSMGI_CPU_USER_BOUND; 795 } 796 } 797 798 if (intr_params_p->avgi_req_flags & PSMGI_REQ_VECTOR) 799 intr_params_p->avgi_vector = irq_p->airq_vector; 800 801 if (intr_params_p->avgi_req_flags & 802 (PSMGI_REQ_NUM_DEVS | PSMGI_REQ_GET_DEVS)) 803 /* Get number of devices from apic_irq table shared field. */ 804 intr_params_p->avgi_num_devs = irq_p->airq_share; 805 806 if (intr_params_p->avgi_req_flags & PSMGI_REQ_GET_DEVS) { 807 808 intr_params_p->avgi_req_flags |= PSMGI_REQ_NUM_DEVS; 809 810 /* Some devices have NULL dip. Don't count these. */ 811 if (intr_params_p->avgi_num_devs > 0) { 812 for (i = 0, av_dev = autovect[irqno].avh_link; 813 av_dev; av_dev = av_dev->av_link) 814 if (av_dev->av_vector && av_dev->av_dip) 815 i++; 816 intr_params_p->avgi_num_devs = 817 MIN(intr_params_p->avgi_num_devs, i); 818 } 819 820 /* There are no viable dips to return. */ 821 if (intr_params_p->avgi_num_devs == 0) 822 intr_params_p->avgi_dip_list = NULL; 823 824 else { /* Return list of dips */ 825 826 /* Allocate space in array for that number of devs. */ 827 intr_params_p->avgi_dip_list = kmem_zalloc( 828 intr_params_p->avgi_num_devs * 829 sizeof (dev_info_t *), 830 KM_SLEEP); 831 832 /* 833 * Loop through the device list of the autovec table 834 * filling in the dip array. 835 * 836 * Note that the autovect table may have some special 837 * entries which contain NULL dips. These will be 838 * ignored. 839 */ 840 for (i = 0, av_dev = autovect[irqno].avh_link; 841 av_dev; av_dev = av_dev->av_link) 842 if (av_dev->av_vector && av_dev->av_dip) 843 intr_params_p->avgi_dip_list[i++] = 844 av_dev->av_dip; 845 } 846 } 847 848 mutex_exit(&airq_mutex); 849 850 return (PSM_SUCCESS); 851 } 852 853 854 #if !defined(__xpv) 855 856 /* 857 * This function provides external interface to the nexus for all 858 * functionalities related to the new DDI interrupt framework. 859 * 860 * Input: 861 * dip - pointer to the dev_info structure of the requested device 862 * hdlp - pointer to the internal interrupt handle structure for the 863 * requested interrupt 864 * intr_op - opcode for this call 865 * result - pointer to the integer that will hold the result to be 866 * passed back if return value is PSM_SUCCESS 867 * 868 * Output: 869 * return value is either PSM_SUCCESS or PSM_FAILURE 870 */ 871 int 872 apic_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp, 873 psm_intr_op_t intr_op, int *result) 874 { 875 int cap; 876 int count_vec; 877 int old_priority; 878 int new_priority; 879 int new_cpu; 880 apic_irq_t *irqp; 881 struct intrspec *ispec, intr_spec; 882 883 DDI_INTR_IMPLDBG((CE_CONT, "apic_intr_ops: dip: %p hdlp: %p " 884 "intr_op: %x\n", (void *)dip, (void *)hdlp, intr_op)); 885 886 ispec = &intr_spec; 887 ispec->intrspec_pri = hdlp->ih_pri; 888 ispec->intrspec_vec = hdlp->ih_inum; 889 ispec->intrspec_func = hdlp->ih_cb_func; 890 891 switch (intr_op) { 892 case PSM_INTR_OP_CHECK_MSI: 893 /* 894 * Check MSI/X is supported or not at APIC level and 895 * masked off the MSI/X bits in hdlp->ih_type if not 896 * supported before return. If MSI/X is supported, 897 * leave the ih_type unchanged and return. 898 * 899 * hdlp->ih_type passed in from the nexus has all the 900 * interrupt types supported by the device. 901 */ 902 if (apic_support_msi == 0) { 903 /* 904 * if apic_support_msi is not set, call 905 * apic_check_msi_support() to check whether msi 906 * is supported first 907 */ 908 if (apic_check_msi_support() == PSM_SUCCESS) 909 apic_support_msi = 1; 910 else 911 apic_support_msi = -1; 912 } 913 if (apic_support_msi == 1) { 914 if (apic_msix_enable) 915 *result = hdlp->ih_type; 916 else 917 *result = hdlp->ih_type & ~DDI_INTR_TYPE_MSIX; 918 } else 919 *result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI | 920 DDI_INTR_TYPE_MSIX); 921 break; 922 case PSM_INTR_OP_ALLOC_VECTORS: 923 if (hdlp->ih_type == DDI_INTR_TYPE_MSI) 924 *result = apic_alloc_msi_vectors(dip, hdlp->ih_inum, 925 hdlp->ih_scratch1, hdlp->ih_pri, 926 (int)(uintptr_t)hdlp->ih_scratch2); 927 else 928 *result = apic_alloc_msix_vectors(dip, hdlp->ih_inum, 929 hdlp->ih_scratch1, hdlp->ih_pri, 930 (int)(uintptr_t)hdlp->ih_scratch2); 931 break; 932 case PSM_INTR_OP_FREE_VECTORS: 933 apic_free_vectors(dip, hdlp->ih_inum, hdlp->ih_scratch1, 934 hdlp->ih_pri, hdlp->ih_type); 935 break; 936 case PSM_INTR_OP_NAVAIL_VECTORS: 937 *result = apic_navail_vector(dip, hdlp->ih_pri); 938 break; 939 case PSM_INTR_OP_XLATE_VECTOR: 940 ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp; 941 *result = apic_introp_xlate(dip, ispec, hdlp->ih_type); 942 break; 943 case PSM_INTR_OP_GET_PENDING: 944 if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL) 945 return (PSM_FAILURE); 946 *result = apic_get_pending(irqp, hdlp->ih_type); 947 break; 948 case PSM_INTR_OP_CLEAR_MASK: 949 if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) 950 return (PSM_FAILURE); 951 irqp = apic_find_irq(dip, ispec, hdlp->ih_type); 952 if (irqp == NULL) 953 return (PSM_FAILURE); 954 apic_clear_mask(irqp); 955 break; 956 case PSM_INTR_OP_SET_MASK: 957 if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) 958 return (PSM_FAILURE); 959 if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL) 960 return (PSM_FAILURE); 961 apic_set_mask(irqp); 962 break; 963 case PSM_INTR_OP_GET_CAP: 964 cap = DDI_INTR_FLAG_PENDING; 965 if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) 966 cap |= DDI_INTR_FLAG_MASKABLE; 967 *result = cap; 968 break; 969 case PSM_INTR_OP_GET_SHARED: 970 if (hdlp->ih_type != DDI_INTR_TYPE_FIXED) 971 return (PSM_FAILURE); 972 if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL) 973 return (PSM_FAILURE); 974 *result = irqp->airq_share ? 1: 0; 975 break; 976 case PSM_INTR_OP_SET_PRI: 977 old_priority = hdlp->ih_pri; /* save old value */ 978 new_priority = *(int *)result; /* try the new value */ 979 980 /* First, check if "hdlp->ih_scratch1" vectors exist? */ 981 if (apic_navail_vector(dip, new_priority) < hdlp->ih_scratch1) 982 return (PSM_FAILURE); 983 984 /* Now allocate the vectors */ 985 if (hdlp->ih_type == DDI_INTR_TYPE_MSI) 986 count_vec = apic_alloc_msi_vectors(dip, hdlp->ih_inum, 987 hdlp->ih_scratch1, new_priority, 988 DDI_INTR_ALLOC_STRICT); 989 else 990 count_vec = apic_alloc_msix_vectors(dip, hdlp->ih_inum, 991 hdlp->ih_scratch1, new_priority, 992 DDI_INTR_ALLOC_STRICT); 993 994 /* Did we get new vectors? */ 995 if (!count_vec) 996 return (PSM_FAILURE); 997 998 /* Finally, free the previously allocated vectors */ 999 apic_free_vectors(dip, hdlp->ih_inum, count_vec, 1000 old_priority, hdlp->ih_type); 1001 hdlp->ih_pri = new_priority; /* set the new value */ 1002 break; 1003 case PSM_INTR_OP_SET_CPU: 1004 case PSM_INTR_OP_GRP_SET_CPU: 1005 /* 1006 * The interrupt handle given here has been allocated 1007 * specifically for this command, and ih_private carries 1008 * a CPU value. 1009 */ 1010 new_cpu = (int)(intptr_t)hdlp->ih_private; 1011 if (!apic_cpu_in_range(new_cpu)) { 1012 DDI_INTR_IMPLDBG((CE_CONT, 1013 "[grp_]set_cpu: cpu out of range: %d\n", new_cpu)); 1014 *result = EINVAL; 1015 return (PSM_FAILURE); 1016 } 1017 if (intr_op == PSM_INTR_OP_SET_CPU) { 1018 if (apic_set_cpu(hdlp->ih_vector, new_cpu, result) != 1019 PSM_SUCCESS) 1020 return (PSM_FAILURE); 1021 } else { 1022 if (apic_grp_set_cpu(hdlp->ih_vector, new_cpu, 1023 result) != PSM_SUCCESS) 1024 return (PSM_FAILURE); 1025 } 1026 break; 1027 case PSM_INTR_OP_GET_INTR: 1028 /* 1029 * The interrupt handle given here has been allocated 1030 * specifically for this command, and ih_private carries 1031 * a pointer to a apic_get_intr_t. 1032 */ 1033 if (apic_get_vector_intr_info( 1034 hdlp->ih_vector, hdlp->ih_private) != PSM_SUCCESS) 1035 return (PSM_FAILURE); 1036 break; 1037 case PSM_INTR_OP_APIC_TYPE: 1038 hdlp->ih_private = apic_get_apic_type(); 1039 hdlp->ih_ver = apic_get_apic_version(); 1040 break; 1041 case PSM_INTR_OP_SET_CAP: 1042 default: 1043 return (PSM_FAILURE); 1044 } 1045 return (PSM_SUCCESS); 1046 } 1047 #endif /* !__xpv */ 1048