xref: /onnv-gate/usr/src/uts/sun4/os/dmv.c (revision 4104:91c60299aa9a)
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
5*4104Sblakej  * Common Development and Distribution License (the "License").
6*4104Sblakej  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*4104Sblakej  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/kobj.h>
320Sstevel@tonic-gate #include <sys/membar.h>
330Sstevel@tonic-gate #include <sys/dmv.h>
340Sstevel@tonic-gate #include <sys/prom_debug.h>
350Sstevel@tonic-gate #include <sys/machsystm.h>
36*4104Sblakej #include <vm/vm_dep.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Implementation of databearing mondo vector handler registration routines.
400Sstevel@tonic-gate  * See PSARC 1998/222 for more details.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * The dmv_interface_*_version variables are provided to protect a
450Sstevel@tonic-gate  * driver against changes in the databearing mondo interfaces.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * The major version is incremented when an incompatible change
480Sstevel@tonic-gate  * is made to an interface; for instance, a routine which used to take
490Sstevel@tonic-gate  * 3 parameters now takes 4, or a routine which used have the semantics
500Sstevel@tonic-gate  * "do X" now has the semantics "do Y".  Before calling any of the
510Sstevel@tonic-gate  * databearing mondo routines, a driver must check the major version
520Sstevel@tonic-gate  * it was compiled with (i.e., the constant DMV_INTERFACE_MAJOR_VERSION)
530Sstevel@tonic-gate  * against the contents of dmv_interface_major_version.  If the two
540Sstevel@tonic-gate  * are different, the driver must refuse to operate.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * The minor version is incremented when an upward-compatible change
570Sstevel@tonic-gate  * is made to an interface; for instance, a routine now supports a new
580Sstevel@tonic-gate  * flag bit (in an existing flags argument).  A client can use the
590Sstevel@tonic-gate  * minor version to see whether a feature it depends on is available
600Sstevel@tonic-gate  * in its environment; in order to enable this, the documentation
610Sstevel@tonic-gate  * for new features should note which major and minor version the
620Sstevel@tonic-gate  * feature first appears in.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate int dmv_interface_major_version = DMV_INTERFACE_MAJOR_VERSION;
660Sstevel@tonic-gate int dmv_interface_minor_version = DMV_INTERFACE_MINOR_VERSION;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * These are where the number of hardware and software DMV inums are kept.
700Sstevel@tonic-gate  * If they're zero, we use the platform's default values.  (These are not
710Sstevel@tonic-gate  * patchable in /etc/system, since the dispatch table is allocated before
720Sstevel@tonic-gate  * /etc/system is loaded; however, you could patch them by adb'ing unix.)
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate uint_t dmv_hwint = 0;
760Sstevel@tonic-gate uint_t dmv_swint = 0;
770Sstevel@tonic-gate uint_t dmv_totalints = 0;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate struct dmv_disp *dmv_dispatch_table = (struct dmv_disp *)0;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * dmv_disp_lock protects the dispatch table from being modified by two
830Sstevel@tonic-gate  * threads concurrently.  It is not used to protect the table from being
840Sstevel@tonic-gate  * modified while being used by the actual interrupt dispatch code; see
850Sstevel@tonic-gate  * comments at the end of dmv.h for the rationale.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate kmutex_t dmv_disp_lock;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * dmv_add_intr is called to add a databearing mondo interrupt handler
920Sstevel@tonic-gate  * for a real device to the system.  Only one handler may be registered
930Sstevel@tonic-gate  * for a dmv_inum at any one time.
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * Note that if a processor receives a databearing mondo interrupt
960Sstevel@tonic-gate  * for which a handler has not been registered, the behavior is
970Sstevel@tonic-gate  * undefined.  (Current practice for normal mondos which are unhandled
980Sstevel@tonic-gate  * depends on whether DEBUG is on; a DEBUG kernel prints an error
990Sstevel@tonic-gate  * and breaks to OBP, while a non-DEBUG kernel simply panics.  This
1000Sstevel@tonic-gate  * model will likely be followed for databearing mondos.)
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * Parameters:
1030Sstevel@tonic-gate  *	dmv_inum	interrupt number for the device.
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  *	routine		pointer to the device's vectored interrupt
1060Sstevel@tonic-gate  *			handler.  This routine is subject to the
1070Sstevel@tonic-gate  *			constraints outlined below in "Handler
1080Sstevel@tonic-gate  *			Characteristics and Environment".
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  *	arg		argument which will be passed to the device's
1110Sstevel@tonic-gate  *			handler.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * Return value:	0 if the handler was added successfully, -1 if
1140Sstevel@tonic-gate  *			handler was already registered for the given
1150Sstevel@tonic-gate  *			dmv_inum.
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  * Handler Characteristics and Environment
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  *   Handler Entry:
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  *	On entry to the handler, the %g registers are set as follows:
1220Sstevel@tonic-gate  *
1230Sstevel@tonic-gate  *	%g1	The argument (arg) passed to dmv_add_intr().
1240Sstevel@tonic-gate  *	%g2	Word 0 of the incoming mondo vector.
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  *   Handler Constraints:
1280Sstevel@tonic-gate  *
1290Sstevel@tonic-gate  *	While executing, the handler must obey the following rules:
1300Sstevel@tonic-gate  *
1310Sstevel@tonic-gate  *	1. The handler is limited to the use of registers %g1 through
1320Sstevel@tonic-gate  *	   %g7.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  *	2. The handler may not modify %cwp (i.e., may not execute a
1350Sstevel@tonic-gate  *	   SAVE or RESTORE instruction).
1360Sstevel@tonic-gate  *
1370Sstevel@tonic-gate  *	3. The handler may not read or write the stack.
1380Sstevel@tonic-gate  *
1390Sstevel@tonic-gate  *	4. The handler may not call any other DDI or kernel routines.
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  *	5. The handler may not call any other routines inside the
1420Sstevel@tonic-gate  *	   handler's own driver, since this would modify %o7; however,
1430Sstevel@tonic-gate  *	   it is permissible to jump to a routine within the handler's
1440Sstevel@tonic-gate  *	   driver.
1450Sstevel@tonic-gate  *
1460Sstevel@tonic-gate  *	6. The handler may read the Incoming Interrupt Vector Data
1470Sstevel@tonic-gate  *	   registers, and the Interrupt Vector Receive register, but
1480Sstevel@tonic-gate  *	   must not modify these registers.  (Note: symbols for the
1490Sstevel@tonic-gate  *	   ASIs and addresses of these registers are in <sys/spitasi.h>
1500Sstevel@tonic-gate  *	   and <sys/intreg.h>.)
1510Sstevel@tonic-gate  *
1520Sstevel@tonic-gate  *	7. The handler may read or write driver-private data
1530Sstevel@tonic-gate  *	   structures; in order to protect against simultaneous
1540Sstevel@tonic-gate  *	   modification by other driver routines, nonblocking data
1550Sstevel@tonic-gate  *	   sharing algorithms must be used.  (For instance,
1560Sstevel@tonic-gate  *	   compare-and-swap could be used to update counters or add
1570Sstevel@tonic-gate  *	   entries to linked lists; producer-consumer queues are
1580Sstevel@tonic-gate  *	   another possibility.)
1590Sstevel@tonic-gate  *
1600Sstevel@tonic-gate  *	8. The handler should neither read nor write any other
1610Sstevel@tonic-gate  *	   processor register nor kernel data item which is not
1620Sstevel@tonic-gate  *	   explicitly mentioned in this list.  [Yes, this is rather
1630Sstevel@tonic-gate  *	   strict; the intent here is that as handler implementations
1640Sstevel@tonic-gate  *	   are done, and more experience is gained, additional items
1650Sstevel@tonic-gate  *	   may be permitted.]
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  *   Handler Exit:
1690Sstevel@tonic-gate  *
1700Sstevel@tonic-gate  *	When the handler's processing is complete, the handler must
1710Sstevel@tonic-gate  *	exit by jumping to the label dmv_finish_intr.  At this time,
1720Sstevel@tonic-gate  *	the handler may optionally request the execution of a soft
1730Sstevel@tonic-gate  *	interrupt routine in order to do further processing at normal
1740Sstevel@tonic-gate  *	interrupt level.  It is strongly advised that drivers do
1750Sstevel@tonic-gate  *	minimal processing in their databearing mondo handlers;
1760Sstevel@tonic-gate  *	whenever possible, tasks should be postponed to a later
1770Sstevel@tonic-gate  *	soft interrupt routine.  (This is analogous to the DDI
1780Sstevel@tonic-gate  *	"high-level interrupt" concept, although a databearing mondo
1790Sstevel@tonic-gate  *	handler's environment is even more restrictive than that of
1800Sstevel@tonic-gate  *	a high-level interrupt routine.)
1810Sstevel@tonic-gate  *
1820Sstevel@tonic-gate  *	Soft interrupt routines should be registered by calling
1830Sstevel@tonic-gate  *	add_softintr(), which will return an interrupt number.  This
1840Sstevel@tonic-gate  *	interrupt number should be saved in a driver-private data
1850Sstevel@tonic-gate  *	structure for later use.
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  *	The contents of %g1 on entry to dmv_finish_intr determine
1880Sstevel@tonic-gate  *	whether a soft interrupt routine will be called, as follows:
1890Sstevel@tonic-gate  *
1900Sstevel@tonic-gate  *		If %g1 is less than zero, no interrupt will be queued.
1910Sstevel@tonic-gate  *
1920Sstevel@tonic-gate  *		Otherwise, %g1 is assumed to be an interrupt number
1930Sstevel@tonic-gate  *		obtained from add_softintr.  This interrupt routine
1940Sstevel@tonic-gate  *		will be executed in the normal way at the requested
1950Sstevel@tonic-gate  *		priority.  (Note that this routine may or may not
1960Sstevel@tonic-gate  *		execute on the same CPU as the current handler.)
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate int
dmv_add_intr(int dmv_inum,void (* routine)(),void * arg)2000Sstevel@tonic-gate dmv_add_intr(int dmv_inum, void (*routine)(), void *arg)
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate 	if (dmv_inum < 0 || dmv_inum >= dmv_hwint)
2030Sstevel@tonic-gate 		return (-1);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	mutex_enter(&dmv_disp_lock);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	if (dmv_dispatch_table[dmv_inum].dmv_func != 0) {
2080Sstevel@tonic-gate 		mutex_exit(&dmv_disp_lock);
2090Sstevel@tonic-gate 		return (-1);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	dmv_dispatch_table[dmv_inum].dmv_arg = arg;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	membar_sync();
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	dmv_dispatch_table[dmv_inum].dmv_func = routine;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	mutex_exit(&dmv_disp_lock);
2190Sstevel@tonic-gate 	return (0);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate  * dmv_add_softintr is called to add a databearing mondo interrupt
2240Sstevel@tonic-gate  * handler for a pseudo-device to the system.
2250Sstevel@tonic-gate  *
2260Sstevel@tonic-gate  * Parameters:
2270Sstevel@tonic-gate  *	routine		pointer to the device's vectored interrupt
2280Sstevel@tonic-gate  *			handler.  This routine is subject to the
2290Sstevel@tonic-gate  *			constraints outlined above in "Handler
2300Sstevel@tonic-gate  *			Characteristics and Environment".
2310Sstevel@tonic-gate  *
2320Sstevel@tonic-gate  *	arg		argument which will be passed to the device's
2330Sstevel@tonic-gate  *			handler.
2340Sstevel@tonic-gate  *
2350Sstevel@tonic-gate  * Return value:	dmv_inum allocated if one was available, -1 if
2360Sstevel@tonic-gate  *			all soft dmv_inums are already allocated
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate int
dmv_add_softintr(void (* routine)(void),void * arg)2400Sstevel@tonic-gate dmv_add_softintr(void (*routine)(void), void *arg)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	int i;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	mutex_enter(&dmv_disp_lock);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	for (i = dmv_hwint; i < dmv_totalints; i++) {
2470Sstevel@tonic-gate 		if (dmv_dispatch_table[i].dmv_func == 0) {
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 			dmv_dispatch_table[i].dmv_arg = arg;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 			membar_sync();
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 			dmv_dispatch_table[i].dmv_func = routine;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 			mutex_exit(&dmv_disp_lock);
2560Sstevel@tonic-gate 			return (i);
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	mutex_exit(&dmv_disp_lock);
2610Sstevel@tonic-gate 	return (-1);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate  * dmv_rem_intr is called to remove a databearing interrupt handler
2660Sstevel@tonic-gate  * from the system.
2670Sstevel@tonic-gate  *
2680Sstevel@tonic-gate  * Parameters:
2690Sstevel@tonic-gate  *	dmv_inum	interrupt number for the device.
2700Sstevel@tonic-gate  *
2710Sstevel@tonic-gate  * Return value:	0 if the handler was removed successfully, -1
2720Sstevel@tonic-gate  *			if no handler was registered for the given
2730Sstevel@tonic-gate  *			dmv_inum.
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate int
dmv_rem_intr(int dmv_inum)2770Sstevel@tonic-gate dmv_rem_intr(int dmv_inum)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	if (dmv_inum < 0 || dmv_inum >= (dmv_totalints))
2800Sstevel@tonic-gate 		return (-1);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	mutex_enter(&dmv_disp_lock);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (dmv_dispatch_table[dmv_inum].dmv_func == 0) {
2850Sstevel@tonic-gate 		mutex_exit(&dmv_disp_lock);
2860Sstevel@tonic-gate 		return (-1);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	dmv_dispatch_table[dmv_inum].dmv_func = 0;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	mutex_exit(&dmv_disp_lock);
2920Sstevel@tonic-gate 	return (0);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * Allocate the dmv dispatch table from nucleus data memory.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate int
ndata_alloc_dmv(struct memlist * ndata)3000Sstevel@tonic-gate ndata_alloc_dmv(struct memlist *ndata)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	size_t alloc_sz;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	uint_t plat_hwint = 0;
3050Sstevel@tonic-gate 	uint_t plat_swint = 0;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	void (*plat_dmv_params)(uint_t *, uint_t *);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	/*
3110Sstevel@tonic-gate 	 * Get platform default values, if they exist
3120Sstevel@tonic-gate 	 */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	plat_dmv_params = (void (*)(uint_t *, uint_t *))
3150Sstevel@tonic-gate 	    kobj_getsymvalue("plat_dmv_params", 0);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (plat_dmv_params)
3180Sstevel@tonic-gate 		(*plat_dmv_params)(&plat_hwint, &plat_swint);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * Set sizes to platform defaults if user hasn't set them
3220Sstevel@tonic-gate 	 */
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (dmv_hwint == 0)
3250Sstevel@tonic-gate 		dmv_hwint = plat_hwint;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (dmv_swint == 0)
3280Sstevel@tonic-gate 		dmv_swint = plat_swint;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * Allocate table if we need it
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	dmv_totalints = dmv_hwint + dmv_swint;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (dmv_totalints != 0) {
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		alloc_sz = sizeof (struct dmv_disp) * (dmv_totalints + 1);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 		dmv_dispatch_table = ndata_alloc(ndata, alloc_sz,
3400Sstevel@tonic-gate 		    sizeof (struct dmv_disp));
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		if (dmv_dispatch_table == NULL)
3430Sstevel@tonic-gate 			return (-1);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		bzero(dmv_dispatch_table, alloc_sz);
346568Seota 		/* use uintptr_t to suppress the gcc warning */
347568Seota 		PRM_DEBUG((uintptr_t)dmv_dispatch_table);
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	return (0);
3510Sstevel@tonic-gate }
352