summaryrefslogtreecommitdiff
path: root/usr/src/psm/stand/cpr/sparcv9/sun4u/machdep.c
blob: 95e4b4e143f5ca279d140ffc0f6f35bd27a55c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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
 * or http://www.opensolaris.org/os/licensing.
 * 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 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/cpr.h>
#include <sys/promimpl.h>
#include <sys/privregs.h>
#include <sys/stack.h>
#include <sys/cpuvar.h>
#include "cprboot.h"


#define	TIMEOUT_MSECS	1000


/*
 * globals
 */
int cpr_test_mode;
csu_md_t mdinfo;
caddr_t tmp_stack;
uint_t cb_mid;
uint_t cb_clock_freq;
uint_t cpu_delay;


/*
 * file scope
 */
typedef void (*tlb_func_t)(int, caddr_t, tte_t *);
static uint_t mdlen;
static cpuset_t slave_set;
static int has_scbc;


/*
 * check machdep desc and cpr_machdep info
 *
 * sets globals:
 *	mdinfo
 *	mdlen
 */
int
cb_check_machdep(void)
{
	uint16_t wst32, wst64;
	char *fmt, *str;
	cmd_t cmach;

	str = "cb_check_machdep";
	CB_VPRINTF((ent_fmt, str, entry));

	/*
	 * get machdep desc and set length of prom words
	 */
	SF_DCOPY(cmach);
	if (cmach.md_magic != CPR_MACHDEP_MAGIC) {
		prom_printf("%s: bad machdep magic 0x%x, expect 0x%x\n",
		    str, cmach.md_magic, CPR_MACHDEP_MAGIC);
		return (ERR);
	}
	mdlen = cmach.md_size - sizeof (csu_md_t);

	/*
	 * get machep info, check for valid stack bias and wstate
	 */
	SF_DCOPY(mdinfo);
	fmt = "found bad statefile data: %s (0x%x), expect 0x%x or 0x%x\n";
	if (mdinfo.ksb != 0x0 && mdinfo.ksb != V9BIAS64) {
		prom_printf(fmt, "stack bias", mdinfo.ksb, 0, V9BIAS64);
		return (ERR);
	}
	wst32 = WSTATE(WSTATE_U32, WSTATE_K32);
	wst64 = WSTATE(WSTATE_U32, WSTATE_K64);
	if (mdinfo.kwstate != wst32 && mdinfo.kwstate != wst64) {
		prom_printf(fmt, "wstate", mdinfo.kwstate, wst32, wst64);
		return (ERR);
	}

	return (0);
}


/*
 * interpret saved prom words
 */
int
cb_interpret(void)
{
	int bytes, wlen, s;
	char minibuf[60];
	char *words;

	CB_VENTRY(cb_interpret);

	/*
	 * The variable length machdep section for sun4u consists of
	 * a sequence of null-terminated strings stored contiguously.
	 *
	 * The first string defines Forth words which help the prom
	 * handle kernel translations.
	 *
	 * The second string defines Forth words required by kadb to
	 * interface with the prom when a trap is taken.
	 */
	words = SF_DATA();
	bytes = mdlen;
	while (bytes) {
		wlen = prom_strlen(words) + 1;	/* include the null */
		if (verbose) {
			s = sizeof (minibuf) - 4;
			(void) prom_strncpy(minibuf, words, s);
			if (wlen > s)
				(void) prom_strcpy(&minibuf[s], "...");
			prom_printf("    interpret \"%s\"\n", minibuf);
		}
		prom_interpret(words, 0, 0, 0, 0, 0);
		words += wlen;
		bytes -= wlen;
	}

	/* advance past prom words */
	SF_ADV(mdlen);

	return (0);
}


/*
 * write dtlb/itlb entries
 */
static void
restore_tlb(struct sun4u_tlb *utp, int cpu_id)
{
	struct sun4u_tlb *tail;
	tlb_func_t tfunc;
	caddr_t virt;
	char tname;

	if (utp == mdinfo.dtte) {
		tfunc = set_dtlb_entry;
		tname = 'd';
	} else if (utp == mdinfo.itte) {
		tfunc = set_itlb_entry;
		tname = 'i';
	}

	for (tail = utp + CPR_MAX_TLB; utp < tail; utp++) {
		if (utp->va_tag == NULL)
			continue;
		virt = (caddr_t)utp->va_tag;
		(*tfunc)(utp->index, virt, &utp->tte);
		if (verbose || CPR_DBG(4)) {
			prom_printf("    cpu_id %d: write %ctlb "
			    "(index %x, virt 0x%lx, size 0x%x)\n",
			    cpu_id, tname, utp->index, utp->va_tag,
			    TTEBYTES(utp->tte.tte_size));
		}
	}
}


