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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2006 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#include <sys/promif.h>
30#include <sys/promimpl.h>
31
32/*
33 * The functions in this file are used to control the pre- and post-processing
34 * functions that bracket calls to the OBP CIF handler.  One set, promif_preprom
35 * and promif_postprom, are provided for general kernel use.  The other set,
36 * promif_preout and promif_postout, are used by the power management subsystem
37 * to ensure that the framebuffer is active when PROM functions that interact
38 * with the console are invoked.
39 *
40 * In some cases, the operation of these functions must be suppressed.  As such,
41 * this file provides the ability to suspend and resume use of both sets
42 * simultaneously.  Complicating matters is the fact that both current uses of
43 * the pre- and post-processor suspension and resume facilities, kmdb and CPR
44 * may be used simultaneously.  We therefore support normal operation and two
45 * levels of suspension.  The pre- and post-processing functions are only
46 * called during normal operation.  With each suspension request, this
47 * subsystem enters the first suspension level, or passes to the second
48 * suspension level, as appropriate.  Resume calls decrement the suspension
49 * level.  Only two nested suspensions are supported.
50 *
51 * As indicated above, the two current users are CPR and kmdb.  CPR must prevent
52 * kernel accesses outside of the nucleus page during the late stages of system
53 * suspension and during the early stages of system resumption.  As such, the
54 * PM-related processing must not occur during these times.
55 *
56 * The platform-specific portions of kmdb live in the platmods, and thus execute
57 * in the linker environment of the platmods.  That is, any promif calls they
58 * may make are executed by the kernel copies of those functions, rather than
59 * the versions included with kmdb.  The only difference between the two copies
60 * being the nonuse of the pre- and post-processing functions in the kmdb
61 * versions, we must ensure that these functions are not used when the kmdb
62 * platmod code executes.  Accordingly, kmdb disables the pre- and post-
63 * processing functions via the KDI prior to passing control to the platmod
64 * debugger code.
65 */
66
67static int promif_suspendlevel;
68
69static promif_preprom_f *promif_preprom_fn;
70static promif_postprom_f *promif_postprom_fn;
71
72/*
73 * When this is set, the PROM output functions attempt to
74 * redirect output to the kernel terminal emulator.
75 */
76promif_redir_t promif_redirect;
77promif_redir_arg_t promif_redirect_arg;
78
79/*
80 * Sets new callback and argument, returns previous callback.
81 */
82void
83prom_set_stdout_redirect(promif_redir_t new_fn, promif_redir_arg_t opaque_arg)
84{
85	promif_redirect_arg = opaque_arg;
86	promif_redirect = new_fn;
87}
88
89void
90prom_set_preprom(promif_preprom_f *new)
91{
92	promif_preprom_fn = new;
93}
94
95void
96prom_set_postprom(promif_postprom_f *new)
97{
98	promif_postprom_fn = new;
99}
100
101void
102promif_preprom(void)
103{
104	if (promif_suspendlevel == 0 && promif_preprom_fn != NULL)
105		promif_preprom_fn();
106}
107
108void
109promif_postprom(void)
110{
111	if (promif_suspendlevel == 0 && promif_postprom_fn != NULL)
112		promif_postprom_fn();
113}
114
115/*
116 * The reader will note that the layout and calling conventions of the
117 * prom_preout and prom_postout functions differ from the prom_preprom and
118 * prom_postprom functions, above.  At the time the preout and postout
119 * functions are initialized, kernel startup is well underway.  There exists
120 * a race condition whereby a PROM call may begin before preout has been
121 * initialized, and may end after postout has been initialized.  In such
122 * cases, there will be a call to postout without a corresponding preout
123 * call.  The preprom and postprom calls above are initialized early enough
124 * that this race condition does not occur.
125 *
126 * To avoid the race condition, the preout/postout functions are designed
127 * such that the initialization is atomic.  Further, the preout call returns
128 * a data structure that includes a pointer to the postout function that
129 * corresponds to the invoked preout function.  This ensures that the preout
130 * and postout functions will only be used as a matched set.
131 */
132
133static void
134null_outfunc(void)
135{
136}
137
138static promif_owrap_t nullwrapper =
139{
140	null_outfunc,
141	null_outfunc
142};
143
144static promif_owrap_t *wrapper = &nullwrapper;
145static promif_owrap_t pmwrapper;
146
147promif_owrap_t
148*promif_preout(void)
149{
150	promif_owrap_t *ow;
151
152	if (promif_suspendlevel > 0)
153		return (&nullwrapper);
154
155	ow = wrapper;
156	if (ow->preout != NULL)
157		(ow->preout)();
158	return (ow);
159}
160
161void
162promif_postout(promif_owrap_t *ow)
163{
164	if (ow->postout != NULL)
165		(ow->postout)();
166}
167
168void
169prom_set_outfuncs(void (*pref)(void), void (*postf)(void))
170{
171	pmwrapper.preout = pref;
172	pmwrapper.postout = postf;
173	wrapper = &pmwrapper;
174}
175
176void
177prom_suspend_prepost(void)
178{
179	ASSERT(promif_suspendlevel < 2);
180
181	promif_suspendlevel++;
182}
183
184void
185prom_resume_prepost(void)
186{
187	ASSERT(promif_suspendlevel >= 0);
188
189	promif_suspendlevel--;
190}
191