apic.c revision e7c3cdae834138050f4382e8c1b001c64e5a9be7
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
* Copyright (c) 2010, Intel Corporation.
* All rights reserved.
*/
/*
* PSMI 1.1 extensions are supported only in 2.6 and later versions.
* PSMI 1.2 extensions are supported only in 2.7 and later versions.
* PSMI 1.3 and 1.4 extensions are supported in Solaris 10.
* PSMI 1.5 extensions are supported in Solaris Nevada.
* PSMI 1.6 extensions are supported in Solaris Nevada.
* PSMI 1.7 extensions are supported in Solaris Nevada.
*/
#define PSMI_1_7
#include <sys/processor.h>
#include <sys/smp_impldefs.h>
#include <sys/psm_common.h>
#include <sys/ddi_impldefs.h>
#include <sys/x86_archext.h>
#include <sys/cpc_impl.h>
#include <sys/archsystm.h>
#include <sys/machsystm.h>
#include <sys/sysmacros.h>
#include <sys/rm_platter.h>
#include <sys/privregs.h>
#include <sys/pci_intr_lib.h>
#include <sys/dditypes.h>
/*
* Local Function Prototypes
*/
static void apic_init_intr();
/*
* standard MP entries
*/
static int apic_probe();
static int apic_clkinit();
static int apic_getclkirq(int ipl);
static hrtime_t apic_gettime();
static hrtime_t apic_gethrtime();
static void apic_init();
static void apic_picinit(void);
static int apic_post_cpu_start(void);
static void apic_setspl(int ipl);
static void x2apic_setspl(int ipl);
static void apic_timer_enable(void);
static void apic_timer_disable(void);
static void apic_post_cyclic_setup(void *arg);
static void apic_intrmap_init(int apic_mode);
static int apic_oneshot = 0;
/* Now the ones for Dynamic Interrupt distribution */
int apic_enable_dynamic_migration = 0;
extern int apic_have_32bit_cr8;
/*
* These variables are frequently accessed in apic_intr_enter(),
* apic_intr_exit and apic_setspl, so group them together
*/
int apic_clkvect;
/* vector at which error interrupts come in */
int apic_errvect;
int apic_enable_error_intr = 1;
int apic_error_display_delay = 100;
/* vector at which performance counter overflow interrupts come in */
int apic_cpcovf_vect;
int apic_enable_cpcovf_intr = 1;
/* maximum loop count when sending Start IPIs. */
int apic_sipi_max_loop_count = 0x1000;
/* vector at which CMCI interrupts come in */
int apic_cmci_vect;
extern int cmi_enable_cmci;
extern void cmi_cmci_trap(void);
static int cmci_cpu_setup_registered;
/* number of CPUs in power-on transition state */
static int apic_poweron_cnt = 0;
static lock_t apic_mode_switch_lock;
/*
* The following vector assignments influence the value of ipltopri and
* vectortoipl. Note that vectors 0 - 0x1f are not used. We can program
* idle to 0 and IPL 0 to 0xf to differentiate idle in case
* we care to do so in future. Note some IPLs which are rarely used
* will share the vector ranges and heavily used IPLs (5 and 6) have
* a wide range.
*
* This array is used to initialize apic_ipls[] (in apic_init()).
*
* IPL Vector range. as passed to intr_enter
* 0 none.
* 1,2,3 0x20-0x2f 0x0-0xf
* 4 0x30-0x3f 0x10-0x1f
* 5 0x40-0x5f 0x20-0x3f
* 6 0x60-0x7f 0x40-0x5f
* 7,8,9 0x80-0x8f 0x60-0x6f
* 10 0x90-0x9f 0x70-0x7f
* 11 0xa0-0xaf 0x80-0x8f
* ... ...
* 15 0xe0-0xef 0xc0-0xcf
* 15 0xf0-0xff 0xd0-0xdf
*/
3, 4, 5, 5, 6, 6, 9, 10, 11, 12, 13, 14, 15, 15
};
/*
* The ipl of an ISR at vector X is apic_vectortoipl[X>>4]
* NOTE that this is vector as passed into intr_enter which is
* programmed vector - 0x20 (APIC_BASE_VECT)
*/
/* The taskpri to be programmed into apic to mask given ipl */
#if defined(__amd64)
#endif
/*
* Correlation of the hardware vector to the IPL in use, initialized
* from apic_vectortoipl[] in apic_init(). The final IPLs may not correlate
* to the IPLs in apic_vectortoipl on some systems that share interrupt lines
* connected to errata-stricken IOAPICs
*/
/*
* Patchable global variables.
*/
int apic_forceload = 0;
/* 1 - use gettime() for performance */
int apic_flat_model = 0; /* 0 - clustered. 1 - flat */
int apic_enable_hwsoftint = 0; /* 0 - disable, 1 - enable */
int apic_panic_on_nmi = 0;
int apic_panic_on_apic_error = 0;
int apic_verbose = 0;
/* minimum number of timer ticks to program to */
int apic_min_timer_ticks = 1;
/*
* Local static data
*/
(int (*)(int))NULL, /* psm_softlvl_to_irq */
(void (*)(int))NULL, /* psm_set_softintr */
(void (*)(void))NULL, /* psm_hrtimeinit */
(void (*)(int, char *))NULL, /* psm_notify_error */
(void (*)(int))NULL, /* psm_notify_func */
apic_intr_ops, /* Advanced DDI Interrupt framework */
apic_state, /* save, restore apic state for S3 */
apic_cpu_ops, /* CPU control interface. */
};
static struct psm_info apic_psm_info = {
PSM_INFO_VER01_7, /* version */
PSM_OWN_EXCLUSIVE, /* ownership */
APIC_PCPLUSMP_NAME, /* machine name */
"pcplusmp v1.4 compatible",
};
static void *apic_hdlp;
#ifdef DEBUG
int apic_debug = 0;
int apic_restrict_vector = 0;
int apic_debug_msgbufindex = 0;
#endif /* DEBUG */
/* Flag to indicate that we need to shut down all processors */
static uint_t apic_shutdown_processors;
uint_t apic_nsec_per_intr = 0;
/*
* apic_let_idle_redistribute can have the following values:
* 0 - If clock decremented it from 1 to 0, clock has to call redistribute.
* apic_redistribute_lock prevents multiple idle cpus from redistributing
*/
int apic_num_idle_redistributions = 0;
static int apic_let_idle_redistribute = 0;
static uint_t apic_nticks = 0;
static uint_t apic_skipped_redistribute = 0;
/* to gather intr data and redistribute */
static void apic_redistribute_compute(void);
static uint_t last_count_read = 0;
static lock_t apic_gethrtime_lock;
volatile int apic_hrtime_stamp = 0;
volatile hrtime_t apic_nsec_since_boot = 0;
static uint_t apic_hertz_count;
static hrtime_t apic_nsec_max;
static hrtime_t apic_last_hrtime = 0;
int apic_hrtime_error = 0;
int apic_remote_hrterr = 0;
int apic_num_nmis = 0;
int apic_apic_error = 0;
int apic_num_apic_errors = 0;
int apic_num_cksum_errors = 0;
int apic_error = 0;
static int apic_cmos_ssb_set = 0;
/* use to make sure only one cpu handles the nmi */
static lock_t apic_nmi_lock;
/* use to make sure only one cpu handles the error interrupt */
static lock_t apic_error_lock;
static struct {
} aspen_bmc[] = {
};
static struct {
int port;
} sitka_bmc[] = {
};
/* Patchable global variables. */
int apic_kmdb_on_nmi = 0; /* 0 - no, 1 - yes enter kmdb */
/* default apic ops without interrupt remapping */
static apic_intrmap_ops_t apic_nointrmap_ops = {
(int (*)(int))return_instr,
(void (*)(int))return_instr,
(void (*)(apic_irq_t *))return_instr,
(void (*)(apic_irq_t *, void *))return_instr,
(void (*)(apic_irq_t *))return_instr,
};
/*
* This is the loadable module wrapper
*/
int
_init(void)
{
if (apic_coarse_hrtime)
}
int
_fini(void)
{
}
int
{
}
static int
{
}
void
{
int i;
int j = 1;
for (i = 0; i < (APIC_AVAIL_VECTOR / APIC_VECTOR_PER_IPL); i++) {
/* get to highest vector at the same ipl */
continue;
for (; j <= apic_vectortoipl[i]; j++) {
apic_ipltopri[j] = (i << APIC_IPL_SHIFT) +
}
}
for (; j < MAXIPL + 1; j++)
/* fill up any empty ipltopri slots */
#if defined(__amd64)
/*
* Make cpu-specific interrupt info point to cr8pri vector
*/
for (i = 0; i <= MAXIPL; i++)
#else
if (cpuid_have_cr8access(CPU))
apic_have_32bit_cr8 = 1;
#endif /* __amd64 */
}
/*
* handler for APIC Error interrupt. Just print a warning and continue
*/
static int
{
uint_t i;
/*
* We need to write before read as per 7.4.17 of system prog manual.
* We do both and or the results to be safe
*/
/*
* Clear the APIC error status (do this on all cpus that enter here)
* (two writes are required due to the semantics of accessing the
* error status register.)
*/
/*
* Prevent more than 1 CPU from handling error interrupt causing
* double printing (interleave of characters from multiple
* CPU's when using prom_printf)
*/
if (lock_try(&apic_error_lock) == 0)
if (error) {
#if DEBUG
if (apic_debug)
debug_enter("pcplusmp: APIC Error interrupt received");
#endif /* DEBUG */
"APIC Error interrupt on CPU %d. Status = %x\n",
psm_get_cpu_id(), error);
else {
if ((error & ~APIC_CS_ERRORS) == 0) {
/* cksum error only */
apic_apic_error |= error;
} else {
/*
* prom_printf is the best shot we have of
* something which is problem free from
*/
prom_printf("APIC Error interrupt on CPU %d. "
"Status 0 = %x, Status 1 = %x\n",
apic_apic_error |= error;
for (i = 0; i < apic_error_display_delay; i++) {
tenmicrosec();
}
/*
* provide more delay next time limited to
* roughly 1 clock tick time
*/
if (apic_error_display_delay < 500)
apic_error_display_delay *= 2;
}
}
return (DDI_INTR_CLAIMED);
} else {
return (DDI_INTR_UNCLAIMED);
}
/* NOTREACHED */
}
/*
* Turn off the mask bit in the performance counter Local Vector Table entry.
*/
static void
apic_cpcovf_mask_clear(void)
{
}
/*ARGSUSED*/
static int
{
return (0);
}
/*ARGSUSED*/
static int
{
return (0);
}
/*ARGSUSED*/
static int
{
switch (what) {
case CPU_ON:
break;
case CPU_OFF:
break;
default:
break;
}
return (0);
}
static void
{
if (apic_mode == LOCAL_APIC) {
/*
* We are running APIC in MMIO mode.
*/
if (apic_flat_model) {
} else {
}
AV_HIGH_ORDER >> cpun);
}
if (apic_directed_EOI_supported()) {
/*
* Setting the 12th bit in the Spurious Interrupt Vector
* Register suppresses broadcast EOIs generated by the local
* APIC. The suppression of broadcast EOIs happens only when
* interrupts are level-triggered.
*/
}
/* need to enable APIC before unmasking NMI */
/*
* Presence of an invalid vector with delivery mode AV_FIXED can
* cause an error interrupt, even if the entry is masked...so
* write a valid vector to LVT entries along with the mask bit
*/
/* All APICs have timer and LINT0/1 */
/*
* On integrated APICs, the number of LVT entries is
* 'Max LVT entry' + 1; on 82489DX's (non-integrated
* APICs), nlvt is "3" (LINT0, LINT1, and timer)
*/
nlvt = 3;
} else {
0xFF) + 1;
}
if (nlvt >= 5) {
/* Enable performance counter overflow interrupt */
if (apic_enable_cpcovf_intr) {
if (apic_cpcovf_vect == 0) {
int ipl = APIC_PCINT_IPL;
}
}
}
if (nlvt >= 6) {
/* Only mask TM intr if the BIOS apparently doesn't use it */
}
}
/* Enable error interrupt */
if (apic_errvect == 0) {
/*
* Not PSMI compliant, but we are going to merge
* with ON anyway
*/
}
}
/* Enable CMCI interrupt */
if (cmi_enable_cmci) {
if (cmci_cpu_setup_registered == 0) {
}
if (apic_cmci_vect == 0) {
int ipl = 0x2;
}
}
}
static void
{
/* local intr reg 0 */
/* disable NMI */
/* and error interrupt */
/* and perf counter intr */
}
static void
apic_picinit(void)
{
int i, j;
/*
* Initialize and enable interrupt remapping before apic
* hardware initialization
*/
/*
* On UniSys Model 6520, the BIOS leaves vector 0x20 isr
* bit on without clearing it with EOI. Since softint
* uses vector 0x20 to interrupt itself, so softint will
* not work on this machine. In order to fix this problem
* a check is made to verify all the isr bits are clear.
* If not, EOIs are issued to clear the bits.
*/
for (i = 7; i >= 1; i--) {
if (isr != 0)
for (j = 0; ((j < 32) && (isr != 0)); j++)
if (isr & (1 << j)) {
APIC_EOI_REG, 0);
isr &= ~(1 << j);
}
}
/* set a flag so we know we have run apic_picinit() */
apic_picinit_called = 1;
picsetup(); /* initialise the 8259 */
/* add nmi handler - least priority nmi handler */
/*
* Check for directed-EOI capability in the local APIC.
*/
if (apic_directed_EOI_supported() == 1) {
}
/* enable apic mode if imcr present */
if (apic_imcrp) {
}
}
static void
{
int loop_count;
/*
* Interrupts on current CPU will be disabled during the
* steps in order to avoid unwanted side effects from
* executing interrupt handlers on a problematic BIOS.
*/
iflag = intr_clear();
if (start) {
}
/*
* According to X2APIC specification in section '2.3.5.1' of
* Interrupt Command Register Semantics, the semantics of
* programming the Interrupt Command Register to dispatch an interrupt
* is simplified. A single MSR write to the 64-bit ICR is required
* for dispatching an interrupt. Specifically, with the 64-bit MSR
* interface to ICR, system software is not required to check the
* status of the delivery status bit prior to writing to the ICR
* to send an IPI. With the removal of the Delivery Status bit,
* system software no longer has a reason to read the ICR. It remains
* readable only to aid in debugging.
*/
#ifdef DEBUG
#else
if (apic_mode == LOCAL_APIC) {
}
#endif /* DEBUG */
/* for integrated - make sure there is one INIT IPI in buffer */
/* for external - it will wake up the cpu */
/* If only 1 CPU is installed, PENDING bit will not go low */
if (apic_mode == LOCAL_APIC &&
apic_ret();
else
break;
}
/* integrated apic */
/* to offset the INIT IPI queue up in the buffer */
/*
* send the second SIPI (Startup IPI) as recommended by Intel
* software development manual.
*/
}
}
/*ARGSUSED1*/
static int
{
if (!apic_cpu_in_range(cpun)) {
return (EINVAL);
}
/*
* Switch to apic_common_send_ipi for safety during starting other CPUs.
*/
if (apic_mode == LOCAL_X2APIC) {
}
apic_cmos_ssb_set = 1;
return (0);
}
/*
* Put CPU into halted state with interrupts disabled.
*/
/*ARGSUSED1*/
static int
{
int rc;
extern cpuset_t cpu_ready_set;
if (!apic_cpu_in_range(cpun)) {
return (EINVAL);
}
return (ENOTSUP);
}
/* Clear CPU_READY flag to disable cross calls. */
if (rc != 0) {
return (rc);
}
/* Intercept target CPU at a safe point before powering it off. */
return (0);
}
static int
{
return (EINVAL);
}
case PSM_CPU_ADD:
return (apic_cpu_add(reqp));
case PSM_CPU_REMOVE:
return (apic_cpu_remove(reqp));
case PSM_CPU_STOP:
default:
return (ENOTSUP);
}
}
#ifdef DEBUG
int apic_break_on_cpu = 9;
int apic_stretch_interrupts = 0;
void
{
}
#endif /* DEBUG */
/*
* platform_intr_enter
*
* Called at the beginning of the interrupt service routine to
* mask all level equal to and below the interrupt priority
* of the interrupting vector. An EOI should be given to
* the interrupt controller to enable other HW interrupts.
*
* Return -1 for spurious interrupts
*
*/
/*ARGSUSED*/
static int
{
int nipl;
int irq;
/*
* The real vector delivered is (*vectorp + 0x20), but our caller
* subtracts 0x20 from the vector before passing it to us.
* (That's why APIC_BASE_VECT is 0x20.)
*/
/* if interrupted by the clock, increment apic_nsec_since_boot */
if (vector == apic_clkvect) {
if (!apic_oneshot) {
/* NOTE: this is not MT aware */
}
/* We will avoid all the book keeping overhead for clock */
if (apic_mode == LOCAL_APIC) {
#if defined(__amd64)
#else
if (apic_have_32bit_cr8)
else
#endif
} else {
X2APIC_WRITE(APIC_EOI_REG, 0);
}
return (nipl);
}
return (APIC_INT_SPURIOUS);
}
/* Check if the vector we got is really what we need */
if (apic_revector_pending) {
/*
* Disable interrupts for the duration of
* the vector translation to prevent a self-race for
* the apic_revector_lock. This cannot be done
* in apic_xlate_vector because it is recursive and
* we want the vector translation to be atomic with
* respect to other (higher-priority) interrupts.
*/
iflag = intr_clear();
}
if (apic_mode == LOCAL_APIC) {
#if defined(__amd64)
#else
if (apic_have_32bit_cr8)
else
#endif
} else {
}
/*
* apic_level_intr could have been assimilated into the irq struct.
* but, having it as a character array is more efficient in terms of
* cache usage. So, we leave it as is.
*/
if (!apic_level_intr[irq]) {
if (apic_mode == LOCAL_APIC) {
} else {
X2APIC_WRITE(APIC_EOI_REG, 0);
}
}
#ifdef DEBUG
if (apic_break_on_cpu == psm_get_cpu_id())
apic_break();
#endif /* DEBUG */
return (nipl);
}
/*
* This macro is a common code used by MMIO local apic and X2APIC
* local apic.
*/
#define APIC_INTR_EXIT() \
{ \
if (apic_level_intr[irq]) \
/* ISR above current pri could not be in progress */ \
}
/*
* Any changes made to this function must also change X2APIC
* version of intr_exit.
*/
void
{
#if defined(__amd64)
#else
if (apic_have_32bit_cr8)
else
#endif
}
/*
* Same as apic_intr_exit() except it uses MSR rather than MMIO
* to access local apic registers.
*/
void
{
}
psm_intr_exit_fn(void)
{
if (apic_mode == LOCAL_X2APIC)
return (x2apic_intr_exit);
return (apic_intr_exit);
}
/*
* Mask all interrupts below or equal to the given IPL.
* Any changes made to this function must also change X2APIC
* version of setspl.
*/
static void
apic_setspl(int ipl)
{
#if defined(__amd64)
#else
if (apic_have_32bit_cr8)
else
#endif
/* interrupts at ipl above this cannot be in progress */
/*
* this is a patch fix for the ALR QSMP P5 machine, so that interrupts
* have enough time to come in before the priority is raised again
* during the idle() loop.
*/
if (apic_setspl_delay)
(void) apic_reg_ops->apic_get_pri();
}
/*
* X2APIC version of setspl.
* Mask all interrupts below or equal to the given IPL
*/
static void
x2apic_setspl(int ipl)
{
/* interrupts at ipl above this cannot be in progress */
}
/*
* generates an interprocessor interrupt to another CPU. Any changes made to
* this routine must be accompanied by similar changes to
* apic_common_send_ipi().
*/
static void
{
int vector;
flag = intr_clear();
vector);
}
/*ARGSUSED*/
static void
{
}
/*ARGSUSED*/
static void
{
}
void
apic_ret()
{
}
/*
* If apic_coarse_time == 1, then apic_gettime() is used instead of
* apic_gethrtime(). This is used for performance instead of accuracy.
*/
static hrtime_t
{
int old_hrtime_stamp;
/*
* In one-shot mode, we do not keep time, so if anyone
* calls psm_gettime() directly, we vector over to
* gethrtime().
* one-shot mode MUST NOT be enabled if this psm is the source of
* hrtime.
*/
if (apic_oneshot)
return (gethrtime());
apic_ret();
goto gettime_again;
}
return (temp);
}
/*
* Here we return the number of nanoseconds since booting. Note every
* clock interrupt increments apic_nsec_since_boot by the appropriate
* amount.
*/
static hrtime_t
{
int old_hrtime_stamp, status;
/*
* In one-shot mode, we do not keep time, so if anyone
* calls psm_gethrtime() directly, we vector over to
* gethrtime().
* one-shot mode MUST NOT be enabled if this psm is the source of
* hrtime.
*/
if (apic_oneshot)
return (gethrtime());
if (apic_mode == LOCAL_APIC)
cpun >>= APIC_ID_BIT_OFFSET;
apic_ret();
/*
* Check to see which CPU we are on. Note the time is kept on
* the local APIC of CPU 0. If on CPU 0, simply read the current
* counter. If on another CPU, issue a remote read command to CPU 0.
*/
} else {
#ifdef DEBUG
#else
if (apic_mode == LOCAL_APIC)
#endif /* DEBUG */
& AV_READ_PENDING) {
apic_ret();
}
else { /* 0 = invalid */
/*
* return last hrtime right now, will need more
* testing if change to retry
*/
return (temp);
}
}
if (countval > last_count_read)
countval = 0;
else
/* we might have clobbered last_count_read. Restore it */
goto gethrtime_again;
}
if (temp < apic_last_hrtime) {
/* return last hrtime if error occurs */
}
else
return (temp);
}
/* apic NMI handler */
/*ARGSUSED*/
static void
{
if (apic_shutdown_processors) {
return;
}
if (!lock_try(&apic_nmi_lock))
return;
if (apic_kmdb_on_nmi && psm_debugger()) {
debug_enter("NMI received: entering kmdb\n");
} else if (apic_panic_on_nmi) {
/* Keep panic from entering kmdb. */
nopanicdebug = 1;
panic("NMI received\n");
} else {
/*
* prom_printf is the best shot we have of something which is
*/
prom_printf("NMI received\n");
}
}
/*ARGSUSED*/
static int
{
}
static int
{
}
static int
{
int cpun;
static int cpus_started = 1;
/* We know this CPU + BSP started successfully. */
cpus_started++;
/*
* On BSP we would have enabled X2APIC, if supported by processor,
* in acpi_probe(), but on AP we do it here.
*
* We enable X2APIC mode only if BSP is running in X2APIC & the
* local APIC mode of the current CPU is MMIO (xAPIC).
*/
apic_local_mode() == LOCAL_APIC) {
}
/*
* Switch back to x2apic IPI sending method for performance when target
* CPU has entered x2apic mode.
*/
if (apic_mode == LOCAL_X2APIC) {
}
/*
* since some systems don't enable the internal cache on the non-boot
* cpus, so we have to enable them here
*/
#ifdef DEBUG
#else
if (apic_mode == LOCAL_APIC)
#endif /* DEBUG */
/*
* We may be booting, or resuming from suspend; aci_status will
* be APIC_CPU_INTR_ENABLE if coming from suspend, so we add the
* APIC_CPU_ONLINE flag here rather than setting aci_status completely.
*/
cpun = psm_get_cpu_id();
return (PSM_SUCCESS);
}
{
int i;
if (cpu_id == -1)
return ((processorid_t)0);
if (apic_cpu_in_range(i))
return (i);
}
return ((processorid_t)-1);
}
static int
{
int i, rv = 0;
/* Check whether CPU hotplug is supported. */
return (ENOTSUP);
}
case MACH_CPU_ARG_LOCAL_APIC:
"!apic: apicid(%u) or procid(%u) is invalid.",
return (EINVAL);
}
break;
if (localid >= UINT32_MAX) {
"!apic: x2apicid(%u) is invalid.", localid);
return (EINVAL);
"can't support x2APIC processor.");
return (ENOTSUP);
}
break;
default:
"!apic: unknown argument type %d to apic_cpu_add().",
return (EINVAL);
}
/* Use apic_ioapic_lock to sync with apic_find_next_cpu_intr. */
iflag = intr_clear();
/* Check whether local APIC id already exists. */
for (i = 0; i < apic_nproc; i++) {
if (!CPU_IN_SET(apic_cpumask, i))
continue;
"!apic: local apic id %u already exists.",
localid);
return (EEXIST);
"!apic: processor id %u already exists.",
(int)procid);
return (EEXIST);
}
/*
* There's no local APIC version number available in MADT table,
* so assume that all CPUs are homogeneous and use local APIC
* version number of the first existing CPU.
*/
if (first) {
}
}
/*
* Try to assign the same cpuid if APIC id exists in the dirty cache.
*/
for (i = 0; i < apic_max_nproc; i++) {
if (CPU_IN_SET(apic_cpumask, i)) {
continue;
}
cpuid = i;
break;
}
}
/* Avoid the dirty cache and allocate fresh slot if possible. */
for (i = 0; i < apic_max_nproc; i++) {
cpuid = i;
break;
}
}
}
/* Try to find any free slot as last resort. */
for (i = 0; i < apic_max_nproc; i++) {
cpuid = i;
break;
}
}
}
"!apic: failed to allocate cpu id for processor %u.",
procid);
"!apic: failed to build mapping for processor %u.",
procid);
} else {
if (cpuid >= apic_nproc) {
}
}
return (rv);
}
static int
{
int i;
/* Check whether CPU hotplug is supported. */
return (ENOTSUP);
}
/* Use apic_ioapic_lock to sync with apic_find_next_cpu_intr. */
iflag = intr_clear();
if (!apic_cpu_in_range(cpuid)) {
"!apic: cpuid %d doesn't exist in apic_cpus array.",
cpuid);
return (ENODEV);
}
return (ENOENT);
}
/*
* We are removing the highest numbered cpuid so we need to
* find the next highest cpuid as the new value for apic_nproc.
*/
for (i = apic_nproc; i > 0; i--) {
apic_nproc = i;
break;
}
}
/* at least one CPU left */
ASSERT(i > 0);
}
/* mark slot as free and keep it in the dirty cache */
return (0);
}
/*
* type == -1 indicates it is an internal request. Do not change
* resv_vector for these requests
*/
static int
{
int irq;
if (type != -1) {
}
return (irq);
}
}
return (-1); /* shouldn't happen */
}
static int
apic_getclkirq(int ipl)
{
int irq;
return (-1);
/*
* Note the vector in apic_clkvect for per clock handling.
*/
apic_clkvect));
return (irq);
}
/*
* Return the number of APIC clock ticks elapsed for 8245 to decrement
* (APIC_TIME_COUNT + pit_ticks_adj) ticks.
*/
static uint_t
{
iflag = intr_clear();
do {
} while (pit_tick < APIC_TIME_MIN ||
/*
* Wait for the 8254 to decrement by 5 ticks to ensure
* we didn't start in the middle of a tick.
* Compare with 0x10 for the wrap around case.
*/
do {
/*
* Wait for the 8254 to decrement by
* (APIC_TIME_COUNT + pit_ticks_adj) ticks
*/
do {
return (start_apic_tick - end_apic_tick);
}
/*
* Initialise the APIC timer on the local APIC of CPU 0 to the desired
* frequency. Note at this stage in the boot sequence, the boot processor
* is the only active processor.
* hertz value of 0 indicates a one-shot mode request. In this case
* the function returns the resolution (in nanoseconds) for the hardware
* timer interrupt. If one-shot mode capability is not available,
* the return value will be 0. apic_enable_oneshot is a global switch
* for disabling the functionality.
* A non-zero positive value for hertz indicates a periodic mode request.
* In this case the hardware will be programmed to generate clock interrupts
* at hertz frequency and returns the resolution of interrupts in
* nanosecond.
*/
static int
apic_clkinit(int hertz)
{
uint_t apic_ticks = 0;
int ret;
static int firsttime = 1;
if (firsttime) {
/* first time calibrate on CPU0 only */
/* total number of PIT ticks corresponding to apic_ticks */
/*
* Determine the number of nanoseconds per APIC clock tick
* and then determine how many APIC ticks to interrupt at the
* desired frequency
* apic_ticks / (pitticks / PIT_HZ) = apic_ticks_per_s
* (apic_ticks * PIT_HZ) / pitticks = apic_ticks_per_s
* apic_ticks_per_ns = (apic_ticks * PIT_HZ) / (pitticks * 10^9)
* pic_ticks_per_SFns =
* (SF * apic_ticks * PIT_HZ) / (pitticks * 10^9)
*/
/* the interval timer initial count is 32 bit max */
firsttime = 0;
}
if (hertz != 0) {
/* periodic */
}
if (hertz == 0) {
/* requested one_shot */
if (!tsc_gethrtime_enable || !apic_oneshot_enable)
return (0);
apic_oneshot = 1;
} else {
/* program the local APIC to interrupt at the given frequency */
apic_oneshot = 0;
}
return (ret);
}
/*
* apic_preshutdown:
* Called early in shutdown whilst we can still access filesystems to do
* things like loading modules which will be required to complete shutdown
* after filesystems are all unmounted.
*/
static void
{
APIC_VERBOSE_POWEROFF(("apic_preshutdown(%d,%d); m=%d a=%d\n",
return;
}
}
static void
{
int i;
/* Send NMI to all CPUs except self to do per processor shutdown */
iflag = intr_clear();
#ifdef DEBUG
#else
if (apic_mode == LOCAL_APIC)
#endif /* DEBUG */
/* restore cmos shutdown byte before reboot */
if (apic_cmos_ssb_set) {
}
/* disable apic mode if imcr present */
if (apic_imcrp) {
}
/* remainder of function is for shutdown cases only */
if (cmd != A_SHUTDOWN)
return;
/*
* Switch system back into Legacy-Mode if using ACPI and
* not powering-off. Some BIOSes need to remain in ACPI-mode
* for power-off to succeed (Dell Dimension 4600)
* Do not disable ACPI while doing fastreboot
*/
(void) AcpiDisable();
if (fcn == AD_FASTREBOOT) {
}
/* remainder of function is for shutdown+poweroff case only */
if (fcn != AD_POWEROFF)
return;
switch (apic_poweroff_method) {
case APIC_POWEROFF_VIA_RTC:
/* select the extended NVRAM bank in the RTC */
/* for Predator must toggle the PAB bit */
/*
* clear power active bar, wakeup alarm and
* kickstart
*/
/* delay before next write */
drv_usecwait(1000);
/* for S40 the following would suffice */
/* power active bar control bit */
break;
restarts = 0;
if (++restarts == 3)
break;
attempts = 0;
do {
byte &= MISMIC_BUSY_MASK;
if (byte != 0) {
drv_usecwait(1000);
if (attempts >= 3)
goto restart_aspen_bmc;
++attempts;
}
} while (byte != 0);
byte |= 0x1;
i = 0;
i++) {
attempts = 0;
do {
byte &= MISMIC_BUSY_MASK;
if (byte != 0) {
drv_usecwait(1000);
if (attempts >= 3)
goto restart_aspen_bmc;
++attempts;
}
} while (byte != 0);
byte |= 0x1;
}
break;
restarts = 0;
if (++restarts == 3)
break;
attempts = 0;
do {
byte &= SMS_STATE_MASK;
if ((byte == SMS_READ_STATE) ||
(byte == SMS_WRITE_STATE)) {
drv_usecwait(1000);
if (attempts >= 3)
goto restart_sitka_bmc;
++attempts;
}
} while ((byte == SMS_READ_STATE) ||
(byte == SMS_WRITE_STATE));
i = 0;
i++) {
attempts = 0;
do {
byte &= SMS_IBF_MASK;
if (byte != 0) {
drv_usecwait(1000);
if (attempts >= 3)
goto restart_sitka_bmc;
++attempts;
}
} while (byte != 0);
}
break;
case APIC_POWEROFF_NONE:
/* If no APIC direct method, we will try using ACPI */
if (apic_enable_acpi) {
if (acpi_poweroff() == 1)
return;
} else
return;
break;
}
/*
* Wait a limited time here for power to go off.
* If the power does not go off, then there was a
* problem and we should continue to the halt which
* prints a message for the user to press a key to
* reboot.
*/
}
/*
* Try and disable all interrupts. We just assign interrupts to other
* processors based on policy. If any were bound by user request, we
* let them continue and return failure. We do not bother to check
* for cache affinity while rebinding.
*/
static int
{
iflag = intr_clear();
for (i = 0; i <= APIC_MAX_VECTOR; i++) {
/*
* CPU is busy -- it's the target of
* a pending reprogramming attempt
*/
return (PSM_FAILURE);
}
}
}
i = apic_min_device_irq;
for (; i <= apic_max_device_irq; i++) {
/*
* If there are bound interrupts on this cpu, then
* rebind them to other processors.
*/
hardbound = 1;
continue;
}
do {
bind_cpu =
}
}
}
if (hardbound) {
"due to user bound interrupts", cpun);
return (PSM_FAILURE);
}
else
return (PSM_SUCCESS);
}
/*
* Bind interrupts to the CPU's local APIC.
* Interrupts should not be bound to a CPU's local APIC until the CPU
* is ready to receive interrupts.
*/
static void
{
int i;
iflag = intr_clear();
i = apic_min_device_irq;
for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
(void) apic_rebind_all(irq_ptr,
}
}
}
}
/*
* This function will reprogram the timer.
*
* When in oneshot mode the argument is the absolute time in future to
* generate the interrupt at.
*
* When in periodic mode, the argument is the interval at which the
* interrupts should be generated. There is no need to support the periodic
* mode timer change at this time.
*/
static void
{
/*
* We should be called from high PIL context (CBE_HIGH_PIL),
* so kpreempt is disabled.
*/
if (!apic_oneshot) {
/* time is the interval for periodic mode */
} else {
/* one shot mode */
if (delta <= 0) {
/*
* requested to generate an interrupt in the past
* generate an interrupt as soon as possible
*/
} else if (delta > apic_nsec_max) {
/*
* requested to generate an interrupt at a time
* further than what we are capable of. Set to max
* the hardware can handle
*/
ticks = APIC_MAXVAL;
#ifdef DEBUG
" %lld too far in future, current time"
#endif
} else
}
if (ticks < apic_min_timer_ticks)
}
/*
* This function will enable timer interrupts.
*/
static void
apic_timer_enable(void)
{
/*
* We should be Called from high PIL context (CBE_HIGH_PIL),
* so kpreempt is disabled.
*/
if (!apic_oneshot) {
} else {
/* one shot */
(apic_clkvect + APIC_BASE_VECT));
}
}
/*
* This function will disable timer interrupts.
*/
static void
apic_timer_disable(void)
{
/*
* We should be Called from high PIL context (CBE_HIGH_PIL),
* so kpreempt is disabled.
*/
}
/*
* Set timer far into the future and return timer
* current Count in nanoseconds.
*/
apic_timer_stop_count(void)
{
int enable_val, count_val;
/*
* Should be called with interrupts disabled.
*/
ASSERT(!interrupts_enabled());
return (ns_val);
}
/*
* Reprogram timer after Deep C-State.
*/
void
{
}
/*
* If this module needs a periodic handler for the interrupt distribution, it
* can be added here. The argument to the periodic handler is not currently
* used, but is reserved for future.
*/
static void
apic_post_cyclic_setup(void *arg)
{
/* cpu_lock is held */
/* set up a periodic handler for intr redistribution */
/*
* In peridoc mode intr redistribution processing is done in
* apic_intr_enter during clk intr processing
*/
if (!apic_oneshot)
return;
/*
* Register a periodical handler for the redistribution processing.
* On X86, CY_LOW_LEVEL is mapped to the level 2 interrupt, so
* DDI_IPL_2 should be passed to ddi_periodic_add() here.
*/
(void (*)(void *))apic_redistribute_compute, NULL,
}
static void
{
int i, j, max_busy;
if (++apic_nticks == apic_sample_factor_redistribution) {
/*
* Time to call apic_intr_redistribute().
* reset apic_nticks. This will cause max_busy
* to be calculated below and if it is more than
* apic_int_busy, we will do the whole thing
*/
apic_nticks = 0;
}
max_busy = 0;
for (i = 0; i < apic_nproc; i++) {
if (!apic_cpu_in_range(i))
continue;
/*
* Check if curipl is non zero & if ISR is in
* progress
*/
if (((j = apic_cpus[i].aci_curipl) != 0) &&
int irq;
}
if (!apic_nticks &&
}
if (!apic_nticks) {
if (max_busy > apic_int_busy_mark) {
/*
* We could make the following check be
* skipped > 1 in which case, we get a
* redistribution at half the busy mark (due to
* double interval). Need to be able to collect
* more empirical data to decide if that is a
* good strategy. Punt for now.
*/
if (apic_skipped_redistribute) {
} else {
}
} else
}
}
}
/*
* The following functions are in the platform specific file so that they
* can be different functions depending on whether we are running on
* bare metal or a hypervisor.
*/
/*
* map an apic for memory-mapped access
*/
uint32_t *
{
/*LINTED: pointer cast may result in improper alignment */
}
uint32_t *
{
}
/*
* unmap an apic
*/
void
{
}
void
{
}
/*
* Check to make sure there are enough irq slots
*/
int
{
int i, avail;
avail = 0;
for (i = APIC_FIRST_FREE_IRQ; i < APIC_RESV_IRQ; i++) {
if ((apic_irq_table[i] == NULL) ||
return (PSM_SUCCESS);
}
}
return (PSM_FAILURE);
}
/*
*/
int
int behavior)
{
int rcount, i;
"inum=0x%x pri=0x%x count=0x%x behavior=%d\n",
if (count > 1) {
if (behavior == DDI_INTR_ALLOC_STRICT &&
apic_multi_msi_enable == 0)
return (0);
if (apic_multi_msi_enable == 0)
count = 1;
}
return (0);
/* if not ISP2, then round it down */
break;
}
if (start == 0) {
/* no vector available */
return (0);
}
/* not enough free irq slots available */
return (0);
}
for (i = 0; i < rcount; i++) {
(uchar_t)-1) {
/*
* shouldn't happen because of the
* apic_check_free_irqs() check earlier
*/
"apic_allocate_irq failed\n"));
return (i);
}
#ifdef DEBUG
"apic_vector_to_irq is not APIC_RESV_IRQ\n"));
#endif
irqptr->airq_share_id = 0;
if (i == 0) /* they all bound to the same cpu */
0xff, 0xff);
else
"dip=0x%p vector=0x%x origirq=0x%x pri=0x%x\n", irqno,
}
return (rcount);
}
/*
*/
int
int behavior)
{
int rcount, i;
behavior == DDI_INTR_ALLOC_STRICT)) {
rcount = 0;
goto out;
}
/* not enough free irq slots available */
rcount = 0;
goto out;
}
for (i = 0; i < rcount; i++) {
(uchar_t)-1) {
/*
* shouldn't happen because of the
* apic_check_free_irqs() check earlier
*/
"apic_allocate_irq failed\n"));
rcount = i;
goto out;
}
/*
* shouldn't happen because of the
* apic_navail_vector() call earlier
*/
"apic_allocate_vector failed\n"));
rcount = i;
goto out;
}
irqptr->airq_share_id = 0;
}
out:
return (rcount);
}
/*
* Allocate a free vector for irq at ipl. Takes care of merging of multiple
* IPLs into a single APIC level as well as stretching some IPLs onto multiple
* levels. APIC_HI_PRI_VECTS interrupts are reserved for high priority
* requests and allocated only when pri is set.
*/
{
#ifdef DEBUG
if (apic_restrict_vector) /* for testing shared interrupt logic */
#endif /* DEBUG */
if (pri == 0)
if (APIC_CHECK_RESERVE_VECTORS(i))
continue;
if (apic_vector_to_irq[i] == APIC_RESV_IRQ) {
return (i);
}
}
return (0);
}
/* Mark vector as not being used by any irq */
void
{
}
{
return (ioapic[APIC_IO_DATA]);
}
void
{
}
void
{
}
/*
* Round-robin algorithm to find the next CPU with interrupts enabled.
* It can't share the same static variable apic_next_bind_cpu with
* apic_get_next_bind_cpu(), since that will cause all interrupts to be
* bound to CPU1 at boot time. During boot, only CPU0 is online with
* interrupts enabled when apic_get_next_bind_cpu() and apic_find_cpu()
* are called. However, the pcplusmp driver assumes that there will be
* boot_ncpus CPUs configured eventually so it tries to distribute all
* interrupts among CPU0 - CPU[boot_ncpus - 1]. Thus to prevent all
* interrupts being targetted at CPU1, we need to use a dedicated static
* variable for find_next_cpu() instead of sharing apic_next_bind_cpu.
*/
static processorid_t
apic_find_cpu(int flag)
{
int i;
static processorid_t acid = 0;
/* Find the first CPU with the passed-in flag set */
for (i = 0; i < apic_nproc; i++) {
if (++acid >= apic_nproc) {
acid = 0;
}
if (apic_cpu_in_range(acid) &&
break;
}
}
return (acid);
}
/*
* Call rebind to do the actual programming.
* Must be called with interrupts disabled and apic_ioapic_lock held
* 'p' is polymorphic -- if this function is called to process a deferred
* reprogramming, p is of type 'struct ioapic_reprogram_data *', from which
* the irq pointer is retrieved. If not doing deferred reprogramming,
* p is of the type 'apic_irq_t *'.
*
* apic_ioapic_lock must be held across this call, as it protects apic_rebind
* and it protects apic_find_next_cpu_intr() from a race in which a CPU can be
* taken offline after a cpu is selected, but before apic_rebind is called to
* bind interrupts to it.
*/
int
{
int rv;
if (deferred) {
drep = (struct ioapic_reprogram_data *)p;
} else
irqptr = (apic_irq_t *)p;
if (rv) {
/*
* CPU is not up or interrupts are disabled. Fall back to
* the first available CPU
*/
drep);
}
return (rv);
}
{
return (vector);
}
char *
{
return (apic_psm_info.p_mach_idstring);
}
/*
* Switch between safe and x2APIC IPI sending method.
* CPU may power on in xapic mode or x2apic mode. If CPU needs to send IPI to
* other CPUs before entering x2APIC mode, it still needs to xAPIC method.
* Before sending StartIPI to target CPU, psm_send_ipi will be changed to
* apic_common_send_ipi, which detects current local APIC mode and use right
* method to send IPI. If some CPUs fail to start up, apic_poweron_cnt
* won't return to zero, so apic_common_send_ipi will always be used.
* psm_send_ipi can't be simply changed back to x2apic_send_ipi if some CPUs
* failed to start up because those failed CPUs may recover itself later at
* unpredictable time.
*/
static void
{
iflag = intr_clear();
if (enter) {
ASSERT(apic_poweron_cnt >= 0);
if (apic_poweron_cnt == 0) {
}
} else {
ASSERT(apic_poweron_cnt > 0);
if (apic_poweron_cnt == 0) {
}
}
}
void
{
}
static void
{
int suppress_brdcst_eoi = 0;
if (psm_vt_ops != NULL) {
/*
* Since X2APIC requires the use of interrupt remapping
* (though this is not documented explicitly in the Intel
* documentation (yet)), initialize interrupt remapping
* support before initializing the X2APIC unit.
*/
if (((apic_intrmap_ops_t *)psm_vt_ops)->
/*
* We leverage the interrupt remapping engine to
* suppress broadcast EOI; thus we must send the
* directed EOI with the directed-EOI handler.
*/
if (apic_directed_EOI_supported() == 0) {
suppress_brdcst_eoi = 1;
}
if (apic_detect_x2apic()) {
}
if (apic_directed_EOI_supported() == 0) {
}
}
}
}
/*ARGSUSED*/
static void
{
}
/*ARGSUSED*/
static void
{
}