xref: /illumos-gate/usr/src/uts/sun4u/cpu/opl_kdi.c (revision 8682d1ef)
125cf1a30Sjl /*
225cf1a30Sjl  * CDDL HEADER START
325cf1a30Sjl  *
425cf1a30Sjl  * The contents of this file are subject to the terms of the
525cf1a30Sjl  * Common Development and Distribution License (the "License").
625cf1a30Sjl  * You may not use this file except in compliance with the License.
725cf1a30Sjl  *
825cf1a30Sjl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925cf1a30Sjl  * or http://www.opensolaris.org/os/licensing.
1025cf1a30Sjl  * See the License for the specific language governing permissions
1125cf1a30Sjl  * and limitations under the License.
1225cf1a30Sjl  *
1325cf1a30Sjl  * When distributing Covered Code, include this CDDL HEADER in each
1425cf1a30Sjl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525cf1a30Sjl  * If applicable, add the following below this CDDL HEADER, with the
1625cf1a30Sjl  * fields enclosed by brackets "[]" replaced with your own identifying
1725cf1a30Sjl  * information: Portions Copyright [yyyy] [name of copyright owner]
1825cf1a30Sjl  *
1925cf1a30Sjl  * CDDL HEADER END
2025cf1a30Sjl  */
2125cf1a30Sjl /*
2225cf1a30Sjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2325cf1a30Sjl  * Use is subject to license terms.
2425cf1a30Sjl  */
2525cf1a30Sjl 
2625cf1a30Sjl /*
2725cf1a30Sjl  * CPU-specific functions needed by the Kernel-Debugger Interface (KDI).  These
2825cf1a30Sjl  * functions are invoked directly by the kernel debugger (kmdb) while the system
2925cf1a30Sjl  * has been stopped, and as such must not use any kernel facilities that block
3025cf1a30Sjl  * or otherwise rely on forward progress by other parts of the kernel.
3125cf1a30Sjl  *
3225cf1a30Sjl  * These functions may also be called before unix`_start, and as such cannot
3325cf1a30Sjl  * use any kernel facilities that must be initialized as part of system start.
3425cf1a30Sjl  * An example of such a facility is drv_usecwait(), which relies on a parameter
3525cf1a30Sjl  * that is initialized by the unix module.  As a result, drv_usecwait() may not
3625cf1a30Sjl  * be used by KDI functions.
3725cf1a30Sjl  */
3825cf1a30Sjl 
3925cf1a30Sjl #include <sys/types.h>
4025cf1a30Sjl #include <sys/systm.h>
4125cf1a30Sjl #include <sys/archsystm.h>
4225cf1a30Sjl #include <sys/machsystm.h>
4325cf1a30Sjl #include <sys/cpu_module.h>
4425cf1a30Sjl #include <sys/xc_impl.h>
4525cf1a30Sjl #include <sys/intreg.h>
4625cf1a30Sjl #include <sys/kdi_impl.h>
4725cf1a30Sjl 
4825cf1a30Sjl /*
4925cf1a30Sjl  * We keep our own copies, used for cache flushing, because we can be called
5025cf1a30Sjl  * before cpu_fiximpl().
5125cf1a30Sjl  */
5225cf1a30Sjl static int kdi_dcache_size;
5325cf1a30Sjl static int kdi_dcache_linesize;
5425cf1a30Sjl static int kdi_icache_size;
5525cf1a30Sjl static int kdi_icache_linesize;
5625cf1a30Sjl 
5725cf1a30Sjl /*
5825cf1a30Sjl  * Assembly support for SPARC64-VI modules in opl_olympus_asm.s
5925cf1a30Sjl  */
6025cf1a30Sjl extern int idsr_busy(void);
6125cf1a30Sjl extern void init_mondo_nocheck(xcfunc_t *func, uint64_t arg1, uint64_t arg2);
6225cf1a30Sjl extern void shipit(int, int);
6325cf1a30Sjl extern void kdi_flush_idcache(int, int, int, int);
6425cf1a30Sjl extern int kdi_get_stick(uint64_t *);
6525cf1a30Sjl 
66*8682d1efSRichard Lowe static void kdi_tickwait(clock_t);
67*8682d1efSRichard Lowe 
6825cf1a30Sjl static int
kdi_cpu_ready_iter(int (* cb)(int,void *),void * arg)6925cf1a30Sjl kdi_cpu_ready_iter(int (*cb)(int, void *), void *arg)
7025cf1a30Sjl {
7125cf1a30Sjl 	int rc, i;
7225cf1a30Sjl 
7325cf1a30Sjl 	for (rc = 0, i = 0; i < NCPU; i++) {
7425cf1a30Sjl 		if (CPU_IN_SET(cpu_ready_set, i))
7525cf1a30Sjl 			rc += cb(i, arg);
7625cf1a30Sjl 	}
7725cf1a30Sjl 
7825cf1a30Sjl 	return (rc);
7925cf1a30Sjl }
8025cf1a30Sjl 
8125cf1a30Sjl /*
8225cf1a30Sjl  * Sends a cross-call to a specified processor.  The caller assumes
8325cf1a30Sjl  * responsibility for repetition of cross-calls, as appropriate (MARSA for
8425cf1a30Sjl  * debugging).
8525cf1a30Sjl  */
8625cf1a30Sjl static int
kdi_xc_one(int cpuid,void (* func)(uintptr_t,uintptr_t),uintptr_t arg1,uintptr_t arg2)8725cf1a30Sjl kdi_xc_one(int cpuid, void (*func)(uintptr_t, uintptr_t), uintptr_t arg1,
8825cf1a30Sjl     uintptr_t arg2)
8925cf1a30Sjl {
9025cf1a30Sjl 	uint64_t idsr;
9125cf1a30Sjl 	uint64_t endtick, tick;
9225cf1a30Sjl 
9325cf1a30Sjl 	init_mondo_nocheck((xcfunc_t *)func, arg1, arg2);
9425cf1a30Sjl 
9525cf1a30Sjl 	shipit(cpuid, 0);
9625cf1a30Sjl 
9725cf1a30Sjl 	/* Spin for at most 1 second for checking */
9825cf1a30Sjl 	endtick = gettick() + (uint64_t)sys_tick_freq;
9925cf1a30Sjl 
10025cf1a30Sjl 	idsr = getidsr();
10125cf1a30Sjl 
10225cf1a30Sjl 	if (idsr & IDSR_BUSY) {
10325cf1a30Sjl 		do {
10425cf1a30Sjl 			idsr = getidsr();
10525cf1a30Sjl 			tick = gettick();
10625cf1a30Sjl 			if (tick > endtick) {
10725cf1a30Sjl 				return (KDI_XC_RES_BUSY);
10825cf1a30Sjl 			}
10925cf1a30Sjl 		} while (idsr & IDSR_BUSY);
11025cf1a30Sjl 	}
11125cf1a30Sjl 
11225cf1a30Sjl 	kdi_tickwait(20000);
11325cf1a30Sjl 
11425cf1a30Sjl 	if (idsr & IDSR_NACK)
11525cf1a30Sjl 		return (KDI_XC_RES_NACK);
11625cf1a30Sjl 	else
11725cf1a30Sjl 		return (KDI_XC_RES_OK);
11825cf1a30Sjl }
11925cf1a30Sjl 
12025cf1a30Sjl static void
kdi_tickwait(clock_t nticks)12125cf1a30Sjl kdi_tickwait(clock_t nticks)
12225cf1a30Sjl {
12325cf1a30Sjl 	clock_t endtick = gettick() + nticks;
12425cf1a30Sjl 
125*8682d1efSRichard Lowe 	while (gettick() < endtick)
126*8682d1efSRichard Lowe 		;
12725cf1a30Sjl }
12825cf1a30Sjl 
12925cf1a30Sjl static void
kdi_cpu_init(int dcache_size,int dcache_linesize,int icache_size,int icache_linesize)13025cf1a30Sjl kdi_cpu_init(int dcache_size, int dcache_linesize, int icache_size,
13125cf1a30Sjl     int icache_linesize)
13225cf1a30Sjl {
13325cf1a30Sjl 	kdi_dcache_size = dcache_size;
13425cf1a30Sjl 	kdi_dcache_linesize = dcache_linesize;
13525cf1a30Sjl 	kdi_icache_size = icache_size;
13625cf1a30Sjl 	kdi_icache_linesize = icache_linesize;
13725cf1a30Sjl }
13825cf1a30Sjl 
13925cf1a30Sjl /* used directly by kdi_read/write_phys */
14025cf1a30Sjl void
kdi_flush_caches(void)14125cf1a30Sjl kdi_flush_caches(void)
14225cf1a30Sjl {
14325cf1a30Sjl 	kdi_flush_idcache(kdi_dcache_size, kdi_dcache_linesize,
14425cf1a30Sjl 	    kdi_icache_size, kdi_icache_linesize);
14525cf1a30Sjl }
14625cf1a30Sjl 
14725cf1a30Sjl void
cpu_kdi_init(kdi_t * kdi)14825cf1a30Sjl cpu_kdi_init(kdi_t *kdi)
14925cf1a30Sjl {
15025cf1a30Sjl 	kdi->kdi_flush_caches = kdi_flush_caches;
15125cf1a30Sjl 	kdi->mkdi_cpu_init = kdi_cpu_init;
15225cf1a30Sjl 	kdi->mkdi_cpu_ready_iter = kdi_cpu_ready_iter;
15325cf1a30Sjl 	kdi->mkdi_xc_one = kdi_xc_one;
15425cf1a30Sjl 	kdi->mkdi_tickwait = kdi_tickwait;
15525cf1a30Sjl 	kdi->mkdi_get_stick = kdi_get_stick;
15625cf1a30Sjl }
157