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 /* 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 /* 26 * Copyright (c) 2010, Intel Corporation. 27 * All rights reserved. 28 */ 29 30 #include <sys/processor.h> 31 #include <sys/time.h> 32 #include <sys/psm.h> 33 #include <sys/smp_impldefs.h> 34 #include <sys/cram.h> 35 #include <sys/acpi/acpi.h> 36 #include <sys/acpica.h> 37 #include <sys/psm_common.h> 38 #include <sys/pit.h> 39 #include <sys/ddi.h> 40 #include <sys/sunddi.h> 41 #include <sys/ddi_impldefs.h> 42 #include <sys/pci.h> 43 #include <sys/promif.h> 44 #include <sys/x86_archext.h> 45 #include <sys/cpc_impl.h> 46 #include <sys/uadmin.h> 47 #include <sys/panic.h> 48 #include <sys/debug.h> 49 #include <sys/archsystm.h> 50 #include <sys/trap.h> 51 #include <sys/machsystm.h> 52 #include <sys/sysmacros.h> 53 #include <sys/cpuvar.h> 54 #include <sys/rm_platter.h> 55 #include <sys/privregs.h> 56 #include <sys/note.h> 57 #include <sys/pci_intr_lib.h> 58 #include <sys/spl.h> 59 #include <sys/clock.h> 60 #include <sys/dditypes.h> 61 #include <sys/sunddi.h> 62 #include <sys/x_call.h> 63 #include <sys/reboot.h> 64 #include <sys/apix.h> 65 66 static int apix_get_avail_vector_oncpu(uint32_t, int, int); 67 static apix_vector_t *apix_init_vector(processorid_t, uchar_t); 68 static void apix_cleanup_vector(apix_vector_t *); 69 static void apix_insert_av(apix_vector_t *, void *, avfunc, caddr_t, caddr_t, 70 uint64_t *, int, dev_info_t *); 71 static void apix_remove_av(apix_vector_t *, struct autovec *); 72 static void apix_clear_dev_map(dev_info_t *, int, int); 73 static boolean_t apix_is_cpu_enabled(processorid_t); 74 static void apix_wait_till_seen(processorid_t, int); 75 76 #define GET_INTR_INUM(ihdlp) \ 77 (((ihdlp) != NULL) ? ((ddi_intr_handle_impl_t *)(ihdlp))->ih_inum : 0) 78 79 apix_rebind_info_t apix_rebindinfo = {0, 0, 0, NULL, 0, NULL}; 80 81 /* 82 * Allocate IPI 83 * 84 * Return vector number or 0 on error 85 */ 86 uchar_t 87 apix_alloc_ipi(int ipl) 88 { 89 apix_vector_t *vecp; 90 uchar_t vector; 91 int cpun; 92 int nproc; 93 94 APIX_ENTER_CPU_LOCK(0); 95 96 vector = apix_get_avail_vector_oncpu(0, APIX_IPI_MIN, APIX_IPI_MAX); 97 if (vector == 0) { 98 APIX_LEAVE_CPU_LOCK(0); 99 cmn_err(CE_WARN, "apix: no available IPI\n"); 100 apic_error |= APIC_ERR_GET_IPIVECT_FAIL; 101 return (0); 102 } 103 104 nproc = max(apic_nproc, apic_max_nproc); 105 for (cpun = 0; cpun < nproc; cpun++) { 106 vecp = xv_vector(cpun, vector); 107 if (vecp == NULL) { 108 vecp = kmem_zalloc(sizeof (apix_vector_t), KM_NOSLEEP); 109 if (vecp == NULL) { 110 cmn_err(CE_WARN, "apix: No memory for ipi"); 111 goto fail; 112 } 113 xv_vector(cpun, vector) = vecp; 114 } 115 vecp->v_state = APIX_STATE_ALLOCED; 116 vecp->v_type = APIX_TYPE_IPI; 117 vecp->v_cpuid = vecp->v_bound_cpuid = cpun; 118 vecp->v_vector = vector; 119 vecp->v_pri = ipl; 120 } 121 APIX_LEAVE_CPU_LOCK(0); 122 return (vector); 123 124 fail: 125 while (--cpun >= 0) 126 apix_cleanup_vector(xv_vector(cpun, vector)); 127 APIX_LEAVE_CPU_LOCK(0); 128 return (0); 129 } 130 131 /* 132 * Add IPI service routine 133 */ 134 static int 135 apix_add_ipi(int ipl, avfunc xxintr, char *name, int vector, 136 caddr_t arg1, caddr_t arg2) 137 { 138 int cpun; 139 apix_vector_t *vecp; 140 int nproc; 141 142 ASSERT(vector >= APIX_IPI_MIN && vector <= APIX_IPI_MAX); 143 144 nproc = max(apic_nproc, apic_max_nproc); 145 for (cpun = 0; cpun < nproc; cpun++) { 146 APIX_ENTER_CPU_LOCK(cpun); 147 vecp = xv_vector(cpun, vector); 148 apix_insert_av(vecp, NULL, xxintr, arg1, arg2, NULL, ipl, NULL); 149 vecp->v_state = APIX_STATE_ENABLED; 150 APIX_LEAVE_CPU_LOCK(cpun); 151 } 152 153 APIC_VERBOSE(IPI, (CE_CONT, "apix: add ipi for %s, vector %x " 154 "ipl %x\n", name, vector, ipl)); 155 156 return (1); 157 } 158 159 /* 160 * Find and return first free vector in range (start, end) 161 */ 162 static int 163 apix_get_avail_vector_oncpu(uint32_t cpuid, int start, int end) 164 { 165 int i; 166 apix_impl_t *apixp = apixs[cpuid]; 167 168 for (i = start; i <= end; i++) { 169 if (APIC_CHECK_RESERVE_VECTORS(i)) 170 continue; 171 if (IS_VECT_FREE(apixp->x_vectbl[i])) 172 return (i); 173 } 174 175 return (0); 176 } 177 178 /* 179 * Allocate a vector on specified cpu 180 * 181 * Return NULL on error 182 */ 183 static apix_vector_t * 184 apix_alloc_vector_oncpu(uint32_t cpuid, dev_info_t *dip, int inum, int type) 185 { 186 processorid_t tocpu = cpuid & ~IRQ_USER_BOUND; 187 apix_vector_t *vecp; 188 int vector; 189 190 ASSERT(APIX_CPU_LOCK_HELD(tocpu)); 191 192 /* find free vector */ 193 vector = apix_get_avail_vector_oncpu(tocpu, APIX_AVINTR_MIN, 194 APIX_AVINTR_MAX); 195 if (vector == 0) 196 return (NULL); 197 198 vecp = apix_init_vector(tocpu, vector); 199 vecp->v_type = (ushort_t)type; 200 vecp->v_inum = inum; 201 vecp->v_flags = (cpuid & IRQ_USER_BOUND) ? APIX_VECT_USER_BOUND : 0; 202 203 if (dip != NULL) 204 apix_set_dev_map(vecp, dip, inum); 205 206 return (vecp); 207 } 208 209 /* 210 * Allocates "count" contiguous MSI vectors starting at the proper alignment. 211 * Caller needs to make sure that count has to be power of 2 and should not 212 * be < 1. 213 * 214 * Return first vector number 215 */ 216 apix_vector_t * 217 apix_alloc_nvectors_oncpu(uint32_t cpuid, dev_info_t *dip, int inum, 218 int count, int type) 219 { 220 int i, msibits, start = 0, navail = 0; 221 apix_vector_t *vecp, *startp = NULL; 222 processorid_t tocpu = cpuid & ~IRQ_USER_BOUND; 223 uint_t flags; 224 225 ASSERT(APIX_CPU_LOCK_HELD(tocpu)); 226 227 /* 228 * msibits is the no. of lower order message data bits for the 229 * allocated MSI vectors and is used to calculate the aligned 230 * starting vector 231 */ 232 msibits = count - 1; 233 234 /* It has to be contiguous */ 235 for (i = APIX_AVINTR_MIN; i <= APIX_AVINTR_MAX; i++) { 236 if (!IS_VECT_FREE(xv_vector(tocpu, i))) 237 continue; 238 239 /* 240 * starting vector has to be aligned accordingly for 241 * multiple MSIs 242 */ 243 if (msibits) 244 i = (i + msibits) & ~msibits; 245 246 for (navail = 0, start = i; i <= APIX_AVINTR_MAX; i++) { 247 if (!IS_VECT_FREE(xv_vector(tocpu, i))) 248 break; 249 if (APIC_CHECK_RESERVE_VECTORS(i)) 250 break; 251 if (++navail == count) 252 goto done; 253 } 254 } 255 256 return (NULL); 257 258 done: 259 flags = (cpuid & IRQ_USER_BOUND) ? APIX_VECT_USER_BOUND : 0; 260 261 for (i = 0; i < count; i++) { 262 if ((vecp = apix_init_vector(tocpu, start + i)) == NULL) 263 goto fail; 264 265 vecp->v_type = (ushort_t)type; 266 vecp->v_inum = inum + i; 267 vecp->v_flags = flags; 268 269 if (dip != NULL) 270 apix_set_dev_map(vecp, dip, inum + i); 271 272 if (i == 0) 273 startp = vecp; 274 } 275 276 return (startp); 277 278 fail: 279 while (i-- > 0) { /* Free allocated vectors */ 280 vecp = xv_vector(tocpu, start + i); 281 apix_clear_dev_map(dip, inum + i, type); 282 apix_cleanup_vector(vecp); 283 } 284 return (NULL); 285 } 286 287 #define APIX_WRITE_MSI_DATA(_hdl, _cap, _ctrl, _v)\ 288 do {\ 289 if ((_ctrl) & PCI_MSI_64BIT_MASK)\ 290 pci_config_put16((_hdl), (_cap) + PCI_MSI_64BIT_DATA, (_v));\ 291 else\ 292 pci_config_put16((_hdl), (_cap) + PCI_MSI_32BIT_DATA, (_v));\ 293 _NOTE(CONSTCOND)} while (0) 294 295 static void 296 apix_pci_msi_enable_vector(apix_vector_t *vecp, dev_info_t *dip, int type, 297 int inum, int count, uchar_t vector, int target_apic_id) 298 { 299 uint64_t msi_addr, msi_data; 300 ushort_t msi_ctrl; 301 int i, cap_ptr = i_ddi_get_msi_msix_cap_ptr(dip); 302 ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(dip); 303 msi_regs_t msi_regs; 304 void *intrmap_tbl[PCI_MSI_MAX_INTRS]; 305 306 DDI_INTR_IMPLDBG((CE_CONT, "apix_pci_msi_enable_vector: dip=0x%p\n" 307 "\tdriver = %s, inum=0x%x vector=0x%x apicid=0x%x\n", (void *)dip, 308 ddi_driver_name(dip), inum, vector, target_apic_id)); 309 310 ASSERT((handle != NULL) && (cap_ptr != 0)); 311 312 msi_regs.mr_data = vector; 313 msi_regs.mr_addr = target_apic_id; 314 315 intrmap_tbl[0] = vecp->v_intrmap_private; 316 apic_vt_ops->apic_intrmap_alloc_entry(intrmap_tbl, dip, type, 317 count, 0xff); 318 for (i = 0; i < count; i++) 319 xv_intrmap_private(vecp->v_cpuid, vector + i) = intrmap_tbl[i]; 320 321 apic_vt_ops->apic_intrmap_map_entry(vecp->v_intrmap_private, 322 (void *)&msi_regs, type, count); 323 apic_vt_ops->apic_intrmap_record_msi(vecp->v_intrmap_private, 324 &msi_regs); 325 326 /* MSI Address */ 327 msi_addr = msi_regs.mr_addr; 328 329 /* MSI Data: MSI is edge triggered according to spec */ 330 msi_data = msi_regs.mr_data; 331 332 DDI_INTR_IMPLDBG((CE_CONT, "apix_pci_msi_enable_vector: addr=0x%lx " 333 "data=0x%lx\n", (long)msi_addr, (long)msi_data)); 334 335 if (type == APIX_TYPE_MSI) { 336 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL); 337 338 /* Set the bits to inform how many MSIs are enabled */ 339 msi_ctrl |= ((highbit(count) - 1) << PCI_MSI_MME_SHIFT); 340 pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl); 341 342 if ((vecp->v_flags & APIX_VECT_MASKABLE) == 0) 343 APIX_WRITE_MSI_DATA(handle, cap_ptr, msi_ctrl, 344 APIX_RESV_VECTOR); 345 346 pci_config_put32(handle, 347 cap_ptr + PCI_MSI_ADDR_OFFSET, msi_addr); 348 if (msi_ctrl & PCI_MSI_64BIT_MASK) 349 pci_config_put32(handle, 350 cap_ptr + PCI_MSI_ADDR_OFFSET + 4, msi_addr >> 32); 351 352 APIX_WRITE_MSI_DATA(handle, cap_ptr, msi_ctrl, msi_data); 353 } else if (type == APIX_TYPE_MSIX) { 354 uintptr_t off; 355 ddi_intr_msix_t *msix_p = i_ddi_get_msix(dip); 356 357 /* Offset into the "inum"th entry in the MSI-X table */ 358 off = (uintptr_t)msix_p->msix_tbl_addr + 359 (inum * PCI_MSIX_VECTOR_SIZE); 360 361 ddi_put32(msix_p->msix_tbl_hdl, 362 (uint32_t *)(off + PCI_MSIX_DATA_OFFSET), msi_data); 363 ddi_put64(msix_p->msix_tbl_hdl, 364 (uint64_t *)(off + PCI_MSIX_LOWER_ADDR_OFFSET), msi_addr); 365 } 366 } 367 368 static void 369 apix_pci_msi_enable_mode(dev_info_t *dip, int type, int inum) 370 { 371 ushort_t msi_ctrl; 372 int cap_ptr = i_ddi_get_msi_msix_cap_ptr(dip); 373 ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(dip); 374 375 ASSERT((handle != NULL) && (cap_ptr != 0)); 376 377 if (type == APIX_TYPE_MSI) { 378 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL); 379 if ((msi_ctrl & PCI_MSI_ENABLE_BIT)) 380 return; 381 382 msi_ctrl |= PCI_MSI_ENABLE_BIT; 383 pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl); 384 385 } else if (type == DDI_INTR_TYPE_MSIX) { 386 uintptr_t off; 387 uint32_t mask; 388 ddi_intr_msix_t *msix_p; 389 390 msix_p = i_ddi_get_msix(dip); 391 392 /* Offset into "inum"th entry in the MSI-X table & clear mask */ 393 off = (uintptr_t)msix_p->msix_tbl_addr + (inum * 394 PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET; 395 396 mask = ddi_get32(msix_p->msix_tbl_hdl, (uint32_t *)off); 397 398 ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, (mask & ~1)); 399 400 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSIX_CTRL); 401 402 if (!(msi_ctrl & PCI_MSIX_ENABLE_BIT)) { 403 msi_ctrl |= PCI_MSIX_ENABLE_BIT; 404 pci_config_put16(handle, cap_ptr + PCI_MSIX_CTRL, 405 msi_ctrl); 406 } 407 } 408 } 409 410 /* 411 * Setup interrupt, pogramming IO-APIC or MSI/X address/data. 412 */ 413 void 414 apix_enable_vector(apix_vector_t *vecp) 415 { 416 int tocpu = vecp->v_cpuid, type = vecp->v_type; 417 apic_cpus_info_t *cpu_infop; 418 ulong_t iflag; 419 420 ASSERT(tocpu < apic_nproc); 421 422 cpu_infop = &apic_cpus[tocpu]; 423 if (vecp->v_flags & APIX_VECT_USER_BOUND) 424 cpu_infop->aci_bound++; 425 else 426 cpu_infop->aci_temp_bound++; 427 428 iflag = intr_clear(); 429 lock_set(&apic_ioapic_lock); 430 431 if (!DDI_INTR_IS_MSI_OR_MSIX(type)) { /* fixed */ 432 apix_intx_enable(vecp->v_inum); 433 } else { 434 int inum = vecp->v_inum; 435 dev_info_t *dip = APIX_GET_DIP(vecp); 436 int count = i_ddi_intr_get_current_nintrs(dip); 437 438 if (type == APIX_TYPE_MSI) { /* MSI */ 439 if (inum == apix_get_max_dev_inum(dip, type)) { 440 /* last one */ 441 uchar_t start_inum = inum + 1 - count; 442 uchar_t start_vect = vecp->v_vector + 1 - count; 443 apix_vector_t *start_vecp = 444 xv_vector(vecp->v_cpuid, start_vect); 445 446 APIC_VERBOSE(INTR, (CE_CONT, "apix: call " 447 "apix_pci_msi_enable_vector\n")); 448 apix_pci_msi_enable_vector(start_vecp, dip, 449 type, start_inum, count, start_vect, 450 cpu_infop->aci_local_id); 451 452 APIC_VERBOSE(INTR, (CE_CONT, "apix: call " 453 "apix_pci_msi_enable_mode\n")); 454 apix_pci_msi_enable_mode(dip, type, inum); 455 } 456 } else { /* MSI-X */ 457 apix_pci_msi_enable_vector(vecp, dip, 458 type, inum, 1, vecp->v_vector, 459 cpu_infop->aci_local_id); 460 apix_pci_msi_enable_mode(dip, type, inum); 461 } 462 } 463 vecp->v_state = APIX_STATE_ENABLED; 464 apic_redist_cpu_skip &= ~(1 << tocpu); 465 466 lock_clear(&apic_ioapic_lock); 467 intr_restore(iflag); 468 } 469 470 /* 471 * Disable the interrupt 472 */ 473 void 474 apix_disable_vector(apix_vector_t *vecp) 475 { 476 struct autovec *avp = vecp->v_autovect; 477 ulong_t iflag; 478 479 ASSERT(avp != NULL); 480 481 iflag = intr_clear(); 482 lock_set(&apic_ioapic_lock); 483 484 switch (vecp->v_type) { 485 case APIX_TYPE_MSI: 486 ASSERT(avp->av_vector != NULL && avp->av_dip != NULL); 487 /* 488 * Disable the MSI vector 489 * Make sure we only disable on the last 490 * of the multi-MSI support 491 */ 492 if (i_ddi_intr_get_current_nenables(avp->av_dip) == 1) { 493 apic_pci_msi_disable_mode(avp->av_dip, 494 DDI_INTR_TYPE_MSI); 495 } 496 break; 497 case APIX_TYPE_MSIX: 498 ASSERT(avp->av_vector != NULL && avp->av_dip != NULL); 499 /* 500 * Disable the MSI-X vector 501 * needs to clear its mask and addr/data for each MSI-X 502 */ 503 apic_pci_msi_unconfigure(avp->av_dip, DDI_INTR_TYPE_MSIX, 504 vecp->v_inum); 505 /* 506 * Make sure we only disable on the last MSI-X 507 */ 508 if (i_ddi_intr_get_current_nenables(avp->av_dip) == 1) { 509 apic_pci_msi_disable_mode(avp->av_dip, 510 DDI_INTR_TYPE_MSIX); 511 } 512 break; 513 default: 514 apix_intx_disable(vecp->v_inum); 515 break; 516 } 517 518 if (!(apic_cpus[vecp->v_cpuid].aci_status & APIC_CPU_SUSPEND)) 519 vecp->v_state = APIX_STATE_DISABLED; 520 apic_vt_ops->apic_intrmap_free_entry(&vecp->v_intrmap_private); 521 vecp->v_intrmap_private = NULL; 522 523 lock_clear(&apic_ioapic_lock); 524 intr_restore(iflag); 525 } 526 527 /* 528 * Mark vector as obsoleted or freed. The vector is marked 529 * obsoleted if there are pending requests on it. Otherwise, 530 * free the vector. The obsoleted vectors get freed after 531 * being serviced. 532 * 533 * Return 1 on being obosoleted and 0 on being freed. 534 */ 535 #define INTR_BUSY(_avp)\ 536 ((((volatile ushort_t)(_avp)->av_flags) &\ 537 (AV_PENTRY_PEND | AV_PENTRY_ONPROC)) != 0) 538 #define LOCAL_WITH_INTR_DISABLED(_cpuid)\ 539 ((_cpuid) == psm_get_cpu_id() && !interrupts_enabled()) 540 static uint64_t dummy_tick; 541 542 int 543 apix_obsolete_vector(apix_vector_t *vecp) 544 { 545 struct autovec *avp = vecp->v_autovect; 546 int repeats, tries, ipl, busy = 0, cpuid = vecp->v_cpuid; 547 apix_impl_t *apixp = apixs[cpuid]; 548 549 ASSERT(APIX_CPU_LOCK_HELD(cpuid)); 550 551 for (avp = vecp->v_autovect; avp != NULL; avp = avp->av_link) { 552 if (avp->av_vector == NULL) 553 continue; 554 555 if (LOCAL_WITH_INTR_DISABLED(cpuid)) { 556 int bit, index, irr; 557 558 if (INTR_BUSY(avp)) { 559 busy++; 560 continue; 561 } 562 563 /* check IRR for pending interrupts */ 564 index = vecp->v_vector / 32; 565 bit = vecp->v_vector % 32; 566 irr = apic_reg_ops->apic_read(APIC_IRR_REG + index); 567 if ((irr & (1 << bit)) != 0) 568 busy++; 569 570 if (!busy) 571 apix_remove_av(vecp, avp); 572 573 continue; 574 } 575 576 repeats = 0; 577 do { 578 repeats++; 579 for (tries = 0; tries < apic_max_reps_clear_pending; 580 tries++) 581 if (!INTR_BUSY(avp)) 582 break; 583 } while (INTR_BUSY(avp) && 584 (repeats < apic_max_reps_clear_pending)); 585 586 if (INTR_BUSY(avp)) 587 busy++; 588 else { 589 /* 590 * Interrupt is not in pending list or being serviced. 591 * However it might be cached in Local APIC's IRR 592 * register. It's impossible to check another CPU's 593 * IRR register. Then wait till lower levels finish 594 * running. 595 */ 596 for (ipl = 1; ipl < MIN(LOCK_LEVEL, vecp->v_pri); ipl++) 597 apix_wait_till_seen(cpuid, ipl); 598 if (INTR_BUSY(avp)) 599 busy++; 600 } 601 602 if (!busy) 603 apix_remove_av(vecp, avp); 604 } 605 606 if (busy) { 607 apix_vector_t *tp = apixp->x_obsoletes; 608 609 if (vecp->v_state == APIX_STATE_OBSOLETED) 610 return (1); 611 612 vecp->v_state = APIX_STATE_OBSOLETED; 613 vecp->v_next = NULL; 614 if (tp == NULL) 615 apixp->x_obsoletes = vecp; 616 else { 617 while (tp->v_next != NULL) 618 tp = tp->v_next; 619 tp->v_next = vecp; 620 } 621 return (1); 622 } 623 624 /* interrupt is not busy */ 625 if (vecp->v_state == APIX_STATE_OBSOLETED) { 626 /* remove from obsoleted list */ 627 apixp->x_obsoletes = vecp->v_next; 628 vecp->v_next = NULL; 629 } 630 apix_cleanup_vector(vecp); 631 return (0); 632 } 633 634 /* 635 * Duplicate number of continuous vectors to specified target vectors. 636 */ 637 static void 638 apix_dup_vectors(apix_vector_t *oldp, apix_vector_t *newp, int count) 639 { 640 struct autovec *avp; 641 apix_vector_t *fromp, *top; 642 processorid_t oldcpu = oldp->v_cpuid, newcpu = newp->v_cpuid; 643 uchar_t oldvec = oldp->v_vector, newvec = newp->v_vector; 644 int i, inum; 645 646 ASSERT(oldp->v_type != APIX_TYPE_IPI); 647 648 for (i = 0; i < count; i++) { 649 fromp = xv_vector(oldcpu, oldvec + i); 650 top = xv_vector(newcpu, newvec + i); 651 ASSERT(fromp != NULL && top != NULL); 652 653 /* copy over original one */ 654 top->v_state = fromp->v_state; 655 top->v_type = fromp->v_type; 656 top->v_bound_cpuid = fromp->v_bound_cpuid; 657 top->v_inum = fromp->v_inum; 658 top->v_flags = fromp->v_flags; 659 top->v_intrmap_private = fromp->v_intrmap_private; 660 661 for (avp = fromp->v_autovect; avp != NULL; avp = avp->av_link) { 662 if (avp->av_vector == NULL) 663 continue; 664 665 apix_insert_av(top, avp->av_intr_id, avp->av_vector, 666 avp->av_intarg1, avp->av_intarg2, avp->av_ticksp, 667 avp->av_prilevel, avp->av_dip); 668 669 if (fromp->v_type == APIX_TYPE_FIXED && 670 avp->av_dip != NULL) { 671 inum = GET_INTR_INUM(avp->av_intr_id); 672 apix_set_dev_map(top, avp->av_dip, inum); 673 } 674 } 675 676 if (DDI_INTR_IS_MSI_OR_MSIX(fromp->v_type) && 677 fromp->v_devp != NULL) 678 apix_set_dev_map(top, fromp->v_devp->dv_dip, 679 fromp->v_devp->dv_inum); 680 } 681 } 682 683 static apix_vector_t * 684 apix_init_vector(processorid_t cpuid, uchar_t vector) 685 { 686 apix_impl_t *apixp = apixs[cpuid]; 687 apix_vector_t *vecp = apixp->x_vectbl[vector]; 688 689 ASSERT(IS_VECT_FREE(vecp)); 690 691 if (vecp == NULL) { 692 vecp = kmem_zalloc(sizeof (apix_vector_t), KM_NOSLEEP); 693 if (vecp == NULL) { 694 cmn_err(CE_WARN, "apix: no memory to allocate vector"); 695 return (NULL); 696 } 697 apixp->x_vectbl[vector] = vecp; 698 } 699 vecp->v_state = APIX_STATE_ALLOCED; 700 vecp->v_cpuid = vecp->v_bound_cpuid = cpuid; 701 vecp->v_vector = vector; 702 703 return (vecp); 704 } 705 706 static void 707 apix_cleanup_vector(apix_vector_t *vecp) 708 { 709 ASSERT(vecp->v_share == 0); 710 vecp->v_bound_cpuid = IRQ_UNINIT; 711 vecp->v_state = APIX_STATE_FREED; 712 vecp->v_type = 0; 713 vecp->v_flags = 0; 714 vecp->v_busy = 0; 715 } 716 717 static void 718 apix_dprint_vector(apix_vector_t *vecp, dev_info_t *dip, int count) 719 { 720 #ifdef DEBUG 721 major_t major; 722 char *name, *drv_name; 723 int instance, len, t_len; 724 char mesg[1024] = "apix: "; 725 726 t_len = sizeof (mesg); 727 len = strlen(mesg); 728 if (dip != NULL) { 729 name = ddi_get_name(dip); 730 major = ddi_name_to_major(name); 731 drv_name = ddi_major_to_name(major); 732 instance = ddi_get_instance(dip); 733 (void) snprintf(mesg + len, t_len - len, "%s (%s) instance %d ", 734 name, drv_name, instance); 735 } 736 len = strlen(mesg); 737 738 switch (vecp->v_type) { 739 case APIX_TYPE_FIXED: 740 (void) snprintf(mesg + len, t_len - len, "irqno %d", 741 vecp->v_inum); 742 break; 743 case APIX_TYPE_MSI: 744 (void) snprintf(mesg + len, t_len - len, 745 "msi inum %d (count %d)", vecp->v_inum, count); 746 break; 747 case APIX_TYPE_MSIX: 748 (void) snprintf(mesg + len, t_len - len, "msi-x inum %d", 749 vecp->v_inum); 750 break; 751 default: 752 break; 753 754 } 755 756 APIC_VERBOSE(ALLOC, (CE_CONT, "%s allocated with vector 0x%x on " 757 "cpu %d\n", mesg, vecp->v_vector, vecp->v_cpuid)); 758 #endif /* DEBUG */ 759 } 760 761 /* 762 * Operations on avintr 763 */ 764 765 #define INIT_AUTOVEC(p, intr_id, f, arg1, arg2, ticksp, ipl, dip) \ 766 do { \ 767 (p)->av_intr_id = intr_id; \ 768 (p)->av_vector = f; \ 769 (p)->av_intarg1 = arg1; \ 770 (p)->av_intarg2 = arg2; \ 771 (p)->av_ticksp = ticksp; \ 772 (p)->av_prilevel = ipl; \ 773 (p)->av_dip = dip; \ 774 (p)->av_flags = 0; \ 775 _NOTE(CONSTCOND)} while (0) 776 777 /* 778 * Insert an interrupt service routine into chain by its priority from 779 * high to low 780 */ 781 static void 782 apix_insert_av(apix_vector_t *vecp, void *intr_id, avfunc f, caddr_t arg1, 783 caddr_t arg2, uint64_t *ticksp, int ipl, dev_info_t *dip) 784 { 785 struct autovec *p, *prep, *mem; 786 787 APIC_VERBOSE(INTR, (CE_CONT, "apix_insert_av: dip %p, vector 0x%x, " 788 "cpu %d\n", (void *)dip, vecp->v_vector, vecp->v_cpuid)); 789 790 mem = kmem_zalloc(sizeof (struct autovec), KM_SLEEP); 791 INIT_AUTOVEC(mem, intr_id, f, arg1, arg2, ticksp, ipl, dip); 792 if (vecp->v_type == APIX_TYPE_FIXED && apic_level_intr[vecp->v_inum]) 793 mem->av_flags |= AV_PENTRY_LEVEL; 794 795 vecp->v_share++; 796 vecp->v_pri = (ipl > vecp->v_pri) ? ipl : vecp->v_pri; 797 if (vecp->v_autovect == NULL) { /* Nothing on list - put it at head */ 798 vecp->v_autovect = mem; 799 return; 800 } 801 802 if (DDI_INTR_IS_MSI_OR_MSIX(vecp->v_type)) { /* MSI/X */ 803 ASSERT(vecp->v_share == 1); /* No sharing for MSI/X */ 804 805 INIT_AUTOVEC(vecp->v_autovect, intr_id, f, arg1, arg2, ticksp, 806 ipl, dip); 807 prep = vecp->v_autovect->av_link; 808 vecp->v_autovect->av_link = NULL; 809 810 /* Free the following autovect chain */ 811 while (prep != NULL) { 812 ASSERT(prep->av_vector == NULL); 813 814 p = prep; 815 prep = prep->av_link; 816 kmem_free(p, sizeof (struct autovec)); 817 } 818 819 kmem_free(mem, sizeof (struct autovec)); 820 return; 821 } 822 823 /* find where it goes in list */ 824 prep = NULL; 825 for (p = vecp->v_autovect; p != NULL; p = p->av_link) { 826 if (p->av_vector && p->av_prilevel <= ipl) 827 break; 828 prep = p; 829 } 830 if (prep != NULL) { 831 if (prep->av_vector == NULL) { /* freed struct available */ 832 INIT_AUTOVEC(prep, intr_id, f, arg1, arg2, 833 ticksp, ipl, dip); 834 prep->av_flags = mem->av_flags; 835 kmem_free(mem, sizeof (struct autovec)); 836 return; 837 } 838 839 mem->av_link = prep->av_link; 840 prep->av_link = mem; 841 } else { 842 /* insert new intpt at beginning of chain */ 843 mem->av_link = vecp->v_autovect; 844 vecp->v_autovect = mem; 845 } 846 } 847 848 /* 849 * After having made a change to an autovector list, wait until we have 850 * seen specified cpu not executing an interrupt at that level--so we 851 * know our change has taken effect completely (no old state in registers, 852 * etc). 853 */ 854 #define APIX_CPU_ENABLED(_cp) \ 855 (quiesce_active == 0 && \ 856 (((_cp)->cpu_flags & (CPU_QUIESCED|CPU_OFFLINE)) == 0)) 857 858 static void 859 apix_wait_till_seen(processorid_t cpuid, int ipl) 860 { 861 struct cpu *cp = cpu[cpuid]; 862 863 if (cp == NULL || LOCAL_WITH_INTR_DISABLED(cpuid)) 864 return; 865 866 /* 867 * Don't wait if the CPU is quiesced or offlined. This can happen 868 * when a CPU is running pause thread but hardware triggered an 869 * interrupt and the interrupt gets queued. 870 */ 871 for (;;) { 872 if (!INTR_ACTIVE((volatile struct cpu *)cpu[cpuid], ipl) && 873 (!APIX_CPU_ENABLED(cp) || 874 !INTR_PENDING((volatile apix_impl_t *)apixs[cpuid], ipl))) 875 return; 876 } 877 } 878 879 static void 880 apix_remove_av(apix_vector_t *vecp, struct autovec *target) 881 { 882 int hi_pri = 0; 883 struct autovec *p; 884 885 if (target == NULL) 886 return; 887 888 APIC_VERBOSE(INTR, (CE_CONT, "apix_remove_av: dip %p, vector 0x%x, " 889 "cpu %d\n", (void *)target->av_dip, vecp->v_vector, vecp->v_cpuid)); 890 891 for (p = vecp->v_autovect; p; p = p->av_link) { 892 if (p == target || p->av_vector == NULL) 893 continue; 894 hi_pri = (p->av_prilevel > hi_pri) ? p->av_prilevel : hi_pri; 895 } 896 897 vecp->v_share--; 898 vecp->v_pri = hi_pri; 899 900 /* 901 * This drops the handler from the chain, it can no longer be called. 902 * However, there is no guarantee that the handler is not currently 903 * still executing. 904 */ 905 target->av_vector = NULL; 906 /* 907 * There is a race where we could be just about to pick up the ticksp 908 * pointer to increment it after returning from the service routine 909 * in av_dispatch_autovect. Rather than NULL it out let's just point 910 * it off to something safe so that any final tick update attempt 911 * won't fault. 912 */ 913 target->av_ticksp = &dummy_tick; 914 apix_wait_till_seen(vecp->v_cpuid, target->av_prilevel); 915 } 916 917 static struct autovec * 918 apix_find_av(apix_vector_t *vecp, void *intr_id, avfunc f) 919 { 920 struct autovec *p; 921 922 for (p = vecp->v_autovect; p; p = p->av_link) { 923 if ((p->av_vector == f) && (p->av_intr_id == intr_id)) { 924 /* found the handler */ 925 return (p); 926 } 927 } 928 929 return (NULL); 930 } 931 932 static apix_vector_t * 933 apix_find_vector_by_avintr(void *intr_id, avfunc f) 934 { 935 apix_vector_t *vecp; 936 processorid_t n; 937 uchar_t v; 938 939 for (n = 0; n < apic_nproc; n++) { 940 if (!apix_is_cpu_enabled(n)) 941 continue; 942 943 for (v = APIX_AVINTR_MIN; v <= APIX_AVINTR_MIN; v++) { 944 vecp = xv_vector(n, v); 945 if (vecp == NULL || 946 vecp->v_state <= APIX_STATE_OBSOLETED) 947 continue; 948 949 if (apix_find_av(vecp, intr_id, f) != NULL) 950 return (vecp); 951 } 952 } 953 954 return (NULL); 955 } 956 957 /* 958 * Add interrupt service routine. 959 * 960 * For legacy interrupts (HPET timer, ACPI SCI), the vector is actually 961 * IRQ no. A vector is then allocated. Otherwise, the vector is already 962 * allocated. The input argument virt_vect is virtual vector of format 963 * APIX_VIRTVEC_VECTOR(cpuid, vector). 964 * 965 * Return 1 on success, 0 on failure. 966 */ 967 int 968 apix_add_avintr(void *intr_id, int ipl, avfunc xxintr, char *name, 969 int virt_vect, caddr_t arg1, caddr_t arg2, uint64_t *ticksp, 970 dev_info_t *dip) 971 { 972 int cpuid; 973 uchar_t v = (uchar_t)APIX_VIRTVEC_VECTOR(virt_vect); 974 apix_vector_t *vecp; 975 976 if (xxintr == NULL) { 977 cmn_err(CE_WARN, "Attempt to add null for %s " 978 "on vector 0x%x,0x%x", name, 979 APIX_VIRTVEC_CPU(virt_vect), 980 APIX_VIRTVEC_VECTOR(virt_vect)); 981 return (0); 982 } 983 984 if (v >= APIX_IPI_MIN) /* IPIs */ 985 return (apix_add_ipi(ipl, xxintr, name, v, arg1, arg2)); 986 987 if (!APIX_IS_VIRTVEC(virt_vect)) { /* got irq */ 988 int irqno = virt_vect; 989 int inum = GET_INTR_INUM(intr_id); 990 991 /* 992 * Senarios include: 993 * a. add_avintr() is called before irqp initialized (legacy) 994 * b. irqp is initialized, vector is not allocated (fixed) 995 * c. irqp is initialized, vector is allocated (fixed & shared) 996 */ 997 if ((vecp = apix_alloc_intx(dip, inum, irqno)) == NULL) 998 return (0); 999 1000 cpuid = vecp->v_cpuid; 1001 v = vecp->v_vector; 1002 virt_vect = APIX_VIRTVECTOR(cpuid, v); 1003 } else { /* got virtual vector */ 1004 cpuid = APIX_VIRTVEC_CPU(virt_vect); 1005 vecp = xv_vector(cpuid, v); 1006 ASSERT(vecp != NULL); 1007 } 1008 1009 lock_set(&apix_lock); 1010 if (vecp->v_state <= APIX_STATE_OBSOLETED) { 1011 vecp = NULL; 1012 1013 /* 1014 * Basically the allocated but not enabled interrupts 1015 * will not get re-targeted. But MSIs in allocated state 1016 * could be re-targeted due to group re-targeting. 1017 */ 1018 if (intr_id != NULL && dip != NULL) { 1019 ddi_intr_handle_impl_t *hdlp = intr_id; 1020 vecp = apix_get_dev_map(dip, hdlp->ih_inum, 1021 hdlp->ih_type); 1022 ASSERT(vecp->v_state == APIX_STATE_ALLOCED); 1023 } 1024 if (vecp == NULL) { 1025 lock_clear(&apix_lock); 1026 cmn_err(CE_WARN, "Invalid interrupt 0x%x,0x%x " 1027 " for %p to add", cpuid, v, intr_id); 1028 return (0); 1029 } 1030 cpuid = vecp->v_cpuid; 1031 virt_vect = APIX_VIRTVECTOR(cpuid, vecp->v_vector); 1032 } 1033 1034 APIX_ENTER_CPU_LOCK(cpuid); 1035 apix_insert_av(vecp, intr_id, xxintr, arg1, arg2, ticksp, ipl, dip); 1036 APIX_LEAVE_CPU_LOCK(cpuid); 1037 1038 (void) apix_addspl(virt_vect, ipl, 0, 0); 1039 1040 lock_clear(&apix_lock); 1041 1042 return (1); 1043 } 1044 1045 /* 1046 * Remove avintr 1047 * 1048 * For fixed, if it's the last one of shared interrupts, free the vector. 1049 * For msi/x, only disable the interrupt but not free the vector, which 1050 * is freed by PSM_XXX_FREE_XXX. 1051 */ 1052 void 1053 apix_rem_avintr(void *intr_id, int ipl, avfunc xxintr, int virt_vect) 1054 { 1055 avfunc f; 1056 apix_vector_t *vecp; 1057 struct autovec *avp; 1058 processorid_t cpuid; 1059 1060 if ((f = xxintr) == NULL) 1061 return; 1062 1063 lock_set(&apix_lock); 1064 1065 if (!APIX_IS_VIRTVEC(virt_vect)) { /* got irq */ 1066 vecp = apix_intx_get_vector(virt_vect); 1067 virt_vect = APIX_VIRTVECTOR(vecp->v_cpuid, vecp->v_vector); 1068 } else /* got virtual vector */ 1069 vecp = xv_vector(APIX_VIRTVEC_CPU(virt_vect), 1070 APIX_VIRTVEC_VECTOR(virt_vect)); 1071 1072 if (vecp == NULL) { 1073 lock_clear(&apix_lock); 1074 cmn_err(CE_CONT, "Invalid interrupt 0x%x,0x%x to remove", 1075 APIX_VIRTVEC_CPU(virt_vect), 1076 APIX_VIRTVEC_VECTOR(virt_vect)); 1077 return; 1078 } 1079 1080 if (vecp->v_state <= APIX_STATE_OBSOLETED || 1081 ((avp = apix_find_av(vecp, intr_id, f)) == NULL)) { 1082 /* 1083 * It's possible that the interrupt is rebound to a 1084 * different cpu before rem_avintr() is called. Search 1085 * through all vectors once it happens. 1086 */ 1087 if ((vecp = apix_find_vector_by_avintr(intr_id, f)) 1088 == NULL) { 1089 lock_clear(&apix_lock); 1090 cmn_err(CE_CONT, "Unknown interrupt 0x%x,0x%x " 1091 "for %p to remove", APIX_VIRTVEC_CPU(virt_vect), 1092 APIX_VIRTVEC_VECTOR(virt_vect), intr_id); 1093 return; 1094 } 1095 virt_vect = APIX_VIRTVECTOR(vecp->v_cpuid, vecp->v_vector); 1096 avp = apix_find_av(vecp, intr_id, f); 1097 } 1098 cpuid = vecp->v_cpuid; 1099 1100 /* disable interrupt */ 1101 (void) apix_delspl(virt_vect, ipl, 0, 0); 1102 1103 /* remove ISR entry */ 1104 APIX_ENTER_CPU_LOCK(cpuid); 1105 apix_remove_av(vecp, avp); 1106 APIX_LEAVE_CPU_LOCK(cpuid); 1107 1108 lock_clear(&apix_lock); 1109 } 1110 1111 /* 1112 * Device to vector mapping table 1113 */ 1114 1115 static void 1116 apix_clear_dev_map(dev_info_t *dip, int inum, int type) 1117 { 1118 char *name; 1119 major_t major; 1120 apix_dev_vector_t *dvp, *prev = NULL; 1121 int found = 0; 1122 1123 name = ddi_get_name(dip); 1124 major = ddi_name_to_major(name); 1125 1126 mutex_enter(&apix_mutex); 1127 1128 for (dvp = apix_dev_vector[major]; dvp != NULL; 1129 prev = dvp, dvp = dvp->dv_next) { 1130 if (dvp->dv_dip == dip && dvp->dv_inum == inum && 1131 dvp->dv_type == type) { 1132 found++; 1133 break; 1134 } 1135 } 1136 1137 if (!found) { 1138 mutex_exit(&apix_mutex); 1139 return; 1140 } 1141 1142 if (prev != NULL) 1143 prev->dv_next = dvp->dv_next; 1144 1145 if (apix_dev_vector[major] == dvp) 1146 apix_dev_vector[major] = dvp->dv_next; 1147 1148 dvp->dv_vector->v_devp = NULL; 1149 1150 mutex_exit(&apix_mutex); 1151 1152 kmem_free(dvp, sizeof (apix_dev_vector_t)); 1153 } 1154 1155 void 1156 apix_set_dev_map(apix_vector_t *vecp, dev_info_t *dip, int inum) 1157 { 1158 apix_dev_vector_t *dvp; 1159 char *name; 1160 major_t major; 1161 uint32_t found = 0; 1162 1163 ASSERT(dip != NULL); 1164 name = ddi_get_name(dip); 1165 major = ddi_name_to_major(name); 1166 1167 mutex_enter(&apix_mutex); 1168 1169 for (dvp = apix_dev_vector[major]; dvp != NULL; 1170 dvp = dvp->dv_next) { 1171 if (dvp->dv_dip == dip && dvp->dv_inum == inum && 1172 dvp->dv_type == vecp->v_type) { 1173 found++; 1174 break; 1175 } 1176 } 1177 1178 if (found == 0) { /* not found */ 1179 dvp = kmem_zalloc(sizeof (apix_dev_vector_t), KM_SLEEP); 1180 dvp->dv_dip = dip; 1181 dvp->dv_inum = inum; 1182 dvp->dv_type = vecp->v_type; 1183 1184 dvp->dv_next = apix_dev_vector[major]; 1185 apix_dev_vector[major] = dvp; 1186 } 1187 dvp->dv_vector = vecp; 1188 vecp->v_devp = dvp; 1189 1190 mutex_exit(&apix_mutex); 1191 1192 DDI_INTR_IMPLDBG((CE_CONT, "apix_set_dev_map: dip=0x%p " 1193 "inum=0x%x vector=0x%x/0x%x\n", 1194 (void *)dip, inum, vecp->v_cpuid, vecp->v_vector)); 1195 } 1196 1197 apix_vector_t * 1198 apix_get_dev_map(dev_info_t *dip, int inum, int type) 1199 { 1200 char *name; 1201 major_t major; 1202 apix_dev_vector_t *dvp; 1203 apix_vector_t *vecp; 1204 1205 name = ddi_get_name(dip); 1206 if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE) 1207 return (NULL); 1208 1209 mutex_enter(&apix_mutex); 1210 for (dvp = apix_dev_vector[major]; dvp != NULL; 1211 dvp = dvp->dv_next) { 1212 if (dvp->dv_dip == dip && dvp->dv_inum == inum && 1213 dvp->dv_type == type) { 1214 vecp = dvp->dv_vector; 1215 mutex_exit(&apix_mutex); 1216 return (vecp); 1217 } 1218 } 1219 mutex_exit(&apix_mutex); 1220 1221 return (NULL); 1222 } 1223 1224 /* 1225 * Get minimum inum for specified device, used for MSI 1226 */ 1227 int 1228 apix_get_min_dev_inum(dev_info_t *dip, int type) 1229 { 1230 char *name; 1231 major_t major; 1232 apix_dev_vector_t *dvp; 1233 int inum = -1; 1234 1235 name = ddi_get_name(dip); 1236 major = ddi_name_to_major(name); 1237 1238 mutex_enter(&apix_mutex); 1239 for (dvp = apix_dev_vector[major]; dvp != NULL; 1240 dvp = dvp->dv_next) { 1241 if (dvp->dv_dip == dip && dvp->dv_type == type) { 1242 if (inum == -1) 1243 inum = dvp->dv_inum; 1244 else 1245 inum = (dvp->dv_inum < inum) ? 1246 dvp->dv_inum : inum; 1247 } 1248 } 1249 mutex_exit(&apix_mutex); 1250 1251 return (inum); 1252 } 1253 1254 int 1255 apix_get_max_dev_inum(dev_info_t *dip, int type) 1256 { 1257 char *name; 1258 major_t major; 1259 apix_dev_vector_t *dvp; 1260 int inum = -1; 1261 1262 name = ddi_get_name(dip); 1263 major = ddi_name_to_major(name); 1264 1265 mutex_enter(&apix_mutex); 1266 for (dvp = apix_dev_vector[major]; dvp != NULL; 1267 dvp = dvp->dv_next) { 1268 if (dvp->dv_dip == dip && dvp->dv_type == type) { 1269 if (inum == -1) 1270 inum = dvp->dv_inum; 1271 else 1272 inum = (dvp->dv_inum > inum) ? 1273 dvp->dv_inum : inum; 1274 } 1275 } 1276 mutex_exit(&apix_mutex); 1277 1278 return (inum); 1279 } 1280 1281 /* 1282 * Major to cpu binding, for INTR_ROUND_ROBIN_WITH_AFFINITY cpu 1283 * binding policy 1284 */ 1285 1286 static uint32_t 1287 apix_get_dev_binding(dev_info_t *dip) 1288 { 1289 major_t major; 1290 char *name; 1291 uint32_t cpu = IRQ_UNINIT; 1292 1293 name = ddi_get_name(dip); 1294 major = ddi_name_to_major(name); 1295 if (major < devcnt) { 1296 mutex_enter(&apix_mutex); 1297 cpu = apix_major_to_cpu[major]; 1298 mutex_exit(&apix_mutex); 1299 } 1300 1301 return (cpu); 1302 } 1303 1304 static void 1305 apix_set_dev_binding(dev_info_t *dip, uint32_t cpu) 1306 { 1307 major_t major; 1308 char *name; 1309 1310 /* setup major to cpu mapping */ 1311 name = ddi_get_name(dip); 1312 major = ddi_name_to_major(name); 1313 if (apix_major_to_cpu[major] == IRQ_UNINIT) { 1314 mutex_enter(&apix_mutex); 1315 apix_major_to_cpu[major] = cpu; 1316 mutex_exit(&apix_mutex); 1317 } 1318 } 1319 1320 /* 1321 * return the cpu to which this intr should be bound. 1322 * Check properties or any other mechanism to see if user wants it 1323 * bound to a specific CPU. If so, return the cpu id with high bit set. 1324 * If not, use the policy to choose a cpu and return the id. 1325 */ 1326 uint32_t 1327 apix_bind_cpu(dev_info_t *dip) 1328 { 1329 int instance, instno, prop_len, bind_cpu, count; 1330 uint_t i, rc; 1331 major_t major; 1332 char *name, *drv_name, *prop_val, *cptr; 1333 char prop_name[32]; 1334 1335 lock_set(&apix_lock); 1336 1337 if (apic_intr_policy == INTR_LOWEST_PRIORITY) { 1338 cmn_err(CE_WARN, "apix: unsupported interrupt binding policy " 1339 "LOWEST PRIORITY, use ROUND ROBIN instead"); 1340 apic_intr_policy = INTR_ROUND_ROBIN; 1341 } 1342 1343 if (apic_nproc == 1) { 1344 lock_clear(&apix_lock); 1345 return (0); 1346 } 1347 1348 drv_name = NULL; 1349 rc = DDI_PROP_NOT_FOUND; 1350 major = (major_t)-1; 1351 if (dip != NULL) { 1352 name = ddi_get_name(dip); 1353 major = ddi_name_to_major(name); 1354 drv_name = ddi_major_to_name(major); 1355 instance = ddi_get_instance(dip); 1356 if (apic_intr_policy == INTR_ROUND_ROBIN_WITH_AFFINITY) { 1357 bind_cpu = apix_get_dev_binding(dip); 1358 if (bind_cpu != IRQ_UNINIT) { 1359 lock_clear(&apix_lock); 1360 return (bind_cpu); 1361 } 1362 } 1363 /* 1364 * search for "drvname"_intpt_bind_cpus property first, the 1365 * syntax of the property should be "a[,b,c,...]" where 1366 * instance 0 binds to cpu a, instance 1 binds to cpu b, 1367 * instance 3 binds to cpu c... 1368 * ddi_getlongprop() will search /option first, then / 1369 * if "drvname"_intpt_bind_cpus doesn't exist, then find 1370 * intpt_bind_cpus property. The syntax is the same, and 1371 * it applies to all the devices if its "drvname" specific 1372 * property doesn't exist 1373 */ 1374 (void) strcpy(prop_name, drv_name); 1375 (void) strcat(prop_name, "_intpt_bind_cpus"); 1376 rc = ddi_getlongprop(DDI_DEV_T_ANY, dip, 0, prop_name, 1377 (caddr_t)&prop_val, &prop_len); 1378 if (rc != DDI_PROP_SUCCESS) { 1379 rc = ddi_getlongprop(DDI_DEV_T_ANY, dip, 0, 1380 "intpt_bind_cpus", (caddr_t)&prop_val, &prop_len); 1381 } 1382 } 1383 if (rc == DDI_PROP_SUCCESS) { 1384 for (i = count = 0; i < (prop_len - 1); i++) 1385 if (prop_val[i] == ',') 1386 count++; 1387 if (prop_val[i-1] != ',') 1388 count++; 1389 /* 1390 * if somehow the binding instances defined in the 1391 * property are not enough for this instno., then 1392 * reuse the pattern for the next instance until 1393 * it reaches the requested instno 1394 */ 1395 instno = instance % count; 1396 i = 0; 1397 cptr = prop_val; 1398 while (i < instno) 1399 if (*cptr++ == ',') 1400 i++; 1401 bind_cpu = stoi(&cptr); 1402 kmem_free(prop_val, prop_len); 1403 /* if specific cpu is bogus, then default to cpu 0 */ 1404 if (bind_cpu >= apic_nproc) { 1405 cmn_err(CE_WARN, "apix: %s=%s: CPU %d not present", 1406 prop_name, prop_val, bind_cpu); 1407 bind_cpu = 0; 1408 } else { 1409 /* indicate that we are bound at user request */ 1410 bind_cpu |= IRQ_USER_BOUND; 1411 } 1412 /* 1413 * no need to check apic_cpus[].aci_status, if specific cpu is 1414 * not up, then post_cpu_start will handle it. 1415 */ 1416 } else { 1417 bind_cpu = apic_get_next_bind_cpu(); 1418 } 1419 1420 lock_clear(&apix_lock); 1421 1422 return ((uint32_t)bind_cpu); 1423 } 1424 1425 static boolean_t 1426 apix_is_cpu_enabled(processorid_t cpuid) 1427 { 1428 apic_cpus_info_t *cpu_infop; 1429 1430 cpu_infop = &apic_cpus[cpuid]; 1431 1432 if ((cpu_infop->aci_status & APIC_CPU_INTR_ENABLE) == 0) 1433 return (B_FALSE); 1434 1435 return (B_TRUE); 1436 } 1437 1438 /* 1439 * Must be called with apix_lock held. This function can be 1440 * called from above lock level by apix_intr_redistribute(). 1441 * 1442 * Arguments: 1443 * vecp : Vector to be rebound 1444 * tocpu : Target cpu. IRQ_UNINIT means target is vecp->v_cpuid. 1445 * count : Number of continuous vectors 1446 * 1447 * Return new vector being bound to 1448 */ 1449 apix_vector_t * 1450 apix_rebind(apix_vector_t *vecp, processorid_t newcpu, int count) 1451 { 1452 apix_vector_t *newp, *oldp; 1453 processorid_t oldcpu = vecp->v_cpuid; 1454 uchar_t newvec, oldvec = vecp->v_vector; 1455 int i; 1456 1457 ASSERT(LOCK_HELD(&apix_lock) && count > 0); 1458 1459 if (!apix_is_cpu_enabled(newcpu)) 1460 return (NULL); 1461 1462 if (vecp->v_cpuid == newcpu) /* rebind to the same cpu */ 1463 return (vecp); 1464 1465 APIX_ENTER_CPU_LOCK(oldcpu); 1466 APIX_ENTER_CPU_LOCK(newcpu); 1467 1468 /* allocate vector */ 1469 if (count == 1) 1470 newp = apix_alloc_vector_oncpu(newcpu, NULL, 0, vecp->v_type); 1471 else { 1472 ASSERT(vecp->v_type == APIX_TYPE_MSI); 1473 newp = apix_alloc_nvectors_oncpu(newcpu, NULL, 0, count, 1474 vecp->v_type); 1475 } 1476 if (newp == NULL) { 1477 APIX_LEAVE_CPU_LOCK(newcpu); 1478 APIX_LEAVE_CPU_LOCK(oldcpu); 1479 return (NULL); 1480 } 1481 1482 newvec = newp->v_vector; 1483 apix_dup_vectors(vecp, newp, count); 1484 1485 APIX_LEAVE_CPU_LOCK(newcpu); 1486 APIX_LEAVE_CPU_LOCK(oldcpu); 1487 1488 if (!DDI_INTR_IS_MSI_OR_MSIX(vecp->v_type)) { 1489 ASSERT(count == 1); 1490 if (apix_intx_rebind(vecp->v_inum, newcpu, newvec) != 0) { 1491 struct autovec *avp; 1492 int inum; 1493 1494 /* undo duplication */ 1495 APIX_ENTER_CPU_LOCK(oldcpu); 1496 APIX_ENTER_CPU_LOCK(newcpu); 1497 for (avp = newp->v_autovect; avp != NULL; 1498 avp = avp->av_link) { 1499 if (avp->av_dip != NULL) { 1500 inum = GET_INTR_INUM(avp->av_intr_id); 1501 apix_set_dev_map(vecp, avp->av_dip, 1502 inum); 1503 } 1504 apix_remove_av(newp, avp); 1505 } 1506 apix_cleanup_vector(newp); 1507 APIX_LEAVE_CPU_LOCK(newcpu); 1508 APIX_LEAVE_CPU_LOCK(oldcpu); 1509 APIC_VERBOSE(REBIND, (CE_CONT, "apix: rebind fixed " 1510 "interrupt 0x%x to cpu %d failed\n", 1511 vecp->v_inum, newcpu)); 1512 return (NULL); 1513 } 1514 1515 APIX_ENTER_CPU_LOCK(oldcpu); 1516 (void) apix_obsolete_vector(vecp); 1517 APIX_LEAVE_CPU_LOCK(oldcpu); 1518 APIC_VERBOSE(REBIND, (CE_CONT, "apix: rebind fixed interrupt" 1519 " 0x%x/0x%x to 0x%x/0x%x\n", 1520 oldcpu, oldvec, newcpu, newvec)); 1521 return (newp); 1522 } 1523 1524 for (i = 0; i < count; i++) { 1525 oldp = xv_vector(oldcpu, oldvec + i); 1526 newp = xv_vector(newcpu, newvec + i); 1527 1528 if (newp->v_share > 0) { 1529 APIX_SET_REBIND_INFO(oldp, newp); 1530 1531 apix_enable_vector(newp); 1532 1533 APIX_CLR_REBIND_INFO(); 1534 } 1535 1536 APIX_ENTER_CPU_LOCK(oldcpu); 1537 (void) apix_obsolete_vector(oldp); 1538 APIX_LEAVE_CPU_LOCK(oldcpu); 1539 } 1540 APIC_VERBOSE(REBIND, (CE_CONT, "apix: rebind vector 0x%x/0x%x " 1541 "to 0x%x/0x%x, count=%d\n", 1542 oldcpu, oldvec, newcpu, newvec, count)); 1543 1544 return (xv_vector(newcpu, newvec)); 1545 } 1546 1547 /* 1548 * Senarios include: 1549 * a. add_avintr() is called before irqp initialized (legacy) 1550 * b. irqp is initialized, vector is not allocated (fixed interrupts) 1551 * c. irqp is initialized, vector is allocated (shared interrupts) 1552 */ 1553 apix_vector_t * 1554 apix_alloc_intx(dev_info_t *dip, int inum, int irqno) 1555 { 1556 apic_irq_t *irqp; 1557 apix_vector_t *vecp; 1558 1559 /* 1560 * Allocate IRQ. Caller is later responsible for the 1561 * initialization 1562 */ 1563 mutex_enter(&airq_mutex); 1564 if ((irqp = apic_irq_table[irqno]) == NULL) { 1565 /* allocate irq */ 1566 irqp = kmem_zalloc(sizeof (apic_irq_t), KM_SLEEP); 1567 irqp->airq_mps_intr_index = FREE_INDEX; 1568 apic_irq_table[irqno] = irqp; 1569 } 1570 if (irqp->airq_mps_intr_index == FREE_INDEX) { 1571 irqp->airq_mps_intr_index = DEFAULT_INDEX; 1572 irqp->airq_cpu = IRQ_UNINIT; 1573 irqp->airq_origirq = (uchar_t)irqno; 1574 } 1575 1576 mutex_exit(&airq_mutex); 1577 1578 /* 1579 * allocate vector 1580 */ 1581 if (irqp->airq_cpu == IRQ_UNINIT) { 1582 uint32_t bindcpu, cpuid; 1583 1584 /* select cpu by system policy */ 1585 bindcpu = apix_bind_cpu(dip); 1586 cpuid = bindcpu & ~IRQ_USER_BOUND; 1587 1588 /* allocate vector */ 1589 APIX_ENTER_CPU_LOCK(cpuid); 1590 1591 if ((vecp = apix_alloc_vector_oncpu(bindcpu, dip, inum, 1592 APIX_TYPE_FIXED)) == NULL) { 1593 cmn_err(CE_WARN, "No interrupt vector for irq %x", 1594 irqno); 1595 APIX_LEAVE_CPU_LOCK(cpuid); 1596 return (NULL); 1597 } 1598 vecp->v_inum = irqno; 1599 vecp->v_flags |= APIX_VECT_MASKABLE; 1600 1601 apix_intx_set_vector(irqno, vecp->v_cpuid, vecp->v_vector); 1602 1603 APIX_LEAVE_CPU_LOCK(cpuid); 1604 } else { 1605 vecp = xv_vector(irqp->airq_cpu, irqp->airq_vector); 1606 ASSERT(!IS_VECT_FREE(vecp)); 1607 1608 if (dip != NULL) 1609 apix_set_dev_map(vecp, dip, inum); 1610 } 1611 1612 if ((dip != NULL) && 1613 (apic_intr_policy == INTR_ROUND_ROBIN_WITH_AFFINITY) && 1614 ((vecp->v_flags & APIX_VECT_USER_BOUND) == 0)) 1615 apix_set_dev_binding(dip, vecp->v_cpuid); 1616 1617 apix_dprint_vector(vecp, dip, 1); 1618 1619 return (vecp); 1620 } 1621 1622 int 1623 apix_alloc_msi(dev_info_t *dip, int inum, int count, int behavior) 1624 { 1625 int i, cap_ptr, rcount = count; 1626 apix_vector_t *vecp; 1627 processorid_t bindcpu, cpuid; 1628 ushort_t msi_ctrl; 1629 ddi_acc_handle_t handle; 1630 1631 DDI_INTR_IMPLDBG((CE_CONT, "apix_alloc_msi_vectors: dip=0x%p " 1632 "inum=0x%x count=0x%x behavior=%d\n", 1633 (void *)dip, inum, count, behavior)); 1634 1635 if (count > 1) { 1636 if (behavior == DDI_INTR_ALLOC_STRICT && 1637 apic_multi_msi_enable == 0) 1638 return (0); 1639 if (apic_multi_msi_enable == 0) 1640 count = 1; 1641 } 1642 1643 /* Check whether it supports per-vector masking */ 1644 cap_ptr = i_ddi_get_msi_msix_cap_ptr(dip); 1645 handle = i_ddi_get_pci_config_handle(dip); 1646 msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL); 1647 1648 /* bind to cpu */ 1649 bindcpu = apix_bind_cpu(dip); 1650 cpuid = bindcpu & ~IRQ_USER_BOUND; 1651 1652 /* if not ISP2, then round it down */ 1653 if (!ISP2(rcount)) 1654 rcount = 1 << (highbit(rcount) - 1); 1655 1656 APIX_ENTER_CPU_LOCK(cpuid); 1657 for (vecp = NULL; rcount > 0; rcount >>= 1) { 1658 vecp = apix_alloc_nvectors_oncpu(bindcpu, dip, inum, rcount, 1659 APIX_TYPE_MSI); 1660 if (vecp != NULL || behavior == DDI_INTR_ALLOC_STRICT) 1661 break; 1662 } 1663 for (i = 0; vecp && i < rcount; i++) 1664 xv_vector(vecp->v_cpuid, vecp->v_vector + i)->v_flags |= 1665 (msi_ctrl & PCI_MSI_PVM_MASK) ? APIX_VECT_MASKABLE : 0; 1666 APIX_LEAVE_CPU_LOCK(cpuid); 1667 if (vecp == NULL) { 1668 APIC_VERBOSE(INTR, (CE_CONT, 1669 "apix_alloc_msi: no %d cont vectors found on cpu 0x%x\n", 1670 count, bindcpu)); 1671 return (0); 1672 } 1673 1674 /* major to cpu binding */ 1675 if ((apic_intr_policy == INTR_ROUND_ROBIN_WITH_AFFINITY) && 1676 ((vecp->v_flags & APIX_VECT_USER_BOUND) == 0)) 1677 apix_set_dev_binding(dip, vecp->v_cpuid); 1678 1679 apix_dprint_vector(vecp, dip, rcount); 1680 1681 return (rcount); 1682 } 1683 1684 int 1685 apix_alloc_msix(dev_info_t *dip, int inum, int count, int behavior) 1686 { 1687 apix_vector_t *vecp; 1688 processorid_t bindcpu, cpuid; 1689 int i; 1690 1691 for (i = 0; i < count; i++) { 1692 /* select cpu by system policy */ 1693 bindcpu = apix_bind_cpu(dip); 1694 cpuid = bindcpu & ~IRQ_USER_BOUND; 1695 1696 /* allocate vector */ 1697 APIX_ENTER_CPU_LOCK(cpuid); 1698 if ((vecp = apix_alloc_vector_oncpu(bindcpu, dip, inum + i, 1699 APIX_TYPE_MSIX)) == NULL) { 1700 APIX_LEAVE_CPU_LOCK(cpuid); 1701 APIC_VERBOSE(INTR, (CE_CONT, "apix_alloc_msix: " 1702 "allocate msix for device dip=%p, inum=%d on" 1703 " cpu %d failed", (void *)dip, inum + i, bindcpu)); 1704 break; 1705 } 1706 vecp->v_flags |= APIX_VECT_MASKABLE; 1707 APIX_LEAVE_CPU_LOCK(cpuid); 1708 1709 /* major to cpu mapping */ 1710 if ((i == 0) && 1711 (apic_intr_policy == INTR_ROUND_ROBIN_WITH_AFFINITY) && 1712 ((vecp->v_flags & APIX_VECT_USER_BOUND) == 0)) 1713 apix_set_dev_binding(dip, vecp->v_cpuid); 1714 1715 apix_dprint_vector(vecp, dip, 1); 1716 } 1717 1718 if (i < count && behavior == DDI_INTR_ALLOC_STRICT) { 1719 APIC_VERBOSE(INTR, (CE_WARN, "apix_alloc_msix: " 1720 "strictly allocate %d vectors failed, got %d\n", 1721 count, i)); 1722 apix_free_vectors(dip, inum, i, APIX_TYPE_MSIX); 1723 i = 0; 1724 } 1725 1726 return (i); 1727 } 1728 1729 /* 1730 * A rollback free for vectors allocated by apix_alloc_xxx(). 1731 */ 1732 void 1733 apix_free_vectors(dev_info_t *dip, int inum, int count, int type) 1734 { 1735 int i, cpuid; 1736 apix_vector_t *vecp; 1737 1738 DDI_INTR_IMPLDBG((CE_CONT, "apix_free_vectors: dip: %p inum: %x " 1739 "count: %x type: %x\n", 1740 (void *)dip, inum, count, type)); 1741 1742 lock_set(&apix_lock); 1743 1744 for (i = 0; i < count; i++, inum++) { 1745 if ((vecp = apix_get_dev_map(dip, inum, type)) == NULL) { 1746 lock_clear(&apix_lock); 1747 DDI_INTR_IMPLDBG((CE_CONT, "apix_free_vectors: " 1748 "dip=0x%p inum=0x%x type=0x%x apix_find_intr() " 1749 "failed\n", (void *)dip, inum, type)); 1750 continue; 1751 } 1752 1753 APIX_ENTER_CPU_LOCK(vecp->v_cpuid); 1754 cpuid = vecp->v_cpuid; 1755 1756 DDI_INTR_IMPLDBG((CE_CONT, "apix_free_vectors: " 1757 "dip=0x%p inum=0x%x type=0x%x vector 0x%x (share %d)\n", 1758 (void *)dip, inum, type, vecp->v_vector, vecp->v_share)); 1759 1760 /* tear down device interrupt to vector mapping */ 1761 apix_clear_dev_map(dip, inum, type); 1762 1763 if (vecp->v_type == APIX_TYPE_FIXED) { 1764 if (vecp->v_share > 0) { /* share IRQ line */ 1765 APIX_LEAVE_CPU_LOCK(cpuid); 1766 continue; 1767 } 1768 1769 /* Free apic_irq_table entry */ 1770 apix_intx_free(vecp->v_inum); 1771 } 1772 1773 /* free vector */ 1774 apix_cleanup_vector(vecp); 1775 1776 APIX_LEAVE_CPU_LOCK(cpuid); 1777 } 1778 1779 lock_clear(&apix_lock); 1780 } 1781 1782 /* 1783 * Must be called with apix_lock held 1784 */ 1785 apix_vector_t * 1786 apix_setup_io_intr(apix_vector_t *vecp) 1787 { 1788 processorid_t bindcpu; 1789 int ret; 1790 1791 ASSERT(LOCK_HELD(&apix_lock)); 1792 1793 /* 1794 * Interrupts are enabled on the CPU, programme IOAPIC RDT 1795 * entry or MSI/X address/data to enable the interrupt. 1796 */ 1797 if (apix_is_cpu_enabled(vecp->v_cpuid)) { 1798 apix_enable_vector(vecp); 1799 return (vecp); 1800 } 1801 1802 /* 1803 * CPU is not up or interrupts are disabled. Fall back to the 1804 * first avialable CPU. 1805 */ 1806 bindcpu = apic_find_cpu(APIC_CPU_INTR_ENABLE); 1807 1808 if (vecp->v_type == APIX_TYPE_MSI) 1809 return (apix_grp_set_cpu(vecp, bindcpu, &ret)); 1810 1811 return (apix_set_cpu(vecp, bindcpu, &ret)); 1812 } 1813 1814 /* 1815 * For interrupts which call add_avintr() before apic is initialized. 1816 * ioapix_setup_intr() will 1817 * - allocate vector 1818 * - copy over ISR 1819 */ 1820 static void 1821 ioapix_setup_intr(int irqno, iflag_t *flagp) 1822 { 1823 extern struct av_head autovect[]; 1824 apix_vector_t *vecp; 1825 apic_irq_t *irqp; 1826 uchar_t ioapicindex, ipin; 1827 ulong_t iflag; 1828 struct autovec *avp; 1829 1830 irqp = apic_irq_table[irqno]; 1831 ioapicindex = acpi_find_ioapic(irqno); 1832 ASSERT(ioapicindex != 0xFF); 1833 ipin = irqno - apic_io_vectbase[ioapicindex]; 1834 1835 if ((irqp != NULL) && (irqp->airq_mps_intr_index == ACPI_INDEX)) { 1836 ASSERT(irqp->airq_intin_no == ipin && 1837 irqp->airq_ioapicindex == ioapicindex); 1838 vecp = xv_vector(irqp->airq_cpu, irqp->airq_vector); 1839 ASSERT(!IS_VECT_FREE(vecp)); 1840 } else { 1841 vecp = apix_alloc_intx(NULL, 0, irqno); 1842 1843 irqp = apic_irq_table[irqno]; 1844 irqp->airq_mps_intr_index = ACPI_INDEX; 1845 irqp->airq_ioapicindex = ioapicindex; 1846 irqp->airq_intin_no = ipin; 1847 irqp->airq_iflag = *flagp; 1848 irqp->airq_share++; 1849 apic_record_rdt_entry(irqp, irqno); 1850 } 1851 1852 /* copy over autovect */ 1853 for (avp = autovect[irqno].avh_link; avp; avp = avp->av_link) 1854 apix_insert_av(vecp, avp->av_intr_id, avp->av_vector, 1855 avp->av_intarg1, avp->av_intarg2, avp->av_ticksp, 1856 avp->av_prilevel, avp->av_dip); 1857 1858 /* Program I/O APIC */ 1859 iflag = intr_clear(); 1860 lock_set(&apix_lock); 1861 1862 (void) apix_setup_io_intr(vecp); 1863 1864 lock_clear(&apix_lock); 1865 intr_restore(iflag); 1866 1867 APIC_VERBOSE_IOAPIC((CE_CONT, "apix: setup ioapic, irqno %x " 1868 "(ioapic %x, ipin %x) is bound to cpu %x, vector %x\n", 1869 irqno, ioapicindex, ipin, irqp->airq_cpu, irqp->airq_vector)); 1870 } 1871 1872 void 1873 ioapix_init_intr(int mask_apic) 1874 { 1875 int ioapicindex; 1876 int i, j; 1877 1878 /* mask interrupt vectors */ 1879 for (j = 0; j < apic_io_max && mask_apic; j++) { 1880 int intin_max; 1881 1882 ioapicindex = j; 1883 /* Bits 23-16 define the maximum redirection entries */ 1884 intin_max = (ioapic_read(ioapicindex, APIC_VERS_CMD) >> 16) 1885 & 0xff; 1886 for (i = 0; i <= intin_max; i++) 1887 ioapic_write(ioapicindex, APIC_RDT_CMD + 2 * i, 1888 AV_MASK); 1889 } 1890 1891 /* 1892 * Hack alert: deal with ACPI SCI interrupt chicken/egg here 1893 */ 1894 if (apic_sci_vect > 0) 1895 ioapix_setup_intr(apic_sci_vect, &apic_sci_flags); 1896 1897 /* 1898 * Hack alert: deal with ACPI HPET interrupt chicken/egg here. 1899 */ 1900 if (apic_hpet_vect > 0) 1901 ioapix_setup_intr(apic_hpet_vect, &apic_hpet_flags); 1902 } 1903