xref: /onnv-gate/usr/src/psm/promif/ieee1275/common/prom_env.c (revision 1253:0df630a41817)
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*1253Slq150181  * Common Development and Distribution License (the "License").
6*1253Slq150181  * 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  */
21*1253Slq150181 
220Sstevel@tonic-gate /*
23*1253Slq150181  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/promif.h>
300Sstevel@tonic-gate #include <sys/promimpl.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * The functions in this file are used to control the pre- and post-processing
340Sstevel@tonic-gate  * functions that bracket calls to the OBP CIF handler.  One set, promif_preprom
350Sstevel@tonic-gate  * and promif_postprom, are provided for general kernel use.  The other set,
360Sstevel@tonic-gate  * promif_preout and promif_postout, are used by the power management subsystem
370Sstevel@tonic-gate  * to ensure that the framebuffer is active when PROM functions that interact
380Sstevel@tonic-gate  * with the console are invoked.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * In some cases, the operation of these functions must be suppressed.  As such,
410Sstevel@tonic-gate  * this file provides the ability to suspend and resume use of both sets
420Sstevel@tonic-gate  * simultaneously.  Complicating matters is the fact that both current uses of
430Sstevel@tonic-gate  * the pre- and post-processor suspension and resume facilities, kmdb and CPR
440Sstevel@tonic-gate  * may be used simultaneously.  We therefore support normal operation and two
450Sstevel@tonic-gate  * levels of suspension.  The pre- and post-processing functions are only
460Sstevel@tonic-gate  * called during normal operation.  With each suspension request, this
470Sstevel@tonic-gate  * subsystem enters the first suspension level, or passes to the second
480Sstevel@tonic-gate  * suspension level, as appropriate.  Resume calls decrement the suspension
490Sstevel@tonic-gate  * level.  Only two nested suspensions are supported.
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * As indicated above, the two current users are CPR and kmdb.  CPR must prevent
520Sstevel@tonic-gate  * kernel accesses outside of the nucleus page during the late stages of system
530Sstevel@tonic-gate  * suspension and during the early stages of system resumption.  As such, the
540Sstevel@tonic-gate  * PM-related processing must not occur during these times.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * The platform-specific portions of kmdb live in the platmods, and thus execute
570Sstevel@tonic-gate  * in the linker environment of the platmods.  That is, any promif calls they
580Sstevel@tonic-gate  * may make are executed by the kernel copies of those functions, rather than
590Sstevel@tonic-gate  * the versions included with kmdb.  The only difference between the two copies
600Sstevel@tonic-gate  * being the nonuse of the pre- and post-processing functions in the kmdb
610Sstevel@tonic-gate  * versions, we must ensure that these functions are not used when the kmdb
620Sstevel@tonic-gate  * platmod code executes.  Accordingly, kmdb disables the pre- and post-
630Sstevel@tonic-gate  * processing functions via the KDI prior to passing control to the platmod
640Sstevel@tonic-gate  * debugger code.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static int promif_suspendlevel;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static promif_preprom_f *promif_preprom_fn;
700Sstevel@tonic-gate static promif_postprom_f *promif_postprom_fn;
710Sstevel@tonic-gate 
72*1253Slq150181 /*
73*1253Slq150181  * When this is set, the PROM output functions attempt to
74*1253Slq150181  * redirect output to the kernel terminal emulator.
75*1253Slq150181  */
76*1253Slq150181 promif_redir_t promif_redirect;
77*1253Slq150181 promif_redir_arg_t promif_redirect_arg;
78*1253Slq150181 
79*1253Slq150181 /*
80*1253Slq150181  * Sets new callback and argument, returns previous callback.
81*1253Slq150181  */
82*1253Slq150181 void
prom_set_stdout_redirect(promif_redir_t new_fn,promif_redir_arg_t opaque_arg)83*1253Slq150181 prom_set_stdout_redirect(promif_redir_t new_fn, promif_redir_arg_t opaque_arg)
84*1253Slq150181 {
85*1253Slq150181 	promif_redirect_arg = opaque_arg;
86*1253Slq150181 	promif_redirect = new_fn;
87*1253Slq150181 }
88*1253Slq150181 
890Sstevel@tonic-gate void
prom_set_preprom(promif_preprom_f * new)900Sstevel@tonic-gate prom_set_preprom(promif_preprom_f *new)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	promif_preprom_fn = new;
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate void
prom_set_postprom(promif_postprom_f * new)960Sstevel@tonic-gate prom_set_postprom(promif_postprom_f *new)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	promif_postprom_fn = new;
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate void
promif_preprom(void)1020Sstevel@tonic-gate promif_preprom(void)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	if (promif_suspendlevel == 0 && promif_preprom_fn != NULL)
1050Sstevel@tonic-gate 		promif_preprom_fn();
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate void
promif_postprom(void)1090Sstevel@tonic-gate promif_postprom(void)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate 	if (promif_suspendlevel == 0 && promif_postprom_fn != NULL)
1120Sstevel@tonic-gate 		promif_postprom_fn();
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * The reader will note that the layout and calling conventions of the
1170Sstevel@tonic-gate  * prom_preout and prom_postout functions differ from the prom_preprom and
1180Sstevel@tonic-gate  * prom_postprom functions, above.  At the time the preout and postout
1190Sstevel@tonic-gate  * functions are initialized, kernel startup is well underway.  There exists
1200Sstevel@tonic-gate  * a race condition whereby a PROM call may begin before preout has been
1210Sstevel@tonic-gate  * initialized, and may end after postout has been initialized.  In such
1220Sstevel@tonic-gate  * cases, there will be a call to postout without a corresponding preout
1230Sstevel@tonic-gate  * call.  The preprom and postprom calls above are initialized early enough
1240Sstevel@tonic-gate  * that this race condition does not occur.
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  * To avoid the race condition, the preout/postout functions are designed
1270Sstevel@tonic-gate  * such that the initialization is atomic.  Further, the preout call returns
1280Sstevel@tonic-gate  * a data structure that includes a pointer to the postout function that
1290Sstevel@tonic-gate  * corresponds to the invoked preout function.  This ensures that the preout
1300Sstevel@tonic-gate  * and postout functions will only be used as a matched set.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static void
null_outfunc(void)1340Sstevel@tonic-gate null_outfunc(void)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static promif_owrap_t nullwrapper =
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	null_outfunc,
1410Sstevel@tonic-gate 	null_outfunc
1420Sstevel@tonic-gate };
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static promif_owrap_t *wrapper = &nullwrapper;
1450Sstevel@tonic-gate static promif_owrap_t pmwrapper;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate promif_owrap_t
promif_preout(void)1480Sstevel@tonic-gate *promif_preout(void)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	promif_owrap_t *ow;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (promif_suspendlevel > 0)
1530Sstevel@tonic-gate 		return (&nullwrapper);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	ow = wrapper;
1560Sstevel@tonic-gate 	if (ow->preout != NULL)
1570Sstevel@tonic-gate 		(ow->preout)();
1580Sstevel@tonic-gate 	return (ow);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate void
promif_postout(promif_owrap_t * ow)1620Sstevel@tonic-gate promif_postout(promif_owrap_t *ow)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	if (ow->postout != NULL)
1650Sstevel@tonic-gate 		(ow->postout)();
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate void
prom_set_outfuncs(void (* pref)(void),void (* postf)(void))1690Sstevel@tonic-gate prom_set_outfuncs(void (*pref)(void), void (*postf)(void))
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	pmwrapper.preout = pref;
1720Sstevel@tonic-gate 	pmwrapper.postout = postf;
1730Sstevel@tonic-gate 	wrapper = &pmwrapper;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate void
prom_suspend_prepost(void)1770Sstevel@tonic-gate prom_suspend_prepost(void)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	ASSERT(promif_suspendlevel < 2);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	promif_suspendlevel++;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate void
prom_resume_prepost(void)1850Sstevel@tonic-gate prom_resume_prepost(void)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	ASSERT(promif_suspendlevel >= 0);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	promif_suspendlevel--;
1900Sstevel@tonic-gate }
191