xref: /netbsd-src/sys/arch/mips/include/cpu.h (revision 37b34d511dea595d3ba03a661cf3b775038ea5f8)
1 /*	$NetBSD: cpu.h,v 1.67 2002/08/05 13:00:47 shin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ralph Campbell and Rick Macklem.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)cpu.h	8.4 (Berkeley) 1/4/94
39  */
40 
41 #ifndef _CPU_H_
42 #define _CPU_H_
43 
44 #include <mips/cpuregs.h>
45 
46 /*
47  * Exported definitions unique to NetBSD/mips cpu support.
48  */
49 
50 #ifndef _LOCORE
51 #include <sys/sched.h>
52 
53 #if defined(_KERNEL_OPT)
54 #include "opt_lockdebug.h"
55 #endif
56 
57 struct cpu_info {
58 	struct schedstate_percpu ci_schedstate; /* scheduler state */
59 	u_long ci_cpu_freq;		/* CPU frequency */
60 	u_long ci_cycles_per_hz;	/* CPU freq / hz */
61 	u_long ci_divisor_delay;	/* for delay/DELAY */
62 	u_long ci_divisor_recip;	/* scaled reciprocal of previous;
63 					   see below */
64 #if defined(DIAGNOSTIC) || defined(LOCKDEBUG)
65 	u_long ci_spin_locks;		/* # of spin locks held */
66 	u_long ci_simple_locks;		/* # of simple locks held */
67 #endif
68 };
69 /*
70  * To implement a more accurate microtime using the CP0 COUNT register
71  * we need to divide that register by the number of cycles per MHz.
72  * But...
73  *
74  * DIV and DIVU are expensive on MIPS (eg 75 clocks on the R4000).  MULT
75  * and MULTU are only 12 clocks on the same CPU.
76  *
77  * The strategy we use is to calculate the reciprical of cycles per MHz,
78  * scaled by 1<<32.  Then we can simply issue a MULTU and pluck of the
79  * HI register and have the results of the division.
80  */
81 #define	MIPS_SET_CI_RECIPRICAL(cpu)					\
82 do {									\
83 	KASSERT((cpu)->ci_divisor_delay != 0);				\
84 	(cpu)->ci_divisor_recip = 0x100000000ULL / (cpu)->ci_divisor_delay; \
85 } while (0)
86 
87 #define	MIPS_COUNT_TO_MHZ(cpu, count, res)				\
88 	asm volatile("multu %1,%2 ; mfhi %0"				\
89 	    : "=r"((res)) : "r"((count)), "r"((cpu)->ci_divisor_recip))
90 #endif /* !defined(_LOCORE) */
91 
92 /*
93  * CTL_MACHDEP definitions.
94  */
95 #define CPU_CONSDEV		1	/* dev_t: console terminal device */
96 #define CPU_BOOTED_KERNEL	2	/* string: booted kernel name */
97 #define CPU_ROOT_DEVICE		3	/* string: root device name */
98 #define CPU_LLSC		4	/* OS/CPU supports LL/SC instruction */
99 
100 /*
101  * Platform can override, but note this breaks userland compatibility
102  * with other mips platforms.
103  */
104 #ifndef CPU_MAXID
105 #define CPU_MAXID		5	/* number of valid machdep ids */
106 
107 #define CTL_MACHDEP_NAMES { \
108 	{ 0, 0 }, \
109 	{ "console_device", CTLTYPE_STRUCT }, \
110 	{ "booted_kernel", CTLTYPE_STRING }, \
111 	{ "root_device", CTLTYPE_STRING }, \
112 	{ "llsc", CTLTYPE_INT }, \
113 }
114 #endif
115 
116 #ifdef _KERNEL
117 #ifndef _LOCORE
118 extern struct cpu_info cpu_info_store;
119 
120 #define	curcpu()	(&cpu_info_store)
121 #define	cpu_number()	(0)
122 #endif /* !_LOCORE */
123 
124 /*
125  * Macros to find the CPU architecture we're on at run-time,
126  * or if possible, at compile-time.
127  */
128 
129 #define	CPU_ARCH_MIPSx	0		/* XXX unknown */
130 #define	CPU_ARCH_MIPS1	(1 << 0)
131 #define	CPU_ARCH_MIPS2	(1 << 1)
132 #define	CPU_ARCH_MIPS3	(1 << 2)
133 #define	CPU_ARCH_MIPS4	(1 << 3)
134 #define	CPU_ARCH_MIPS5	(1 << 4)
135 #define	CPU_ARCH_MIPS32	(1 << 5)
136 #define	CPU_ARCH_MIPS64	(1 << 6)
137 
138 #ifndef _LOCORE
139 /* XXX simonb
140  * Should the following be in a cpu_info type structure?
141  * And how many of these are per-cpu vs. per-system?  (Ie,
142  * we can assume that all cpus have the same mmu-type, but
143  * maybe not that all cpus run at the same clock speed.
144  * Some SGI's apparently support R12k and R14k in the same
145  * box.)
146  */
147 extern int cpu_arch;
148 extern int mips_cpu_flags;
149 extern int mips_has_r4k_mmu;
150 extern int mips_has_llsc;
151 extern int mips3_pg_cached;
152 
153 #define	CPU_MIPS_R4K_MMU		0x0001
154 #define	CPU_MIPS_NO_LLSC		0x0002
155 #define	CPU_MIPS_CAUSE_IV		0x0004
156 #define	CPU_MIPS_HAVE_SPECIAL_CCA	0x0008	/* Defaults to '3' if not set. */
157 #define	CPU_MIPS_CACHED_CCA_MASK	0x0070
158 #define	CPU_MIPS_CACHED_CCA_SHIFT	 4
159 #define	CPU_MIPS_DOUBLE_COUNT		0x0080	/* 1 cp0 count == 2 clock cycles */
160 #define	CPU_MIPS_USE_WAIT		0x0100	/* Use "wait"-based cpu_idle() */
161 #define	CPU_MIPS_NO_WAIT		0x0200	/* Inverse of previous, for mips32/64 */
162 #define	MIPS_NOT_SUPP			0x8000
163 
164 #ifdef _LKM
165 /* Assume all CPU architectures are valid for LKM's */
166 #define	MIPS1	1
167 #define	MIPS3	1
168 #define	MIPS4	1
169 #define	MIPS32	1
170 #define	MIPS64	1
171 #endif
172 
173 #if (MIPS1 + MIPS3 + MIPS4 + MIPS32 + MIPS64) == 0
174 #error at least one of MIPS1, MIPS3, MIPS4, MIPS32 or MIPS64 must be specified
175 #endif
176 
177 #if (MIPS1 + MIPS3 + MIPS4 + MIPS32 + MIPS64) == 1
178 #ifdef MIPS1
179 # define CPUISMIPS3		0
180 # define CPUIS64BITS		0
181 # define CPUISMIPS32		0
182 # define CPUISMIPS64		0
183 # define CPUISMIPSNN		0
184 # define MIPS_HAS_R4K_MMU	0
185 # define MIPS_HAS_CLOCK		0
186 # define MIPS_HAS_LLSC		0
187 #endif /* MIPS1 */
188 
189 #if defined(MIPS3) || defined(MIPS4)
190 # define CPUISMIPS3		1
191 # define CPUIS64BITS		1
192 # define CPUISMIPS32		0
193 # define CPUISMIPS64		0
194 # define CPUISMIPSNN		0
195 # define MIPS_HAS_R4K_MMU	1
196 # define MIPS_HAS_CLOCK		1
197 # define MIPS_HAS_LLSC		(mips_has_llsc)
198 #endif /* MIPS3 || MIPS4 */
199 
200 #ifdef MIPS32
201 # define CPUISMIPS3		1
202 # define CPUIS64BITS		0
203 # define CPUISMIPS32		1
204 # define CPUISMIPS64		0
205 # define CPUISMIPSNN		1
206 # define MIPS_HAS_R4K_MMU	1
207 # define MIPS_HAS_CLOCK		1
208 # define MIPS_HAS_LLSC		1
209 #endif /* MIPS32 */
210 
211 #ifdef MIPS64
212 # define CPUISMIPS3		1
213 # define CPUIS64BITS		1
214 # define CPUISMIPS32		0
215 # define CPUISMIPS64		1
216 # define CPUISMIPSNN		1
217 # define MIPS_HAS_R4K_MMU	1
218 # define MIPS_HAS_CLOCK		1
219 # define MIPS_HAS_LLSC		1
220 #endif /* MIPS64 */
221 
222 #else /* run-time test */
223 
224 #define	MIPS_HAS_R4K_MMU	(mips_has_r4k_mmu)
225 #define	MIPS_HAS_LLSC		(mips_has_llsc)
226 
227 /* This test is ... rather bogus */
228 #define	CPUISMIPS3	((cpu_arch & \
229 	(CPU_ARCH_MIPS3 | CPU_ARCH_MIPS4 | CPU_ARCH_MIPS32 | CPU_ARCH_MIPS64)) != 0)
230 
231 /* And these aren't much better while the previous test exists as is... */
232 #define	CPUISMIPS32	((cpu_arch & CPU_ARCH_MIPS32) != 0)
233 #define	CPUISMIPS64	((cpu_arch & CPU_ARCH_MIPS64) != 0)
234 #define	CPUISMIPSNN	((cpu_arch & (CPU_ARCH_MIPS32 | CPU_ARCH_MIPS64)) != 0)
235 #define	CPUIS64BITS	((cpu_arch & \
236 	(CPU_ARCH_MIPS3 | CPU_ARCH_MIPS4 | CPU_ARCH_MIPS64)) != 0)
237 
238 #define	MIPS_HAS_CLOCK	(cpu_arch >= CPU_ARCH_MIPS3)
239 #endif /* run-time test */
240 
241 /* Shortcut for MIPS3 or above defined */
242 #if defined(MIPS3) || defined(MIPS4) || defined(MIPS32) || defined(MIPS64)
243 #define	MIPS3_PLUS	1
244 #else
245 #undef MIPS3_PLUS
246 #endif
247 
248 
249 /*
250  * definitions of cpu-dependent requirements
251  * referenced in generic code
252  */
253 #define	cpu_wait(p)			/* nothing */
254 #define	cpu_swapout(p)			panic("cpu_swapout: can't get here");
255 
256 void cpu_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
257 
258 /*
259  * Arguments to hardclock and gatherstats encapsulate the previous
260  * machine state in an opaque clockframe.
261  */
262 struct clockframe {
263 	int	pc;	/* program counter at time of interrupt */
264 	int	sr;	/* status register at time of interrupt */
265 	int	ppl;	/* previous priority level at time of interrupt */
266 };
267 
268 /*
269  * A port must provde CLKF_USERMODE() and CLKF_BASEPRI() for use
270  * in machine-independent code. These differ on r4000 and r3000 systems;
271  * provide them in the port-dependent file that includes this one, using
272  * the macros below.
273  */
274 
275 /* mips1 versions */
276 #define	MIPS1_CLKF_USERMODE(framep)	((framep)->sr & MIPS_SR_KU_PREV)
277 #define	MIPS1_CLKF_BASEPRI(framep)	\
278 	((~(framep)->sr & (MIPS_INT_MASK | MIPS_SR_INT_ENA_PREV)) == 0)
279 
280 /* mips3 versions */
281 #define	MIPS3_CLKF_USERMODE(framep)	((framep)->sr & MIPS_SR_KSU_USER)
282 #define	MIPS3_CLKF_BASEPRI(framep)	\
283 	((~(framep)->sr & (MIPS_INT_MASK | MIPS_SR_INT_IE)) == 0)
284 
285 #ifdef IPL_ICU_MASK
286 #define ICU_CLKF_BASEPRI(framep)	((framep)->ppl == 0)
287 #endif
288 
289 #define	CLKF_PC(framep)		((framep)->pc)
290 #define	CLKF_INTR(framep)	(0)
291 
292 #if defined(MIPS3_PLUS) && !defined(MIPS1)		/* XXX bogus! */
293 #define	CLKF_USERMODE(framep)	MIPS3_CLKF_USERMODE(framep)
294 #define	CLKF_BASEPRI(framep)	MIPS3_CLKF_BASEPRI(framep)
295 #endif
296 
297 #if !defined(MIPS3_PLUS) && defined(MIPS1)		/* XXX bogus! */
298 #define	CLKF_USERMODE(framep)	MIPS1_CLKF_USERMODE(framep)
299 #define	CLKF_BASEPRI(framep)	MIPS1_CLKF_BASEPRI(framep)
300 #endif
301 
302 #ifdef IPL_ICU_MASK
303 #undef CLKF_BASEPRI
304 #define CLKF_BASEPRI(framep)	ICU_CLKF_BASEPRI(framep)
305 #endif
306 
307 #if defined(MIPS3_PLUS) && defined(MIPS1)		/* XXX bogus! */
308 #define CLKF_USERMODE(framep) \
309     ((CPUISMIPS3) ? MIPS3_CLKF_USERMODE(framep):  MIPS1_CLKF_USERMODE(framep))
310 #define CLKF_BASEPRI(framep) \
311     ((CPUISMIPS3) ? MIPS3_CLKF_BASEPRI(framep):  MIPS1_CLKF_BASEPRI(framep))
312 #endif
313 
314 /*
315  * This is used during profiling to integrate system time.  It can safely
316  * assume that the process is resident.
317  */
318 #define	PROC_PC(p)							\
319 	(((struct frame *)(p)->p_md.md_regs)->f_regs[37])	/* XXX PC */
320 
321 /*
322  * Preempt the current process if in interrupt from user mode,
323  * or after the current trap/syscall if in system mode.
324  */
325 #define	need_resched(ci)						\
326 do {									\
327 	want_resched = 1;						\
328 	if (curproc != NULL)						\
329 		aston(curproc);						\
330 } while (/*CONSTCOND*/0)
331 
332 /*
333  * Give a profiling tick to the current process when the user profiling
334  * buffer pages are invalid.  On the MIPS, request an ast to send us
335  * through trap, marking the proc as needing a profiling tick.
336  */
337 #define	need_proftick(p)						\
338 do {									\
339 	(p)->p_flag |= P_OWEUPC;					\
340 	aston(p);							\
341 } while (/*CONSTCOND*/0)
342 
343 /*
344  * Notify the current process (p) that it has a signal pending,
345  * process as soon as possible.
346  */
347 #define	signotify(p)	aston(p)
348 
349 #define aston(p)	((p)->p_md.md_astpending = 1)
350 
351 extern int want_resched;		/* resched() was called */
352 
353 /*
354  * Misc prototypes and variable declarations.
355  */
356 struct proc;
357 struct user;
358 
359 extern struct proc *fpcurproc;
360 
361 /* trap.c */
362 void	netintr(void);
363 int	kdbpeek(vaddr_t);
364 
365 /* mips_machdep.c */
366 void	dumpsys(void);
367 int	savectx(struct user *);
368 void	mips_init_msgbuf(void);
369 void	savefpregs(struct proc *);
370 void	loadfpregs(struct proc *);
371 
372 /* locore*.S */
373 int	badaddr(void *, size_t);
374 int	badaddr64(uint64_t, size_t);
375 
376 /* mips_machdep.c */
377 void	cpu_identify(void);
378 void	mips_vector_init(void);
379 
380 #endif /* ! _LOCORE */
381 #endif /* _KERNEL */
382 #endif /* _CPU_H_ */
383