/*
 * install locked tlb entries for the kernel and cpr module;
 * also sets up the tmp stack
 */
int
cb_ksetup(void)
{
	CB_VENTRY(cb_ksetup);

	restore_tlb(mdinfo.dtte, cb_mid);
	restore_tlb(mdinfo.itte, cb_mid);
	tmp_stack = (caddr_t)(mdinfo.tmp_stack + mdinfo.tmp_stacksize);

	return (0);
}


static void
cb_park_err(int cpu_id)
{
	prom_printf("\ncpu_id %d did not stop!...\n", cpu_id);
	cb_exit_to_mon();
}


/*
 * local copy of an older interface for OBP revs < 4.6
 */
static int
cb_prom_stop_self(void)
{
	cell_t ci[3];

	ci[0] = p1275_ptr2cell("SUNW,stop-self");	/* Service name */
	ci[1] = (cell_t)0;			/* #argument cells */
	ci[2] = (cell_t)0;			/* #result cells */
	(void) p1275_cif_handler(&ci);		/* Do NOT lock */
	return (0);
}


/*
 * install locked tlb entries and spin or park in a prom idle-loop
 */
void
slave_init(int cpu_id)
{
	restore_tlb(mdinfo.dtte, cpu_id);
	restore_tlb(mdinfo.itte, cpu_id);
	CPUSET_ADD(slave_set, cpu_id);
	membar_stld();
	if (has_scbc) {
		/* just spin, master will park this cpu */
		/* CONSTCOND */
		while (1);
	} else {
		(void) cb_prom_stop_self();
		cb_park_err(cpu_id);
	}
}


/*
 * when any cpu is started, they naturally rely on the prom for all
 * text/data translations until switching to the kernel trap table.
 * to jump back into the cpr module and to restart slave cpus, cprboot
 * needs to reinstall translations for the nucleus and some cpr pages.
 *
 * the easy method is creating one set of global translations available
 * to all cpus with prom_map(); unfortunately, a 4MB "map" request will
 * allocate and overwrite a few pages, and these are often kernel pages
 * that were just restored.
 *
 * to solve the "map" problem, all cpus install their own set of locked
 * tlb entries to translate the nucleus and parts of the cpr module;
 * after all cpus have switched to kernel traps, any of these locked
 * tlb entries for pages outside the nucleus will be cleared.
 */
int
cb_mpsetup(void)
{
	struct sun4u_cpu_info *scip, *tail;
	int timeout, ncpu;
	char *str, *intf;

	intf = "SUNW,stop-cpu-by-cpuid";
	has_scbc = (prom_test(intf) == 0);
	CB_VPRINTF(("\n\"%s\" test %d\n", intf, has_scbc));

	str = "cb_mp_setup";
	CB_VPRINTF((ent_fmt, str, entry));

	/*
	 * launch any slave cpus from the .sci array into cprboot text
	 * and wait about a second for them to checkin with slave_set
	 */
	ncpu = 0;
	CPUSET_ZERO(slave_set);
	for (scip = mdinfo.sci, tail = scip + NCPU; scip < tail; scip++) {
		if (scip->node == 0 || scip->cpu_id == cb_mid)
			continue;
		(void) prom_startcpu(scip->node,
		    (caddr_t)cpu_launch, scip->cpu_id);

		for (timeout = TIMEOUT_MSECS; timeout; timeout--) {
			if (CPU_IN_SET(slave_set, scip->cpu_id))
				break;
			cb_usec_wait(MILLISEC);
		}

		if (timeout == 0) {
			prom_printf("\n%s: cpu did not start, "
			    "cpu_id %d, node 0x%x\n",
			    prog, scip->cpu_id, scip->node);
			return (ERR);
		}

		if (has_scbc && prom_stopcpu_bycpuid(scip->cpu_id))
			cb_park_err(scip->cpu_id);

		ncpu++;
	}

	if (verbose && ncpu)
		prom_printf("\n%s: slave cpu count: %d\n", str, ncpu);

	return (0);
}