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