xref: /illumos-gate/usr/src/uts/sun4u/cpu/opl_kdi.c (revision 2aeafac3612e19716bf8164f89c3c9196342979c)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * CPU-specific functions needed by the Kernel-Debugger Interface (KDI).  These
28  * functions are invoked directly by the kernel debugger (kmdb) while the system
29  * has been stopped, and as such must not use any kernel facilities that block
30  * or otherwise rely on forward progress by other parts of the kernel.
31  *
32  * These functions may also be called before unix`_start, and as such cannot
33  * use any kernel facilities that must be initialized as part of system start.
34  * An example of such a facility is drv_usecwait(), which relies on a parameter
35  * that is initialized by the unix module.  As a result, drv_usecwait() may not
36  * be used by KDI functions.
37  */
38 
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/archsystm.h>
42 #include <sys/machsystm.h>
43 #include <sys/cpu_module.h>
44 #include <sys/xc_impl.h>
45 #include <sys/intreg.h>
46 #include <sys/kdi_impl.h>
47 
48 /*
49  * We keep our own copies, used for cache flushing, because we can be called
50  * before cpu_fiximpl().
51  */
52 static int kdi_dcache_size;
53 static int kdi_dcache_linesize;
54 static int kdi_icache_size;
55 static int kdi_icache_linesize;
56 
57 /*
58  * Assembly support for SPARC64-VI modules in opl_olympus_asm.s
59  */
60 extern int idsr_busy(void);
61 extern void init_mondo_nocheck(xcfunc_t *func, uint64_t arg1, uint64_t arg2);
62 extern void shipit(int, int);
63 extern void kdi_flush_idcache(int, int, int, int);
64 extern int kdi_get_stick(uint64_t *);
65 
66 static void kdi_tickwait(clock_t);
67 
68 static int
69 kdi_cpu_ready_iter(int (*cb)(int, void *), void *arg)
70 {
71 	int rc, i;
72 
73 	for (rc = 0, i = 0; i < NCPU; i++) {
74 		if (CPU_IN_SET(cpu_ready_set, i))
75 			rc += cb(i, arg);
76 	}
77 
78 	return (rc);
79 }
80 
81 /*
82  * Sends a cross-call to a specified processor.  The caller assumes
83  * responsibility for repetition of cross-calls, as appropriate (MARSA for
84  * debugging).
85  */
86 static int
87 kdi_xc_one(int cpuid, void (*func)(uintptr_t, uintptr_t), uintptr_t arg1,
88     uintptr_t arg2)
89 {
90 	uint64_t idsr;
91 	uint64_t endtick, tick;
92 
93 	init_mondo_nocheck((xcfunc_t *)func, arg1, arg2);
94 
95 	shipit(cpuid, 0);
96 
97 	/* Spin for at most 1 second for checking */
98 	endtick = gettick() + (uint64_t)sys_tick_freq;
99 
100 	idsr = getidsr();
101 
102 	if (idsr & IDSR_BUSY) {
103 		do {
104 			idsr = getidsr();
105 			tick = gettick();
106 			if (tick > endtick) {
107 				return (KDI_XC_RES_BUSY);
108 			}
109 		} while (idsr & IDSR_BUSY);
110 	}
111 
112 	kdi_tickwait(20000);
113 
114 	if (idsr & IDSR_NACK)
115 		return (KDI_XC_RES_NACK);
116 	else
117 		return (KDI_XC_RES_OK);
118 }
119 
120 static void
121 kdi_tickwait(clock_t nticks)
122 {
123 	clock_t endtick = gettick() + nticks;
124 
125 	while (gettick() < endtick)
126 		;
127 }
128 
129 static void
130 kdi_cpu_init(int dcache_size, int dcache_linesize, int icache_size,
131     int icache_linesize)
132 {
133 	kdi_dcache_size = dcache_size;
134 	kdi_dcache_linesize = dcache_linesize;
135 	kdi_icache_size = icache_size;
136 	kdi_icache_linesize = icache_linesize;
137 }
138 
139 /* used directly by kdi_read/write_phys */
140 void
141 kdi_flush_caches(void)
142 {
143 	kdi_flush_idcache(kdi_dcache_size, kdi_dcache_linesize,
144 	    kdi_icache_size, kdi_icache_linesize);
145 }
146 
147 void
148 cpu_kdi_init(kdi_t *kdi)
149 {
150 	kdi->kdi_flush_caches = kdi_flush_caches;
151 	kdi->mkdi_cpu_init = kdi_cpu_init;
152 	kdi->mkdi_cpu_ready_iter = kdi_cpu_ready_iter;
153 	kdi->mkdi_xc_one = kdi_xc_one;
154 	kdi->mkdi_tickwait = kdi_tickwait;
155 	kdi->mkdi_get_stick = kdi_get_stick;
156 }
157