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 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26/*
27 * SPARC-specific portions of the KDI
28 */
29
30#include <sys/types.h>
31#include <sys/kdi_impl.h>
32
33#include <kmdb/kaif.h>
34#include <kmdb/kmdb_dpi.h>
35#include <kmdb/kmdb_promif.h>
36#include <mdb/mdb_debug.h>
37#include <mdb/mdb_err.h>
38#include <mdb/mdb.h>
39
40#define	KDI_XC_RETRIES			10
41
42static size_t kdi_dcache_size;
43static size_t kdi_dcache_linesize;
44static size_t kdi_icache_size;
45static size_t kdi_icache_linesize;
46
47static uint_t kdi_max_cpu_freq;
48static uint_t kdi_sticks_per_usec;
49
50/* XXX	needs to go into a header */
51
52void
53kdi_usecwait(clock_t n)
54{
55	mdb.m_kdi->mkdi_tickwait(n * kdi_sticks_per_usec);
56}
57
58static int
59kdi_cpu_ready_iter(int (*cb)(int, void *), void *arg)
60{
61	return (mdb.m_kdi->mkdi_cpu_ready_iter(cb, arg));
62}
63
64static int
65kdi_xc_one(int cpuid, void (*cb)(void))
66{
67	return (mdb.m_kdi->mkdi_xc_one(cpuid, (void (*)())cb, (uintptr_t)NULL,
68	    (uintptr_t)NULL));
69}
70
71/*ARGSUSED1*/
72static int
73kdi_init_cpus_cb(pnode_t node, void *arg, void *result)
74{
75	/*
76	 * Sun4v dosen't support virtual address cache
77	 */
78#ifndef	sun4v
79	int dcache_size, dcache_linesize;
80	int icache_size, icache_linesize;
81#endif
82	int cpu_freq;
83
84#ifndef	sun4v
85	/* Get the real cpu property node if needed */
86	node = kmdb_prom_getcpu_propnode(node);
87
88	/*
89	 * data cache
90	 */
91
92	if (kmdb_prom_getprop(node, "dcache-size",
93	    (caddr_t)&dcache_size) == -1 &&
94	    kmdb_prom_getprop(node, "l1-dcache-size",
95	    (caddr_t)&dcache_size) == -1)
96		fail("can't get dcache size for node %x\n", node);
97
98	if (kdi_dcache_size == 0 || dcache_size > kdi_dcache_size)
99		kdi_dcache_size = dcache_size;
100
101	if (kmdb_prom_getprop(node, "dcache-line-size",
102	    (caddr_t)&dcache_linesize) == -1 &&
103	    kmdb_prom_getprop(node, "l1-dcache-line-size",
104	    (caddr_t)&dcache_linesize) == -1)
105		fail("can't get dcache line size for node %x\n", node);
106
107	if (kdi_dcache_linesize == 0 || dcache_linesize < kdi_dcache_linesize)
108		kdi_dcache_linesize = dcache_linesize;
109
110	/*
111	 * instruction cache
112	 */
113
114	if (kmdb_prom_getprop(node, "icache-size",
115	    (caddr_t)&icache_size) == -1 &&
116	    kmdb_prom_getprop(node, "l1-icache-size",
117	    (caddr_t)&icache_size) == -1)
118		fail("can't get icache size for node %x\n", node);
119
120	if (kdi_icache_size == 0 || icache_size > kdi_icache_size)
121		kdi_icache_size = icache_size;
122
123	if (kmdb_prom_getprop(node, "icache-line-size",
124	    (caddr_t)&icache_linesize) == -1 &&
125	    kmdb_prom_getprop(node, "l1-icache-line-size",
126	    (caddr_t)&icache_linesize) == -1)
127		fail("can't get icache size for node %x\n", node);
128
129	if (kdi_icache_linesize == 0 || icache_linesize < kdi_icache_linesize)
130		kdi_icache_linesize = icache_linesize;
131#endif
132
133	if (kmdb_prom_getprop(node, "clock-frequency",
134	    (caddr_t)&cpu_freq) == -1) {
135		fail("can't get cpu frequency for node %x\n", node);
136	}
137
138	kdi_max_cpu_freq = MAX(kdi_max_cpu_freq, cpu_freq);
139
140	return (0);
141}
142
143/*
144 * Called on an individual CPU.  Tries to send it off to the state saver if it
145 * hasn't already entered the debugger.  Returns non-zero if it *fails* to stop
146 * the CPU.
147 */
148static int
149kdi_halt_cpu(int cpuid, void *state_saverp)
150{
151	void (*state_saver)(void) = (void (*)(void))state_saverp;
152	int state = kmdb_dpi_get_cpu_state(cpuid);
153	const char *msg;
154	int rc = 0;
155	int res;
156
157	if (state != DPI_CPU_STATE_MASTER && state != DPI_CPU_STATE_SLAVE) {
158		res = kdi_xc_one(cpuid, state_saver);
159		rc = 1;
160
161		if (res == KDI_XC_RES_OK)
162			msg = "accepted the";
163		else {
164			if (res == KDI_XC_RES_BUSY)
165				msg = "too busy for";
166			else if (res == KDI_XC_RES_NACK)
167				msg = "NACKED the";
168			else
169				msg = "errored the";
170		}
171		mdb_dprintf(MDB_DBG_KDI, "CPU %d %s halt\n", cpuid, msg);
172	}
173
174	return (rc);
175}
176
177/*ARGSUSED1*/
178static int
179kdi_report_unhalted(int cpuid, void *junk)
180{
181	int state = kmdb_dpi_get_cpu_state(cpuid);
182
183	if (state != DPI_CPU_STATE_MASTER && state != DPI_CPU_STATE_SLAVE)
184		mdb_warn("CPU %d: stop failed\n", cpuid);
185
186	return (0);
187}
188
189/*ARGSUSED*/
190void
191kmdb_kdi_stop_slaves(int my_cpuid, int doxc)
192{
193	int i;
194
195	for (i = 0; i < KDI_XC_RETRIES; i++) {
196		if (kdi_cpu_ready_iter(kdi_halt_cpu,
197		    (void *)kaif_slave_entry) == 0)
198			break;
199
200		kdi_usecwait(2000);
201	}
202	(void) kdi_cpu_ready_iter(kdi_report_unhalted, NULL);
203}
204
205void
206kmdb_kdi_start_slaves(void)
207{
208}
209
210void
211kmdb_kdi_slave_wait(void)
212{
213}
214
215int
216kmdb_kdi_get_stick(uint64_t *stickp)
217{
218	return (mdb.m_kdi->mkdi_get_stick(stickp));
219}
220
221caddr_t
222kmdb_kdi_get_trap_vatotte(void)
223{
224	return ((caddr_t)mdb.m_kdi->mkdi_trap_vatotte);
225}
226
227void
228kmdb_kdi_kernpanic(struct regs *regs, uint_t tt)
229{
230	uintptr_t args[2];
231
232	args[0] = (uintptr_t)regs;
233	args[1] = tt;
234
235	(void) kmdb_dpi_call((uintptr_t)mdb.m_kdi->mkdi_kernpanic, 2, args);
236}
237
238/*ARGSUSED*/
239void
240kmdb_kdi_init_isadep(kdi_t *kdi, kmdb_auxv_t *kav)
241{
242	kdi_dcache_size = kdi_dcache_linesize =
243	    kdi_icache_size = kdi_icache_linesize = 0;
244
245	kdi_max_cpu_freq = kdi_sticks_per_usec = 0;
246
247	mdb_dprintf(MDB_DBG_KDI, "Initializing CPUs\n");
248
249	kmdb_prom_walk_cpus(kdi_init_cpus_cb, NULL, NULL);
250
251	/*
252	 * If we can't find one, guess high.  The CPU frequency is going to be
253	 * used to determine the length of various delays, such as the mondo
254	 * interrupt retry delay.  Too long is generally better than too short.
255	 */
256	if (kdi_max_cpu_freq == 0) {
257		mdb_dprintf(MDB_DBG_KDI, "No CPU freq found - assuming "
258		    "500MHz\n");
259		kdi_max_cpu_freq = 500 * MICROSEC;
260	}
261
262	kdi_sticks_per_usec =
263	    MAX((kdi_max_cpu_freq + (MICROSEC - 1)) / MICROSEC, 1);
264
265	mdb.m_kdi->mkdi_cpu_init(kdi_dcache_size, kdi_dcache_linesize,
266	    kdi_icache_size, kdi_icache_linesize);
267
268#ifndef sun4v
269	kmdb_prom_preserve_kctx_init();
270#endif /* sun4v */
271
272}
273