xref: /onnv-gate/usr/src/uts/i86pc/io/cbe.c (revision 999:e4fc41bb3bfb)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/cyclic.h>
300Sstevel@tonic-gate #include <sys/cyclic_impl.h>
310Sstevel@tonic-gate #include <sys/spl.h>
320Sstevel@tonic-gate #include <sys/x_call.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/machsystm.h>
350Sstevel@tonic-gate #include <sys/smp_impldefs.h>
360Sstevel@tonic-gate #include <sys/psm_types.h>
370Sstevel@tonic-gate #include <sys/atomic.h>
380Sstevel@tonic-gate #include <sys/clock.h>
390Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
400Sstevel@tonic-gate #include <sys/ddi_intr.h>
41*999Slq150181 #include <sys/avintr.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static int cbe_vector;
440Sstevel@tonic-gate static int cbe_ticks = 0;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static cyc_func_t volatile cbe_xcall_func;
470Sstevel@tonic-gate static cpu_t *volatile cbe_xcall_cpu;
480Sstevel@tonic-gate static void *cbe_xcall_farg;
490Sstevel@tonic-gate static cpuset_t cbe_enabled;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static ddi_softint_hdl_impl_t cbe_low_hdl =
52*999Slq150181 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL};
530Sstevel@tonic-gate static ddi_softint_hdl_impl_t cbe_clock_hdl =
54*999Slq150181 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL};
550Sstevel@tonic-gate 
560Sstevel@tonic-gate cyclic_id_t cbe_hres_cyclic;
570Sstevel@tonic-gate int cbe_psm_timer_mode = TIMER_ONESHOT;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate void cbe_hres_tick(void);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate int
620Sstevel@tonic-gate cbe_softclock(void)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	cyclic_softint(CPU, CY_LOCK_LEVEL);
650Sstevel@tonic-gate 	return (1);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate int
690Sstevel@tonic-gate cbe_low_level(void)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	cpu_t *cpu = CPU;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	cyclic_softint(cpu, CY_LOW_LEVEL);
740Sstevel@tonic-gate 	return (1);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * We can be in cbe_fire() either due to a cyclic-induced cross call, or due
790Sstevel@tonic-gate  * to the timer firing at level-14.  Because cyclic_fire() can tolerate
800Sstevel@tonic-gate  * spurious calls, it would not matter if we called cyclic_fire() in both
810Sstevel@tonic-gate  * cases.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate int
850Sstevel@tonic-gate cbe_fire(void)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	cpu_t *cpu = CPU;
880Sstevel@tonic-gate 	processorid_t me = cpu->cpu_id, i;
890Sstevel@tonic-gate 	int cross_call = (cbe_xcall_func != NULL && cbe_xcall_cpu == cpu);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	cyclic_fire(cpu);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (cbe_psm_timer_mode != TIMER_ONESHOT && me == 0 && !cross_call) {
940Sstevel@tonic-gate 		for (i = 1; i < NCPU; i++) {
950Sstevel@tonic-gate 			if (CPU_IN_SET(cbe_enabled, i))
960Sstevel@tonic-gate 				send_dirint(i, CBE_HIGH_PIL);
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (cross_call) {
1010Sstevel@tonic-gate 		ASSERT(cbe_xcall_func != NULL && cbe_xcall_cpu == cpu);
1020Sstevel@tonic-gate 		(*cbe_xcall_func)(cbe_xcall_farg);
1030Sstevel@tonic-gate 		cbe_xcall_func = NULL;
1040Sstevel@tonic-gate 		cbe_xcall_cpu = NULL;
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	return (1);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*ARGSUSED*/
1110Sstevel@tonic-gate void
1120Sstevel@tonic-gate cbe_softint(void *arg, cyc_level_t level)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	switch (level) {
1150Sstevel@tonic-gate 	case CY_LOW_LEVEL:
116*999Slq150181 		(*setsoftint)(CBE_LOW_PIL, cbe_low_hdl.ih_pending);
1170Sstevel@tonic-gate 		break;
1180Sstevel@tonic-gate 	case CY_LOCK_LEVEL:
119*999Slq150181 		(*setsoftint)(CBE_LOCK_PIL, cbe_clock_hdl.ih_pending);
1200Sstevel@tonic-gate 		break;
1210Sstevel@tonic-gate 	default:
1220Sstevel@tonic-gate 		panic("cbe_softint: unexpected soft level %d", level);
1230Sstevel@tonic-gate 	}
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*ARGSUSED*/
1270Sstevel@tonic-gate void
1280Sstevel@tonic-gate cbe_reprogram(void *arg, hrtime_t time)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	if (cbe_psm_timer_mode == TIMER_ONESHOT)
1310Sstevel@tonic-gate 		(*psm_timer_reprogram)(time);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*ARGSUSED*/
1350Sstevel@tonic-gate cyc_cookie_t
1360Sstevel@tonic-gate cbe_set_level(void *arg, cyc_level_t level)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	int ipl;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	switch (level) {
1410Sstevel@tonic-gate 	case CY_LOW_LEVEL:
1420Sstevel@tonic-gate 		ipl = CBE_LOW_PIL;
1430Sstevel@tonic-gate 		break;
1440Sstevel@tonic-gate 	case CY_LOCK_LEVEL:
1450Sstevel@tonic-gate 		ipl = CBE_LOCK_PIL;
1460Sstevel@tonic-gate 		break;
1470Sstevel@tonic-gate 	case CY_HIGH_LEVEL:
1480Sstevel@tonic-gate 		ipl = CBE_HIGH_PIL;
1490Sstevel@tonic-gate 		break;
1500Sstevel@tonic-gate 	default:
1510Sstevel@tonic-gate 		panic("cbe_set_level: unexpected level %d", level);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	return (splr(ipltospl(ipl)));
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*ARGSUSED*/
1580Sstevel@tonic-gate void
1590Sstevel@tonic-gate cbe_restore_level(void *arg, cyc_cookie_t cookie)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	splx(cookie);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*ARGSUSED*/
1650Sstevel@tonic-gate void
1660Sstevel@tonic-gate cbe_xcall(void *arg, cpu_t *dest, cyc_func_t func, void *farg)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	kpreempt_disable();
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (dest == CPU) {
1710Sstevel@tonic-gate 		(*func)(farg);
1720Sstevel@tonic-gate 		kpreempt_enable();
1730Sstevel@tonic-gate 		return;
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	ASSERT(cbe_xcall_func == NULL);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	cbe_xcall_farg = farg;
1790Sstevel@tonic-gate 	membar_producer();
1800Sstevel@tonic-gate 	cbe_xcall_cpu = dest;
1810Sstevel@tonic-gate 	cbe_xcall_func = func;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	send_dirint(dest->cpu_id, CBE_HIGH_PIL);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	while (cbe_xcall_func != NULL || cbe_xcall_cpu != NULL)
1860Sstevel@tonic-gate 		continue;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	kpreempt_enable();
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	ASSERT(cbe_xcall_func == NULL && cbe_xcall_cpu == NULL);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate void *
1940Sstevel@tonic-gate cbe_configure(cpu_t *cpu)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	return (cpu);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate void
2000Sstevel@tonic-gate cbe_enable(void *arg)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	processorid_t me = ((cpu_t *)arg)->cpu_id;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	if ((cbe_psm_timer_mode != TIMER_ONESHOT) && (me == 0))
2050Sstevel@tonic-gate 		return;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	ASSERT(!CPU_IN_SET(cbe_enabled, me));
2080Sstevel@tonic-gate 	CPUSET_ADD(cbe_enabled, me);
2090Sstevel@tonic-gate 	if (cbe_psm_timer_mode == TIMER_ONESHOT)
2100Sstevel@tonic-gate 		(*psm_timer_enable)();
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate void
2140Sstevel@tonic-gate cbe_disable(void *arg)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	processorid_t me = ((cpu_t *)arg)->cpu_id;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (me == 0) {
2190Sstevel@tonic-gate 		/*
2200Sstevel@tonic-gate 		 * If this is the boot CPU, we'll quietly refuse to disable
2210Sstevel@tonic-gate 		 * our clock interrupt.
2220Sstevel@tonic-gate 		 */
2230Sstevel@tonic-gate 		return;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	ASSERT(CPU_IN_SET(cbe_enabled, me));
2270Sstevel@tonic-gate 	CPUSET_DEL(cbe_enabled, me);
2280Sstevel@tonic-gate 	if (cbe_psm_timer_mode == TIMER_ONESHOT)
2290Sstevel@tonic-gate 		(*psm_timer_disable)();
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * Called only on CPU 0. This is done since TSCs can have deltas between
2340Sstevel@tonic-gate  * different cpus see tsc_tick()
2350Sstevel@tonic-gate  */
2360Sstevel@tonic-gate void
2370Sstevel@tonic-gate cbe_hres_tick(void)
2380Sstevel@tonic-gate {
2390Sstevel@tonic-gate 	int s;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	dtrace_hres_tick();
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	/*
2440Sstevel@tonic-gate 	 * Because hres_tick effectively locks hres_lock, we must be at the
2450Sstevel@tonic-gate 	 * same PIL as that used for CLOCK_LOCK.
2460Sstevel@tonic-gate 	 */
2470Sstevel@tonic-gate 	s = splr(ipltospl(XC_HI_PIL));
2480Sstevel@tonic-gate 	hres_tick();
2490Sstevel@tonic-gate 	splx(s);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if ((cbe_ticks % hz) == 0)
2520Sstevel@tonic-gate 		(*hrtime_tick)();
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	cbe_ticks++;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate void
2590Sstevel@tonic-gate cbe_init(void)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	cyc_backend_t cbe = {
2620Sstevel@tonic-gate 		cbe_configure,		/* cyb_configure */
2630Sstevel@tonic-gate 		NULL,			/* cyb_unconfigure */
2640Sstevel@tonic-gate 		cbe_enable,		/* cyb_enable */
2650Sstevel@tonic-gate 		cbe_disable,		/* cyb_disable */
2660Sstevel@tonic-gate 		cbe_reprogram,		/* cyb_reprogram */
2670Sstevel@tonic-gate 		cbe_softint,		/* cyb_softint */
2680Sstevel@tonic-gate 		cbe_set_level,		/* cyb_set_level */
2690Sstevel@tonic-gate 		cbe_restore_level,	/* cyb_restore_level */
2700Sstevel@tonic-gate 		cbe_xcall,		/* cyb_xcall */
2710Sstevel@tonic-gate 		NULL,			/* cyb_suspend */
2720Sstevel@tonic-gate 		NULL			/* cyb_resume */
2730Sstevel@tonic-gate 	};
2740Sstevel@tonic-gate 	hrtime_t resolution;
2750Sstevel@tonic-gate 	cyc_handler_t hdlr;
2760Sstevel@tonic-gate 	cyc_time_t when;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	cbe_vector = (*psm_get_clockirq)(CBE_HIGH_PIL);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	CPUSET_ZERO(cbe_enabled);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	resolution = (*clkinitf)(TIMER_ONESHOT, &cbe_psm_timer_mode);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
2850Sstevel@tonic-gate 	cyclic_init(&cbe, resolution);
2860Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	(void) add_avintr(NULL, CBE_HIGH_PIL, (avfunc)cbe_fire,
289916Sschwartz 	    "cbe_fire_master", cbe_vector, 0, NULL, NULL, NULL);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (psm_get_ipivect != NULL) {
2920Sstevel@tonic-gate 		(void) add_avintr(NULL, CBE_HIGH_PIL, (avfunc)cbe_fire,
2930Sstevel@tonic-gate 		    "cbe_fire_slave",
2940Sstevel@tonic-gate 		    (*psm_get_ipivect)(CBE_HIGH_PIL, PSM_INTR_IPI_HI),
295916Sschwartz 		    0, NULL, NULL, NULL);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&cbe_clock_hdl, CBE_LOCK_PIL,
2990Sstevel@tonic-gate 	    (avfunc)cbe_softclock, "softclock", NULL, NULL);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	(void) add_avsoftintr((void *)&cbe_low_hdl, CBE_LOW_PIL,
3020Sstevel@tonic-gate 	    (avfunc)cbe_low_level, "low level", NULL, NULL);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	hdlr.cyh_level = CY_HIGH_LEVEL;
3070Sstevel@tonic-gate 	hdlr.cyh_func = (cyc_func_t)cbe_hres_tick;
3080Sstevel@tonic-gate 	hdlr.cyh_arg = NULL;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	when.cyt_when = 0;
3110Sstevel@tonic-gate 	when.cyt_interval = nsec_per_tick;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	cbe_hres_cyclic = cyclic_add(&hdlr, &when);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/* bind to cpu 0, which is also the boot cpu */
3160Sstevel@tonic-gate 	cyclic_bind(cbe_hres_cyclic, CPU, NULL);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (psm_post_cyclic_setup != NULL)
3190Sstevel@tonic-gate 		(*psm_post_cyclic_setup)(NULL);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate }
324