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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * PSMI 1.1 extensions are supported only in 2.6 and later versions. 31 * PSMI 1.2 extensions are supported only in 2.7 and later versions. 32 * PSMI 1.3 and 1.4 extensions are supported in Solaris 10. 33 * PSMI 1.5 extensions are supported in Solaris Nevada. 34 */ 35 #define PSMI_1_5 36 37 #include <sys/processor.h> 38 #include <sys/time.h> 39 #include <sys/psm.h> 40 #include <sys/smp_impldefs.h> 41 #include <sys/cram.h> 42 #include <sys/acpi/acpi.h> 43 #include <sys/acpica.h> 44 #include <sys/psm_common.h> 45 #include "apic.h" 46 #include <sys/pit.h> 47 #include <sys/ddi.h> 48 #include <sys/sunddi.h> 49 #include <sys/ddi_impldefs.h> 50 #include <sys/pci.h> 51 #include <sys/promif.h> 52 #include <sys/x86_archext.h> 53 #include <sys/cpc_impl.h> 54 #include <sys/uadmin.h> 55 #include <sys/panic.h> 56 #include <sys/debug.h> 57 #include <sys/archsystm.h> 58 #include <sys/trap.h> 59 #include <sys/machsystm.h> 60 #include <sys/cpuvar.h> 61 #include <sys/rm_platter.h> 62 #include <sys/privregs.h> 63 #include <sys/cyclic.h> 64 #include <sys/note.h> 65 #include <sys/pci_intr_lib.h> 66 67 /* 68 * Local Function Prototypes 69 */ 70 static void apic_init_intr(); 71 static void apic_ret(); 72 static int apic_handle_defconf(); 73 static int apic_parse_mpct(caddr_t mpct, int bypass); 74 static struct apic_mpfps_hdr *apic_find_fps_sig(caddr_t fptr, int size); 75 static int apic_checksum(caddr_t bptr, int len); 76 static int get_apic_cmd1(); 77 static int get_apic_pri(); 78 static int apic_find_bus_type(char *bus); 79 static int apic_find_bus(int busid); 80 static int apic_find_bus_id(int bustype); 81 static struct apic_io_intr *apic_find_io_intr(int irqno); 82 int apic_allocate_irq(int irq); 83 static int apic_find_free_irq(int start, int end); 84 static uchar_t apic_allocate_vector(int ipl, int irq, int pri); 85 static void apic_modify_vector(uchar_t vector, int irq); 86 static void apic_mark_vector(uchar_t oldvector, uchar_t newvector); 87 static uchar_t apic_xlate_vector(uchar_t oldvector); 88 static void apic_xlate_vector_free_timeout_handler(void *arg); 89 static void apic_free_vector(uchar_t vector); 90 static void apic_reprogram_timeout_handler(void *arg); 91 static int apic_check_stuck_interrupt(apic_irq_t *irq_ptr, int old_bind_cpu, 92 int new_bind_cpu, volatile int32_t *ioapic, int intin_no, int which_irq); 93 static int apic_setup_io_intr(apic_irq_t *irqptr, int irq); 94 static int apic_setup_io_intr_deferred(apic_irq_t *irqptr, int irq); 95 static void apic_record_rdt_entry(apic_irq_t *irqptr, int irq); 96 static struct apic_io_intr *apic_find_io_intr_w_busid(int irqno, int busid); 97 static int apic_find_intin(uchar_t ioapic, uchar_t intin); 98 static int apic_handle_pci_pci_bridge(dev_info_t *idip, int child_devno, 99 int child_ipin, struct apic_io_intr **intrp); 100 static int apic_setup_irq_table(dev_info_t *dip, int irqno, 101 struct apic_io_intr *intrp, struct intrspec *ispec, iflag_t *intr_flagp, 102 int type); 103 static int apic_setup_sci_irq_table(int irqno, uchar_t ipl, 104 iflag_t *intr_flagp); 105 static void apic_nmi_intr(caddr_t arg); 106 uchar_t apic_bind_intr(dev_info_t *dip, int irq, uchar_t ioapicid, 107 uchar_t intin); 108 static int apic_rebind(apic_irq_t *irq_ptr, int bind_cpu, int acquire_lock, 109 int when); 110 static int apic_rebind_all(apic_irq_t *irq_ptr, int bind_cpu, int safe); 111 static void apic_intr_redistribute(); 112 static void apic_cleanup_busy(); 113 static void apic_set_pwroff_method_from_mpcnfhdr(struct apic_mp_cnf_hdr *hdrp); 114 int apic_introp_xlate(dev_info_t *dip, struct intrspec *ispec, int type); 115 116 /* ACPI support routines */ 117 static int acpi_probe(void); 118 static int apic_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip, 119 int *pci_irqp, iflag_t *intr_flagp); 120 121 static int apic_acpi_translate_pci_irq(dev_info_t *dip, int busid, int devid, 122 int ipin, int *pci_irqp, iflag_t *intr_flagp); 123 static uchar_t acpi_find_ioapic(int irq); 124 static int acpi_intr_compatible(iflag_t iflag1, iflag_t iflag2); 125 126 /* 127 * standard MP entries 128 */ 129 static int apic_probe(); 130 static int apic_clkinit(); 131 static int apic_getclkirq(int ipl); 132 static uint_t apic_calibrate(volatile uint32_t *addr, 133 uint16_t *pit_ticks_adj); 134 static hrtime_t apic_gettime(); 135 static hrtime_t apic_gethrtime(); 136 static void apic_init(); 137 static void apic_picinit(void); 138 static void apic_cpu_start(processorid_t cpun, caddr_t rm_code); 139 static int apic_post_cpu_start(void); 140 static void apic_send_ipi(int cpun, int ipl); 141 static void apic_set_softintr(int softintr); 142 static void apic_set_idlecpu(processorid_t cpun); 143 static void apic_unset_idlecpu(processorid_t cpun); 144 static int apic_softlvl_to_irq(int ipl); 145 static int apic_intr_enter(int ipl, int *vect); 146 static void apic_intr_exit(int ipl, int vect); 147 static void apic_setspl(int ipl); 148 static int apic_addspl(int ipl, int vector, int min_ipl, int max_ipl); 149 static int apic_delspl(int ipl, int vector, int min_ipl, int max_ipl); 150 static void apic_shutdown(int cmd, int fcn); 151 static void apic_preshutdown(int cmd, int fcn); 152 static int apic_disable_intr(processorid_t cpun); 153 static void apic_enable_intr(processorid_t cpun); 154 static processorid_t apic_get_next_processorid(processorid_t cpun); 155 static int apic_get_ipivect(int ipl, int type); 156 static void apic_timer_reprogram(hrtime_t time); 157 static void apic_timer_enable(void); 158 static void apic_timer_disable(void); 159 static void apic_post_cyclic_setup(void *arg); 160 extern int apic_intr_ops(dev_info_t *, ddi_intr_handle_impl_t *, 161 psm_intr_op_t, int *); 162 163 static int apic_oneshot = 0; 164 int apic_oneshot_enable = 1; /* to allow disabling one-shot capability */ 165 166 /* 167 * These variables are frequently accessed in apic_intr_enter(), 168 * apic_intr_exit and apic_setspl, so group them together 169 */ 170 volatile uint32_t *apicadr = NULL; /* virtual addr of local APIC */ 171 int apic_setspl_delay = 1; /* apic_setspl - delay enable */ 172 int apic_clkvect; 173 174 /* ACPI SCI interrupt configuration; -1 if SCI not used */ 175 int apic_sci_vect = -1; 176 iflag_t apic_sci_flags; 177 178 /* vector at which error interrupts come in */ 179 int apic_errvect; 180 int apic_enable_error_intr = 1; 181 int apic_error_display_delay = 100; 182 183 /* vector at which performance counter overflow interrupts come in */ 184 int apic_cpcovf_vect; 185 int apic_enable_cpcovf_intr = 1; 186 187 /* Max wait time (in microsecs) for flags to clear in an RDT entry. */ 188 static int apic_max_usecs_clear_pending = 1000; 189 190 /* Amt of usecs to wait before checking if RDT flags have reset. */ 191 #define APIC_USECS_PER_WAIT_INTERVAL 100 192 193 /* Maximum number of times to retry reprogramming via the timeout */ 194 #define APIC_REPROGRAM_MAX_TIMEOUTS 10 195 196 /* timeout delay for IOAPIC delayed reprogramming */ 197 #define APIC_REPROGRAM_TIMEOUT_DELAY 5 /* microseconds */ 198 199 /* Parameter to apic_rebind(): Should reprogramming be done now or later? */ 200 #define DEFERRED 1 201 #define IMMEDIATE 0 202 203 /* 204 * number of bits per byte, from <sys/param.h> 205 */ 206 #define UCHAR_MAX ((1 << NBBY) - 1) 207 208 uchar_t apic_reserved_irqlist[MAX_ISA_IRQ]; 209 210 /* 211 * The following vector assignments influence the value of ipltopri and 212 * vectortoipl. Note that vectors 0 - 0x1f are not used. We can program 213 * idle to 0 and IPL 0 to 0x10 to differentiate idle in case 214 * we care to do so in future. Note some IPLs which are rarely used 215 * will share the vector ranges and heavily used IPLs (5 and 6) have 216 * a wide range. 217 * IPL Vector range. as passed to intr_enter 218 * 0 none. 219 * 1,2,3 0x20-0x2f 0x0-0xf 220 * 4 0x30-0x3f 0x10-0x1f 221 * 5 0x40-0x5f 0x20-0x3f 222 * 6 0x60-0x7f 0x40-0x5f 223 * 7,8,9 0x80-0x8f 0x60-0x6f 224 * 10 0x90-0x9f 0x70-0x7f 225 * 11 0xa0-0xaf 0x80-0x8f 226 * ... ... 227 * 16 0xf0-0xff 0xd0-0xdf 228 */ 229 uchar_t apic_vectortoipl[APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL] = { 230 3, 4, 5, 5, 6, 6, 9, 10, 11, 12, 13, 14, 15, 16 231 }; 232 /* 233 * The ipl of an ISR at vector X is apic_vectortoipl[X<<4] 234 * NOTE that this is vector as passed into intr_enter which is 235 * programmed vector - 0x20 (APIC_BASE_VECT) 236 */ 237 238 uchar_t apic_ipltopri[MAXIPL + 1]; /* unix ipl to apic pri */ 239 /* The taskpri to be programmed into apic to mask given ipl */ 240 241 #if defined(__amd64) 242 uchar_t apic_cr8pri[MAXIPL + 1]; /* unix ipl to cr8 pri */ 243 #endif 244 245 /* 246 * Patchable global variables. 247 */ 248 int apic_forceload = 0; 249 250 #define INTR_ROUND_ROBIN_WITH_AFFINITY 0 251 #define INTR_ROUND_ROBIN 1 252 #define INTR_LOWEST_PRIORITY 2 253 254 int apic_intr_policy = INTR_ROUND_ROBIN_WITH_AFFINITY; 255 256 static int apic_next_bind_cpu = 2; /* For round robin assignment */ 257 /* start with cpu 1 */ 258 259 int apic_coarse_hrtime = 1; /* 0 - use accurate slow gethrtime() */ 260 /* 1 - use gettime() for performance */ 261 int apic_flat_model = 0; /* 0 - clustered. 1 - flat */ 262 int apic_enable_hwsoftint = 0; /* 0 - disable, 1 - enable */ 263 int apic_enable_bind_log = 1; /* 1 - display interrupt binding log */ 264 int apic_panic_on_nmi = 0; 265 int apic_panic_on_apic_error = 0; 266 267 int apic_verbose = 0; 268 269 /* Flag definitions for apic_verbose */ 270 #define APIC_VERBOSE_IOAPIC_FLAG 0x00000001 271 #define APIC_VERBOSE_IRQ_FLAG 0x00000002 272 #define APIC_VERBOSE_POWEROFF_FLAG 0x00000004 273 #define APIC_VERBOSE_POWEROFF_PAUSE_FLAG 0x00000008 274 275 276 #define APIC_VERBOSE_IOAPIC(fmt) \ 277 if (apic_verbose & APIC_VERBOSE_IOAPIC_FLAG) \ 278 cmn_err fmt; 279 280 #define APIC_VERBOSE_IRQ(fmt) \ 281 if (apic_verbose & APIC_VERBOSE_IRQ_FLAG) \ 282 cmn_err fmt; 283 284 #define APIC_VERBOSE_POWEROFF(fmt) \ 285 if (apic_verbose & APIC_VERBOSE_POWEROFF_FLAG) \ 286 prom_printf fmt; 287 288 289 /* Now the ones for Dynamic Interrupt distribution */ 290 int apic_enable_dynamic_migration = 1; 291 292 /* 293 * If enabled, the distribution works as follows: 294 * On every interrupt entry, the current ipl for the CPU is set in cpu_info 295 * and the irq corresponding to the ipl is also set in the aci_current array. 296 * interrupt exit and setspl (due to soft interrupts) will cause the current 297 * ipl to be be changed. This is cache friendly as these frequently used 298 * paths write into a per cpu structure. 299 * 300 * Sampling is done by checking the structures for all CPUs and incrementing 301 * the busy field of the irq (if any) executing on each CPU and the busy field 302 * of the corresponding CPU. 303 * In periodic mode this is done on every clock interrupt. 304 * In one-shot mode, this is done thru a cyclic with an interval of 305 * apic_redistribute_sample_interval (default 10 milli sec). 306 * 307 * Every apic_sample_factor_redistribution times we sample, we do computations 308 * to decide which interrupt needs to be migrated (see comments 309 * before apic_intr_redistribute(). 310 */ 311 312 /* 313 * Following 3 variables start as % and can be patched or set using an 314 * API to be defined in future. They will be scaled to 315 * sample_factor_redistribution which is in turn set to hertz+1 (in periodic 316 * mode), or 101 in one-shot mode to stagger it away from one sec processing 317 */ 318 319 int apic_int_busy_mark = 60; 320 int apic_int_free_mark = 20; 321 int apic_diff_for_redistribution = 10; 322 323 /* sampling interval for interrupt redistribution for dynamic migration */ 324 int apic_redistribute_sample_interval = NANOSEC / 100; /* 10 millisec */ 325 326 /* 327 * number of times we sample before deciding to redistribute interrupts 328 * for dynamic migration 329 */ 330 int apic_sample_factor_redistribution = 101; 331 332 /* timeout for xlate_vector, mark_vector */ 333 int apic_revector_timeout = 16 * 10000; /* 160 millisec */ 334 335 int apic_redist_cpu_skip = 0; 336 int apic_num_imbalance = 0; 337 int apic_num_rebind = 0; 338 339 int apic_nproc = 0; 340 int apic_defconf = 0; 341 int apic_irq_translate = 0; 342 int apic_spec_rev = 0; 343 int apic_imcrp = 0; 344 345 int apic_use_acpi = 1; /* 1 = use ACPI, 0 = don't use ACPI */ 346 int apic_use_acpi_madt_only = 0; /* 1=ONLY use MADT from ACPI */ 347 348 /* 349 * For interrupt link devices, if apic_unconditional_srs is set, an irq resource 350 * will be assigned (via _SRS). If it is not set, use the current 351 * irq setting (via _CRS), but only if that irq is in the set of possible 352 * irqs (returned by _PRS) for the device. 353 */ 354 int apic_unconditional_srs = 1; 355 356 /* 357 * For interrupt link devices, if apic_prefer_crs is set when we are 358 * assigning an IRQ resource to a device, prefer the current IRQ setting 359 * over other possible irq settings under same conditions. 360 */ 361 362 int apic_prefer_crs = 1; 363 364 365 /* minimum number of timer ticks to program to */ 366 int apic_min_timer_ticks = 1; 367 /* 368 * Local static data 369 */ 370 static struct psm_ops apic_ops = { 371 apic_probe, 372 373 apic_init, 374 apic_picinit, 375 apic_intr_enter, 376 apic_intr_exit, 377 apic_setspl, 378 apic_addspl, 379 apic_delspl, 380 apic_disable_intr, 381 apic_enable_intr, 382 apic_softlvl_to_irq, 383 apic_set_softintr, 384 385 apic_set_idlecpu, 386 apic_unset_idlecpu, 387 388 apic_clkinit, 389 apic_getclkirq, 390 (void (*)(void))NULL, /* psm_hrtimeinit */ 391 apic_gethrtime, 392 393 apic_get_next_processorid, 394 apic_cpu_start, 395 apic_post_cpu_start, 396 apic_shutdown, 397 apic_get_ipivect, 398 apic_send_ipi, 399 400 (int (*)(dev_info_t *, int))NULL, /* psm_translate_irq */ 401 (int (*)(todinfo_t *))NULL, /* psm_tod_get */ 402 (int (*)(todinfo_t *))NULL, /* psm_tod_set */ 403 (void (*)(int, char *))NULL, /* psm_notify_error */ 404 (void (*)(int))NULL, /* psm_notify_func */ 405 apic_timer_reprogram, 406 apic_timer_enable, 407 apic_timer_disable, 408 apic_post_cyclic_setup, 409 apic_preshutdown, 410 apic_intr_ops /* Advanced DDI Interrupt framework */ 411 }; 412 413 414 static struct psm_info apic_psm_info = { 415 PSM_INFO_VER01_5, /* version */ 416 PSM_OWN_EXCLUSIVE, /* ownership */ 417 (struct psm_ops *)&apic_ops, /* operation */ 418 "pcplusmp", /* machine name */ 419 "pcplusmp v1.4 compatible %I%", 420 }; 421 422 static void *apic_hdlp; 423 424 #ifdef DEBUG 425 #define DENT 0x0001 426 int apic_debug = 0; 427 /* 428 * set apic_restrict_vector to the # of vectors we want to allow per range 429 * useful in testing shared interrupt logic by setting it to 2 or 3 430 */ 431 int apic_restrict_vector = 0; 432 433 #define APIC_DEBUG_MSGBUFSIZE 2048 434 int apic_debug_msgbuf[APIC_DEBUG_MSGBUFSIZE]; 435 int apic_debug_msgbufindex = 0; 436 437 /* 438 * Put "int" info into debug buffer. No MP consistency, but light weight. 439 * Good enough for most debugging. 440 */ 441 #define APIC_DEBUG_BUF_PUT(x) \ 442 apic_debug_msgbuf[apic_debug_msgbufindex++] = x; \ 443 if (apic_debug_msgbufindex >= (APIC_DEBUG_MSGBUFSIZE - NCPU)) \ 444 apic_debug_msgbufindex = 0; 445 446 #endif /* DEBUG */ 447 448 apic_cpus_info_t *apic_cpus; 449 450 static uint_t apic_cpumask = 0; 451 static uint_t apic_flag; 452 453 /* Flag to indicate that we need to shut down all processors */ 454 static uint_t apic_shutdown_processors; 455 456 uint_t apic_nsec_per_intr = 0; 457 458 /* 459 * apic_let_idle_redistribute can have the following values: 460 * 0 - If clock decremented it from 1 to 0, clock has to call redistribute. 461 * apic_redistribute_lock prevents multiple idle cpus from redistributing 462 */ 463 int apic_num_idle_redistributions = 0; 464 static int apic_let_idle_redistribute = 0; 465 static uint_t apic_nticks = 0; 466 static uint_t apic_skipped_redistribute = 0; 467 468 /* to gather intr data and redistribute */ 469 static void apic_redistribute_compute(void); 470 471 static uint_t last_count_read = 0; 472 static lock_t apic_gethrtime_lock; 473 volatile int apic_hrtime_stamp = 0; 474 volatile hrtime_t apic_nsec_since_boot = 0; 475 static uint_t apic_hertz_count, apic_nsec_per_tick; 476 static hrtime_t apic_nsec_max; 477 478 static hrtime_t apic_last_hrtime = 0; 479 int apic_hrtime_error = 0; 480 int apic_remote_hrterr = 0; 481 int apic_num_nmis = 0; 482 int apic_apic_error = 0; 483 int apic_num_apic_errors = 0; 484 int apic_num_cksum_errors = 0; 485 486 static uchar_t apic_io_id[MAX_IO_APIC]; 487 static uchar_t apic_io_ver[MAX_IO_APIC]; 488 static uchar_t apic_io_vectbase[MAX_IO_APIC]; 489 static uchar_t apic_io_vectend[MAX_IO_APIC]; 490 volatile int32_t *apicioadr[MAX_IO_APIC]; 491 /* 492 * apic_ioapic_lock protects the ioapics (reg select), the status, temp_bound 493 * and bound elements of cpus_info and the temp_cpu element of irq_struct 494 */ 495 lock_t apic_ioapic_lock; 496 497 /* 498 * apic_ioapic_reprogram_lock prevents a CPU from exiting 499 * apic_intr_exit before IOAPIC reprogramming information 500 * is collected. 501 */ 502 static lock_t apic_ioapic_reprogram_lock; 503 static int apic_io_max = 0; /* no. of i/o apics enabled */ 504 505 static struct apic_io_intr *apic_io_intrp = 0; 506 static struct apic_bus *apic_busp; 507 508 uchar_t apic_vector_to_irq[APIC_MAX_VECTOR+1]; 509 static uchar_t apic_resv_vector[MAXIPL+1]; 510 511 static char apic_level_intr[APIC_MAX_VECTOR+1]; 512 static int apic_error = 0; 513 /* values which apic_error can take. Not catastrophic, but may help debug */ 514 #define APIC_ERR_BOOT_EOI 0x1 515 #define APIC_ERR_GET_IPIVECT_FAIL 0x2 516 #define APIC_ERR_INVALID_INDEX 0x4 517 #define APIC_ERR_MARK_VECTOR_FAIL 0x8 518 #define APIC_ERR_APIC_ERROR 0x40000000 519 #define APIC_ERR_NMI 0x80000000 520 521 static int apic_cmos_ssb_set = 0; 522 523 static uint32_t eisa_level_intr_mask = 0; 524 /* At least MSB will be set if EISA bus */ 525 526 static int apic_pci_bus_total = 0; 527 static uchar_t apic_single_pci_busid = 0; 528 529 530 /* 531 * airq_mutex protects additions to the apic_irq_table - the first 532 * pointer and any airq_nexts off of that one. It also protects 533 * apic_max_device_irq & apic_min_device_irq. It also guarantees 534 * that share_id is unique as new ids are generated only when new 535 * irq_t structs are linked in. Once linked in the structs are never 536 * deleted. temp_cpu & mps_intr_index field indicate if it is programmed 537 * or allocated. Note that there is a slight gap between allocating in 538 * apic_introp_xlate and programming in addspl. 539 */ 540 kmutex_t airq_mutex; 541 apic_irq_t *apic_irq_table[APIC_MAX_VECTOR+1]; 542 int apic_max_device_irq = 0; 543 int apic_min_device_irq = APIC_MAX_VECTOR; 544 545 /* use to make sure only one cpu handles the nmi */ 546 static lock_t apic_nmi_lock; 547 /* use to make sure only one cpu handles the error interrupt */ 548 static lock_t apic_error_lock; 549 550 /* 551 * Following declarations are for revectoring; used when ISRs at different 552 * IPLs share an irq. 553 */ 554 static lock_t apic_revector_lock; 555 static int apic_revector_pending = 0; 556 static uchar_t *apic_oldvec_to_newvec; 557 static uchar_t *apic_newvec_to_oldvec; 558 559 /* Ensures that the IOAPIC-reprogramming timeout is not reentrant */ 560 static kmutex_t apic_reprogram_timeout_mutex; 561 562 static struct ioapic_reprogram_data { 563 int valid; /* This entry is valid */ 564 int bindcpu; /* The CPU to which the int will be bound */ 565 unsigned timeouts; /* # times the reprogram timeout was called */ 566 } apic_reprogram_info[APIC_MAX_VECTOR+1]; 567 /* 568 * APIC_MAX_VECTOR + 1 is the maximum # of IRQs as well. apic_reprogram_info 569 * is indexed by IRQ number, NOT by vector number. 570 */ 571 572 573 /* 574 * The following added to identify a software poweroff method if available. 575 */ 576 577 static struct { 578 int poweroff_method; 579 char oem_id[APIC_MPS_OEM_ID_LEN + 1]; /* MAX + 1 for NULL */ 580 char prod_id[APIC_MPS_PROD_ID_LEN + 1]; /* MAX + 1 for NULL */ 581 } apic_mps_ids[] = { 582 { APIC_POWEROFF_VIA_RTC, "INTEL", "ALDER" }, /* 4300 */ 583 { APIC_POWEROFF_VIA_RTC, "NCR", "AMC" }, /* 4300 */ 584 { APIC_POWEROFF_VIA_ASPEN_BMC, "INTEL", "A450NX" }, /* 4400? */ 585 { APIC_POWEROFF_VIA_ASPEN_BMC, "INTEL", "AD450NX" }, /* 4400 */ 586 { APIC_POWEROFF_VIA_ASPEN_BMC, "INTEL", "AC450NX" }, /* 4400R */ 587 { APIC_POWEROFF_VIA_SITKA_BMC, "INTEL", "S450NX" }, /* S50 */ 588 { APIC_POWEROFF_VIA_SITKA_BMC, "INTEL", "SC450NX" } /* S50? */ 589 }; 590 591 int apic_poweroff_method = APIC_POWEROFF_NONE; 592 593 static struct { 594 uchar_t cntl; 595 uchar_t data; 596 } aspen_bmc[] = { 597 { CC_SMS_WR_START, 0x18 }, /* NetFn/LUN */ 598 { CC_SMS_WR_NEXT, 0x24 }, /* Cmd SET_WATCHDOG_TIMER */ 599 { CC_SMS_WR_NEXT, 0x84 }, /* DataByte 1: SMS/OS no log */ 600 { CC_SMS_WR_NEXT, 0x2 }, /* DataByte 2: Power Down */ 601 { CC_SMS_WR_NEXT, 0x0 }, /* DataByte 3: no pre-timeout */ 602 { CC_SMS_WR_NEXT, 0x0 }, /* DataByte 4: timer expir. */ 603 { CC_SMS_WR_NEXT, 0xa }, /* DataByte 5: init countdown */ 604 { CC_SMS_WR_END, 0x0 }, /* DataByte 6: init countdown */ 605 606 { CC_SMS_WR_START, 0x18 }, /* NetFn/LUN */ 607 { CC_SMS_WR_END, 0x22 } /* Cmd RESET_WATCHDOG_TIMER */ 608 }; 609 610 static struct { 611 int port; 612 uchar_t data; 613 } sitka_bmc[] = { 614 { SMS_COMMAND_REGISTER, SMS_WRITE_START }, 615 { SMS_DATA_REGISTER, 0x18 }, /* NetFn/LUN */ 616 { SMS_DATA_REGISTER, 0x24 }, /* Cmd SET_WATCHDOG_TIMER */ 617 { SMS_DATA_REGISTER, 0x84 }, /* DataByte 1: SMS/OS no log */ 618 { SMS_DATA_REGISTER, 0x2 }, /* DataByte 2: Power Down */ 619 { SMS_DATA_REGISTER, 0x0 }, /* DataByte 3: no pre-timeout */ 620 { SMS_DATA_REGISTER, 0x0 }, /* DataByte 4: timer expir. */ 621 { SMS_DATA_REGISTER, 0xa }, /* DataByte 5: init countdown */ 622 { SMS_COMMAND_REGISTER, SMS_WRITE_END }, 623 { SMS_DATA_REGISTER, 0x0 }, /* DataByte 6: init countdown */ 624 625 { SMS_COMMAND_REGISTER, SMS_WRITE_START }, 626 { SMS_DATA_REGISTER, 0x18 }, /* NetFn/LUN */ 627 { SMS_COMMAND_REGISTER, SMS_WRITE_END }, 628 { SMS_DATA_REGISTER, 0x22 } /* Cmd RESET_WATCHDOG_TIMER */ 629 }; 630 631 632 /* Patchable global variables. */ 633 int apic_kmdb_on_nmi = 0; /* 0 - no, 1 - yes enter kmdb */ 634 int apic_debug_mps_id = 0; /* 1 - print MPS ID strings */ 635 636 /* 637 * ACPI definitions 638 */ 639 /* _PIC method arguments */ 640 #define ACPI_PIC_MODE 0 641 #define ACPI_APIC_MODE 1 642 643 /* APIC error flags we care about */ 644 #define APIC_SEND_CS_ERROR 0x01 645 #define APIC_RECV_CS_ERROR 0x02 646 #define APIC_CS_ERRORS (APIC_SEND_CS_ERROR|APIC_RECV_CS_ERROR) 647 648 /* 649 * ACPI variables 650 */ 651 /* 1 = acpi is enabled & working, 0 = acpi is not enabled or not there */ 652 static int apic_enable_acpi = 0; 653 654 /* ACPI Multiple APIC Description Table ptr */ 655 static MULTIPLE_APIC_TABLE *acpi_mapic_dtp = NULL; 656 657 /* ACPI Interrupt Source Override Structure ptr */ 658 static MADT_INTERRUPT_OVERRIDE *acpi_isop = NULL; 659 static int acpi_iso_cnt = 0; 660 661 /* ACPI Non-maskable Interrupt Sources ptr */ 662 static MADT_NMI_SOURCE *acpi_nmi_sp = NULL; 663 static int acpi_nmi_scnt = 0; 664 static MADT_LOCAL_APIC_NMI *acpi_nmi_cp = NULL; 665 static int acpi_nmi_ccnt = 0; 666 667 /* 668 * extern declarations 669 */ 670 extern int intr_clear(void); 671 extern void intr_restore(uint_t); 672 #if defined(__amd64) 673 extern int intpri_use_cr8; 674 #endif /* __amd64 */ 675 676 extern int apic_pci_msi_enable_vector(dev_info_t *, int, int, 677 int, int, int); 678 extern apic_irq_t *apic_find_irq(dev_info_t *, struct intrspec *, int); 679 680 /* 681 * This is the loadable module wrapper 682 */ 683 684 int 685 _init(void) 686 { 687 if (apic_coarse_hrtime) 688 apic_ops.psm_gethrtime = &apic_gettime; 689 return (psm_mod_init(&apic_hdlp, &apic_psm_info)); 690 } 691 692 int 693 _fini(void) 694 { 695 return (psm_mod_fini(&apic_hdlp, &apic_psm_info)); 696 } 697 698 int 699 _info(struct modinfo *modinfop) 700 { 701 return (psm_mod_info(&apic_hdlp, &apic_psm_info, modinfop)); 702 } 703 704 /* 705 * Auto-configuration routines 706 */ 707 708 /* 709 * Look at MPSpec 1.4 (Intel Order # 242016-005) for details of what we do here 710 * May work with 1.1 - but not guaranteed. 711 * According to the MP Spec, the MP floating pointer structure 712 * will be searched in the order described below: 713 * 1. In the first kilobyte of Extended BIOS Data Area (EBDA) 714 * 2. Within the last kilobyte of system base memory 715 * 3. In the BIOS ROM address space between 0F0000h and 0FFFFh 716 * Once we find the right signature with proper checksum, we call 717 * either handle_defconf or parse_mpct to get all info necessary for 718 * subsequent operations. 719 */ 720 static int 721 apic_probe() 722 { 723 uint32_t mpct_addr, ebda_start = 0, base_mem_end; 724 caddr_t biosdatap; 725 caddr_t mpct; 726 caddr_t fptr; 727 int i, mpct_size, mapsize, retval = PSM_FAILURE; 728 ushort_t ebda_seg, base_mem_size; 729 struct apic_mpfps_hdr *fpsp; 730 struct apic_mp_cnf_hdr *hdrp; 731 int bypass_cpu_and_ioapics_in_mptables; 732 int acpi_user_options; 733 734 if (apic_forceload < 0) 735 return (retval); 736 737 /* Allow override for MADT-only mode */ 738 acpi_user_options = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_root_node(), 0, 739 "acpi-user-options", 0); 740 apic_use_acpi_madt_only = ((acpi_user_options & ACPI_OUSER_MADT) != 0); 741 742 /* Allow apic_use_acpi to override MADT-only mode */ 743 if (!apic_use_acpi) 744 apic_use_acpi_madt_only = 0; 745 746 retval = acpi_probe(); 747 748 /* 749 * mapin the bios data area 40:0 750 * 40:13h - two-byte location reports the base memory size 751 * 40:0Eh - two-byte location for the exact starting address of 752 * the EBDA segment for EISA 753 */ 754 biosdatap = psm_map_phys(0x400, 0x20, PROT_READ); 755 if (!biosdatap) 756 return (retval); 757 fpsp = (struct apic_mpfps_hdr *)NULL; 758 mapsize = MPFPS_RAM_WIN_LEN; 759 /*LINTED: pointer cast may result in improper alignment */ 760 ebda_seg = *((ushort_t *)(biosdatap+0xe)); 761 /* check the 1k of EBDA */ 762 if (ebda_seg) { 763 ebda_start = ((uint32_t)ebda_seg) << 4; 764 fptr = psm_map_phys(ebda_start, MPFPS_RAM_WIN_LEN, PROT_READ); 765 if (fptr) { 766 if (!(fpsp = 767 apic_find_fps_sig(fptr, MPFPS_RAM_WIN_LEN))) 768 psm_unmap_phys(fptr, MPFPS_RAM_WIN_LEN); 769 } 770 } 771 /* If not in EBDA, check the last k of system base memory */ 772 if (!fpsp) { 773 /*LINTED: pointer cast may result in improper alignment */ 774 base_mem_size = *((ushort_t *)(biosdatap + 0x13)); 775 776 if (base_mem_size > 512) 777 base_mem_end = 639 * 1024; 778 else 779 base_mem_end = 511 * 1024; 780 /* if ebda == last k of base mem, skip to check BIOS ROM */ 781 if (base_mem_end != ebda_start) { 782 783 fptr = psm_map_phys(base_mem_end, MPFPS_RAM_WIN_LEN, 784 PROT_READ); 785 786 if (fptr) { 787 if (!(fpsp = apic_find_fps_sig(fptr, 788 MPFPS_RAM_WIN_LEN))) 789 psm_unmap_phys(fptr, MPFPS_RAM_WIN_LEN); 790 } 791 } 792 } 793 psm_unmap_phys(biosdatap, 0x20); 794 795 /* If still cannot find it, check the BIOS ROM space */ 796 if (!fpsp) { 797 mapsize = MPFPS_ROM_WIN_LEN; 798 fptr = psm_map_phys(MPFPS_ROM_WIN_START, 799 MPFPS_ROM_WIN_LEN, PROT_READ); 800 if (fptr) { 801 if (!(fpsp = 802 apic_find_fps_sig(fptr, MPFPS_ROM_WIN_LEN))) { 803 psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN); 804 return (retval); 805 } 806 } 807 } 808 809 if (apic_checksum((caddr_t)fpsp, fpsp->mpfps_length * 16) != 0) { 810 psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN); 811 return (retval); 812 } 813 814 apic_spec_rev = fpsp->mpfps_spec_rev; 815 if ((apic_spec_rev != 04) && (apic_spec_rev != 01)) { 816 psm_unmap_phys(fptr, MPFPS_ROM_WIN_LEN); 817 return (retval); 818 } 819 820 /* check IMCR is present or not */ 821 apic_imcrp = fpsp->mpfps_featinfo2 & MPFPS_FEATINFO2_IMCRP; 822 823 /* check default configuration (dual CPUs) */ 824 if ((apic_defconf = fpsp->mpfps_featinfo1) != 0) { 825 psm_unmap_phys(fptr, mapsize); 826 return (apic_handle_defconf()); 827 } 828 829 /* MP Configuration Table */ 830 mpct_addr = (uint32_t)(fpsp->mpfps_mpct_paddr); 831 832 psm_unmap_phys(fptr, mapsize); /* unmap floating ptr struct */ 833 834 /* 835 * Map in enough memory for the MP Configuration Table Header. 836 * Use this table to read the total length of the BIOS data and 837 * map in all the info 838 */ 839 /*LINTED: pointer cast may result in improper alignment */ 840 hdrp = (struct apic_mp_cnf_hdr *)psm_map_phys(mpct_addr, 841 sizeof (struct apic_mp_cnf_hdr), PROT_READ); 842 if (!hdrp) 843 return (retval); 844 845 /* check mp configuration table signature PCMP */ 846 if (hdrp->mpcnf_sig != 0x504d4350) { 847 psm_unmap_phys((caddr_t)hdrp, sizeof (struct apic_mp_cnf_hdr)); 848 return (retval); 849 } 850 mpct_size = (int)hdrp->mpcnf_tbl_length; 851 852 apic_set_pwroff_method_from_mpcnfhdr(hdrp); 853 854 psm_unmap_phys((caddr_t)hdrp, sizeof (struct apic_mp_cnf_hdr)); 855 856 if ((retval == PSM_SUCCESS) && !apic_use_acpi_madt_only) { 857 /* This is an ACPI machine No need for further checks */ 858 return (retval); 859 } 860 861 /* 862 * Map in the entries for this machine, ie. Processor 863 * Entry Tables, Bus Entry Tables, etc. 864 * They are in fixed order following one another 865 */ 866 mpct = psm_map_phys(mpct_addr, mpct_size, PROT_READ); 867 if (!mpct) 868 return (retval); 869 870 if (apic_checksum(mpct, mpct_size) != 0) 871 goto apic_fail1; 872 873 874 /*LINTED: pointer cast may result in improper alignment */ 875 hdrp = (struct apic_mp_cnf_hdr *)mpct; 876 /*LINTED: pointer cast may result in improper alignment */ 877 apicadr = (uint32_t *)psm_map_phys((uint32_t)hdrp->mpcnf_local_apic, 878 APIC_LOCAL_MEMLEN, PROT_READ | PROT_WRITE); 879 if (!apicadr) 880 goto apic_fail1; 881 882 /* Parse all information in the tables */ 883 bypass_cpu_and_ioapics_in_mptables = (retval == PSM_SUCCESS); 884 if (apic_parse_mpct(mpct, bypass_cpu_and_ioapics_in_mptables) == 885 PSM_SUCCESS) 886 return (PSM_SUCCESS); 887 888 for (i = 0; i < apic_io_max; i++) 889 psm_unmap_phys((caddr_t)apicioadr[i], APIC_IO_MEMLEN); 890 if (apic_cpus) 891 kmem_free(apic_cpus, sizeof (*apic_cpus) * apic_nproc); 892 if (apicadr) 893 psm_unmap_phys((caddr_t)apicadr, APIC_LOCAL_MEMLEN); 894 apic_fail1: 895 psm_unmap_phys(mpct, mpct_size); 896 return (retval); 897 } 898 899 static void 900 apic_set_pwroff_method_from_mpcnfhdr(struct apic_mp_cnf_hdr *hdrp) 901 { 902 int i; 903 904 for (i = 0; i < (sizeof (apic_mps_ids) / sizeof (apic_mps_ids[0])); 905 i++) { 906 if ((strncmp(hdrp->mpcnf_oem_str, apic_mps_ids[i].oem_id, 907 strlen(apic_mps_ids[i].oem_id)) == 0) && 908 (strncmp(hdrp->mpcnf_prod_str, apic_mps_ids[i].prod_id, 909 strlen(apic_mps_ids[i].prod_id)) == 0)) { 910 911 apic_poweroff_method = apic_mps_ids[i].poweroff_method; 912 break; 913 } 914 } 915 916 if (apic_debug_mps_id != 0) { 917 cmn_err(CE_CONT, "pcplusmp: MPS OEM ID = '%c%c%c%c%c%c%c%c'" 918 "Product ID = '%c%c%c%c%c%c%c%c%c%c%c%c'\n", 919 hdrp->mpcnf_oem_str[0], 920 hdrp->mpcnf_oem_str[1], 921 hdrp->mpcnf_oem_str[2], 922 hdrp->mpcnf_oem_str[3], 923 hdrp->mpcnf_oem_str[4], 924 hdrp->mpcnf_oem_str[5], 925 hdrp->mpcnf_oem_str[6], 926 hdrp->mpcnf_oem_str[7], 927 hdrp->mpcnf_prod_str[0], 928 hdrp->mpcnf_prod_str[1], 929 hdrp->mpcnf_prod_str[2], 930 hdrp->mpcnf_prod_str[3], 931 hdrp->mpcnf_prod_str[4], 932 hdrp->mpcnf_prod_str[5], 933 hdrp->mpcnf_prod_str[6], 934 hdrp->mpcnf_prod_str[7], 935 hdrp->mpcnf_prod_str[8], 936 hdrp->mpcnf_prod_str[9], 937 hdrp->mpcnf_prod_str[10], 938 hdrp->mpcnf_prod_str[11]); 939 } 940 } 941 942 static int 943 acpi_probe(void) 944 { 945 int i, id, intmax, ver, index, rv; 946 int acpi_verboseflags = 0; 947 int madt_seen, madt_size; 948 APIC_HEADER *ap; 949 MADT_PROCESSOR_APIC *mpa; 950 MADT_IO_APIC *mia; 951 MADT_IO_SAPIC *misa; 952 MADT_INTERRUPT_OVERRIDE *mio; 953 MADT_NMI_SOURCE *mns; 954 MADT_INTERRUPT_SOURCE *mis; 955 MADT_LOCAL_APIC_NMI *mlan; 956 MADT_ADDRESS_OVERRIDE *mao; 957 ACPI_OBJECT_LIST arglist; 958 ACPI_OBJECT arg; 959 int sci; 960 iflag_t sci_flags; 961 volatile int32_t *ioapic; 962 char local_ids[NCPU]; 963 char proc_ids[NCPU]; 964 uchar_t hid; 965 966 if (!apic_use_acpi) 967 return (PSM_FAILURE); 968 969 if (AcpiGetFirmwareTable(APIC_SIG, 1, ACPI_LOGICAL_ADDRESSING, 970 (ACPI_TABLE_HEADER **) &acpi_mapic_dtp) != AE_OK) 971 return (PSM_FAILURE); 972 973 apicadr = (uint32_t *)psm_map_phys( 974 (uint32_t)acpi_mapic_dtp->LocalApicAddress, 975 APIC_LOCAL_MEMLEN, PROT_READ | PROT_WRITE); 976 if (!apicadr) 977 return (PSM_FAILURE); 978 979 id = apicadr[APIC_LID_REG]; 980 local_ids[0] = (uchar_t)(((uint_t)id) >> 24); 981 apic_nproc = index = 1; 982 apic_io_max = 0; 983 984 ap = (APIC_HEADER *) (acpi_mapic_dtp + 1); 985 madt_size = acpi_mapic_dtp->Length; 986 madt_seen = sizeof (*acpi_mapic_dtp); 987 988 while (madt_seen < madt_size) { 989 switch (ap->Type) { 990 case APIC_PROCESSOR: 991 mpa = (MADT_PROCESSOR_APIC *) ap; 992 if (mpa->ProcessorEnabled) { 993 if (mpa->LocalApicId == local_ids[0]) 994 proc_ids[0] = mpa->ProcessorId; 995 else if (apic_nproc < NCPU) { 996 local_ids[index] = mpa->LocalApicId; 997 proc_ids[index] = mpa->ProcessorId; 998 index++; 999 apic_nproc++; 1000 } else 1001 cmn_err(CE_WARN, "pcplusmp: exceeded " 1002 "maximum no. of CPUs (= %d)", NCPU); 1003 } 1004 break; 1005 1006 case APIC_IO: 1007 mia = (MADT_IO_APIC *) ap; 1008 if (apic_io_max < MAX_IO_APIC) { 1009 apic_io_id[apic_io_max] = mia->IoApicId; 1010 apic_io_vectbase[apic_io_max] = 1011 mia->Interrupt; 1012 ioapic = apicioadr[apic_io_max] = 1013 (int32_t *)psm_map_phys( 1014 (uint32_t)mia->Address, 1015 APIC_IO_MEMLEN, PROT_READ | PROT_WRITE); 1016 if (!ioapic) 1017 goto cleanup; 1018 apic_io_max++; 1019 } 1020 break; 1021 1022 case APIC_XRUPT_OVERRIDE: 1023 mio = (MADT_INTERRUPT_OVERRIDE *) ap; 1024 if (acpi_isop == NULL) 1025 acpi_isop = mio; 1026 acpi_iso_cnt++; 1027 break; 1028 1029 case APIC_NMI: 1030 /* UNIMPLEMENTED */ 1031 mns = (MADT_NMI_SOURCE *) ap; 1032 if (acpi_nmi_sp == NULL) 1033 acpi_nmi_sp = mns; 1034 acpi_nmi_scnt++; 1035 1036 cmn_err(CE_NOTE, "!apic: nmi source: %d %d %d\n", 1037 mns->Interrupt, mns->Polarity, 1038 mns->TriggerMode); 1039 break; 1040 1041 case APIC_LOCAL_NMI: 1042 /* UNIMPLEMENTED */ 1043 mlan = (MADT_LOCAL_APIC_NMI *) ap; 1044 if (acpi_nmi_cp == NULL) 1045 acpi_nmi_cp = mlan; 1046 acpi_nmi_ccnt++; 1047 1048 cmn_err(CE_NOTE, "!apic: local nmi: %d %d %d %d\n", 1049 mlan->ProcessorId, mlan->Polarity, 1050 mlan->TriggerMode, mlan->Lint); 1051 break; 1052 1053 case APIC_ADDRESS_OVERRIDE: 1054 /* UNIMPLEMENTED */ 1055 mao = (MADT_ADDRESS_OVERRIDE *) ap; 1056 cmn_err(CE_NOTE, "!apic: address override: %lx\n", 1057 (long)mao->Address); 1058 break; 1059 1060 case APIC_IO_SAPIC: 1061 /* UNIMPLEMENTED */ 1062 misa = (MADT_IO_SAPIC *) ap; 1063 1064 cmn_err(CE_NOTE, "!apic: io sapic: %d %d %lx\n", 1065 misa->IoSapicId, misa->InterruptBase, 1066 (long)misa->Address); 1067 break; 1068 1069 case APIC_XRUPT_SOURCE: 1070 /* UNIMPLEMENTED */ 1071 mis = (MADT_INTERRUPT_SOURCE *) ap; 1072 1073 cmn_err(CE_NOTE, 1074 "!apic: irq source: %d %d %d %d %d %d %d\n", 1075 mis->ProcessorId, mis->ProcessorEid, 1076 mis->Interrupt, mis->Polarity, 1077 mis->TriggerMode, mis->InterruptType, 1078 mis->IoSapicVector); 1079 break; 1080 case APIC_RESERVED: 1081 default: 1082 goto cleanup; 1083 } 1084 1085 /* advance to next entry */ 1086 madt_seen += ap->Length; 1087 ap = (APIC_HEADER *)(((char *)ap) + ap->Length); 1088 } 1089 1090 if ((apic_cpus = kmem_zalloc(sizeof (*apic_cpus) * apic_nproc, 1091 KM_NOSLEEP)) == NULL) 1092 goto cleanup; 1093 1094 apic_cpumask = (1 << apic_nproc) - 1; 1095 1096 /* 1097 * ACPI doesn't provide the local apic ver, get it directly from the 1098 * local apic 1099 */ 1100 ver = apicadr[APIC_VERS_REG]; 1101 for (i = 0; i < apic_nproc; i++) { 1102 apic_cpus[i].aci_local_id = local_ids[i]; 1103 apic_cpus[i].aci_local_ver = (uchar_t)(ver & 0xFF); 1104 } 1105 for (i = 0; i < apic_io_max; i++) { 1106 ioapic = apicioadr[i]; 1107 1108 /* 1109 * need to check Sitka on the following acpi problem 1110 * On the Sitka, the ioapic's apic_id field isn't reporting 1111 * the actual io apic id. We have reported this problem 1112 * to Intel. Until they fix the problem, we will get the 1113 * actual id directly from the ioapic. 1114 */ 1115 ioapic[APIC_IO_REG] = APIC_ID_CMD; 1116 id = ioapic[APIC_IO_DATA]; 1117 hid = (uchar_t)(((uint_t)id) >> 24); 1118 1119 if (hid != apic_io_id[i]) { 1120 if (apic_io_id[i] == 0) 1121 apic_io_id[i] = hid; 1122 else { /* set ioapic id to whatever reported by ACPI */ 1123 id = ((int32_t)apic_io_id[i]) << 24; 1124 ioapic[APIC_IO_REG] = APIC_ID_CMD; 1125 ioapic[APIC_IO_DATA] = id; 1126 } 1127 } 1128 ioapic[APIC_IO_REG] = APIC_VERS_CMD; 1129 ver = ioapic[APIC_IO_DATA]; 1130 apic_io_ver[i] = (uchar_t)(ver & 0xff); 1131 intmax = (ver >> 16) & 0xff; 1132 apic_io_vectend[i] = apic_io_vectbase[i] + intmax; 1133 } 1134 1135 1136 /* 1137 * Process SCI configuration here 1138 * An error may be returned here if 1139 * acpi-user-options specifies legacy mode 1140 * (no SCI, no ACPI mode) 1141 */ 1142 if (acpica_get_sci(&sci, &sci_flags) != AE_OK) 1143 sci = -1; 1144 1145 /* 1146 * Now call acpi_init() to generate namespaces 1147 * If this fails, we don't attempt to use ACPI 1148 * even if we were able to get a MADT above 1149 */ 1150 if (acpica_init() != AE_OK) 1151 goto cleanup; 1152 1153 /* 1154 * Squirrel away the SCI and flags for later on 1155 * in apic_picinit() when we're ready 1156 */ 1157 apic_sci_vect = sci; 1158 apic_sci_flags = sci_flags; 1159 1160 if (apic_verbose & APIC_VERBOSE_IRQ_FLAG) 1161 acpi_verboseflags |= PSM_VERBOSE_IRQ_FLAG; 1162 1163 if (apic_verbose & APIC_VERBOSE_POWEROFF_FLAG) 1164 acpi_verboseflags |= PSM_VERBOSE_POWEROFF_FLAG; 1165 1166 if (apic_verbose & APIC_VERBOSE_POWEROFF_PAUSE_FLAG) 1167 acpi_verboseflags |= PSM_VERBOSE_POWEROFF_PAUSE_FLAG; 1168 1169 if (acpi_psm_init(apic_psm_info.p_mach_idstring, acpi_verboseflags) == 1170 ACPI_PSM_FAILURE) 1171 goto cleanup; 1172 1173 /* Enable ACPI APIC interrupt routing */ 1174 arglist.Count = 1; 1175 arglist.Pointer = &arg; 1176 arg.Type = ACPI_TYPE_INTEGER; 1177 arg.Integer.Value = ACPI_APIC_MODE; /* 1 */ 1178 rv = AcpiEvaluateObject(NULL, "\\_PIC", &arglist, NULL); 1179 if (rv == AE_OK) { 1180 build_reserved_irqlist((uchar_t *)apic_reserved_irqlist); 1181 apic_enable_acpi = 1; 1182 if (apic_use_acpi_madt_only) { 1183 cmn_err(CE_CONT, 1184 "?Using ACPI for CPU/IOAPIC information ONLY\n"); 1185 } 1186 return (PSM_SUCCESS); 1187 } 1188 /* if setting APIC mode failed above, we fall through to cleanup */ 1189 1190 cleanup: 1191 if (apicadr != NULL) { 1192 psm_unmap_phys((caddr_t)apicadr, APIC_LOCAL_MEMLEN); 1193 apicadr = NULL; 1194 } 1195 apic_nproc = 0; 1196 for (i = 0; i < apic_io_max; i++) { 1197 psm_unmap_phys((caddr_t)apicioadr[i], APIC_IO_MEMLEN); 1198 apicioadr[i] = NULL; 1199 } 1200 apic_io_max = 0; 1201 acpi_isop = NULL; 1202 acpi_iso_cnt = 0; 1203 acpi_nmi_sp = NULL; 1204 acpi_nmi_scnt = 0; 1205 acpi_nmi_cp = NULL; 1206 acpi_nmi_ccnt = 0; 1207 return (PSM_FAILURE); 1208 } 1209 1210 /* 1211 * Handle default configuration. Fill in reqd global variables & tables 1212 * Fill all details as MP table does not give any more info 1213 */ 1214 static int 1215 apic_handle_defconf() 1216 { 1217 uint_t lid; 1218 1219 /*LINTED: pointer cast may result in improper alignment */ 1220 apicioadr[0] = (int32_t *)psm_map_phys(APIC_IO_ADDR, 1221 APIC_IO_MEMLEN, PROT_READ | PROT_WRITE); 1222 /*LINTED: pointer cast may result in improper alignment */ 1223 apicadr = (uint32_t *)psm_map_phys(APIC_LOCAL_ADDR, 1224 APIC_LOCAL_MEMLEN, PROT_READ | PROT_WRITE); 1225 apic_cpus = (apic_cpus_info_t *) 1226 kmem_zalloc(sizeof (*apic_cpus) * 2, KM_NOSLEEP); 1227 if ((!apicadr) || (!apicioadr[0]) || (!apic_cpus)) 1228 goto apic_handle_defconf_fail; 1229 apic_cpumask = 3; 1230 apic_nproc = 2; 1231 lid = apicadr[APIC_LID_REG]; 1232 apic_cpus[0].aci_local_id = (uchar_t)(lid >> APIC_ID_BIT_OFFSET); 1233 /* 1234 * According to the PC+MP spec 1.1, the local ids 1235 * for the default configuration has to be 0 or 1 1236 */ 1237 if (apic_cpus[0].aci_local_id == 1) 1238 apic_cpus[1].aci_local_id = 0; 1239 else if (apic_cpus[0].aci_local_id == 0) 1240 apic_cpus[1].aci_local_id = 1; 1241 else 1242 goto apic_handle_defconf_fail; 1243 1244 apic_io_id[0] = 2; 1245 apic_io_max = 1; 1246 if (apic_defconf >= 5) { 1247 apic_cpus[0].aci_local_ver = APIC_INTEGRATED_VERS; 1248 apic_cpus[1].aci_local_ver = APIC_INTEGRATED_VERS; 1249 apic_io_ver[0] = APIC_INTEGRATED_VERS; 1250 } else { 1251 apic_cpus[0].aci_local_ver = 0; /* 82489 DX */ 1252 apic_cpus[1].aci_local_ver = 0; 1253 apic_io_ver[0] = 0; 1254 } 1255 if (apic_defconf == 2 || apic_defconf == 3 || apic_defconf == 6) 1256 eisa_level_intr_mask = (inb(EISA_LEVEL_CNTL + 1) << 8) | 1257 inb(EISA_LEVEL_CNTL) | ((uint_t)INT32_MAX + 1); 1258 return (PSM_SUCCESS); 1259 1260 apic_handle_defconf_fail: 1261 if (apic_cpus) 1262 kmem_free(apic_cpus, sizeof (*apic_cpus) * 2); 1263 if (apicadr) 1264 psm_unmap_phys((caddr_t)apicadr, APIC_LOCAL_MEMLEN); 1265 if (apicioadr[0]) 1266 psm_unmap_phys((caddr_t)apicioadr[0], APIC_IO_MEMLEN); 1267 return (PSM_FAILURE); 1268 } 1269 1270 /* Parse the entries in MP configuration table and collect info that we need */ 1271 static int 1272 apic_parse_mpct(caddr_t mpct, int bypass_cpus_and_ioapics) 1273 { 1274 struct apic_procent *procp; 1275 struct apic_bus *busp; 1276 struct apic_io_entry *ioapicp; 1277 struct apic_io_intr *intrp; 1278 volatile int32_t *ioapic; 1279 uint_t lid; 1280 int id; 1281 uchar_t hid; 1282 1283 /*LINTED: pointer cast may result in improper alignment */ 1284 procp = (struct apic_procent *)(mpct + sizeof (struct apic_mp_cnf_hdr)); 1285 1286 /* No need to count cpu entries if we won't use them */ 1287 if (!bypass_cpus_and_ioapics) { 1288 1289 /* Find max # of CPUS and allocate structure accordingly */ 1290 apic_nproc = 0; 1291 while (procp->proc_entry == APIC_CPU_ENTRY) { 1292 if (procp->proc_cpuflags & CPUFLAGS_EN) { 1293 apic_nproc++; 1294 } 1295 procp++; 1296 } 1297 if (apic_nproc > NCPU) 1298 cmn_err(CE_WARN, "pcplusmp: exceeded " 1299 "maximum no. of CPUs (= %d)", NCPU); 1300 if (!apic_nproc || !(apic_cpus = (apic_cpus_info_t *) 1301 kmem_zalloc(sizeof (*apic_cpus)*apic_nproc, KM_NOSLEEP))) 1302 return (PSM_FAILURE); 1303 } 1304 1305 /*LINTED: pointer cast may result in improper alignment */ 1306 procp = (struct apic_procent *)(mpct + sizeof (struct apic_mp_cnf_hdr)); 1307 1308 /* 1309 * start with index 1 as 0 needs to be filled in with Boot CPU, but 1310 * if we're bypassing this information, it has already been filled 1311 * in by acpi_probe(), so don't overwrite it. 1312 */ 1313 if (!bypass_cpus_and_ioapics) 1314 apic_nproc = 1; 1315 1316 while (procp->proc_entry == APIC_CPU_ENTRY) { 1317 /* check whether the cpu exists or not */ 1318 if (!bypass_cpus_and_ioapics && 1319 procp->proc_cpuflags & CPUFLAGS_EN) { 1320 if (procp->proc_cpuflags & CPUFLAGS_BP) { /* Boot CPU */ 1321 lid = apicadr[APIC_LID_REG]; 1322 apic_cpus[0].aci_local_id = procp->proc_apicid; 1323 if (apic_cpus[0].aci_local_id != 1324 (uchar_t)(lid >> APIC_ID_BIT_OFFSET)) { 1325 return (PSM_FAILURE); 1326 } 1327 apic_cpus[0].aci_local_ver = 1328 procp->proc_version; 1329 } else { 1330 1331 apic_cpus[apic_nproc].aci_local_id = 1332 procp->proc_apicid; 1333 apic_cpus[apic_nproc].aci_local_ver = 1334 procp->proc_version; 1335 apic_nproc++; 1336 1337 } 1338 } 1339 procp++; 1340 } 1341 1342 if (!bypass_cpus_and_ioapics) { 1343 /* convert the number of processors into a cpumask */ 1344 apic_cpumask = (1 << apic_nproc) - 1; 1345 } 1346 1347 /* 1348 * Save start of bus entries for later use. 1349 * Get EISA level cntrl if EISA bus is present. 1350 * Also get the CPI bus id for single CPI bus case 1351 */ 1352 apic_busp = busp = (struct apic_bus *)procp; 1353 while (busp->bus_entry == APIC_BUS_ENTRY) { 1354 lid = apic_find_bus_type((char *)&busp->bus_str1); 1355 if (lid == BUS_EISA) { 1356 eisa_level_intr_mask = (inb(EISA_LEVEL_CNTL + 1) << 8) | 1357 inb(EISA_LEVEL_CNTL) | ((uint_t)INT32_MAX + 1); 1358 } else if (lid == BUS_PCI) { 1359 /* 1360 * apic_single_pci_busid will be used only if 1361 * apic_pic_bus_total is equal to 1 1362 */ 1363 apic_pci_bus_total++; 1364 apic_single_pci_busid = busp->bus_id; 1365 } 1366 busp++; 1367 } 1368 1369 ioapicp = (struct apic_io_entry *)busp; 1370 1371 if (!bypass_cpus_and_ioapics) 1372 apic_io_max = 0; 1373 do { 1374 if (!bypass_cpus_and_ioapics && apic_io_max < MAX_IO_APIC) { 1375 if (ioapicp->io_flags & IOAPIC_FLAGS_EN) { 1376 apic_io_id[apic_io_max] = ioapicp->io_apicid; 1377 apic_io_ver[apic_io_max] = ioapicp->io_version; 1378 /*LINTED: pointer cast may result in improper alignment */ 1379 apicioadr[apic_io_max] = 1380 (int32_t *)psm_map_phys( 1381 (uint32_t)ioapicp->io_apic_addr, 1382 APIC_IO_MEMLEN, PROT_READ | PROT_WRITE); 1383 1384 if (!apicioadr[apic_io_max]) 1385 return (PSM_FAILURE); 1386 1387 ioapic = apicioadr[apic_io_max]; 1388 ioapic[APIC_IO_REG] = APIC_ID_CMD; 1389 id = ioapic[APIC_IO_DATA]; 1390 hid = (uchar_t)(((uint_t)id) >> 24); 1391 1392 if (hid != apic_io_id[apic_io_max]) { 1393 if (apic_io_id[apic_io_max] == 0) 1394 apic_io_id[apic_io_max] = hid; 1395 else { 1396 /* 1397 * set ioapic id to whatever 1398 * reported by MPS 1399 * 1400 * may not need to set index 1401 * again ??? 1402 * take it out and try 1403 */ 1404 1405 id = ((int32_t) 1406 apic_io_id[apic_io_max]) << 1407 24; 1408 1409 ioapic[APIC_IO_REG] = 1410 APIC_ID_CMD; 1411 1412 ioapic[APIC_IO_DATA] = id; 1413 1414 } 1415 } 1416 apic_io_max++; 1417 } 1418 } 1419 ioapicp++; 1420 } while (ioapicp->io_entry == APIC_IO_ENTRY); 1421 1422 apic_io_intrp = (struct apic_io_intr *)ioapicp; 1423 1424 intrp = apic_io_intrp; 1425 while (intrp->intr_entry == APIC_IO_INTR_ENTRY) { 1426 if ((intrp->intr_irq > APIC_MAX_ISA_IRQ) || 1427 (apic_find_bus(intrp->intr_busid) == BUS_PCI)) { 1428 apic_irq_translate = 1; 1429 break; 1430 } 1431 intrp++; 1432 } 1433 1434 return (PSM_SUCCESS); 1435 } 1436 1437 static struct apic_mpfps_hdr * 1438 apic_find_fps_sig(caddr_t cptr, int len) 1439 { 1440 int i; 1441 1442 /* Look for the pattern "_MP_" */ 1443 for (i = 0; i < len; i += 16) { 1444 if ((*(cptr+i) == '_') && 1445 (*(cptr+i+1) == 'M') && 1446 (*(cptr+i+2) == 'P') && 1447 (*(cptr+i+3) == '_')) 1448 /*LINTED: pointer cast may result in improper alignment */ 1449 return ((struct apic_mpfps_hdr *)(cptr + i)); 1450 } 1451 return (NULL); 1452 } 1453 1454 static int 1455 apic_checksum(caddr_t bptr, int len) 1456 { 1457 int i; 1458 uchar_t cksum; 1459 1460 cksum = 0; 1461 for (i = 0; i < len; i++) 1462 cksum += *bptr++; 1463 return ((int)cksum); 1464 } 1465 1466 1467 /* 1468 * Initialise vector->ipl and ipl->pri arrays. level_intr and irqtable 1469 * are also set to NULL. vector->irq is set to a value which cannot map 1470 * to a real irq to show that it is free. 1471 */ 1472 void 1473 apic_init() 1474 { 1475 int i; 1476 int *iptr; 1477 1478 int j = 1; 1479 apic_ipltopri[0] = APIC_VECTOR_PER_IPL; /* leave 0 for idle */ 1480 for (i = 0; i < (APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL); i++) { 1481 if ((i < ((APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL) - 1)) && 1482 (apic_vectortoipl[i + 1] == apic_vectortoipl[i])) 1483 /* get to highest vector at the same ipl */ 1484 continue; 1485 for (; j <= apic_vectortoipl[i]; j++) { 1486 apic_ipltopri[j] = (i << APIC_IPL_SHIFT) + 1487 APIC_BASE_VECT; 1488 } 1489 } 1490 for (; j < MAXIPL + 1; j++) 1491 /* fill up any empty ipltopri slots */ 1492 apic_ipltopri[j] = (i << APIC_IPL_SHIFT) + APIC_BASE_VECT; 1493 1494 /* cpu 0 is always up */ 1495 apic_cpus[0].aci_status = APIC_CPU_ONLINE | APIC_CPU_INTR_ENABLE; 1496 1497 iptr = (int *)&apic_irq_table[0]; 1498 for (i = 0; i <= APIC_MAX_VECTOR; i++) { 1499 apic_level_intr[i] = 0; 1500 *iptr++ = NULL; 1501 apic_vector_to_irq[i] = APIC_RESV_IRQ; 1502 apic_reprogram_info[i].valid = 0; 1503 apic_reprogram_info[i].bindcpu = 0; 1504 apic_reprogram_info[i].timeouts = 0; 1505 } 1506 1507 /* 1508 * Allocate a dummy irq table entry for the reserved entry. 1509 * This takes care of the race between removing an irq and 1510 * clock detecting a CPU in that irq during interrupt load 1511 * sampling. 1512 */ 1513 apic_irq_table[APIC_RESV_IRQ] = 1514 kmem_zalloc(sizeof (apic_irq_t), KM_NOSLEEP); 1515 1516 mutex_init(&airq_mutex, NULL, MUTEX_DEFAULT, NULL); 1517 mutex_init(&apic_reprogram_timeout_mutex, NULL, MUTEX_DEFAULT, NULL); 1518 #if defined(__amd64) 1519 /* 1520 * Make cpu-specific interrupt info point to cr8pri vector 1521 */ 1522 for (i = 0; i <= MAXIPL; i++) 1523 apic_cr8pri[i] = apic_ipltopri[i] >> APIC_IPL_SHIFT; 1524 CPU->cpu_pri_data = apic_cr8pri; 1525 intpri_use_cr8 = 1; 1526 #endif /* __amd64 */ 1527 } 1528 1529 /* 1530 * handler for APIC Error interrupt. Just print a warning and continue 1531 */ 1532 static int 1533 apic_error_intr() 1534 { 1535 uint_t error0, error1, error; 1536 uint_t i; 1537 1538 /* 1539 * We need to write before read as per 7.4.17 of system prog manual. 1540 * We do both and or the results to be safe 1541 */ 1542 error0 = apicadr[APIC_ERROR_STATUS]; 1543 apicadr[APIC_ERROR_STATUS] = 0; 1544 error1 = apicadr[APIC_ERROR_STATUS]; 1545 error = error0 | error1; 1546 1547 /* 1548 * Clear the APIC error status (do this on all cpus that enter here) 1549 * (two writes are required due to the semantics of accessing the 1550 * error status register.) 1551 */ 1552 apicadr[APIC_ERROR_STATUS] = 0; 1553 apicadr[APIC_ERROR_STATUS] = 0; 1554 1555 /* 1556 * Prevent more than 1 CPU from handling error interrupt causing 1557 * double printing (interleave of characters from multiple 1558 * CPU's when using prom_printf) 1559 */ 1560 if (lock_try(&apic_error_lock) == 0) 1561 return (error ? DDI_INTR_CLAIMED : DDI_INTR_UNCLAIMED); 1562 if (error) { 1563 #if DEBUG 1564 if (apic_debug) 1565 debug_enter("pcplusmp: APIC Error interrupt received"); 1566 #endif /* DEBUG */ 1567 if (apic_panic_on_apic_error) 1568 cmn_err(CE_PANIC, 1569 "APIC Error interrupt on CPU %d. Status = %x\n", 1570 psm_get_cpu_id(), error); 1571 else { 1572 if ((error & ~APIC_CS_ERRORS) == 0) { 1573 /* cksum error only */ 1574 apic_error |= APIC_ERR_APIC_ERROR; 1575 apic_apic_error |= error; 1576 apic_num_apic_errors++; 1577 apic_num_cksum_errors++; 1578 } else { 1579 /* 1580 * prom_printf is the best shot we have of 1581 * something which is problem free from 1582 * high level/NMI type of interrupts 1583 */ 1584 prom_printf("APIC Error interrupt on CPU %d. " 1585 "Status 0 = %x, Status 1 = %x\n", 1586 psm_get_cpu_id(), error0, error1); 1587 apic_error |= APIC_ERR_APIC_ERROR; 1588 apic_apic_error |= error; 1589 apic_num_apic_errors++; 1590 for (i = 0; i < apic_error_display_delay; i++) { 1591 tenmicrosec(); 1592 } 1593 /* 1594 * provide more delay next time limited to 1595 * roughly 1 clock tick time 1596 */ 1597 if (apic_error_display_delay < 500) 1598 apic_error_display_delay *= 2; 1599 } 1600 } 1601 lock_clear(&apic_error_lock); 1602 return (DDI_INTR_CLAIMED); 1603 } else { 1604 lock_clear(&apic_error_lock); 1605 return (DDI_INTR_UNCLAIMED); 1606 } 1607 /* NOTREACHED */ 1608 } 1609 1610 /* 1611 * Turn off the mask bit in the performance counter Local Vector Table entry. 1612 */ 1613 static void 1614 apic_cpcovf_mask_clear(void) 1615 { 1616 apicadr[APIC_PCINT_VECT] &= ~APIC_LVT_MASK; 1617 } 1618 1619 static void 1620 apic_init_intr() 1621 { 1622 processorid_t cpun = psm_get_cpu_id(); 1623 1624 #if defined(__amd64) 1625 setcr8((ulong_t)(APIC_MASK_ALL >> APIC_IPL_SHIFT)); 1626 #else 1627 apicadr[APIC_TASK_REG] = APIC_MASK_ALL; 1628 #endif 1629 1630 if (apic_flat_model) 1631 apicadr[APIC_FORMAT_REG] = APIC_FLAT_MODEL; 1632 else 1633 apicadr[APIC_FORMAT_REG] = APIC_CLUSTER_MODEL; 1634 apicadr[APIC_DEST_REG] = AV_HIGH_ORDER >> cpun; 1635 1636 /* need to enable APIC before unmasking NMI */ 1637 apicadr[APIC_SPUR_INT_REG] = AV_UNIT_ENABLE | APIC_SPUR_INTR; 1638 1639 apicadr[APIC_LOCAL_TIMER] = AV_MASK; 1640 apicadr[APIC_INT_VECT0] = AV_MASK; /* local intr reg 0 */ 1641 apicadr[APIC_INT_VECT1] = AV_NMI; /* enable NMI */ 1642 1643 if (apic_cpus[cpun].aci_local_ver < APIC_INTEGRATED_VERS) 1644 return; 1645 1646 /* Enable performance counter overflow interrupt */ 1647 1648 if ((x86_feature & X86_MSR) != X86_MSR) 1649 apic_enable_cpcovf_intr = 0; 1650 if (apic_enable_cpcovf_intr) { 1651 if (apic_cpcovf_vect == 0) { 1652 int ipl = APIC_PCINT_IPL; 1653 int irq = apic_get_ipivect(ipl, -1); 1654 1655 ASSERT(irq != -1); 1656 apic_cpcovf_vect = apic_irq_table[irq]->airq_vector; 1657 ASSERT(apic_cpcovf_vect); 1658 (void) add_avintr(NULL, ipl, 1659 (avfunc)kcpc_hw_overflow_intr, 1660 "apic pcint", irq, NULL, NULL, NULL); 1661 kcpc_hw_overflow_intr_installed = 1; 1662 kcpc_hw_enable_cpc_intr = apic_cpcovf_mask_clear; 1663 } 1664 apicadr[APIC_PCINT_VECT] = apic_cpcovf_vect; 1665 } 1666 1667 /* Enable error interrupt */ 1668 1669 if (apic_enable_error_intr) { 1670 if (apic_errvect == 0) { 1671 int ipl = 0xf; /* get highest priority intr */ 1672 int irq = apic_get_ipivect(ipl, -1); 1673 1674 ASSERT(irq != -1); 1675 apic_errvect = apic_irq_table[irq]->airq_vector; 1676 ASSERT(apic_errvect); 1677 /* 1678 * Not PSMI compliant, but we are going to merge 1679 * with ON anyway 1680 */ 1681 (void) add_avintr((void *)NULL, ipl, 1682 (avfunc)apic_error_intr, "apic error intr", 1683 irq, NULL, NULL, NULL); 1684 } 1685 apicadr[APIC_ERR_VECT] = apic_errvect; 1686 apicadr[APIC_ERROR_STATUS] = 0; 1687 apicadr[APIC_ERROR_STATUS] = 0; 1688 } 1689 } 1690 1691 static void 1692 apic_disable_local_apic() 1693 { 1694 apicadr[APIC_TASK_REG] = APIC_MASK_ALL; 1695 apicadr[APIC_LOCAL_TIMER] = AV_MASK; 1696 apicadr[APIC_INT_VECT0] = AV_MASK; /* local intr reg 0 */ 1697 apicadr[APIC_INT_VECT1] = AV_MASK; /* disable NMI */ 1698 apicadr[APIC_ERR_VECT] = AV_MASK; /* and error interrupt */ 1699 apicadr[APIC_PCINT_VECT] = AV_MASK; /* and perf counter intr */ 1700 apicadr[APIC_SPUR_INT_REG] = APIC_SPUR_INTR; 1701 } 1702 1703 static void 1704 apic_picinit(void) 1705 { 1706 int i, j; 1707 uint_t isr; 1708 volatile int32_t *ioapic; 1709 apic_irq_t *irqptr; 1710 struct intrspec ispec; 1711 1712 /* 1713 * On UniSys Model 6520, the BIOS leaves vector 0x20 isr 1714 * bit on without clearing it with EOI. Since softint 1715 * uses vector 0x20 to interrupt itself, so softint will 1716 * not work on this machine. In order to fix this problem 1717 * a check is made to verify all the isr bits are clear. 1718 * If not, EOIs are issued to clear the bits. 1719 */ 1720 for (i = 7; i >= 1; i--) { 1721 if ((isr = apicadr[APIC_ISR_REG + (i * 4)]) != 0) 1722 for (j = 0; ((j < 32) && (isr != 0)); j++) 1723 if (isr & (1 << j)) { 1724 apicadr[APIC_EOI_REG] = 0; 1725 isr &= ~(1 << j); 1726 apic_error |= APIC_ERR_BOOT_EOI; 1727 } 1728 } 1729 1730 /* set a flag so we know we have run apic_picinit() */ 1731 apic_flag = 1; 1732 LOCK_INIT_CLEAR(&apic_gethrtime_lock); 1733 LOCK_INIT_CLEAR(&apic_ioapic_lock); 1734 LOCK_INIT_CLEAR(&apic_revector_lock); 1735 LOCK_INIT_CLEAR(&apic_ioapic_reprogram_lock); 1736 LOCK_INIT_CLEAR(&apic_error_lock); 1737 1738 picsetup(); /* initialise the 8259 */ 1739 1740 /* add nmi handler - least priority nmi handler */ 1741 LOCK_INIT_CLEAR(&apic_nmi_lock); 1742 1743 if (!psm_add_nmintr(0, (avfunc) apic_nmi_intr, 1744 "pcplusmp NMI handler", (caddr_t)NULL)) 1745 cmn_err(CE_WARN, "pcplusmp: Unable to add nmi handler"); 1746 1747 apic_init_intr(); 1748 1749 /* enable apic mode if imcr present */ 1750 if (apic_imcrp) { 1751 outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT); 1752 outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_APIC); 1753 } 1754 1755 /* mask interrupt vectors */ 1756 for (j = 0; j < apic_io_max; j++) { 1757 int intin_max; 1758 ioapic = apicioadr[j]; 1759 ioapic[APIC_IO_REG] = APIC_VERS_CMD; 1760 /* Bits 23-16 define the maximum redirection entries */ 1761 intin_max = (ioapic[APIC_IO_DATA] >> 16) & 0xff; 1762 for (i = 0; i < intin_max; i++) { 1763 ioapic[APIC_IO_REG] = APIC_RDT_CMD + 2 * i; 1764 ioapic[APIC_IO_DATA] = AV_MASK; 1765 } 1766 } 1767 1768 /* 1769 * Hack alert: deal with ACPI SCI interrupt chicken/egg here 1770 */ 1771 if (apic_sci_vect > 0) { 1772 /* 1773 * acpica has already done add_avintr(); we just 1774 * to finish the job by mimicing translate_irq() 1775 * 1776 * Fake up an intrspec and setup the tables 1777 */ 1778 ispec.intrspec_vec = apic_sci_vect; 1779 ispec.intrspec_pri = SCI_IPL; 1780 1781 if (apic_setup_irq_table(NULL, apic_sci_vect, NULL, 1782 &ispec, &apic_sci_flags, DDI_INTR_TYPE_FIXED) < 0) { 1783 cmn_err(CE_WARN, "!apic: SCI setup failed"); 1784 return; 1785 } 1786 irqptr = apic_irq_table[apic_sci_vect]; 1787 1788 /* Program I/O APIC */ 1789 (void) apic_setup_io_intr(irqptr, apic_sci_vect); 1790 } 1791 } 1792 1793 1794 static void 1795 apic_cpu_start(processorid_t cpun, caddr_t rm_code) 1796 { 1797 int loop_count; 1798 uint32_t vector; 1799 uint_t cpu_id, iflag; 1800 1801 cpu_id = apic_cpus[cpun].aci_local_id; 1802 1803 apic_cmos_ssb_set = 1; 1804 1805 /* 1806 * Interrupts on BSP cpu will be disabled during these startup 1807 * steps in order to avoid unwanted side effects from 1808 * executing interrupt handlers on a problematic BIOS. 1809 */ 1810 1811 iflag = intr_clear(); 1812 outb(CMOS_ADDR, SSB); 1813 outb(CMOS_DATA, BIOS_SHUTDOWN); 1814 1815 while (get_apic_cmd1() & AV_PENDING) 1816 apic_ret(); 1817 1818 /* for integrated - make sure there is one INIT IPI in buffer */ 1819 /* for external - it will wake up the cpu */ 1820 apicadr[APIC_INT_CMD2] = cpu_id << APIC_ICR_ID_BIT_OFFSET; 1821 apicadr[APIC_INT_CMD1] = AV_ASSERT | AV_RESET; 1822 1823 /* If only 1 CPU is installed, PENDING bit will not go low */ 1824 for (loop_count = 0x1000; loop_count; loop_count--) 1825 if (get_apic_cmd1() & AV_PENDING) 1826 apic_ret(); 1827 else 1828 break; 1829 1830 apicadr[APIC_INT_CMD2] = cpu_id << APIC_ICR_ID_BIT_OFFSET; 1831 apicadr[APIC_INT_CMD1] = AV_DEASSERT | AV_RESET; 1832 1833 drv_usecwait(20000); /* 20 milli sec */ 1834 1835 if (apic_cpus[cpun].aci_local_ver >= APIC_INTEGRATED_VERS) { 1836 /* integrated apic */ 1837 1838 rm_code = (caddr_t)(uintptr_t)rm_platter_pa; 1839 vector = (rm_platter_pa >> MMU_PAGESHIFT) & 1840 (APIC_VECTOR_MASK | APIC_IPL_MASK); 1841 1842 /* to offset the INIT IPI queue up in the buffer */ 1843 apicadr[APIC_INT_CMD2] = cpu_id << APIC_ICR_ID_BIT_OFFSET; 1844 apicadr[APIC_INT_CMD1] = vector | AV_STARTUP; 1845 1846 drv_usecwait(200); /* 20 micro sec */ 1847 1848 apicadr[APIC_INT_CMD2] = cpu_id << APIC_ICR_ID_BIT_OFFSET; 1849 apicadr[APIC_INT_CMD1] = vector | AV_STARTUP; 1850 1851 drv_usecwait(200); /* 20 micro sec */ 1852 } 1853 intr_restore(iflag); 1854 } 1855 1856 1857 #ifdef DEBUG 1858 int apic_break_on_cpu = 9; 1859 int apic_stretch_interrupts = 0; 1860 int apic_stretch_ISR = 1 << 3; /* IPL of 3 matches nothing now */ 1861 1862 void 1863 apic_break() 1864 { 1865 } 1866 #endif /* DEBUG */ 1867 1868 /* 1869 * platform_intr_enter 1870 * 1871 * Called at the beginning of the interrupt service routine to 1872 * mask all level equal to and below the interrupt priority 1873 * of the interrupting vector. An EOI should be given to 1874 * the interrupt controller to enable other HW interrupts. 1875 * 1876 * Return -1 for spurious interrupts 1877 * 1878 */ 1879 /*ARGSUSED*/ 1880 static int 1881 apic_intr_enter(int ipl, int *vectorp) 1882 { 1883 uchar_t vector; 1884 int nipl; 1885 int irq, iflag; 1886 apic_cpus_info_t *cpu_infop; 1887 1888 /* 1889 * The real vector programmed in APIC is *vectorp + 0x20 1890 * But, cmnint code subtracts 0x20 before pushing it. 1891 * Hence APIC_BASE_VECT is 0x20. 1892 */ 1893 1894 vector = (uchar_t)*vectorp; 1895 1896 /* if interrupted by the clock, increment apic_nsec_since_boot */ 1897 if (vector == apic_clkvect) { 1898 if (!apic_oneshot) { 1899 /* NOTE: this is not MT aware */ 1900 apic_hrtime_stamp++; 1901 apic_nsec_since_boot += apic_nsec_per_intr; 1902 apic_hrtime_stamp++; 1903 last_count_read = apic_hertz_count; 1904 apic_redistribute_compute(); 1905 } 1906 1907 /* We will avoid all the book keeping overhead for clock */ 1908 nipl = apic_vectortoipl[vector >> APIC_IPL_SHIFT]; 1909 #if defined(__amd64) 1910 setcr8((ulong_t)apic_cr8pri[nipl]); 1911 #else 1912 apicadr[APIC_TASK_REG] = apic_ipltopri[nipl]; 1913 #endif 1914 *vectorp = apic_vector_to_irq[vector + APIC_BASE_VECT]; 1915 apicadr[APIC_EOI_REG] = 0; 1916 return (nipl); 1917 } 1918 1919 cpu_infop = &apic_cpus[psm_get_cpu_id()]; 1920 1921 if (vector == (APIC_SPUR_INTR - APIC_BASE_VECT)) { 1922 cpu_infop->aci_spur_cnt++; 1923 return (APIC_INT_SPURIOUS); 1924 } 1925 1926 /* Check if the vector we got is really what we need */ 1927 if (apic_revector_pending) { 1928 /* 1929 * Disable interrupts for the duration of 1930 * the vector translation to prevent a self-race for 1931 * the apic_revector_lock. This cannot be done 1932 * in apic_xlate_vector because it is recursive and 1933 * we want the vector translation to be atomic with 1934 * respect to other (higher-priority) interrupts. 1935 */ 1936 iflag = intr_clear(); 1937 vector = apic_xlate_vector(vector + APIC_BASE_VECT) - 1938 APIC_BASE_VECT; 1939 intr_restore(iflag); 1940 } 1941 1942 nipl = apic_vectortoipl[vector >> APIC_IPL_SHIFT]; 1943 *vectorp = irq = apic_vector_to_irq[vector + APIC_BASE_VECT]; 1944 1945 #if defined(__amd64) 1946 setcr8((ulong_t)apic_cr8pri[nipl]); 1947 #else 1948 apicadr[APIC_TASK_REG] = apic_ipltopri[nipl]; 1949 #endif 1950 1951 cpu_infop->aci_current[nipl] = (uchar_t)irq; 1952 cpu_infop->aci_curipl = (uchar_t)nipl; 1953 cpu_infop->aci_ISR_in_progress |= 1 << nipl; 1954 1955 /* 1956 * apic_level_intr could have been assimilated into the irq struct. 1957 * but, having it as a character array is more efficient in terms of 1958 * cache usage. So, we leave it as is. 1959 */ 1960 if (!apic_level_intr[irq]) 1961 apicadr[APIC_EOI_REG] = 0; 1962 1963 #ifdef DEBUG 1964 APIC_DEBUG_BUF_PUT(vector); 1965 APIC_DEBUG_BUF_PUT(irq); 1966 APIC_DEBUG_BUF_PUT(nipl); 1967 APIC_DEBUG_BUF_PUT(psm_get_cpu_id()); 1968 if ((apic_stretch_interrupts) && (apic_stretch_ISR & (1 << nipl))) 1969 drv_usecwait(apic_stretch_interrupts); 1970 1971 if (apic_break_on_cpu == psm_get_cpu_id()) 1972 apic_break(); 1973 #endif /* DEBUG */ 1974 return (nipl); 1975 } 1976 1977 static void 1978 apic_intr_exit(int prev_ipl, int irq) 1979 { 1980 apic_cpus_info_t *cpu_infop; 1981 1982 #if defined(__amd64) 1983 setcr8((ulong_t)apic_cr8pri[prev_ipl]); 1984 #else 1985 apicadr[APIC_TASK_REG] = apic_ipltopri[prev_ipl]; 1986 #endif 1987 1988 cpu_infop = &apic_cpus[psm_get_cpu_id()]; 1989 if (apic_level_intr[irq]) 1990 apicadr[APIC_EOI_REG] = 0; 1991 1992 cpu_infop->aci_curipl = (uchar_t)prev_ipl; 1993 /* ISR above current pri could not be in progress */ 1994 cpu_infop->aci_ISR_in_progress &= (2 << prev_ipl) - 1; 1995 } 1996 1997 /* 1998 * Mask all interrupts below or equal to the given IPL 1999 */ 2000 static void 2001 apic_setspl(int ipl) 2002 { 2003 2004 #if defined(__amd64) 2005 setcr8((ulong_t)apic_cr8pri[ipl]); 2006 #else 2007 apicadr[APIC_TASK_REG] = apic_ipltopri[ipl]; 2008 #endif 2009 2010 /* interrupts at ipl above this cannot be in progress */ 2011 apic_cpus[psm_get_cpu_id()].aci_ISR_in_progress &= (2 << ipl) - 1; 2012 /* 2013 * this is a patch fix for the ALR QSMP P5 machine, so that interrupts 2014 * have enough time to come in before the priority is raised again 2015 * during the idle() loop. 2016 */ 2017 if (apic_setspl_delay) 2018 (void) get_apic_pri(); 2019 } 2020 2021 /* 2022 * trigger a software interrupt at the given IPL 2023 */ 2024 static void 2025 apic_set_softintr(int ipl) 2026 { 2027 int vector; 2028 uint_t flag; 2029 2030 vector = apic_resv_vector[ipl]; 2031 2032 flag = intr_clear(); 2033 2034 while (get_apic_cmd1() & AV_PENDING) 2035 apic_ret(); 2036 2037 /* generate interrupt at vector on itself only */ 2038 apicadr[APIC_INT_CMD1] = AV_SH_SELF | vector; 2039 2040 intr_restore(flag); 2041 } 2042 2043 /* 2044 * generates an interprocessor interrupt to another CPU 2045 */ 2046 static void 2047 apic_send_ipi(int cpun, int ipl) 2048 { 2049 int vector; 2050 uint_t flag; 2051 2052 vector = apic_resv_vector[ipl]; 2053 2054 flag = intr_clear(); 2055 2056 while (get_apic_cmd1() & AV_PENDING) 2057 apic_ret(); 2058 2059 apicadr[APIC_INT_CMD2] = 2060 apic_cpus[cpun].aci_local_id << APIC_ICR_ID_BIT_OFFSET; 2061 apicadr[APIC_INT_CMD1] = vector; 2062 2063 intr_restore(flag); 2064 } 2065 2066 2067 /*ARGSUSED*/ 2068 static void 2069 apic_set_idlecpu(processorid_t cpun) 2070 { 2071 } 2072 2073 /*ARGSUSED*/ 2074 static void 2075 apic_unset_idlecpu(processorid_t cpun) 2076 { 2077 } 2078 2079 2080 static void 2081 apic_ret() 2082 { 2083 } 2084 2085 static int 2086 get_apic_cmd1() 2087 { 2088 return (apicadr[APIC_INT_CMD1]); 2089 } 2090 2091 static int 2092 get_apic_pri() 2093 { 2094 #if defined(__amd64) 2095 return ((int)getcr8()); 2096 #else 2097 return (apicadr[APIC_TASK_REG]); 2098 #endif 2099 } 2100 2101 /* 2102 * If apic_coarse_time == 1, then apic_gettime() is used instead of 2103 * apic_gethrtime(). This is used for performance instead of accuracy. 2104 */ 2105 2106 static hrtime_t 2107 apic_gettime() 2108 { 2109 int old_hrtime_stamp; 2110 hrtime_t temp; 2111 2112 /* 2113 * In one-shot mode, we do not keep time, so if anyone 2114 * calls psm_gettime() directly, we vector over to 2115 * gethrtime(). 2116 * one-shot mode MUST NOT be enabled if this psm is the source of 2117 * hrtime. 2118 */ 2119 2120 if (apic_oneshot) 2121 return (gethrtime()); 2122 2123 2124 gettime_again: 2125 while ((old_hrtime_stamp = apic_hrtime_stamp) & 1) 2126 apic_ret(); 2127 2128 temp = apic_nsec_since_boot; 2129 2130 if (apic_hrtime_stamp != old_hrtime_stamp) { /* got an interrupt */ 2131 goto gettime_again; 2132 } 2133 return (temp); 2134 } 2135 2136 /* 2137 * Here we return the number of nanoseconds since booting. Note every 2138 * clock interrupt increments apic_nsec_since_boot by the appropriate 2139 * amount. 2140 */ 2141 static hrtime_t 2142 apic_gethrtime() 2143 { 2144 int curr_timeval, countval, elapsed_ticks, oflags; 2145 int old_hrtime_stamp, status; 2146 hrtime_t temp; 2147 uchar_t cpun; 2148 2149 2150 /* 2151 * In one-shot mode, we do not keep time, so if anyone 2152 * calls psm_gethrtime() directly, we vector over to 2153 * gethrtime(). 2154 * one-shot mode MUST NOT be enabled if this psm is the source of 2155 * hrtime. 2156 */ 2157 2158 if (apic_oneshot) 2159 return (gethrtime()); 2160 2161 oflags = intr_clear(); /* prevent migration */ 2162 2163 cpun = (uchar_t)((uint_t)apicadr[APIC_LID_REG] >> APIC_ID_BIT_OFFSET); 2164 2165 lock_set(&apic_gethrtime_lock); 2166 2167 gethrtime_again: 2168 while ((old_hrtime_stamp = apic_hrtime_stamp) & 1) 2169 apic_ret(); 2170 2171 /* 2172 * Check to see which CPU we are on. Note the time is kept on 2173 * the local APIC of CPU 0. If on CPU 0, simply read the current 2174 * counter. If on another CPU, issue a remote read command to CPU 0. 2175 */ 2176 if (cpun == apic_cpus[0].aci_local_id) { 2177 countval = apicadr[APIC_CURR_COUNT]; 2178 } else { 2179 while (get_apic_cmd1() & AV_PENDING) 2180 apic_ret(); 2181 2182 apicadr[APIC_INT_CMD2] = 2183 apic_cpus[0].aci_local_id << APIC_ICR_ID_BIT_OFFSET; 2184 apicadr[APIC_INT_CMD1] = APIC_CURR_ADD|AV_REMOTE; 2185 2186 while ((status = get_apic_cmd1()) & AV_READ_PENDING) 2187 apic_ret(); 2188 2189 if (status & AV_REMOTE_STATUS) /* 1 = valid */ 2190 countval = apicadr[APIC_REMOTE_READ]; 2191 else { /* 0 = invalid */ 2192 apic_remote_hrterr++; 2193 /* 2194 * return last hrtime right now, will need more 2195 * testing if change to retry 2196 */ 2197 temp = apic_last_hrtime; 2198 2199 lock_clear(&apic_gethrtime_lock); 2200 2201 intr_restore(oflags); 2202 2203 return (temp); 2204 } 2205 } 2206 if (countval > last_count_read) 2207 countval = 0; 2208 else 2209 last_count_read = countval; 2210 2211 elapsed_ticks = apic_hertz_count - countval; 2212 2213 curr_timeval = elapsed_ticks * apic_nsec_per_tick; 2214 temp = apic_nsec_since_boot + curr_timeval; 2215 2216 if (apic_hrtime_stamp != old_hrtime_stamp) { /* got an interrupt */ 2217 /* we might have clobbered last_count_read. Restore it */ 2218 last_count_read = apic_hertz_count; 2219 goto gethrtime_again; 2220 } 2221 2222 if (temp < apic_last_hrtime) { 2223 /* return last hrtime if error occurs */ 2224 apic_hrtime_error++; 2225 temp = apic_last_hrtime; 2226 } 2227 else 2228 apic_last_hrtime = temp; 2229 2230 lock_clear(&apic_gethrtime_lock); 2231 intr_restore(oflags); 2232 2233 return (temp); 2234 } 2235 2236 /* apic NMI handler */ 2237 /*ARGSUSED*/ 2238 static void 2239 apic_nmi_intr(caddr_t arg) 2240 { 2241 if (apic_shutdown_processors) { 2242 apic_disable_local_apic(); 2243 return; 2244 } 2245 2246 if (lock_try(&apic_nmi_lock)) { 2247 if (apic_kmdb_on_nmi) { 2248 if (psm_debugger() == 0) { 2249 cmn_err(CE_PANIC, 2250 "NMI detected, kmdb is not available."); 2251 } else { 2252 debug_enter("\nNMI detected, entering kmdb.\n"); 2253 } 2254 } else { 2255 if (apic_panic_on_nmi) { 2256 /* Keep panic from entering kmdb. */ 2257 nopanicdebug = 1; 2258 cmn_err(CE_PANIC, "pcplusmp: NMI received"); 2259 } else { 2260 /* 2261 * prom_printf is the best shot we have 2262 * of something which is problem free from 2263 * high level/NMI type of interrupts 2264 */ 2265 prom_printf("pcplusmp: NMI received\n"); 2266 apic_error |= APIC_ERR_NMI; 2267 apic_num_nmis++; 2268 } 2269 } 2270 lock_clear(&apic_nmi_lock); 2271 } 2272 } 2273 2274 /* 2275 * Add mask bits to disable interrupt vector from happening 2276 * at or above IPL. In addition, it should remove mask bits 2277 * to enable interrupt vectors below the given IPL. 2278 * 2279 * Both add and delspl are complicated by the fact that different interrupts 2280 * may share IRQs. This can happen in two ways. 2281 * 1. The same H/W line is shared by more than 1 device 2282 * 1a. with interrupts at different IPLs 2283 * 1b. with interrupts at same IPL 2284 * 2. We ran out of vectors at a given IPL and started sharing vectors. 2285 * 1b and 2 should be handled gracefully, except for the fact some ISRs 2286 * will get called often when no interrupt is pending for the device. 2287 * For 1a, we just hope that the machine blows up with the person who 2288 * set it up that way!. In the meantime, we handle it at the higher IPL. 2289 */ 2290 /*ARGSUSED*/ 2291 static int 2292 apic_addspl(int irqno, int ipl, int min_ipl, int max_ipl) 2293 { 2294 uchar_t vector; 2295 int iflag; 2296 apic_irq_t *irqptr, *irqheadptr; 2297 int irqindex; 2298 2299 ASSERT(max_ipl <= UCHAR_MAX); 2300 irqindex = IRQINDEX(irqno); 2301 2302 if ((irqindex == -1) || (!apic_irq_table[irqindex])) 2303 return (PSM_FAILURE); 2304 2305 irqptr = irqheadptr = apic_irq_table[irqindex]; 2306 2307 DDI_INTR_IMPLDBG((CE_CONT, "apic_addspl: dip=0x%p type=%d irqno=0x%x " 2308 "vector=0x%x\n", (void *)irqptr->airq_dip, 2309 irqptr->airq_mps_intr_index, irqno, irqptr->airq_vector)); 2310 2311 while (irqptr) { 2312 if (VIRTIRQ(irqindex, irqptr->airq_share_id) == irqno) 2313 break; 2314 irqptr = irqptr->airq_next; 2315 } 2316 irqptr->airq_share++; 2317 2318 /* return if it is not hardware interrupt */ 2319 if (irqptr->airq_mps_intr_index == RESERVE_INDEX) 2320 return (PSM_SUCCESS); 2321 2322 /* Or if there are more interupts at a higher IPL */ 2323 if (ipl != max_ipl) 2324 return (PSM_SUCCESS); 2325 2326 /* 2327 * if apic_picinit() has not been called yet, just return. 2328 * At the end of apic_picinit(), we will call setup_io_intr(). 2329 */ 2330 2331 if (!apic_flag) 2332 return (PSM_SUCCESS); 2333 2334 iflag = intr_clear(); 2335 2336 /* 2337 * Upgrade vector if max_ipl is not earlier ipl. If we cannot allocate, 2338 * return failure. Not very elegant, but then we hope the 2339 * machine will blow up with ... 2340 */ 2341 if (irqptr->airq_ipl != max_ipl) { 2342 vector = apic_allocate_vector(max_ipl, irqindex, 1); 2343 if (vector == 0) { 2344 intr_restore(iflag); 2345 irqptr->airq_share--; 2346 return (PSM_FAILURE); 2347 } 2348 irqptr = irqheadptr; 2349 apic_mark_vector(irqptr->airq_vector, vector); 2350 while (irqptr) { 2351 irqptr->airq_vector = vector; 2352 irqptr->airq_ipl = (uchar_t)max_ipl; 2353 /* 2354 * reprogram irq being added and every one else 2355 * who is not in the UNINIT state 2356 */ 2357 if ((VIRTIRQ(irqindex, irqptr->airq_share_id) == 2358 irqno) || (irqptr->airq_temp_cpu != IRQ_UNINIT)) { 2359 apic_record_rdt_entry(irqptr, irqindex); 2360 (void) apic_setup_io_intr(irqptr, irqindex); 2361 } 2362 irqptr = irqptr->airq_next; 2363 } 2364 intr_restore(iflag); 2365 return (PSM_SUCCESS); 2366 } 2367 2368 ASSERT(irqptr); 2369 (void) apic_setup_io_intr(irqptr, irqindex); 2370 intr_restore(iflag); 2371 return (PSM_SUCCESS); 2372 } 2373 2374 /* 2375 * Recompute mask bits for the given interrupt vector. 2376 * If there is no interrupt servicing routine for this 2377 * vector, this function should disable interrupt vector 2378 * from happening at all IPLs. If there are still 2379 * handlers using the given vector, this function should 2380 * disable the given vector from happening below the lowest 2381 * IPL of the remaining hadlers. 2382 */ 2383 /*ARGSUSED*/ 2384 static int 2385 apic_delspl(int irqno, int ipl, int min_ipl, int max_ipl) 2386 { 2387 uchar_t vector, bind_cpu; 2388 int iflag, intin, irqindex; 2389 volatile int32_t *ioapic; 2390 apic_irq_t *irqptr, *irqheadptr; 2391 2392 irqindex = IRQINDEX(irqno); 2393 irqptr = irqheadptr = apic_irq_table[irqindex]; 2394 2395 DDI_INTR_IMPLDBG((CE_CONT, "apic_delspl: dip=0x%p type=%d irqno=0x%x " 2396 "vector=0x%x\n", (void *)irqptr->airq_dip, 2397 irqptr->airq_mps_intr_index, irqno, irqptr->airq_vector)); 2398 2399 while (irqptr) { 2400 if (VIRTIRQ(irqindex, irqptr->airq_share_id) == irqno) 2401 break; 2402 irqptr = irqptr->airq_next; 2403 } 2404 ASSERT(irqptr); 2405 2406 irqptr->airq_share--; 2407 2408 if (ipl < max_ipl) 2409 return (PSM_SUCCESS); 2410 2411 /* return if it is not hardware interrupt */ 2412 if (irqptr->airq_mps_intr_index == RESERVE_INDEX) 2413 return (PSM_SUCCESS); 2414 2415 if (!apic_flag) { 2416 /* 2417 * Clear irq_struct. If two devices shared an intpt 2418 * line & 1 unloaded before picinit, we are hosed. But, then 2419 * we hope the machine will ... 2420 */ 2421 irqptr->airq_mps_intr_index = FREE_INDEX; 2422 irqptr->airq_temp_cpu = IRQ_UNINIT; 2423 apic_free_vector(irqptr->airq_vector); 2424 return (PSM_SUCCESS); 2425 } 2426 /* 2427 * Downgrade vector to new max_ipl if needed.If we cannot allocate, 2428 * use old IPL. Not very elegant, but then we hope ... 2429 */ 2430 if ((irqptr->airq_ipl != max_ipl) && (max_ipl != PSM_INVALID_IPL)) { 2431 apic_irq_t *irqp; 2432 if (vector = apic_allocate_vector(max_ipl, irqno, 1)) { 2433 apic_mark_vector(irqheadptr->airq_vector, vector); 2434 irqp = irqheadptr; 2435 while (irqp) { 2436 irqp->airq_vector = vector; 2437 irqp->airq_ipl = (uchar_t)max_ipl; 2438 if (irqp->airq_temp_cpu != IRQ_UNINIT) { 2439 apic_record_rdt_entry(irqp, irqindex); 2440 (void) apic_setup_io_intr(irqp, 2441 irqindex); 2442 } 2443 irqp = irqp->airq_next; 2444 } 2445 } 2446 } 2447 2448 if (irqptr->airq_share) 2449 return (PSM_SUCCESS); 2450 2451 ioapic = apicioadr[irqptr->airq_ioapicindex]; 2452 intin = irqptr->airq_intin_no; 2453 iflag = intr_clear(); 2454 lock_set(&apic_ioapic_lock); 2455 ioapic[APIC_IO_REG] = APIC_RDT_CMD + 2 * intin; 2456 ioapic[APIC_IO_DATA] = AV_MASK; 2457 2458 /* Disable the MSI/X vector */ 2459 if (APIC_IS_MSI_OR_MSIX_INDEX(irqptr->airq_mps_intr_index)) { 2460 int type = (irqptr->airq_mps_intr_index == MSI_INDEX) ? 2461 DDI_INTR_TYPE_MSI : DDI_INTR_TYPE_MSIX; 2462 2463 /* 2464 * Make sure we only disable on the last 2465 * of the multi-MSI support 2466 */ 2467 if (i_ddi_intr_get_current_nintrs(irqptr->airq_dip) == 1) { 2468 (void) pci_msi_unconfigure(irqptr->airq_dip, type, 2469 irqptr->airq_ioapicindex); 2470 2471 (void) pci_msi_disable_mode(irqptr->airq_dip, type, 2472 irqptr->airq_ioapicindex); 2473 } 2474 } 2475 2476 if (max_ipl == PSM_INVALID_IPL) { 2477 ASSERT(irqheadptr == irqptr); 2478 bind_cpu = irqptr->airq_temp_cpu; 2479 if (((uchar_t)bind_cpu != IRQ_UNBOUND) && 2480 ((uchar_t)bind_cpu != IRQ_UNINIT)) { 2481 ASSERT((bind_cpu & ~IRQ_USER_BOUND) < apic_nproc); 2482 if (bind_cpu & IRQ_USER_BOUND) { 2483 /* If hardbound, temp_cpu == cpu */ 2484 bind_cpu &= ~IRQ_USER_BOUND; 2485 apic_cpus[bind_cpu].aci_bound--; 2486 } else 2487 apic_cpus[bind_cpu].aci_temp_bound--; 2488 } 2489 lock_clear(&apic_ioapic_lock); 2490 intr_restore(iflag); 2491 irqptr->airq_temp_cpu = IRQ_UNINIT; 2492 irqptr->airq_mps_intr_index = FREE_INDEX; 2493 apic_free_vector(irqptr->airq_vector); 2494 return (PSM_SUCCESS); 2495 } 2496 lock_clear(&apic_ioapic_lock); 2497 intr_restore(iflag); 2498 2499 mutex_enter(&airq_mutex); 2500 if ((irqptr == apic_irq_table[irqindex])) { 2501 apic_irq_t *oldirqptr; 2502 /* Move valid irq entry to the head */ 2503 irqheadptr = oldirqptr = irqptr; 2504 irqptr = irqptr->airq_next; 2505 ASSERT(irqptr); 2506 while (irqptr) { 2507 if (irqptr->airq_mps_intr_index != FREE_INDEX) 2508 break; 2509 oldirqptr = irqptr; 2510 irqptr = irqptr->airq_next; 2511 } 2512 /* remove all invalid ones from the beginning */ 2513 apic_irq_table[irqindex] = irqptr; 2514 /* 2515 * and link them back after the head. The invalid ones 2516 * begin with irqheadptr and end at oldirqptr 2517 */ 2518 oldirqptr->airq_next = irqptr->airq_next; 2519 irqptr->airq_next = irqheadptr; 2520 } 2521 mutex_exit(&airq_mutex); 2522 2523 irqptr->airq_temp_cpu = IRQ_UNINIT; 2524 irqptr->airq_mps_intr_index = FREE_INDEX; 2525 return (PSM_SUCCESS); 2526 } 2527 2528 /* 2529 * Return HW interrupt number corresponding to the given IPL 2530 */ 2531 /*ARGSUSED*/ 2532 static int 2533 apic_softlvl_to_irq(int ipl) 2534 { 2535 /* 2536 * Do not use apic to trigger soft interrupt. 2537 * It will cause the system to hang when 2 hardware interrupts 2538 * at the same priority with the softint are already accepted 2539 * by the apic. Cause the AV_PENDING bit will not be cleared 2540 * until one of the hardware interrupt is eoi'ed. If we need 2541 * to send an ipi at this time, we will end up looping forever 2542 * to wait for the AV_PENDING bit to clear. 2543 */ 2544 return (PSM_SV_SOFTWARE); 2545 } 2546 2547 static int 2548 apic_post_cpu_start() 2549 { 2550 int i, cpun; 2551 apic_irq_t *irq_ptr; 2552 2553 apic_init_intr(); 2554 2555 /* 2556 * since some systems don't enable the internal cache on the non-boot 2557 * cpus, so we have to enable them here 2558 */ 2559 setcr0(getcr0() & ~(0x60000000)); 2560 2561 while (get_apic_cmd1() & AV_PENDING) 2562 apic_ret(); 2563 2564 cpun = psm_get_cpu_id(); 2565 apic_cpus[cpun].aci_status = APIC_CPU_ONLINE | APIC_CPU_INTR_ENABLE; 2566 2567 for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) { 2568 irq_ptr = apic_irq_table[i]; 2569 if ((irq_ptr == NULL) || 2570 ((irq_ptr->airq_cpu & ~IRQ_USER_BOUND) != cpun)) 2571 continue; 2572 2573 while (irq_ptr) { 2574 if (irq_ptr->airq_temp_cpu != IRQ_UNINIT) 2575 (void) apic_rebind(irq_ptr, cpun, 1, IMMEDIATE); 2576 irq_ptr = irq_ptr->airq_next; 2577 } 2578 } 2579 2580 return (PSM_SUCCESS); 2581 } 2582 2583 processorid_t 2584 apic_get_next_processorid(processorid_t cpu_id) 2585 { 2586 2587 int i; 2588 2589 if (cpu_id == -1) 2590 return ((processorid_t)0); 2591 2592 for (i = cpu_id + 1; i < NCPU; i++) { 2593 if (apic_cpumask & (1 << i)) 2594 return (i); 2595 } 2596 2597 return ((processorid_t)-1); 2598 } 2599 2600 2601 /* 2602 * type == -1 indicates it is an internal request. Do not change 2603 * resv_vector for these requests 2604 */ 2605 static int 2606 apic_get_ipivect(int ipl, int type) 2607 { 2608 uchar_t vector; 2609 int irq; 2610 2611 if (irq = apic_allocate_irq(APIC_VECTOR(ipl))) { 2612 if (vector = apic_allocate_vector(ipl, irq, 1)) { 2613 apic_irq_table[irq]->airq_mps_intr_index = 2614 RESERVE_INDEX; 2615 apic_irq_table[irq]->airq_vector = vector; 2616 if (type != -1) { 2617 apic_resv_vector[ipl] = vector; 2618 } 2619 return (irq); 2620 } 2621 } 2622 apic_error |= APIC_ERR_GET_IPIVECT_FAIL; 2623 return (-1); /* shouldn't happen */ 2624 } 2625 2626 static int 2627 apic_getclkirq(int ipl) 2628 { 2629 int irq; 2630 2631 if ((irq = apic_get_ipivect(ipl, -1)) == -1) 2632 return (-1); 2633 /* 2634 * Note the vector in apic_clkvect for per clock handling. 2635 */ 2636 apic_clkvect = apic_irq_table[irq]->airq_vector - APIC_BASE_VECT; 2637 APIC_VERBOSE_IOAPIC((CE_NOTE, "get_clkirq: vector = %x\n", 2638 apic_clkvect)); 2639 return (irq); 2640 } 2641 2642 /* 2643 * Return the number of APIC clock ticks elapsed for 8245 to decrement 2644 * (APIC_TIME_COUNT + pit_ticks_adj) ticks. 2645 */ 2646 static uint_t 2647 apic_calibrate(volatile uint32_t *addr, uint16_t *pit_ticks_adj) 2648 { 2649 uint8_t pit_tick_lo; 2650 uint16_t pit_tick, target_pit_tick; 2651 uint32_t start_apic_tick, end_apic_tick; 2652 int iflag; 2653 2654 addr += APIC_CURR_COUNT; 2655 2656 iflag = intr_clear(); 2657 2658 do { 2659 pit_tick_lo = inb(PITCTR0_PORT); 2660 pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo; 2661 } while (pit_tick < APIC_TIME_MIN || 2662 pit_tick_lo <= APIC_LB_MIN || pit_tick_lo >= APIC_LB_MAX); 2663 2664 /* 2665 * Wait for the 8254 to decrement by 5 ticks to ensure 2666 * we didn't start in the middle of a tick. 2667 * Compare with 0x10 for the wrap around case. 2668 */ 2669 target_pit_tick = pit_tick - 5; 2670 do { 2671 pit_tick_lo = inb(PITCTR0_PORT); 2672 pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo; 2673 } while (pit_tick > target_pit_tick || pit_tick_lo < 0x10); 2674 2675 start_apic_tick = *addr; 2676 2677 /* 2678 * Wait for the 8254 to decrement by 2679 * (APIC_TIME_COUNT + pit_ticks_adj) ticks 2680 */ 2681 target_pit_tick = pit_tick - APIC_TIME_COUNT; 2682 do { 2683 pit_tick_lo = inb(PITCTR0_PORT); 2684 pit_tick = (inb(PITCTR0_PORT) << 8) | pit_tick_lo; 2685 } while (pit_tick > target_pit_tick || pit_tick_lo < 0x10); 2686 2687 end_apic_tick = *addr; 2688 2689 *pit_ticks_adj = target_pit_tick - pit_tick; 2690 2691 intr_restore(iflag); 2692 2693 return (start_apic_tick - end_apic_tick); 2694 } 2695 2696 /* 2697 * Initialise the APIC timer on the local APIC of CPU 0 to the desired 2698 * frequency. Note at this stage in the boot sequence, the boot processor 2699 * is the only active processor. 2700 * hertz value of 0 indicates a one-shot mode request. In this case 2701 * the function returns the resolution (in nanoseconds) for the hardware 2702 * timer interrupt. If one-shot mode capability is not available, 2703 * the return value will be 0. apic_enable_oneshot is a global switch 2704 * for disabling the functionality. 2705 * A non-zero positive value for hertz indicates a periodic mode request. 2706 * In this case the hardware will be programmed to generate clock interrupts 2707 * at hertz frequency and returns the resolution of interrupts in 2708 * nanosecond. 2709 */ 2710 2711 static int 2712 apic_clkinit(int hertz) 2713 { 2714 2715 uint_t apic_ticks = 0; 2716 uint_t pit_time; 2717 int ret; 2718 uint16_t pit_ticks_adj; 2719 static int firsttime = 1; 2720 2721 if (firsttime) { 2722 /* first time calibrate */ 2723 2724 apicadr[APIC_DIVIDE_REG] = 0x0; 2725 apicadr[APIC_INIT_COUNT] = APIC_MAXVAL; 2726 2727 /* set periodic interrupt based on CLKIN */ 2728 apicadr[APIC_LOCAL_TIMER] = 2729 (apic_clkvect + APIC_BASE_VECT) | AV_TIME; 2730 tenmicrosec(); 2731 2732 apic_ticks = apic_calibrate(apicadr, &pit_ticks_adj); 2733 2734 apicadr[APIC_LOCAL_TIMER] = 2735 (apic_clkvect + APIC_BASE_VECT) | AV_MASK; 2736 /* 2737 * pit time is the amount of real time (in nanoseconds ) it took 2738 * the 8254 to decrement (APIC_TIME_COUNT + pit_ticks_adj) ticks 2739 */ 2740 pit_time = ((longlong_t)(APIC_TIME_COUNT + 2741 pit_ticks_adj) * NANOSEC) / PIT_HZ; 2742 2743 /* 2744 * Determine the number of nanoseconds per APIC clock tick 2745 * and then determine how many APIC ticks to interrupt at the 2746 * desired frequency 2747 */ 2748 apic_nsec_per_tick = pit_time / apic_ticks; 2749 if (apic_nsec_per_tick == 0) 2750 apic_nsec_per_tick = 1; 2751 2752 /* the interval timer initial count is 32 bit max */ 2753 apic_nsec_max = (hrtime_t)apic_nsec_per_tick * APIC_MAXVAL; 2754 firsttime = 0; 2755 } 2756 2757 if (hertz != 0) { 2758 /* periodic */ 2759 apic_nsec_per_intr = NANOSEC / hertz; 2760 apic_hertz_count = (longlong_t)apic_nsec_per_intr / 2761 apic_nsec_per_tick; 2762 apic_sample_factor_redistribution = hertz + 1; 2763 } 2764 2765 apic_int_busy_mark = (apic_int_busy_mark * 2766 apic_sample_factor_redistribution) / 100; 2767 apic_int_free_mark = (apic_int_free_mark * 2768 apic_sample_factor_redistribution) / 100; 2769 apic_diff_for_redistribution = (apic_diff_for_redistribution * 2770 apic_sample_factor_redistribution) / 100; 2771 2772 if (hertz == 0) { 2773 /* requested one_shot */ 2774 if (!apic_oneshot_enable) 2775 return (0); 2776 apic_oneshot = 1; 2777 ret = (int)apic_nsec_per_tick; 2778 } else { 2779 /* program the local APIC to interrupt at the given frequency */ 2780 apicadr[APIC_INIT_COUNT] = apic_hertz_count; 2781 apicadr[APIC_LOCAL_TIMER] = 2782 (apic_clkvect + APIC_BASE_VECT) | AV_TIME; 2783 apic_oneshot = 0; 2784 ret = NANOSEC / hertz; 2785 } 2786 2787 return (ret); 2788 2789 } 2790 2791 /* 2792 * apic_preshutdown: 2793 * Called early in shutdown whilst we can still access filesystems to do 2794 * things like loading modules which will be required to complete shutdown 2795 * after filesystems are all unmounted. 2796 */ 2797 static void 2798 apic_preshutdown(int cmd, int fcn) 2799 { 2800 APIC_VERBOSE_POWEROFF(("apic_preshutdown(%d,%d); m=%d a=%d\n", 2801 cmd, fcn, apic_poweroff_method, apic_enable_acpi)); 2802 2803 if ((cmd != A_SHUTDOWN) || (fcn != AD_POWEROFF)) { 2804 return; 2805 } 2806 } 2807 2808 static void 2809 apic_shutdown(int cmd, int fcn) 2810 { 2811 int iflag, restarts, attempts; 2812 int i, j; 2813 volatile int32_t *ioapic; 2814 uchar_t byte; 2815 2816 /* Send NMI to all CPUs except self to do per processor shutdown */ 2817 iflag = intr_clear(); 2818 while (get_apic_cmd1() & AV_PENDING) 2819 apic_ret(); 2820 apic_shutdown_processors = 1; 2821 apicadr[APIC_INT_CMD1] = AV_NMI | AV_LEVEL | AV_SH_ALL_EXCSELF; 2822 2823 /* restore cmos shutdown byte before reboot */ 2824 if (apic_cmos_ssb_set) { 2825 outb(CMOS_ADDR, SSB); 2826 outb(CMOS_DATA, 0); 2827 } 2828 /* Disable the I/O APIC redirection entries */ 2829 for (j = 0; j < apic_io_max; j++) { 2830 int intin_max; 2831 ioapic = apicioadr[j]; 2832 ioapic[APIC_IO_REG] = APIC_VERS_CMD; 2833 /* Bits 23-16 define the maximum redirection entries */ 2834 intin_max = (ioapic[APIC_IO_DATA] >> 16) & 0xff; 2835 for (i = 0; i < intin_max; i++) { 2836 ioapic[APIC_IO_REG] = APIC_RDT_CMD + 2 * i; 2837 ioapic[APIC_IO_DATA] = AV_MASK; 2838 } 2839 } 2840 2841 /* disable apic mode if imcr present */ 2842 if (apic_imcrp) { 2843 outb(APIC_IMCR_P1, (uchar_t)APIC_IMCR_SELECT); 2844 outb(APIC_IMCR_P2, (uchar_t)APIC_IMCR_PIC); 2845 } 2846 2847 apic_disable_local_apic(); 2848 2849 intr_restore(iflag); 2850 2851 if ((cmd != A_SHUTDOWN) || (fcn != AD_POWEROFF)) { 2852 return; 2853 } 2854 2855 switch (apic_poweroff_method) { 2856 case APIC_POWEROFF_VIA_RTC: 2857 2858 /* select the extended NVRAM bank in the RTC */ 2859 outb(CMOS_ADDR, RTC_REGA); 2860 byte = inb(CMOS_DATA); 2861 outb(CMOS_DATA, (byte | EXT_BANK)); 2862 2863 outb(CMOS_ADDR, PFR_REG); 2864 2865 /* for Predator must toggle the PAB bit */ 2866 byte = inb(CMOS_DATA); 2867 2868 /* 2869 * clear power active bar, wakeup alarm and 2870 * kickstart 2871 */ 2872 byte &= ~(PAB_CBIT | WF_FLAG | KS_FLAG); 2873 outb(CMOS_DATA, byte); 2874 2875 /* delay before next write */ 2876 drv_usecwait(1000); 2877 2878 /* for S40 the following would suffice */ 2879 byte = inb(CMOS_DATA); 2880 2881 /* power active bar control bit */ 2882 byte |= PAB_CBIT; 2883 outb(CMOS_DATA, byte); 2884 2885 break; 2886 2887 case APIC_POWEROFF_VIA_ASPEN_BMC: 2888 restarts = 0; 2889 restart_aspen_bmc: 2890 if (++restarts == 3) 2891 break; 2892 attempts = 0; 2893 do { 2894 byte = inb(MISMIC_FLAG_REGISTER); 2895 byte &= MISMIC_BUSY_MASK; 2896 if (byte != 0) { 2897 drv_usecwait(1000); 2898 if (attempts >= 3) 2899 goto restart_aspen_bmc; 2900 ++attempts; 2901 } 2902 } while (byte != 0); 2903 outb(MISMIC_CNTL_REGISTER, CC_SMS_GET_STATUS); 2904 byte = inb(MISMIC_FLAG_REGISTER); 2905 byte |= 0x1; 2906 outb(MISMIC_FLAG_REGISTER, byte); 2907 i = 0; 2908 for (; i < (sizeof (aspen_bmc)/sizeof (aspen_bmc[0])); 2909 i++) { 2910 attempts = 0; 2911 do { 2912 byte = inb(MISMIC_FLAG_REGISTER); 2913 byte &= MISMIC_BUSY_MASK; 2914 if (byte != 0) { 2915 drv_usecwait(1000); 2916 if (attempts >= 3) 2917 goto restart_aspen_bmc; 2918 ++attempts; 2919 } 2920 } while (byte != 0); 2921 outb(MISMIC_CNTL_REGISTER, aspen_bmc[i].cntl); 2922 outb(MISMIC_DATA_REGISTER, aspen_bmc[i].data); 2923 byte = inb(MISMIC_FLAG_REGISTER); 2924 byte |= 0x1; 2925 outb(MISMIC_FLAG_REGISTER, byte); 2926 } 2927 break; 2928 2929 case APIC_POWEROFF_VIA_SITKA_BMC: 2930 restarts = 0; 2931 restart_sitka_bmc: 2932 if (++restarts == 3) 2933 break; 2934 attempts = 0; 2935 do { 2936 byte = inb(SMS_STATUS_REGISTER); 2937 byte &= SMS_STATE_MASK; 2938 if ((byte == SMS_READ_STATE) || 2939 (byte == SMS_WRITE_STATE)) { 2940 drv_usecwait(1000); 2941 if (attempts >= 3) 2942 goto restart_sitka_bmc; 2943 ++attempts; 2944 } 2945 } while ((byte == SMS_READ_STATE) || 2946 (byte == SMS_WRITE_STATE)); 2947 outb(SMS_COMMAND_REGISTER, SMS_GET_STATUS); 2948 i = 0; 2949 for (; i < (sizeof (sitka_bmc)/sizeof (sitka_bmc[0])); 2950 i++) { 2951 attempts = 0; 2952 do { 2953 byte = inb(SMS_STATUS_REGISTER); 2954 byte &= SMS_IBF_MASK; 2955 if (byte != 0) { 2956 drv_usecwait(1000); 2957 if (attempts >= 3) 2958 goto restart_sitka_bmc; 2959 ++attempts; 2960 } 2961 } while (byte != 0); 2962 outb(sitka_bmc[i].port, sitka_bmc[i].data); 2963 } 2964 break; 2965 2966 case APIC_POWEROFF_NONE: 2967 2968 /* If no APIC direct method, we will try using ACPI */ 2969 if (apic_enable_acpi) { 2970 if (acpi_poweroff() == 1) 2971 return; 2972 } else 2973 return; 2974 2975 break; 2976 } 2977 /* 2978 * Wait a limited time here for power to go off. 2979 * If the power does not go off, then there was a 2980 * problem and we should continue to the halt which 2981 * prints a message for the user to press a key to 2982 * reboot. 2983 */ 2984 drv_usecwait(7000000); /* wait seven seconds */ 2985 2986 } 2987 2988 /* 2989 * Try and disable all interrupts. We just assign interrupts to other 2990 * processors based on policy. If any were bound by user request, we 2991 * let them continue and return failure. We do not bother to check 2992 * for cache affinity while rebinding. 2993 */ 2994 2995 static int 2996 apic_disable_intr(processorid_t cpun) 2997 { 2998 int bind_cpu = 0, i, hardbound = 0, iflag; 2999 apic_irq_t *irq_ptr; 3000 3001 if (cpun == 0) 3002 return (PSM_FAILURE); 3003 3004 iflag = intr_clear(); 3005 lock_set(&apic_ioapic_lock); 3006 apic_cpus[cpun].aci_status &= ~APIC_CPU_INTR_ENABLE; 3007 lock_clear(&apic_ioapic_lock); 3008 intr_restore(iflag); 3009 apic_cpus[cpun].aci_curipl = 0; 3010 i = apic_min_device_irq; 3011 for (; i <= apic_max_device_irq; i++) { 3012 /* 3013 * If there are bound interrupts on this cpu, then 3014 * rebind them to other processors. 3015 */ 3016 if ((irq_ptr = apic_irq_table[i]) != NULL) { 3017 ASSERT((irq_ptr->airq_temp_cpu == IRQ_UNBOUND) || 3018 (irq_ptr->airq_temp_cpu == IRQ_UNINIT) || 3019 ((irq_ptr->airq_temp_cpu & ~IRQ_USER_BOUND) < 3020 apic_nproc)); 3021 3022 if (irq_ptr->airq_temp_cpu == (cpun | IRQ_USER_BOUND)) { 3023 hardbound = 1; 3024 continue; 3025 } 3026 3027 if (irq_ptr->airq_temp_cpu == cpun) { 3028 do { 3029 apic_next_bind_cpu += 2; 3030 bind_cpu = apic_next_bind_cpu / 2; 3031 if (bind_cpu >= apic_nproc) { 3032 apic_next_bind_cpu = 1; 3033 bind_cpu = 0; 3034 3035 } 3036 } while (apic_rebind_all(irq_ptr, bind_cpu, 1)); 3037 } 3038 } 3039 } 3040 if (hardbound) { 3041 cmn_err(CE_WARN, "Could not disable interrupts on %d" 3042 "due to user bound interrupts", cpun); 3043 return (PSM_FAILURE); 3044 } 3045 else 3046 return (PSM_SUCCESS); 3047 } 3048 3049 static void 3050 apic_enable_intr(processorid_t cpun) 3051 { 3052 int i, iflag; 3053 apic_irq_t *irq_ptr; 3054 3055 iflag = intr_clear(); 3056 lock_set(&apic_ioapic_lock); 3057 apic_cpus[cpun].aci_status |= APIC_CPU_INTR_ENABLE; 3058 lock_clear(&apic_ioapic_lock); 3059 intr_restore(iflag); 3060 3061 i = apic_min_device_irq; 3062 for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) { 3063 if ((irq_ptr = apic_irq_table[i]) != NULL) { 3064 if ((irq_ptr->airq_cpu & ~IRQ_USER_BOUND) == cpun) { 3065 (void) apic_rebind_all(irq_ptr, 3066 irq_ptr->airq_cpu, 1); 3067 } 3068 } 3069 } 3070 } 3071 3072 /* 3073 * apic_introp_xlate() replaces apic_translate_irq() and is 3074 * called only from apic_intr_ops(). With the new ADII framework, 3075 * the priority can no longer be retrived through i_ddi_get_intrspec(). 3076 * It has to be passed in from the caller. 3077 */ 3078 int 3079 apic_introp_xlate(dev_info_t *dip, struct intrspec *ispec, int type) 3080 { 3081 char dev_type[16]; 3082 int dev_len, pci_irq, newirq, bustype, devid, busid, i; 3083 int irqno = ispec->intrspec_vec; 3084 ddi_acc_handle_t cfg_handle; 3085 uchar_t ipin; 3086 struct apic_io_intr *intrp; 3087 iflag_t intr_flag; 3088 APIC_HEADER *hp; 3089 MADT_INTERRUPT_OVERRIDE *isop; 3090 apic_irq_t *airqp; 3091 3092 DDI_INTR_IMPLDBG((CE_CONT, "apic_introp_xlate: dip=0x%p name=%s " 3093 "type=%d irqno=0x%x\n", (void *)dip, ddi_get_name(dip), type, 3094 irqno)); 3095 3096 if (DDI_INTR_IS_MSI_OR_MSIX(type)) { 3097 if ((airqp = apic_find_irq(dip, ispec, type)) != NULL) 3098 return (apic_vector_to_irq[airqp->airq_vector]); 3099 return (apic_setup_irq_table(dip, irqno, NULL, ispec, 3100 NULL, type)); 3101 } 3102 3103 bustype = 0; 3104 3105 /* check if we have already translated this irq */ 3106 mutex_enter(&airq_mutex); 3107 newirq = apic_min_device_irq; 3108 for (; newirq <= apic_max_device_irq; newirq++) { 3109 airqp = apic_irq_table[newirq]; 3110 while (airqp) { 3111 if ((airqp->airq_dip == dip) && 3112 (airqp->airq_origirq == irqno) && 3113 (airqp->airq_mps_intr_index != FREE_INDEX)) { 3114 3115 mutex_exit(&airq_mutex); 3116 return (VIRTIRQ(newirq, airqp->airq_share_id)); 3117 } 3118 airqp = airqp->airq_next; 3119 } 3120 } 3121 mutex_exit(&airq_mutex); 3122 3123 if (apic_defconf) 3124 goto defconf; 3125 3126 if ((dip == NULL) || (!apic_irq_translate && !apic_enable_acpi)) 3127 goto nonpci; 3128 3129 dev_len = sizeof (dev_type); 3130 if (ddi_getlongprop_buf(DDI_DEV_T_ANY, ddi_get_parent(dip), 3131 DDI_PROP_DONTPASS, "device_type", (caddr_t)dev_type, 3132 &dev_len) != DDI_PROP_SUCCESS) { 3133 goto nonpci; 3134 } 3135 3136 if (strcmp(dev_type, "pci") == 0) { 3137 /* pci device */ 3138 if (acpica_get_bdf(dip, &busid, &devid, NULL) != 0) 3139 goto nonpci; 3140 if (busid == 0 && apic_pci_bus_total == 1) 3141 busid = (int)apic_single_pci_busid; 3142 3143 if (pci_config_setup(dip, &cfg_handle) != DDI_SUCCESS) 3144 goto nonpci; 3145 ipin = pci_config_get8(cfg_handle, PCI_CONF_IPIN) - PCI_INTA; 3146 pci_config_teardown(&cfg_handle); 3147 if (apic_enable_acpi && !apic_use_acpi_madt_only) { 3148 if (apic_acpi_translate_pci_irq(dip, busid, devid, 3149 ipin, &pci_irq, &intr_flag) != ACPI_PSM_SUCCESS) 3150 goto nonpci; 3151 3152 intr_flag.bustype = BUS_PCI; 3153 if ((newirq = apic_setup_irq_table(dip, pci_irq, NULL, 3154 ispec, &intr_flag, type)) == -1) 3155 goto nonpci; 3156 return (newirq); 3157 } else { 3158 pci_irq = ((devid & 0x1f) << 2) | (ipin & 0x3); 3159 if ((intrp = apic_find_io_intr_w_busid(pci_irq, busid)) 3160 == NULL) { 3161 if ((pci_irq = apic_handle_pci_pci_bridge(dip, 3162 devid, ipin, &intrp)) == -1) 3163 goto nonpci; 3164 } 3165 if ((newirq = apic_setup_irq_table(dip, pci_irq, intrp, 3166 ispec, NULL, type)) == -1) 3167 goto nonpci; 3168 return (newirq); 3169 } 3170 } else if (strcmp(dev_type, "isa") == 0) 3171 bustype = BUS_ISA; 3172 else if (strcmp(dev_type, "eisa") == 0) 3173 bustype = BUS_EISA; 3174 3175 nonpci: 3176 if (apic_enable_acpi && !apic_use_acpi_madt_only) { 3177 /* search iso entries first */ 3178 if (acpi_iso_cnt != 0) { 3179 hp = (APIC_HEADER *)acpi_isop; 3180 i = 0; 3181 while (i < acpi_iso_cnt) { 3182 if (hp->Type == APIC_XRUPT_OVERRIDE) { 3183 isop = (MADT_INTERRUPT_OVERRIDE *)hp; 3184 if (isop->Bus == 0 && 3185 isop->Source == irqno) { 3186 newirq = isop->Interrupt; 3187 intr_flag.intr_po = 3188 isop->Polarity; 3189 intr_flag.intr_el = 3190 isop->TriggerMode; 3191 intr_flag.bustype = BUS_ISA; 3192 3193 return (apic_setup_irq_table( 3194 dip, newirq, NULL, ispec, 3195 &intr_flag, type)); 3196 3197 } 3198 i++; 3199 } 3200 hp = (APIC_HEADER *)(((char *)hp) + 3201 hp->Length); 3202 } 3203 } 3204 intr_flag.intr_po = INTR_PO_ACTIVE_HIGH; 3205 intr_flag.intr_el = INTR_EL_EDGE; 3206 intr_flag.bustype = BUS_ISA; 3207 return (apic_setup_irq_table(dip, irqno, NULL, ispec, 3208 &intr_flag, type)); 3209 } else { 3210 if (bustype == 0) 3211 bustype = eisa_level_intr_mask ? BUS_EISA : BUS_ISA; 3212 for (i = 0; i < 2; i++) { 3213 if (((busid = apic_find_bus_id(bustype)) != -1) && 3214 ((intrp = apic_find_io_intr_w_busid(irqno, busid)) 3215 != NULL)) { 3216 if ((newirq = apic_setup_irq_table(dip, irqno, 3217 intrp, ispec, NULL, type)) != -1) { 3218 return (newirq); 3219 } 3220 goto defconf; 3221 } 3222 bustype = (bustype == BUS_EISA) ? BUS_ISA : BUS_EISA; 3223 } 3224 } 3225 3226 /* MPS default configuration */ 3227 defconf: 3228 newirq = apic_setup_irq_table(dip, irqno, NULL, ispec, NULL, type); 3229 if (newirq == -1) 3230 return (newirq); 3231 ASSERT(IRQINDEX(newirq) == irqno); 3232 ASSERT(apic_irq_table[irqno]); 3233 return (newirq); 3234 } 3235 3236 3237 3238 3239 3240 3241 /* 3242 * On machines with PCI-PCI bridges, a device behind a PCI-PCI bridge 3243 * needs special handling. We may need to chase up the device tree, 3244 * using the PCI-PCI Bridge specification's "rotating IPIN assumptions", 3245 * to find the IPIN at the root bus that relates to the IPIN on the 3246 * subsidiary bus (for ACPI or MP). We may, however, have an entry 3247 * in the MP table or the ACPI namespace for this device itself. 3248 * We handle both cases in the search below. 3249 */ 3250 /* this is the non-acpi version */ 3251 static int 3252 apic_handle_pci_pci_bridge(dev_info_t *idip, int child_devno, int child_ipin, 3253 struct apic_io_intr **intrp) 3254 { 3255 dev_info_t *dipp, *dip; 3256 int pci_irq; 3257 ddi_acc_handle_t cfg_handle; 3258 int bridge_devno, bridge_bus; 3259 int ipin; 3260 3261 dip = idip; 3262 3263 /*CONSTCOND*/ 3264 while (1) { 3265 if ((dipp = ddi_get_parent(dip)) == (dev_info_t *)NULL) 3266 return (-1); 3267 if ((pci_config_setup(dipp, &cfg_handle) == DDI_SUCCESS) && 3268 (pci_config_get8(cfg_handle, PCI_CONF_BASCLASS) == 3269 PCI_CLASS_BRIDGE) && (pci_config_get8(cfg_handle, 3270 PCI_CONF_SUBCLASS) == PCI_BRIDGE_PCI)) { 3271 pci_config_teardown(&cfg_handle); 3272 if (acpica_get_bdf(dipp, &bridge_bus, &bridge_devno, 3273 NULL) != 0) 3274 return (-1); 3275 /* 3276 * This is the rotating scheme that Compaq is using 3277 * and documented in the pci to pci spec. Also, if 3278 * the pci to pci bridge is behind another pci to 3279 * pci bridge, then it need to keep transversing 3280 * up until an interrupt entry is found or reach 3281 * the top of the tree 3282 */ 3283 ipin = (child_devno + child_ipin) % PCI_INTD; 3284 if (bridge_bus == 0 && apic_pci_bus_total == 1) 3285 bridge_bus = (int)apic_single_pci_busid; 3286 pci_irq = ((bridge_devno & 0x1f) << 2) | 3287 (ipin & 0x3); 3288 if ((*intrp = apic_find_io_intr_w_busid(pci_irq, 3289 bridge_bus)) != NULL) { 3290 return (pci_irq); 3291 } 3292 dip = dipp; 3293 child_devno = bridge_devno; 3294 child_ipin = ipin; 3295 } else 3296 return (-1); 3297 } 3298 /*LINTED: function will not fall off the bottom */ 3299 } 3300 3301 3302 3303 3304 static uchar_t 3305 acpi_find_ioapic(int irq) 3306 { 3307 int i; 3308 3309 for (i = 0; i < apic_io_max; i++) { 3310 if (irq >= apic_io_vectbase[i] && irq <= apic_io_vectend[i]) 3311 return (i); 3312 } 3313 return (0xFF); /* shouldn't happen */ 3314 } 3315 3316 /* 3317 * See if two irqs are compatible for sharing a vector. 3318 * Currently we only support sharing of PCI devices. 3319 */ 3320 static int 3321 acpi_intr_compatible(iflag_t iflag1, iflag_t iflag2) 3322 { 3323 uint_t level1, po1; 3324 uint_t level2, po2; 3325 3326 /* Assume active high by default */ 3327 po1 = 0; 3328 po2 = 0; 3329 3330 if (iflag1.bustype != iflag2.bustype || iflag1.bustype != BUS_PCI) 3331 return (0); 3332 3333 if (iflag1.intr_el == INTR_EL_CONFORM) 3334 level1 = AV_LEVEL; 3335 else 3336 level1 = (iflag1.intr_el == INTR_EL_LEVEL) ? AV_LEVEL : 0; 3337 3338 if (level1 && ((iflag1.intr_po == INTR_PO_ACTIVE_LOW) || 3339 (iflag1.intr_po == INTR_PO_CONFORM))) 3340 po1 = AV_ACTIVE_LOW; 3341 3342 if (iflag2.intr_el == INTR_EL_CONFORM) 3343 level2 = AV_LEVEL; 3344 else 3345 level2 = (iflag2.intr_el == INTR_EL_LEVEL) ? AV_LEVEL : 0; 3346 3347 if (level2 && ((iflag2.intr_po == INTR_PO_ACTIVE_LOW) || 3348 (iflag2.intr_po == INTR_PO_CONFORM))) 3349 po2 = AV_ACTIVE_LOW; 3350 3351 if ((level1 == level2) && (po1 == po2)) 3352 return (1); 3353 3354 return (0); 3355 } 3356 3357 /* 3358 * Attempt to share vector with someone else 3359 */ 3360 static int 3361 apic_share_vector(int irqno, iflag_t *intr_flagp, short intr_index, int ipl, 3362 uchar_t ioapicindex, uchar_t ipin, apic_irq_t **irqptrp) 3363 { 3364 #ifdef DEBUG 3365 apic_irq_t *tmpirqp = NULL; 3366 #endif /* DEBUG */ 3367 apic_irq_t *irqptr, dummyirq; 3368 int newirq, chosen_irq = -1, share = 127; 3369 int lowest, highest, i; 3370 uchar_t share_id; 3371 3372 DDI_INTR_IMPLDBG((CE_CONT, "apic_share_vector: irqno=0x%x " 3373 "intr_index=0x%x ipl=0x%x\n", irqno, intr_index, ipl)); 3374 3375 highest = apic_ipltopri[ipl] + APIC_VECTOR_MASK; 3376 lowest = apic_ipltopri[ipl-1] + APIC_VECTOR_PER_IPL; 3377 3378 if (highest < lowest) /* Both ipl and ipl-1 map to same pri */ 3379 lowest -= APIC_VECTOR_PER_IPL; 3380 dummyirq.airq_mps_intr_index = intr_index; 3381 dummyirq.airq_ioapicindex = ioapicindex; 3382 dummyirq.airq_intin_no = ipin; 3383 if (intr_flagp) 3384 dummyirq.airq_iflag = *intr_flagp; 3385 apic_record_rdt_entry(&dummyirq, irqno); 3386 for (i = lowest; i <= highest; i++) { 3387 newirq = apic_vector_to_irq[i]; 3388 if (newirq == APIC_RESV_IRQ) 3389 continue; 3390 irqptr = apic_irq_table[newirq]; 3391 3392 if ((dummyirq.airq_rdt_entry & 0xFF00) != 3393 (irqptr->airq_rdt_entry & 0xFF00)) 3394 /* not compatible */ 3395 continue; 3396 3397 if (irqptr->airq_share < share) { 3398 share = irqptr->airq_share; 3399 chosen_irq = newirq; 3400 } 3401 } 3402 if (chosen_irq != -1) { 3403 /* 3404 * Assign a share id which is free or which is larger 3405 * than the largest one. 3406 */ 3407 share_id = 1; 3408 mutex_enter(&airq_mutex); 3409 irqptr = apic_irq_table[chosen_irq]; 3410 while (irqptr) { 3411 if (irqptr->airq_mps_intr_index == FREE_INDEX) { 3412 share_id = irqptr->airq_share_id; 3413 break; 3414 } 3415 if (share_id <= irqptr->airq_share_id) 3416 share_id = irqptr->airq_share_id + 1; 3417 #ifdef DEBUG 3418 tmpirqp = irqptr; 3419 #endif /* DEBUG */ 3420 irqptr = irqptr->airq_next; 3421 } 3422 if (!irqptr) { 3423 irqptr = kmem_zalloc(sizeof (apic_irq_t), KM_SLEEP); 3424 irqptr->airq_temp_cpu = IRQ_UNINIT; 3425 irqptr->airq_next = 3426 apic_irq_table[chosen_irq]->airq_next; 3427 apic_irq_table[chosen_irq]->airq_next = irqptr; 3428 #ifdef DEBUG 3429 tmpirqp = apic_irq_table[chosen_irq]; 3430 #endif /* DEBUG */ 3431 } 3432 irqptr->airq_mps_intr_index = intr_index; 3433 irqptr->airq_ioapicindex = ioapicindex; 3434 irqptr->airq_intin_no = ipin; 3435 if (intr_flagp) 3436 irqptr->airq_iflag = *intr_flagp; 3437 irqptr->airq_vector = apic_irq_table[chosen_irq]->airq_vector; 3438 irqptr->airq_share_id = share_id; 3439 apic_record_rdt_entry(irqptr, irqno); 3440 *irqptrp = irqptr; 3441 #ifdef DEBUG 3442 /* shuffle the pointers to test apic_delspl path */ 3443 if (tmpirqp) { 3444 tmpirqp->airq_next = irqptr->airq_next; 3445 irqptr->airq_next = apic_irq_table[chosen_irq]; 3446 apic_irq_table[chosen_irq] = irqptr; 3447 } 3448 #endif /* DEBUG */ 3449 mutex_exit(&airq_mutex); 3450 return (VIRTIRQ(chosen_irq, share_id)); 3451 } 3452 return (-1); 3453 } 3454 3455 /* 3456 * 3457 */ 3458 static int 3459 apic_setup_irq_table(dev_info_t *dip, int irqno, struct apic_io_intr *intrp, 3460 struct intrspec *ispec, iflag_t *intr_flagp, int type) 3461 { 3462 int origirq = ispec->intrspec_vec; 3463 uchar_t ipl = ispec->intrspec_pri; 3464 int newirq, intr_index; 3465 uchar_t ipin, ioapic, ioapicindex, vector; 3466 apic_irq_t *irqptr; 3467 major_t major; 3468 dev_info_t *sdip; 3469 3470 DDI_INTR_IMPLDBG((CE_CONT, "apic_setup_irq_table: dip=0x%p type=%d " 3471 "irqno=0x%x origirq=0x%x\n", (void *)dip, type, irqno, origirq)); 3472 3473 ASSERT(ispec != NULL); 3474 3475 major = (dip != NULL) ? ddi_name_to_major(ddi_get_name(dip)) : 0; 3476 3477 if (DDI_INTR_IS_MSI_OR_MSIX(type)) { 3478 /* MSI/X doesn't need to setup ioapic stuffs */ 3479 ioapicindex = 0xff; 3480 ioapic = 0xff; 3481 ipin = (uchar_t)0xff; 3482 intr_index = (type == DDI_INTR_TYPE_MSI) ? MSI_INDEX : 3483 MSIX_INDEX; 3484 mutex_enter(&airq_mutex); 3485 if ((irqno = apic_allocate_irq(APIC_FIRST_FREE_IRQ)) == -1) { 3486 mutex_exit(&airq_mutex); 3487 /* need an irq for MSI/X to index into autovect[] */ 3488 cmn_err(CE_WARN, "No interrupt irq: %s instance %d", 3489 ddi_get_name(dip), ddi_get_instance(dip)); 3490 return (-1); 3491 } 3492 mutex_exit(&airq_mutex); 3493 3494 } else if (intrp != NULL) { 3495 intr_index = (int)(intrp - apic_io_intrp); 3496 ioapic = intrp->intr_destid; 3497 ipin = intrp->intr_destintin; 3498 /* Find ioapicindex. If destid was ALL, we will exit with 0. */ 3499 for (ioapicindex = apic_io_max - 1; ioapicindex; ioapicindex--) 3500 if (apic_io_id[ioapicindex] == ioapic) 3501 break; 3502 ASSERT((ioapic == apic_io_id[ioapicindex]) || 3503 (ioapic == INTR_ALL_APIC)); 3504 3505 /* check whether this intin# has been used by another irqno */ 3506 if ((newirq = apic_find_intin(ioapicindex, ipin)) != -1) { 3507 return (newirq); 3508 } 3509 3510 } else if (intr_flagp != NULL) { 3511 /* ACPI case */ 3512 intr_index = ACPI_INDEX; 3513 ioapicindex = acpi_find_ioapic(irqno); 3514 ASSERT(ioapicindex != 0xFF); 3515 ioapic = apic_io_id[ioapicindex]; 3516 ipin = irqno - apic_io_vectbase[ioapicindex]; 3517 if (apic_irq_table[irqno] && 3518 apic_irq_table[irqno]->airq_mps_intr_index == ACPI_INDEX) { 3519 ASSERT(apic_irq_table[irqno]->airq_intin_no == ipin && 3520 apic_irq_table[irqno]->airq_ioapicindex == 3521 ioapicindex); 3522 return (irqno); 3523 } 3524 3525 } else { 3526 /* default configuration */ 3527 ioapicindex = 0; 3528 ioapic = apic_io_id[ioapicindex]; 3529 ipin = (uchar_t)irqno; 3530 intr_index = DEFAULT_INDEX; 3531 } 3532 3533 if (ispec == NULL) { 3534 APIC_VERBOSE_IOAPIC((CE_WARN, "No intrspec for irqno = %x\n", 3535 irqno)); 3536 } else if ((vector = apic_allocate_vector(ipl, irqno, 0)) == 0) { 3537 if ((newirq = apic_share_vector(irqno, intr_flagp, intr_index, 3538 ipl, ioapicindex, ipin, &irqptr)) != -1) { 3539 irqptr->airq_ipl = ipl; 3540 irqptr->airq_origirq = (uchar_t)origirq; 3541 irqptr->airq_dip = dip; 3542 irqptr->airq_major = major; 3543 sdip = apic_irq_table[IRQINDEX(newirq)]->airq_dip; 3544 /* This is OK to do really */ 3545 if (sdip == NULL) { 3546 cmn_err(CE_WARN, "Sharing vectors: %s" 3547 " instance %d and SCI", 3548 ddi_get_name(dip), ddi_get_instance(dip)); 3549 } else { 3550 cmn_err(CE_WARN, "Sharing vectors: %s" 3551 " instance %d and %s instance %d", 3552 ddi_get_name(sdip), ddi_get_instance(sdip), 3553 ddi_get_name(dip), ddi_get_instance(dip)); 3554 } 3555 return (newirq); 3556 } 3557 /* try high priority allocation now that share has failed */ 3558 if ((vector = apic_allocate_vector(ipl, irqno, 1)) == 0) { 3559 cmn_err(CE_WARN, "No interrupt vector: %s instance %d", 3560 ddi_get_name(dip), ddi_get_instance(dip)); 3561 return (-1); 3562 } 3563 } 3564 3565 mutex_enter(&airq_mutex); 3566 if (apic_irq_table[irqno] == NULL) { 3567 irqptr = kmem_zalloc(sizeof (apic_irq_t), KM_SLEEP); 3568 irqptr->airq_temp_cpu = IRQ_UNINIT; 3569 apic_irq_table[irqno] = irqptr; 3570 } else { 3571 irqptr = apic_irq_table[irqno]; 3572 if (irqptr->airq_mps_intr_index != FREE_INDEX) { 3573 /* 3574 * The slot is used by another irqno, so allocate 3575 * a free irqno for this interrupt 3576 */ 3577 newirq = apic_allocate_irq(APIC_FIRST_FREE_IRQ); 3578 if (newirq == -1) { 3579 mutex_exit(&airq_mutex); 3580 return (-1); 3581 } 3582 irqno = newirq; 3583 irqptr = apic_irq_table[irqno]; 3584 if (irqptr == NULL) { 3585 irqptr = kmem_zalloc(sizeof (apic_irq_t), 3586 KM_SLEEP); 3587 irqptr->airq_temp_cpu = IRQ_UNINIT; 3588 apic_irq_table[irqno] = irqptr; 3589 } 3590 apic_modify_vector(vector, newirq); 3591 } 3592 } 3593 apic_max_device_irq = max(irqno, apic_max_device_irq); 3594 apic_min_device_irq = min(irqno, apic_min_device_irq); 3595 mutex_exit(&airq_mutex); 3596 irqptr->airq_ioapicindex = ioapicindex; 3597 irqptr->airq_intin_no = ipin; 3598 irqptr->airq_ipl = ipl; 3599 irqptr->airq_vector = vector; 3600 irqptr->airq_origirq = (uchar_t)origirq; 3601 irqptr->airq_share_id = 0; 3602 irqptr->airq_mps_intr_index = (short)intr_index; 3603 irqptr->airq_dip = dip; 3604 irqptr->airq_major = major; 3605 irqptr->airq_cpu = apic_bind_intr(dip, irqno, ioapic, ipin); 3606 if (intr_flagp) 3607 irqptr->airq_iflag = *intr_flagp; 3608 3609 if (!DDI_INTR_IS_MSI_OR_MSIX(type)) { 3610 /* setup I/O APIC entry for non-MSI/X interrupts */ 3611 apic_record_rdt_entry(irqptr, irqno); 3612 } 3613 return (irqno); 3614 } 3615 3616 /* 3617 * return the cpu to which this intr should be bound. 3618 * Check properties or any other mechanism to see if user wants it 3619 * bound to a specific CPU. If so, return the cpu id with high bit set. 3620 * If not, use the policy to choose a cpu and return the id. 3621 */ 3622 uchar_t 3623 apic_bind_intr(dev_info_t *dip, int irq, uchar_t ioapicid, uchar_t intin) 3624 { 3625 int instance, instno, prop_len, bind_cpu, count; 3626 uint_t i, rc; 3627 uchar_t cpu; 3628 major_t major; 3629 char *name, *drv_name, *prop_val, *cptr; 3630 char prop_name[32]; 3631 3632 3633 if (apic_intr_policy == INTR_LOWEST_PRIORITY) 3634 return (IRQ_UNBOUND); 3635 3636 drv_name = NULL; 3637 rc = DDI_PROP_NOT_FOUND; 3638 major = (major_t)-1; 3639 if (dip != NULL) { 3640 name = ddi_get_name(dip); 3641 major = ddi_name_to_major(name); 3642 drv_name = ddi_major_to_name(major); 3643 instance = ddi_get_instance(dip); 3644 if (apic_intr_policy == INTR_ROUND_ROBIN_WITH_AFFINITY) { 3645 i = apic_min_device_irq; 3646 for (; i <= apic_max_device_irq; i++) { 3647 3648 if ((i == irq) || (apic_irq_table[i] == NULL) || 3649 (apic_irq_table[i]->airq_mps_intr_index 3650 == FREE_INDEX)) 3651 continue; 3652 3653 if ((apic_irq_table[i]->airq_major == major) && 3654 (!(apic_irq_table[i]->airq_cpu & 3655 IRQ_USER_BOUND))) { 3656 3657 cpu = apic_irq_table[i]->airq_cpu; 3658 3659 cmn_err(CE_CONT, 3660 "!pcplusmp: %s (%s) instance #%d " 3661 "vector 0x%x ioapic 0x%x " 3662 "intin 0x%x is bound to cpu %d\n", 3663 name, drv_name, instance, irq, 3664 ioapicid, intin, cpu); 3665 return (cpu); 3666 } 3667 } 3668 } 3669 /* 3670 * search for "drvname"_intpt_bind_cpus property first, the 3671 * syntax of the property should be "a[,b,c,...]" where 3672 * instance 0 binds to cpu a, instance 1 binds to cpu b, 3673 * instance 3 binds to cpu c... 3674 * ddi_getlongprop() will search /option first, then / 3675 * if "drvname"_intpt_bind_cpus doesn't exist, then find 3676 * intpt_bind_cpus property. The syntax is the same, and 3677 * it applies to all the devices if its "drvname" specific 3678 * property doesn't exist 3679 */ 3680 (void) strcpy(prop_name, drv_name); 3681 (void) strcat(prop_name, "_intpt_bind_cpus"); 3682 rc = ddi_getlongprop(DDI_DEV_T_ANY, dip, 0, prop_name, 3683 (caddr_t)&prop_val, &prop_len); 3684 if (rc != DDI_PROP_SUCCESS) { 3685 rc = ddi_getlongprop(DDI_DEV_T_ANY, dip, 0, 3686 "intpt_bind_cpus", (caddr_t)&prop_val, &prop_len); 3687 } 3688 } 3689 if (rc == DDI_PROP_SUCCESS) { 3690 for (i = count = 0; i < (prop_len - 1); i++) 3691 if (prop_val[i] == ',') 3692 count++; 3693 if (prop_val[i-1] != ',') 3694 count++; 3695 /* 3696 * if somehow the binding instances defined in the 3697 * property are not enough for this instno., then 3698 * reuse the pattern for the next instance until 3699 * it reaches the requested instno 3700 */ 3701 instno = instance % count; 3702 i = 0; 3703 cptr = prop_val; 3704 while (i < instno) 3705 if (*cptr++ == ',') 3706 i++; 3707 bind_cpu = stoi(&cptr); 3708 kmem_free(prop_val, prop_len); 3709 /* if specific cpu is bogus, then default to cpu 0 */ 3710 if (bind_cpu >= apic_nproc) { 3711 cmn_err(CE_WARN, "pcplusmp: %s=%s: CPU %d not present", 3712 prop_name, prop_val, bind_cpu); 3713 bind_cpu = 0; 3714 } else { 3715 /* indicate that we are bound at user request */ 3716 bind_cpu |= IRQ_USER_BOUND; 3717 } 3718 /* 3719 * no need to check apic_cpus[].aci_status, if specific cpu is 3720 * not up, then post_cpu_start will handle it. 3721 */ 3722 } else { 3723 /* 3724 * We change bind_cpu only for every two calls 3725 * as most drivers still do 2 add_intrs for every 3726 * interrupt 3727 */ 3728 bind_cpu = (apic_next_bind_cpu++) / 2; 3729 if (bind_cpu >= apic_nproc) { 3730 apic_next_bind_cpu = 1; 3731 bind_cpu = 0; 3732 } 3733 } 3734 if (drv_name != NULL) 3735 cmn_err(CE_CONT, "!pcplusmp: %s (%s) instance %d " 3736 "vector 0x%x ioapic 0x%x intin 0x%x is bound to cpu %d\n", 3737 name, drv_name, instance, 3738 irq, ioapicid, intin, bind_cpu & ~IRQ_USER_BOUND); 3739 else 3740 cmn_err(CE_CONT, "!pcplusmp: " 3741 "vector 0x%x ioapic 0x%x intin 0x%x is bound to cpu %d\n", 3742 irq, ioapicid, intin, bind_cpu & ~IRQ_USER_BOUND); 3743 3744 return ((uchar_t)bind_cpu); 3745 } 3746 3747 static struct apic_io_intr * 3748 apic_find_io_intr_w_busid(int irqno, int busid) 3749 { 3750 struct apic_io_intr *intrp; 3751 3752 /* 3753 * It can have more than 1 entry with same source bus IRQ, 3754 * but unique with the source bus id 3755 */ 3756 intrp = apic_io_intrp; 3757 if (intrp != NULL) { 3758 while (intrp->intr_entry == APIC_IO_INTR_ENTRY) { 3759 if (intrp->intr_irq == irqno && 3760 intrp->intr_busid == busid && 3761 intrp->intr_type == IO_INTR_INT) 3762 return (intrp); 3763 intrp++; 3764 } 3765 } 3766 APIC_VERBOSE_IOAPIC((CE_NOTE, "Did not find io intr for irqno:" 3767 "busid %x:%x\n", irqno, busid)); 3768 return ((struct apic_io_intr *)NULL); 3769 } 3770 3771 3772 struct mps_bus_info { 3773 char *bus_name; 3774 int bus_id; 3775 } bus_info_array[] = { 3776 "ISA ", BUS_ISA, 3777 "PCI ", BUS_PCI, 3778 "EISA ", BUS_EISA, 3779 "XPRESS", BUS_XPRESS, 3780 "PCMCIA", BUS_PCMCIA, 3781 "VL ", BUS_VL, 3782 "CBUS ", BUS_CBUS, 3783 "CBUSII", BUS_CBUSII, 3784 "FUTURE", BUS_FUTURE, 3785 "INTERN", BUS_INTERN, 3786 "MBI ", BUS_MBI, 3787 "MBII ", BUS_MBII, 3788 "MPI ", BUS_MPI, 3789 "MPSA ", BUS_MPSA, 3790 "NUBUS ", BUS_NUBUS, 3791 "TC ", BUS_TC, 3792 "VME ", BUS_VME 3793 }; 3794 3795 static int 3796 apic_find_bus_type(char *bus) 3797 { 3798 int i = 0; 3799 3800 for (; i < sizeof (bus_info_array)/sizeof (struct mps_bus_info); i++) 3801 if (strncmp(bus, bus_info_array[i].bus_name, 3802 strlen(bus_info_array[i].bus_name)) == 0) 3803 return (bus_info_array[i].bus_id); 3804 APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus type for bus %s", bus)); 3805 return (0); 3806 } 3807 3808 static int 3809 apic_find_bus(int busid) 3810 { 3811 struct apic_bus *busp; 3812 3813 busp = apic_busp; 3814 while (busp->bus_entry == APIC_BUS_ENTRY) { 3815 if (busp->bus_id == busid) 3816 return (apic_find_bus_type((char *)&busp->bus_str1)); 3817 busp++; 3818 } 3819 APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus for bus id %x", busid)); 3820 return (0); 3821 } 3822 3823 static int 3824 apic_find_bus_id(int bustype) 3825 { 3826 struct apic_bus *busp; 3827 3828 busp = apic_busp; 3829 while (busp->bus_entry == APIC_BUS_ENTRY) { 3830 if (apic_find_bus_type((char *)&busp->bus_str1) == bustype) 3831 return (busp->bus_id); 3832 busp++; 3833 } 3834 APIC_VERBOSE_IOAPIC((CE_WARN, "Did not find bus id for bustype %x", 3835 bustype)); 3836 return (-1); 3837 } 3838 3839 /* 3840 * Check if a particular irq need to be reserved for any io_intr 3841 */ 3842 static struct apic_io_intr * 3843 apic_find_io_intr(int irqno) 3844 { 3845 struct apic_io_intr *intrp; 3846 3847 intrp = apic_io_intrp; 3848 if (intrp != NULL) { 3849 while (intrp->intr_entry == APIC_IO_INTR_ENTRY) { 3850 if (intrp->intr_irq == irqno && 3851 intrp->intr_type == IO_INTR_INT) 3852 return (intrp); 3853 intrp++; 3854 } 3855 } 3856 return ((struct apic_io_intr *)NULL); 3857 } 3858 3859 /* 3860 * Check if the given ioapicindex intin combination has already been assigned 3861 * an irq. If so return irqno. Else -1 3862 */ 3863 static int 3864 apic_find_intin(uchar_t ioapic, uchar_t intin) 3865 { 3866 apic_irq_t *irqptr; 3867 int i; 3868 3869 /* find ioapic and intin in the apic_irq_table[] and return the index */ 3870 for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) { 3871 irqptr = apic_irq_table[i]; 3872 while (irqptr) { 3873 if ((irqptr->airq_mps_intr_index >= 0) && 3874 (irqptr->airq_intin_no == intin) && 3875 (irqptr->airq_ioapicindex == ioapic)) { 3876 APIC_VERBOSE_IOAPIC((CE_NOTE, "!Found irq " 3877 "entry for ioapic:intin %x:%x " 3878 "shared interrupts ?", ioapic, intin)); 3879 return (i); 3880 } 3881 irqptr = irqptr->airq_next; 3882 } 3883 } 3884 return (-1); 3885 } 3886 3887 int 3888 apic_allocate_irq(int irq) 3889 { 3890 int freeirq, i; 3891 3892 if ((freeirq = apic_find_free_irq(irq, (APIC_RESV_IRQ - 1))) == -1) 3893 if ((freeirq = apic_find_free_irq(APIC_FIRST_FREE_IRQ, 3894 (irq - 1))) == -1) { 3895 /* 3896 * if BIOS really defines every single irq in the mps 3897 * table, then don't worry about conflicting with 3898 * them, just use any free slot in apic_irq_table 3899 */ 3900 for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) { 3901 if ((apic_irq_table[i] == NULL) || 3902 apic_irq_table[i]->airq_mps_intr_index == 3903 FREE_INDEX) { 3904 freeirq = i; 3905 break; 3906 } 3907 } 3908 if (freeirq == -1) { 3909 /* This shouldn't happen, but just in case */ 3910 cmn_err(CE_WARN, "pcplusmp: NO available IRQ"); 3911 return (-1); 3912 } 3913 } 3914 if (apic_irq_table[freeirq] == NULL) { 3915 apic_irq_table[freeirq] = 3916 kmem_zalloc(sizeof (apic_irq_t), KM_NOSLEEP); 3917 if (apic_irq_table[freeirq] == NULL) { 3918 cmn_err(CE_WARN, "pcplusmp: NO memory to allocate IRQ"); 3919 return (-1); 3920 } 3921 apic_irq_table[freeirq]->airq_mps_intr_index = FREE_INDEX; 3922 } 3923 return (freeirq); 3924 } 3925 3926 static int 3927 apic_find_free_irq(int start, int end) 3928 { 3929 int i; 3930 3931 for (i = start; i <= end; i++) 3932 /* Check if any I/O entry needs this IRQ */ 3933 if (apic_find_io_intr(i) == NULL) { 3934 /* Then see if it is free */ 3935 if ((apic_irq_table[i] == NULL) || 3936 (apic_irq_table[i]->airq_mps_intr_index == 3937 FREE_INDEX)) { 3938 return (i); 3939 } 3940 } 3941 return (-1); 3942 } 3943 3944 /* 3945 * Allocate a free vector for irq at ipl. Takes care of merging of multiple 3946 * IPLs into a single APIC level as well as stretching some IPLs onto multiple 3947 * levels. APIC_HI_PRI_VECTS interrupts are reserved for high priority 3948 * requests and allocated only when pri is set. 3949 */ 3950 static uchar_t 3951 apic_allocate_vector(int ipl, int irq, int pri) 3952 { 3953 int lowest, highest, i; 3954 3955 highest = apic_ipltopri[ipl] + APIC_VECTOR_MASK; 3956 lowest = apic_ipltopri[ipl - 1] + APIC_VECTOR_PER_IPL; 3957 3958 if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */ 3959 lowest -= APIC_VECTOR_PER_IPL; 3960 3961 #ifdef DEBUG 3962 if (apic_restrict_vector) /* for testing shared interrupt logic */ 3963 highest = lowest + apic_restrict_vector + APIC_HI_PRI_VECTS; 3964 #endif /* DEBUG */ 3965 if (pri == 0) 3966 highest -= APIC_HI_PRI_VECTS; 3967 3968 for (i = lowest; i < highest; i++) { 3969 if ((i == T_FASTTRAP) || (i == APIC_SPUR_INTR) || 3970 (i == T_SYSCALLINT) || (i == T_DTRACE_PROBE) || 3971 (i == T_DTRACE_RET)) 3972 continue; 3973 if (apic_vector_to_irq[i] == APIC_RESV_IRQ) { 3974 apic_vector_to_irq[i] = (uchar_t)irq; 3975 return (i); 3976 } 3977 } 3978 3979 return (0); 3980 } 3981 3982 static void 3983 apic_modify_vector(uchar_t vector, int irq) 3984 { 3985 apic_vector_to_irq[vector] = (uchar_t)irq; 3986 } 3987 3988 /* 3989 * Mark vector as being in the process of being deleted. Interrupts 3990 * may still come in on some CPU. The moment an interrupt comes with 3991 * the new vector, we know we can free the old one. Called only from 3992 * addspl and delspl with interrupts disabled. Because an interrupt 3993 * can be shared, but no interrupt from either device may come in, 3994 * we also use a timeout mechanism, which we arbitrarily set to 3995 * apic_revector_timeout microseconds. 3996 */ 3997 static void 3998 apic_mark_vector(uchar_t oldvector, uchar_t newvector) 3999 { 4000 int iflag = intr_clear(); 4001 lock_set(&apic_revector_lock); 4002 if (!apic_oldvec_to_newvec) { 4003 apic_oldvec_to_newvec = 4004 kmem_zalloc(sizeof (newvector) * APIC_MAX_VECTOR * 2, 4005 KM_NOSLEEP); 4006 4007 if (!apic_oldvec_to_newvec) { 4008 /* 4009 * This failure is not catastrophic. 4010 * But, the oldvec will never be freed. 4011 */ 4012 apic_error |= APIC_ERR_MARK_VECTOR_FAIL; 4013 lock_clear(&apic_revector_lock); 4014 intr_restore(iflag); 4015 return; 4016 } 4017 apic_newvec_to_oldvec = &apic_oldvec_to_newvec[APIC_MAX_VECTOR]; 4018 } 4019 4020 /* See if we already did this for drivers which do double addintrs */ 4021 if (apic_oldvec_to_newvec[oldvector] != newvector) { 4022 apic_oldvec_to_newvec[oldvector] = newvector; 4023 apic_newvec_to_oldvec[newvector] = oldvector; 4024 apic_revector_pending++; 4025 } 4026 lock_clear(&apic_revector_lock); 4027 intr_restore(iflag); 4028 (void) timeout(apic_xlate_vector_free_timeout_handler, 4029 (void *)(uintptr_t)oldvector, drv_usectohz(apic_revector_timeout)); 4030 } 4031 4032 /* 4033 * xlate_vector is called from intr_enter if revector_pending is set. 4034 * It will xlate it if needed and mark the old vector as free. 4035 */ 4036 static uchar_t 4037 apic_xlate_vector(uchar_t vector) 4038 { 4039 uchar_t newvector, oldvector = 0; 4040 4041 lock_set(&apic_revector_lock); 4042 /* Do we really need to do this ? */ 4043 if (!apic_revector_pending) { 4044 lock_clear(&apic_revector_lock); 4045 return (vector); 4046 } 4047 if ((newvector = apic_oldvec_to_newvec[vector]) != 0) 4048 oldvector = vector; 4049 else { 4050 /* 4051 * The incoming vector is new . See if a stale entry is 4052 * remaining 4053 */ 4054 if ((oldvector = apic_newvec_to_oldvec[vector]) != 0) 4055 newvector = vector; 4056 } 4057 4058 if (oldvector) { 4059 apic_revector_pending--; 4060 apic_oldvec_to_newvec[oldvector] = 0; 4061 apic_newvec_to_oldvec[newvector] = 0; 4062 apic_free_vector(oldvector); 4063 lock_clear(&apic_revector_lock); 4064 /* There could have been more than one reprogramming! */ 4065 return (apic_xlate_vector(newvector)); 4066 } 4067 lock_clear(&apic_revector_lock); 4068 return (vector); 4069 } 4070 4071 void 4072 apic_xlate_vector_free_timeout_handler(void *arg) 4073 { 4074 int iflag; 4075 uchar_t oldvector, newvector; 4076 4077 oldvector = (uchar_t)(uintptr_t)arg; 4078 iflag = intr_clear(); 4079 lock_set(&apic_revector_lock); 4080 if ((newvector = apic_oldvec_to_newvec[oldvector]) != 0) { 4081 apic_free_vector(oldvector); 4082 apic_oldvec_to_newvec[oldvector] = 0; 4083 apic_newvec_to_oldvec[newvector] = 0; 4084 apic_revector_pending--; 4085 } 4086 4087 lock_clear(&apic_revector_lock); 4088 intr_restore(iflag); 4089 } 4090 4091 4092 /* Mark vector as not being used by any irq */ 4093 static void 4094 apic_free_vector(uchar_t vector) 4095 { 4096 apic_vector_to_irq[vector] = APIC_RESV_IRQ; 4097 } 4098 4099 /* 4100 * compute the polarity, trigger mode and vector for programming into 4101 * the I/O apic and record in airq_rdt_entry. 4102 */ 4103 static void 4104 apic_record_rdt_entry(apic_irq_t *irqptr, int irq) 4105 { 4106 int ioapicindex, bus_type, vector; 4107 short intr_index; 4108 uint_t level, po, io_po; 4109 struct apic_io_intr *iointrp; 4110 4111 intr_index = irqptr->airq_mps_intr_index; 4112 DDI_INTR_IMPLDBG((CE_CONT, "apic_record_rdt_entry: intr_index=%d " 4113 "irq = 0x%x dip = 0x%p vector = 0x%x\n", intr_index, irq, 4114 (void *)irqptr->airq_dip, irqptr->airq_vector)); 4115 4116 if (intr_index == RESERVE_INDEX) { 4117 apic_error |= APIC_ERR_INVALID_INDEX; 4118 return; 4119 } else if (APIC_IS_MSI_OR_MSIX_INDEX(intr_index)) { 4120 return; 4121 } 4122 4123 vector = irqptr->airq_vector; 4124 ioapicindex = irqptr->airq_ioapicindex; 4125 /* Assume edge triggered by default */ 4126 level = 0; 4127 /* Assume active high by default */ 4128 po = 0; 4129 4130 if (intr_index == DEFAULT_INDEX || intr_index == FREE_INDEX) { 4131 ASSERT(irq < 16); 4132 if (eisa_level_intr_mask & (1 << irq)) 4133 level = AV_LEVEL; 4134 if (intr_index == FREE_INDEX && apic_defconf == 0) 4135 apic_error |= APIC_ERR_INVALID_INDEX; 4136 } else if (intr_index == ACPI_INDEX) { 4137 bus_type = irqptr->airq_iflag.bustype; 4138 if (irqptr->airq_iflag.intr_el == INTR_EL_CONFORM) { 4139 if (bus_type == BUS_PCI) 4140 level = AV_LEVEL; 4141 } else 4142 level = (irqptr->airq_iflag.intr_el == INTR_EL_LEVEL) ? 4143 AV_LEVEL : 0; 4144 if (level && 4145 ((irqptr->airq_iflag.intr_po == INTR_PO_ACTIVE_LOW) || 4146 (irqptr->airq_iflag.intr_po == INTR_PO_CONFORM && 4147 bus_type == BUS_PCI))) 4148 po = AV_ACTIVE_LOW; 4149 } else { 4150 iointrp = apic_io_intrp + intr_index; 4151 bus_type = apic_find_bus(iointrp->intr_busid); 4152 if (iointrp->intr_el == INTR_EL_CONFORM) { 4153 if ((irq < 16) && (eisa_level_intr_mask & (1 << irq))) 4154 level = AV_LEVEL; 4155 else if (bus_type == BUS_PCI) 4156 level = AV_LEVEL; 4157 } else 4158 level = (iointrp->intr_el == INTR_EL_LEVEL) ? 4159 AV_LEVEL : 0; 4160 if (level && ((iointrp->intr_po == INTR_PO_ACTIVE_LOW) || 4161 (iointrp->intr_po == INTR_PO_CONFORM && 4162 bus_type == BUS_PCI))) 4163 po = AV_ACTIVE_LOW; 4164 } 4165 if (level) 4166 apic_level_intr[irq] = 1; 4167 /* 4168 * The 82489DX External APIC cannot do active low polarity interrupts. 4169 */ 4170 if (po && (apic_io_ver[ioapicindex] != IOAPIC_VER_82489DX)) 4171 io_po = po; 4172 else 4173 io_po = 0; 4174 4175 if (apic_verbose & APIC_VERBOSE_IOAPIC_FLAG) 4176 printf("setio: ioapic=%x intin=%x level=%x po=%x vector=%x\n", 4177 ioapicindex, irqptr->airq_intin_no, level, io_po, vector); 4178 4179 irqptr->airq_rdt_entry = level|io_po|vector; 4180 } 4181 4182 /* 4183 * Call rebind to do the actual programming. 4184 */ 4185 static int 4186 apic_setup_io_intr(apic_irq_t *irqptr, int irq) 4187 { 4188 int rv; 4189 4190 if (rv = apic_rebind(irqptr, apic_irq_table[irq]->airq_cpu, 1, 4191 IMMEDIATE)) 4192 /* CPU is not up or interrupt is disabled. Fall back to 0 */ 4193 rv = apic_rebind(irqptr, 0, 1, IMMEDIATE); 4194 4195 return (rv); 4196 } 4197 4198 /* 4199 * Deferred reprogramming: Call apic_rebind to do the real work. 4200 */ 4201 static int 4202 apic_setup_io_intr_deferred(apic_irq_t *irqptr, int irq) 4203 { 4204 int rv; 4205 4206 if (rv = apic_rebind(irqptr, apic_irq_table[irq]->airq_cpu, 1, 4207 DEFERRED)) 4208 /* CPU is not up or interrupt is disabled. Fall back to 0 */ 4209 rv = apic_rebind(irqptr, 0, 1, DEFERRED); 4210 4211 return (rv); 4212 } 4213 4214 /* 4215 * Bind interrupt corresponding to irq_ptr to bind_cpu. acquire_lock 4216 * if false (0) means lock is already held (e.g: in rebind_all). 4217 */ 4218 static int 4219 apic_rebind(apic_irq_t *irq_ptr, int bind_cpu, int acquire_lock, int when) 4220 { 4221 int intin_no; 4222 volatile int32_t *ioapic; 4223 uchar_t airq_temp_cpu; 4224 apic_cpus_info_t *cpu_infop; 4225 int iflag; 4226 int which_irq = apic_vector_to_irq[irq_ptr->airq_vector]; 4227 4228 intin_no = irq_ptr->airq_intin_no; 4229 ioapic = apicioadr[irq_ptr->airq_ioapicindex]; 4230 airq_temp_cpu = irq_ptr->airq_temp_cpu; 4231 if (airq_temp_cpu != IRQ_UNINIT && airq_temp_cpu != IRQ_UNBOUND) { 4232 if (airq_temp_cpu & IRQ_USER_BOUND) 4233 /* Mask off high bit so it can be used as array index */ 4234 airq_temp_cpu &= ~IRQ_USER_BOUND; 4235 4236 ASSERT(airq_temp_cpu < apic_nproc); 4237 } 4238 4239 iflag = intr_clear(); 4240 4241 if (acquire_lock) 4242 lock_set(&apic_ioapic_lock); 4243 4244 /* 4245 * Can't bind to a CPU that's not online: 4246 */ 4247 cpu_infop = &apic_cpus[bind_cpu & ~IRQ_USER_BOUND]; 4248 if (!(cpu_infop->aci_status & APIC_CPU_INTR_ENABLE)) { 4249 4250 if (acquire_lock) 4251 lock_clear(&apic_ioapic_lock); 4252 4253 intr_restore(iflag); 4254 return (1); 4255 } 4256 4257 /* 4258 * If this is a deferred reprogramming attempt, ensure we have 4259 * not been passed stale data: 4260 */ 4261 if ((when == DEFERRED) && 4262 (apic_reprogram_info[which_irq].valid == 0)) { 4263 /* stale info, so just return */ 4264 if (acquire_lock) 4265 lock_clear(&apic_ioapic_lock); 4266 4267 intr_restore(iflag); 4268 return (0); 4269 } 4270 4271 /* 4272 * If this interrupt has been delivered to a CPU and that CPU 4273 * has not handled it yet, we cannot reprogram the IOAPIC now: 4274 */ 4275 if (!APIC_IS_MSI_OR_MSIX_INDEX(irq_ptr->airq_mps_intr_index) && 4276 apic_check_stuck_interrupt(irq_ptr, airq_temp_cpu, bind_cpu, 4277 ioapic, intin_no, which_irq) != 0) { 4278 4279 if (acquire_lock) 4280 lock_clear(&apic_ioapic_lock); 4281 4282 intr_restore(iflag); 4283 return (0); 4284 } 4285 4286 /* 4287 * NOTE: We do not unmask the RDT here, as an interrupt MAY still 4288 * come in before we have a chance to reprogram it below. The 4289 * reprogramming below will simultaneously change and unmask the 4290 * RDT entry. 4291 */ 4292 4293 if ((uchar_t)bind_cpu == IRQ_UNBOUND) { 4294 /* Write the RDT entry -- no specific CPU binding */ 4295 WRITE_IOAPIC_RDT_ENTRY_HIGH_DWORD(ioapic, intin_no, AV_TOALL); 4296 4297 if (airq_temp_cpu != IRQ_UNINIT && airq_temp_cpu != IRQ_UNBOUND) 4298 apic_cpus[airq_temp_cpu].aci_temp_bound--; 4299 4300 /* Write the vector, trigger, and polarity portion of the RDT */ 4301 WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no, 4302 AV_LDEST | AV_LOPRI | irq_ptr->airq_rdt_entry); 4303 if (acquire_lock) 4304 lock_clear(&apic_ioapic_lock); 4305 irq_ptr->airq_temp_cpu = IRQ_UNBOUND; 4306 intr_restore(iflag); 4307 return (0); 4308 } 4309 4310 if (bind_cpu & IRQ_USER_BOUND) { 4311 cpu_infop->aci_bound++; 4312 } else { 4313 cpu_infop->aci_temp_bound++; 4314 } 4315 ASSERT((bind_cpu & ~IRQ_USER_BOUND) < apic_nproc); 4316 if (!APIC_IS_MSI_OR_MSIX_INDEX(irq_ptr->airq_mps_intr_index)) { 4317 /* Write the RDT entry -- bind to a specific CPU: */ 4318 WRITE_IOAPIC_RDT_ENTRY_HIGH_DWORD(ioapic, intin_no, 4319 cpu_infop->aci_local_id << APIC_ID_BIT_OFFSET); 4320 } 4321 if ((airq_temp_cpu != IRQ_UNBOUND) && (airq_temp_cpu != IRQ_UNINIT)) { 4322 apic_cpus[airq_temp_cpu].aci_temp_bound--; 4323 } 4324 if (!APIC_IS_MSI_OR_MSIX_INDEX(irq_ptr->airq_mps_intr_index)) { 4325 /* Write the vector, trigger, and polarity portion of the RDT */ 4326 WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no, 4327 AV_PDEST | AV_FIXED | irq_ptr->airq_rdt_entry); 4328 } else { 4329 if (irq_ptr->airq_ioapicindex == irq_ptr->airq_origirq) { 4330 /* first one */ 4331 DDI_INTR_IMPLDBG((CE_CONT, "apic_rebind: call " 4332 "apic_pci_msi_enable_vector\n")); 4333 if (apic_pci_msi_enable_vector(irq_ptr->airq_dip, 4334 (irq_ptr->airq_mps_intr_index == MSI_INDEX) ? 4335 DDI_INTR_TYPE_MSI : DDI_INTR_TYPE_MSIX, which_irq, 4336 irq_ptr->airq_vector, irq_ptr->airq_intin_no, 4337 cpu_infop->aci_local_id) != PSM_SUCCESS) { 4338 cmn_err(CE_WARN, "pcplusmp: " 4339 "apic_pci_msi_enable_vector " 4340 "returned PSM_FAILURE"); 4341 } 4342 } 4343 if ((irq_ptr->airq_ioapicindex + irq_ptr->airq_intin_no - 1) == 4344 irq_ptr->airq_origirq) { /* last one */ 4345 DDI_INTR_IMPLDBG((CE_CONT, "apic_rebind: call " 4346 "pci_msi_enable_mode\n")); 4347 if (pci_msi_enable_mode(irq_ptr->airq_dip, 4348 (irq_ptr->airq_mps_intr_index == MSI_INDEX) ? 4349 DDI_INTR_TYPE_MSI : DDI_INTR_TYPE_MSIX, 4350 which_irq) != DDI_SUCCESS) { 4351 DDI_INTR_IMPLDBG((CE_CONT, "pcplusmp: " 4352 "pci_msi_enable failed\n")); 4353 (void) pci_msi_unconfigure(irq_ptr->airq_dip, 4354 (irq_ptr->airq_mps_intr_index == MSI_INDEX) ? 4355 DDI_INTR_TYPE_MSI : DDI_INTR_TYPE_MSIX, 4356 which_irq); 4357 } 4358 } 4359 } 4360 if (acquire_lock) 4361 lock_clear(&apic_ioapic_lock); 4362 irq_ptr->airq_temp_cpu = (uchar_t)bind_cpu; 4363 apic_redist_cpu_skip &= ~(1 << (bind_cpu & ~IRQ_USER_BOUND)); 4364 intr_restore(iflag); 4365 return (0); 4366 } 4367 4368 /* 4369 * Checks to see if the IOAPIC interrupt entry specified has its Remote IRR 4370 * bit set. Sets up a timeout to perform the reprogramming at a later time 4371 * if it cannot wait for the Remote IRR bit to clear (or if waiting did not 4372 * result in the bit's clearing). 4373 * 4374 * This function will mask the RDT entry if the Remote IRR bit is set. 4375 * 4376 * Returns non-zero if the caller should defer IOAPIC reprogramming. 4377 */ 4378 static int 4379 apic_check_stuck_interrupt(apic_irq_t *irq_ptr, int old_bind_cpu, 4380 int new_bind_cpu, volatile int32_t *ioapic, int intin_no, int which_irq) 4381 { 4382 int32_t rdt_entry; 4383 int waited; 4384 4385 /* Mask the RDT entry, but only if it's a level-triggered interrupt */ 4386 rdt_entry = READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no); 4387 if ((rdt_entry & (AV_LEVEL|AV_MASK)) == AV_LEVEL) { 4388 4389 /* Mask it */ 4390 WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no, 4391 AV_MASK | rdt_entry); 4392 } 4393 4394 /* 4395 * Wait for the delivery pending bit to clear. 4396 */ 4397 if ((READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no) & 4398 (AV_LEVEL|AV_PENDING)) == (AV_LEVEL|AV_PENDING)) { 4399 4400 /* 4401 * If we're still waiting on the delivery of this interrupt, 4402 * continue to wait here until it is delivered (this should be 4403 * a very small amount of time, but include a timeout just in 4404 * case). 4405 */ 4406 for (waited = 0; waited < apic_max_usecs_clear_pending; 4407 waited += APIC_USECS_PER_WAIT_INTERVAL) { 4408 if ((READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no) 4409 & AV_PENDING) == 0) { 4410 break; 4411 } 4412 drv_usecwait(APIC_USECS_PER_WAIT_INTERVAL); 4413 } 4414 4415 if ((READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no) & 4416 AV_PENDING) != 0) { 4417 cmn_err(CE_WARN, "!IOAPIC %d intin %d: Could not " 4418 "deliver interrupt to local APIC within " 4419 "%d usecs.", irq_ptr->airq_ioapicindex, 4420 irq_ptr->airq_intin_no, 4421 apic_max_usecs_clear_pending); 4422 } 4423 } 4424 4425 /* 4426 * If the remote IRR bit is set, then the interrupt has been sent 4427 * to a CPU for processing. We have no choice but to wait for 4428 * that CPU to process the interrupt, at which point the remote IRR 4429 * bit will be cleared. 4430 */ 4431 if ((READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no) & 4432 (AV_LEVEL|AV_REMOTE_IRR)) == (AV_LEVEL|AV_REMOTE_IRR)) { 4433 4434 /* 4435 * If the CPU that this RDT is bound to is NOT the current 4436 * CPU, wait until that CPU handles the interrupt and ACKs 4437 * it. If this interrupt is not bound to any CPU (that is, 4438 * if it's bound to the logical destination of "anyone"), it 4439 * may have been delivered to the current CPU so handle that 4440 * case by deferring the reprogramming (below). 4441 */ 4442 kpreempt_disable(); 4443 if ((old_bind_cpu != IRQ_UNBOUND) && 4444 (old_bind_cpu != IRQ_UNINIT) && 4445 (old_bind_cpu != psm_get_cpu_id())) { 4446 for (waited = 0; waited < apic_max_usecs_clear_pending; 4447 waited += APIC_USECS_PER_WAIT_INTERVAL) { 4448 if ((READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, 4449 intin_no) & AV_REMOTE_IRR) == 0) { 4450 4451 /* Clear the reprogramming state: */ 4452 lock_set(&apic_ioapic_reprogram_lock); 4453 4454 apic_reprogram_info[which_irq].valid 4455 = 0; 4456 apic_reprogram_info[which_irq].bindcpu 4457 = 0; 4458 apic_reprogram_info[which_irq].timeouts 4459 = 0; 4460 4461 lock_clear(&apic_ioapic_reprogram_lock); 4462 4463 /* Remote IRR has cleared! */ 4464 kpreempt_enable(); 4465 return (0); 4466 } 4467 drv_usecwait(APIC_USECS_PER_WAIT_INTERVAL); 4468 } 4469 } 4470 kpreempt_enable(); 4471 4472 /* 4473 * If we waited and the Remote IRR bit is still not cleared, 4474 * AND if we've invoked the timeout APIC_REPROGRAM_MAX_TIMEOUTS 4475 * times for this interrupt, try the last-ditch workarounds: 4476 */ 4477 if (apic_reprogram_info[which_irq].timeouts >= 4478 APIC_REPROGRAM_MAX_TIMEOUTS) { 4479 4480 if ((READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, intin_no) 4481 & AV_REMOTE_IRR) != 0) { 4482 /* 4483 * Trying to clear the bit through normal 4484 * channels has failed. So as a last-ditch 4485 * effort, try to set the trigger mode to 4486 * edge, then to level. This has been 4487 * observed to work on many systems. 4488 */ 4489 WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, 4490 intin_no, 4491 READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, 4492 intin_no) & ~AV_LEVEL); 4493 4494 WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, 4495 intin_no, 4496 READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, 4497 intin_no) | AV_LEVEL); 4498 4499 /* 4500 * If the bit's STILL set, declare total and 4501 * utter failure 4502 */ 4503 if ((READ_IOAPIC_RDT_ENTRY_LOW_DWORD(ioapic, 4504 intin_no) & AV_REMOTE_IRR) != 0) { 4505 cmn_err(CE_WARN, "!IOAPIC %d intin %d: " 4506 "Remote IRR failed to reset " 4507 "within %d usecs. Interrupts to " 4508 "this pin may cease to function.", 4509 irq_ptr->airq_ioapicindex, 4510 irq_ptr->airq_intin_no, 4511 apic_max_usecs_clear_pending); 4512 } 4513 } 4514 /* Clear the reprogramming state: */ 4515 lock_set(&apic_ioapic_reprogram_lock); 4516 4517 apic_reprogram_info[which_irq].valid = 0; 4518 apic_reprogram_info[which_irq].bindcpu = 0; 4519 apic_reprogram_info[which_irq].timeouts = 0; 4520 4521 lock_clear(&apic_ioapic_reprogram_lock); 4522 } else { 4523 #ifdef DEBUG 4524 cmn_err(CE_WARN, "Deferring reprogramming of irq %d", 4525 which_irq); 4526 #endif /* DEBUG */ 4527 /* 4528 * If waiting for the Remote IRR bit (above) didn't 4529 * allow it to clear, defer the reprogramming: 4530 */ 4531 lock_set(&apic_ioapic_reprogram_lock); 4532 4533 apic_reprogram_info[which_irq].valid = 1; 4534 apic_reprogram_info[which_irq].bindcpu = new_bind_cpu; 4535 apic_reprogram_info[which_irq].timeouts++; 4536 4537 lock_clear(&apic_ioapic_reprogram_lock); 4538 4539 /* Fire up a timeout to handle this later */ 4540 (void) timeout(apic_reprogram_timeout_handler, 4541 (void *) 0, 4542 drv_usectohz(APIC_REPROGRAM_TIMEOUT_DELAY)); 4543 4544 /* Inform caller to defer IOAPIC programming: */ 4545 return (1); 4546 } 4547 } 4548 return (0); 4549 } 4550 4551 /* 4552 * Timeout handler that performs the APIC reprogramming 4553 */ 4554 /*ARGSUSED*/ 4555 static void 4556 apic_reprogram_timeout_handler(void *arg) 4557 { 4558 /*LINTED: set but not used in function*/ 4559 int i, result; 4560 4561 /* Serialize access to this function */ 4562 mutex_enter(&apic_reprogram_timeout_mutex); 4563 4564 /* 4565 * For each entry in the reprogramming state that's valid, 4566 * try the reprogramming again: 4567 */ 4568 for (i = 0; i < APIC_MAX_VECTOR; i++) { 4569 if (apic_reprogram_info[i].valid == 0) 4570 continue; 4571 /* 4572 * Though we can't really do anything about errors 4573 * at this point, keep track of them for reporting. 4574 * Note that it is very possible for apic_setup_io_intr 4575 * to re-register this very timeout if the Remote IRR bit 4576 * has not yet cleared. 4577 */ 4578 result = apic_setup_io_intr_deferred(apic_irq_table[i], i); 4579 4580 #ifdef DEBUG 4581 if (result) 4582 cmn_err(CE_WARN, "apic_reprogram_timeout: " 4583 "apic_setup_io_intr returned nonzero for " 4584 "irq=%d!", i); 4585 #endif /* DEBUG */ 4586 } 4587 4588 mutex_exit(&apic_reprogram_timeout_mutex); 4589 } 4590 4591 4592 /* 4593 * Called to migrate all interrupts at an irq to another cpu. safe 4594 * if true means we are not being called from an interrupt 4595 * context and hence it is safe to do a lock_set. If false 4596 * do only a lock_try and return failure ( non 0 ) if we cannot get it 4597 */ 4598 static int 4599 apic_rebind_all(apic_irq_t *irq_ptr, int bind_cpu, int safe) 4600 { 4601 apic_irq_t *irqptr = irq_ptr; 4602 int retval = 0; 4603 int iflag; 4604 4605 iflag = intr_clear(); 4606 if (!safe) { 4607 if (lock_try(&apic_ioapic_lock) == 0) { 4608 intr_restore(iflag); 4609 return (1); 4610 } 4611 } else 4612 lock_set(&apic_ioapic_lock); 4613 4614 while (irqptr) { 4615 if (irqptr->airq_temp_cpu != IRQ_UNINIT) 4616 retval |= apic_rebind(irqptr, bind_cpu, 0, IMMEDIATE); 4617 irqptr = irqptr->airq_next; 4618 } 4619 lock_clear(&apic_ioapic_lock); 4620 intr_restore(iflag); 4621 return (retval); 4622 } 4623 4624 /* 4625 * apic_intr_redistribute does all the messy computations for identifying 4626 * which interrupt to move to which CPU. Currently we do just one interrupt 4627 * at a time. This reduces the time we spent doing all this within clock 4628 * interrupt. When it is done in idle, we could do more than 1. 4629 * First we find the most busy and the most free CPU (time in ISR only) 4630 * skipping those CPUs that has been identified as being ineligible (cpu_skip) 4631 * Then we look for IRQs which are closest to the difference between the 4632 * most busy CPU and the average ISR load. We try to find one whose load 4633 * is less than difference.If none exists, then we chose one larger than the 4634 * difference, provided it does not make the most idle CPU worse than the 4635 * most busy one. In the end, we clear all the busy fields for CPUs. For 4636 * IRQs, they are cleared as they are scanned. 4637 */ 4638 static void 4639 apic_intr_redistribute() 4640 { 4641 int busiest_cpu, most_free_cpu; 4642 int cpu_free, cpu_busy, max_busy, min_busy; 4643 int min_free, diff; 4644 int average_busy, cpus_online; 4645 int i, busy; 4646 apic_cpus_info_t *cpu_infop; 4647 apic_irq_t *min_busy_irq = NULL; 4648 apic_irq_t *max_busy_irq = NULL; 4649 4650 busiest_cpu = most_free_cpu = -1; 4651 cpu_free = cpu_busy = max_busy = average_busy = 0; 4652 min_free = apic_sample_factor_redistribution; 4653 cpus_online = 0; 4654 /* 4655 * Below we will check for CPU_INTR_ENABLE, bound, temp_bound, temp_cpu 4656 * without ioapic_lock. That is OK as we are just doing statistical 4657 * sampling anyway and any inaccuracy now will get corrected next time 4658 * The call to rebind which actually changes things will make sure 4659 * we are consistent. 4660 */ 4661 for (i = 0; i < apic_nproc; i++) { 4662 if (!(apic_redist_cpu_skip & (1 << i)) && 4663 (apic_cpus[i].aci_status & APIC_CPU_INTR_ENABLE)) { 4664 4665 cpu_infop = &apic_cpus[i]; 4666 /* 4667 * If no unbound interrupts or only 1 total on this 4668 * CPU, skip 4669 */ 4670 if (!cpu_infop->aci_temp_bound || 4671 (cpu_infop->aci_bound + cpu_infop->aci_temp_bound) 4672 == 1) { 4673 apic_redist_cpu_skip |= 1 << i; 4674 continue; 4675 } 4676 4677 busy = cpu_infop->aci_busy; 4678 average_busy += busy; 4679 cpus_online++; 4680 if (max_busy < busy) { 4681 max_busy = busy; 4682 busiest_cpu = i; 4683 } 4684 if (min_free > busy) { 4685 min_free = busy; 4686 most_free_cpu = i; 4687 } 4688 if (busy > apic_int_busy_mark) { 4689 cpu_busy |= 1 << i; 4690 } else { 4691 if (busy < apic_int_free_mark) 4692 cpu_free |= 1 << i; 4693 } 4694 } 4695 } 4696 if ((cpu_busy && cpu_free) || 4697 (max_busy >= (min_free + apic_diff_for_redistribution))) { 4698 4699 apic_num_imbalance++; 4700 #ifdef DEBUG 4701 if (apic_verbose & APIC_VERBOSE_IOAPIC_FLAG) { 4702 prom_printf( 4703 "redistribute busy=%x free=%x max=%x min=%x", 4704 cpu_busy, cpu_free, max_busy, min_free); 4705 } 4706 #endif /* DEBUG */ 4707 4708 4709 average_busy /= cpus_online; 4710 4711 diff = max_busy - average_busy; 4712 min_busy = max_busy; /* start with the max possible value */ 4713 max_busy = 0; 4714 min_busy_irq = max_busy_irq = NULL; 4715 i = apic_min_device_irq; 4716 for (; i < apic_max_device_irq; i++) { 4717 apic_irq_t *irq_ptr; 4718 /* Change to linked list per CPU ? */ 4719 if ((irq_ptr = apic_irq_table[i]) == NULL) 4720 continue; 4721 /* Check for irq_busy & decide which one to move */ 4722 /* Also zero them for next round */ 4723 if ((irq_ptr->airq_temp_cpu == busiest_cpu) && 4724 irq_ptr->airq_busy) { 4725 if (irq_ptr->airq_busy < diff) { 4726 /* 4727 * Check for least busy CPU, 4728 * best fit or what ? 4729 */ 4730 if (max_busy < irq_ptr->airq_busy) { 4731 /* 4732 * Most busy within the 4733 * required differential 4734 */ 4735 max_busy = irq_ptr->airq_busy; 4736 max_busy_irq = irq_ptr; 4737 } 4738 } else { 4739 if (min_busy > irq_ptr->airq_busy) { 4740 /* 4741 * least busy, but more than 4742 * the reqd diff 4743 */ 4744 if (min_busy < 4745 (diff + average_busy - 4746 min_free)) { 4747 /* 4748 * Making sure new cpu 4749 * will not end up 4750 * worse 4751 */ 4752 min_busy = 4753 irq_ptr->airq_busy; 4754 4755 min_busy_irq = irq_ptr; 4756 } 4757 } 4758 } 4759 } 4760 irq_ptr->airq_busy = 0; 4761 } 4762 4763 if (max_busy_irq != NULL) { 4764 #ifdef DEBUG 4765 if (apic_verbose & APIC_VERBOSE_IOAPIC_FLAG) { 4766 prom_printf("rebinding %x to %x", 4767 max_busy_irq->airq_vector, most_free_cpu); 4768 } 4769 #endif /* DEBUG */ 4770 if (apic_rebind_all(max_busy_irq, most_free_cpu, 0) 4771 == 0) 4772 /* Make change permenant */ 4773 max_busy_irq->airq_cpu = (uchar_t)most_free_cpu; 4774 } else if (min_busy_irq != NULL) { 4775 #ifdef DEBUG 4776 if (apic_verbose & APIC_VERBOSE_IOAPIC_FLAG) { 4777 prom_printf("rebinding %x to %x", 4778 min_busy_irq->airq_vector, most_free_cpu); 4779 } 4780 #endif /* DEBUG */ 4781 4782 if (apic_rebind_all(min_busy_irq, most_free_cpu, 0) == 4783 0) 4784 /* Make change permenant */ 4785 min_busy_irq->airq_cpu = (uchar_t)most_free_cpu; 4786 } else { 4787 if (cpu_busy != (1 << busiest_cpu)) { 4788 apic_redist_cpu_skip |= 1 << busiest_cpu; 4789 /* 4790 * We leave cpu_skip set so that next time we 4791 * can choose another cpu 4792 */ 4793 } 4794 } 4795 apic_num_rebind++; 4796 } else { 4797 /* 4798 * found nothing. Could be that we skipped over valid CPUs 4799 * or we have balanced everything. If we had a variable 4800 * ticks_for_redistribution, it could be increased here. 4801 * apic_int_busy, int_free etc would also need to be 4802 * changed. 4803 */ 4804 if (apic_redist_cpu_skip) 4805 apic_redist_cpu_skip = 0; 4806 } 4807 for (i = 0; i < apic_nproc; i++) { 4808 apic_cpus[i].aci_busy = 0; 4809 } 4810 } 4811 4812 static void 4813 apic_cleanup_busy() 4814 { 4815 int i; 4816 apic_irq_t *irq_ptr; 4817 4818 for (i = 0; i < apic_nproc; i++) { 4819 apic_cpus[i].aci_busy = 0; 4820 } 4821 4822 for (i = apic_min_device_irq; i < apic_max_device_irq; i++) { 4823 if ((irq_ptr = apic_irq_table[i]) != NULL) 4824 irq_ptr->airq_busy = 0; 4825 } 4826 apic_skipped_redistribute = 0; 4827 } 4828 4829 4830 /* 4831 * This function will reprogram the timer. 4832 * 4833 * When in oneshot mode the argument is the absolute time in future to 4834 * generate the interrupt at. 4835 * 4836 * When in periodic mode, the argument is the interval at which the 4837 * interrupts should be generated. There is no need to support the periodic 4838 * mode timer change at this time. 4839 */ 4840 static void 4841 apic_timer_reprogram(hrtime_t time) 4842 { 4843 hrtime_t now; 4844 uint_t ticks; 4845 4846 /* 4847 * We should be called from high PIL context (CBE_HIGH_PIL), 4848 * so kpreempt is disabled. 4849 */ 4850 4851 if (!apic_oneshot) { 4852 /* time is the interval for periodic mode */ 4853 ticks = (uint_t)((time) / apic_nsec_per_tick); 4854 } else { 4855 /* one shot mode */ 4856 4857 now = gethrtime(); 4858 4859 if (time <= now) { 4860 /* 4861 * requested to generate an interrupt in the past 4862 * generate an interrupt as soon as possible 4863 */ 4864 ticks = apic_min_timer_ticks; 4865 } else if ((time - now) > apic_nsec_max) { 4866 /* 4867 * requested to generate an interrupt at a time 4868 * further than what we are capable of. Set to max 4869 * the hardware can handle 4870 */ 4871 4872 ticks = APIC_MAXVAL; 4873 #ifdef DEBUG 4874 cmn_err(CE_CONT, "apic_timer_reprogram, request at" 4875 " %lld too far in future, current time" 4876 " %lld \n", time, now); 4877 #endif /* DEBUG */ 4878 } else 4879 ticks = (uint_t)((time - now) / apic_nsec_per_tick); 4880 } 4881 4882 if (ticks < apic_min_timer_ticks) 4883 ticks = apic_min_timer_ticks; 4884 4885 apicadr[APIC_INIT_COUNT] = ticks; 4886 4887 } 4888 4889 /* 4890 * This function will enable timer interrupts. 4891 */ 4892 static void 4893 apic_timer_enable(void) 4894 { 4895 /* 4896 * We should be Called from high PIL context (CBE_HIGH_PIL), 4897 * so kpreempt is disabled. 4898 */ 4899 4900 if (!apic_oneshot) 4901 apicadr[APIC_LOCAL_TIMER] = 4902 (apic_clkvect + APIC_BASE_VECT) | AV_TIME; 4903 else { 4904 /* one shot */ 4905 apicadr[APIC_LOCAL_TIMER] = (apic_clkvect + APIC_BASE_VECT); 4906 } 4907 } 4908 4909 /* 4910 * This function will disable timer interrupts. 4911 */ 4912 static void 4913 apic_timer_disable(void) 4914 { 4915 /* 4916 * We should be Called from high PIL context (CBE_HIGH_PIL), 4917 * so kpreempt is disabled. 4918 */ 4919 4920 apicadr[APIC_LOCAL_TIMER] = (apic_clkvect + APIC_BASE_VECT) | AV_MASK; 4921 } 4922 4923 4924 cyclic_id_t apic_cyclic_id; 4925 4926 /* 4927 * If this module needs to be a consumer of cyclic subsystem, they 4928 * can be added here, since at this time kernel cyclic subsystem is initialized 4929 * argument is not currently used, and is reserved for future. 4930 */ 4931 static void 4932 apic_post_cyclic_setup(void *arg) 4933 { 4934 _NOTE(ARGUNUSED(arg)) 4935 cyc_handler_t hdlr; 4936 cyc_time_t when; 4937 4938 /* cpu_lock is held */ 4939 4940 /* set up cyclics for intr redistribution */ 4941 4942 /* 4943 * In peridoc mode intr redistribution processing is done in 4944 * apic_intr_enter during clk intr processing 4945 */ 4946 if (!apic_oneshot) 4947 return; 4948 4949 hdlr.cyh_level = CY_LOW_LEVEL; 4950 hdlr.cyh_func = (cyc_func_t)apic_redistribute_compute; 4951 hdlr.cyh_arg = NULL; 4952 4953 when.cyt_when = 0; 4954 when.cyt_interval = apic_redistribute_sample_interval; 4955 apic_cyclic_id = cyclic_add(&hdlr, &when); 4956 4957 4958 } 4959 4960 static void 4961 apic_redistribute_compute(void) 4962 { 4963 int i, j, max_busy; 4964 4965 if (apic_enable_dynamic_migration) { 4966 if (++apic_nticks == apic_sample_factor_redistribution) { 4967 /* 4968 * Time to call apic_intr_redistribute(). 4969 * reset apic_nticks. This will cause max_busy 4970 * to be calculated below and if it is more than 4971 * apic_int_busy, we will do the whole thing 4972 */ 4973 apic_nticks = 0; 4974 } 4975 max_busy = 0; 4976 for (i = 0; i < apic_nproc; i++) { 4977 4978 /* 4979 * Check if curipl is non zero & if ISR is in 4980 * progress 4981 */ 4982 if (((j = apic_cpus[i].aci_curipl) != 0) && 4983 (apic_cpus[i].aci_ISR_in_progress & (1 << j))) { 4984 4985 int irq; 4986 apic_cpus[i].aci_busy++; 4987 irq = apic_cpus[i].aci_current[j]; 4988 apic_irq_table[irq]->airq_busy++; 4989 } 4990 4991 if (!apic_nticks && 4992 (apic_cpus[i].aci_busy > max_busy)) 4993 max_busy = apic_cpus[i].aci_busy; 4994 } 4995 if (!apic_nticks) { 4996 if (max_busy > apic_int_busy_mark) { 4997 /* 4998 * We could make the following check be 4999 * skipped > 1 in which case, we get a 5000 * redistribution at half the busy mark (due to 5001 * double interval). Need to be able to collect 5002 * more empirical data to decide if that is a 5003 * good strategy. Punt for now. 5004 */ 5005 if (apic_skipped_redistribute) 5006 apic_cleanup_busy(); 5007 else 5008 apic_intr_redistribute(); 5009 } else 5010 apic_skipped_redistribute++; 5011 } 5012 } 5013 } 5014 5015 5016 static int 5017 apic_acpi_translate_pci_irq(dev_info_t *dip, int busid, int devid, 5018 int ipin, int *pci_irqp, iflag_t *intr_flagp) 5019 { 5020 5021 int status; 5022 acpi_psm_lnk_t acpipsmlnk; 5023 5024 if ((status = acpi_get_irq_cache_ent(busid, devid, ipin, pci_irqp, 5025 intr_flagp)) == ACPI_PSM_SUCCESS) { 5026 APIC_VERBOSE_IRQ((CE_CONT, "!pcplusmp: Found irqno %d " 5027 "from cache for device %s, instance #%d\n", *pci_irqp, 5028 ddi_get_name(dip), ddi_get_instance(dip))); 5029 return (status); 5030 } 5031 5032 bzero(&acpipsmlnk, sizeof (acpi_psm_lnk_t)); 5033 5034 if ((status = acpi_translate_pci_irq(dip, ipin, pci_irqp, intr_flagp, 5035 &acpipsmlnk)) == ACPI_PSM_FAILURE) { 5036 APIC_VERBOSE_IRQ((CE_WARN, "pcplusmp: " 5037 " acpi_translate_pci_irq failed for device %s, instance" 5038 " #%d", ddi_get_name(dip), ddi_get_instance(dip))); 5039 return (status); 5040 } 5041 5042 if (status == ACPI_PSM_PARTIAL && acpipsmlnk.lnkobj != NULL) { 5043 status = apic_acpi_irq_configure(&acpipsmlnk, dip, pci_irqp, 5044 intr_flagp); 5045 if (status != ACPI_PSM_SUCCESS) { 5046 status = acpi_get_current_irq_resource(&acpipsmlnk, 5047 pci_irqp, intr_flagp); 5048 } 5049 } 5050 5051 if (status == ACPI_PSM_SUCCESS) { 5052 acpi_new_irq_cache_ent(busid, devid, ipin, *pci_irqp, 5053 intr_flagp, &acpipsmlnk); 5054 5055 APIC_VERBOSE_IRQ((CE_CONT, "pcplusmp: [ACPI] " 5056 "new irq %d for device %s, instance #%d\n", 5057 *pci_irqp, ddi_get_name(dip), ddi_get_instance(dip))); 5058 } 5059 5060 return (status); 5061 } 5062 5063 /* 5064 * Configures the irq for the interrupt link device identified by 5065 * acpipsmlnkp. 5066 * 5067 * Gets the current and the list of possible irq settings for the 5068 * device. If apic_unconditional_srs is not set, and the current 5069 * resource setting is in the list of possible irq settings, 5070 * current irq resource setting is passed to the caller. 5071 * 5072 * Otherwise, picks an irq number from the list of possible irq 5073 * settings, and sets the irq of the device to this value. 5074 * If prefer_crs is set, among a set of irq numbers in the list that have 5075 * the least number of devices sharing the interrupt, we pick current irq 5076 * resource setting if it is a member of this set. 5077 * 5078 * Passes the irq number in the value pointed to by pci_irqp, and 5079 * polarity and sensitivity in the structure pointed to by dipintrflagp 5080 * to the caller. 5081 * 5082 * Note that if setting the irq resource failed, but successfuly obtained 5083 * the current irq resource settings, passes the current irq resources 5084 * and considers it a success. 5085 * 5086 * Returns: 5087 * ACPI_PSM_SUCCESS on success. 5088 * 5089 * ACPI_PSM_FAILURE if an error occured during the configuration or 5090 * if a suitable irq was not found for this device, or if setting the 5091 * irq resource and obtaining the current resource fails. 5092 * 5093 */ 5094 static int 5095 apic_acpi_irq_configure(acpi_psm_lnk_t *acpipsmlnkp, dev_info_t *dip, 5096 int *pci_irqp, iflag_t *dipintr_flagp) 5097 { 5098 5099 int i, min_share, foundnow, done = 0; 5100 int32_t irq; 5101 int32_t share_irq = -1; 5102 int32_t chosen_irq = -1; 5103 int cur_irq = -1; 5104 acpi_irqlist_t *irqlistp; 5105 acpi_irqlist_t *irqlistent; 5106 5107 if ((acpi_get_possible_irq_resources(acpipsmlnkp, &irqlistp)) 5108 == ACPI_PSM_FAILURE) { 5109 APIC_VERBOSE_IRQ((CE_WARN, "!pcplusmp: Unable to determine " 5110 "or assign IRQ for device %s, instance #%d: The system was " 5111 "unable to get the list of potential IRQs from ACPI.", 5112 ddi_get_name(dip), ddi_get_instance(dip))); 5113 5114 return (ACPI_PSM_FAILURE); 5115 } 5116 5117 if ((acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq, 5118 dipintr_flagp) == ACPI_PSM_SUCCESS) && (!apic_unconditional_srs) && 5119 (cur_irq > 0)) { 5120 /* 5121 * If an IRQ is set in CRS and that IRQ exists in the set 5122 * returned from _PRS, return that IRQ, otherwise print 5123 * a warning 5124 */ 5125 5126 if (acpi_irqlist_find_irq(irqlistp, cur_irq, NULL) 5127 == ACPI_PSM_SUCCESS) { 5128 5129 acpi_free_irqlist(irqlistp); 5130 ASSERT(pci_irqp != NULL); 5131 *pci_irqp = cur_irq; 5132 return (ACPI_PSM_SUCCESS); 5133 } 5134 5135 APIC_VERBOSE_IRQ((CE_WARN, "!pcplusmp: Could not find the " 5136 "current irq %d for device %s, instance #%d in ACPI's " 5137 "list of possible irqs for this device. Picking one from " 5138 " the latter list.", cur_irq, ddi_get_name(dip), 5139 ddi_get_instance(dip))); 5140 } 5141 5142 irqlistent = irqlistp; 5143 min_share = 255; 5144 5145 while (irqlistent != NULL) { 5146 irqlistent->intr_flags.bustype = BUS_PCI; 5147 5148 for (foundnow = 0, i = 0; i < irqlistent->num_irqs; i++) { 5149 5150 irq = irqlistent->irqs[i]; 5151 5152 if ((irq < 16) && (apic_reserved_irqlist[irq])) 5153 continue; 5154 5155 if (irq == 0) { 5156 /* invalid irq number */ 5157 continue; 5158 } 5159 5160 if ((apic_irq_table[irq] == NULL) || 5161 (apic_irq_table[irq]->airq_dip == dip)) { 5162 chosen_irq = irq; 5163 foundnow = 1; 5164 /* 5165 * If we do not prefer current irq from crs 5166 * or if we do and this irq is the same as 5167 * current irq from crs, this is the one 5168 * to pick. 5169 */ 5170 if (!(apic_prefer_crs) || (irq == cur_irq)) { 5171 done = 1; 5172 break; 5173 } 5174 continue; 5175 } 5176 5177 if (irqlistent->intr_flags.intr_el == INTR_EL_EDGE) 5178 continue; 5179 5180 if (!acpi_intr_compatible(irqlistent->intr_flags, 5181 apic_irq_table[irq]->airq_iflag)) 5182 continue; 5183 5184 if ((apic_irq_table[irq]->airq_share < min_share) || 5185 ((apic_irq_table[irq]->airq_share == min_share) && 5186 (cur_irq == irq) && (apic_prefer_crs))) { 5187 min_share = apic_irq_table[irq]->airq_share; 5188 share_irq = irq; 5189 foundnow = 1; 5190 } 5191 } 5192 5193 /* 5194 * If we found an IRQ in the inner loop this time, save the 5195 * details from the irqlist for later use. 5196 */ 5197 if (foundnow && ((chosen_irq != -1) || (share_irq != -1))) { 5198 /* 5199 * Copy the acpi_prs_private_t and flags from this 5200 * irq list entry, since we found an irq from this 5201 * entry. 5202 */ 5203 acpipsmlnkp->acpi_prs_prv = irqlistent->acpi_prs_prv; 5204 *dipintr_flagp = irqlistent->intr_flags; 5205 } 5206 5207 if (done) 5208 break; 5209 5210 /* Go to the next irqlist entry */ 5211 irqlistent = irqlistent->next; 5212 } 5213 5214 5215 acpi_free_irqlist(irqlistp); 5216 if (chosen_irq != -1) 5217 irq = chosen_irq; 5218 else if (share_irq != -1) 5219 irq = share_irq; 5220 else { 5221 APIC_VERBOSE_IRQ((CE_WARN, "!pcplusmp: Could not find a " 5222 "suitable irq from the list of possible irqs for device " 5223 "%s, instance #%d in ACPI's list of possible irqs", 5224 ddi_get_name(dip), ddi_get_instance(dip))); 5225 return (ACPI_PSM_FAILURE); 5226 } 5227 5228 APIC_VERBOSE_IRQ((CE_CONT, "!pcplusmp: Setting irq %d for device %s " 5229 "instance #%d\n", irq, ddi_get_name(dip), ddi_get_instance(dip))); 5230 5231 if ((acpi_set_irq_resource(acpipsmlnkp, irq)) == ACPI_PSM_SUCCESS) { 5232 /* 5233 * setting irq was successful, check to make sure CRS 5234 * reflects that. If CRS does not agree with what we 5235 * set, return the irq that was set. 5236 */ 5237 5238 if (acpi_get_current_irq_resource(acpipsmlnkp, &cur_irq, 5239 dipintr_flagp) == ACPI_PSM_SUCCESS) { 5240 5241 if (cur_irq != irq) 5242 APIC_VERBOSE_IRQ((CE_WARN, "!pcplusmp: " 5243 "IRQ resource set (irqno %d) for device %s " 5244 "instance #%d, differs from current " 5245 "setting irqno %d", 5246 irq, ddi_get_name(dip), 5247 ddi_get_instance(dip), cur_irq)); 5248 } 5249 5250 /* 5251 * return the irq that was set, and not what CRS reports, 5252 * since CRS has been seen to be bogus on some systems 5253 */ 5254 cur_irq = irq; 5255 } else { 5256 APIC_VERBOSE_IRQ((CE_WARN, "!pcplusmp: set resource irq %d " 5257 "failed for device %s instance #%d", 5258 irq, ddi_get_name(dip), ddi_get_instance(dip))); 5259 5260 if (cur_irq == -1) 5261 return (ACPI_PSM_FAILURE); 5262 } 5263 5264 ASSERT(pci_irqp != NULL); 5265 *pci_irqp = cur_irq; 5266 return (ACPI_PSM_SUCCESS); 5267 } 5268