cpu.h revision 1.69 1 /* $NetBSD: cpu.h,v 1.69 2002/12/17 12:04:29 simonb 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 #ifdef _KERNEL
51 #ifndef _LOCORE
52 #include <sys/sched.h>
53
54 #if defined(_KERNEL_OPT)
55 #include "opt_lockdebug.h"
56 #endif
57
58 struct cpu_info {
59 struct schedstate_percpu ci_schedstate; /* scheduler state */
60 u_long ci_cpu_freq; /* CPU frequency */
61 u_long ci_cycles_per_hz; /* CPU freq / hz */
62 u_long ci_divisor_delay; /* for delay/DELAY */
63 u_long ci_divisor_recip; /* scaled reciprocal of previous;
64 see below */
65 #if defined(DIAGNOSTIC) || defined(LOCKDEBUG)
66 u_long ci_spin_locks; /* # of spin locks held */
67 u_long ci_simple_locks; /* # of simple locks held */
68 #endif
69 };
70
71 /*
72 * To implement a more accurate microtime using the CP0 COUNT register
73 * we need to divide that register by the number of cycles per MHz.
74 * But...
75 *
76 * DIV and DIVU are expensive on MIPS (eg 75 clocks on the R4000). MULT
77 * and MULTU are only 12 clocks on the same CPU.
78 *
79 * The strategy we use is to calculate the reciprical of cycles per MHz,
80 * scaled by 1<<32. Then we can simply issue a MULTU and pluck of the
81 * HI register and have the results of the division.
82 */
83 #define MIPS_SET_CI_RECIPRICAL(cpu) \
84 do { \
85 KASSERT((cpu)->ci_divisor_delay != 0); \
86 (cpu)->ci_divisor_recip = 0x100000000ULL / (cpu)->ci_divisor_delay; \
87 } while (0)
88
89 #define MIPS_COUNT_TO_MHZ(cpu, count, res) \
90 asm volatile("multu %1,%2 ; mfhi %0" \
91 : "=r"((res)) : "r"((count)), "r"((cpu)->ci_divisor_recip))
92
93 #endif /* !_LOCORE */
94 #endif /* _KERNEL */
95
96 /*
97 * CTL_MACHDEP definitions.
98 */
99 #define CPU_CONSDEV 1 /* dev_t: console terminal device */
100 #define CPU_BOOTED_KERNEL 2 /* string: booted kernel name */
101 #define CPU_ROOT_DEVICE 3 /* string: root device name */
102 #define CPU_LLSC 4 /* OS/CPU supports LL/SC instruction */
103
104 /*
105 * Platform can override, but note this breaks userland compatibility
106 * with other mips platforms.
107 */
108 #ifndef CPU_MAXID
109 #define CPU_MAXID 5 /* number of valid machdep ids */
110
111 #define CTL_MACHDEP_NAMES { \
112 { 0, 0 }, \
113 { "console_device", CTLTYPE_STRUCT }, \
114 { "booted_kernel", CTLTYPE_STRING }, \
115 { "root_device", CTLTYPE_STRING }, \
116 { "llsc", CTLTYPE_INT }, \
117 }
118 #endif
119
120 #ifdef _KERNEL
121 #ifndef _LOCORE
122 extern struct cpu_info cpu_info_store;
123
124 #define curcpu() (&cpu_info_store)
125 #define cpu_number() (0)
126 #endif /* !_LOCORE */
127
128 /*
129 * Macros to find the CPU architecture we're on at run-time,
130 * or if possible, at compile-time.
131 */
132
133 #define CPU_ARCH_MIPSx 0 /* XXX unknown */
134 #define CPU_ARCH_MIPS1 (1 << 0)
135 #define CPU_ARCH_MIPS2 (1 << 1)
136 #define CPU_ARCH_MIPS3 (1 << 2)
137 #define CPU_ARCH_MIPS4 (1 << 3)
138 #define CPU_ARCH_MIPS5 (1 << 4)
139 #define CPU_ARCH_MIPS32 (1 << 5)
140 #define CPU_ARCH_MIPS64 (1 << 6)
141
142 #ifndef _LOCORE
143 /* XXX simonb
144 * Should the following be in a cpu_info type structure?
145 * And how many of these are per-cpu vs. per-system? (Ie,
146 * we can assume that all cpus have the same mmu-type, but
147 * maybe not that all cpus run at the same clock speed.
148 * Some SGI's apparently support R12k and R14k in the same
149 * box.)
150 */
151 extern int cpu_arch;
152 extern int mips_cpu_flags;
153 extern int mips_has_r4k_mmu;
154 extern int mips_has_llsc;
155 extern int mips3_pg_cached;
156
157 #define CPU_MIPS_R4K_MMU 0x0001
158 #define CPU_MIPS_NO_LLSC 0x0002
159 #define CPU_MIPS_CAUSE_IV 0x0004
160 #define CPU_MIPS_HAVE_SPECIAL_CCA 0x0008 /* Defaults to '3' if not set. */
161 #define CPU_MIPS_CACHED_CCA_MASK 0x0070
162 #define CPU_MIPS_CACHED_CCA_SHIFT 4
163 #define CPU_MIPS_DOUBLE_COUNT 0x0080 /* 1 cp0 count == 2 clock cycles */
164 #define CPU_MIPS_USE_WAIT 0x0100 /* Use "wait"-based cpu_idle() */
165 #define CPU_MIPS_NO_WAIT 0x0200 /* Inverse of previous, for mips32/64 */
166 #define CPU_MIPS_D_CACHE_COHERENT 0x0400 /* D-cache is fully coherent */
167 #define CPU_MIPS_I_D_CACHE_COHERENT 0x0800 /* I-cache funcs don't need to flush the D-cache */
168 #define MIPS_NOT_SUPP 0x8000
169
170 #ifdef _LKM
171 /* Assume all CPU architectures are valid for LKM's */
172 #define MIPS1 1
173 #define MIPS3 1
174 #define MIPS4 1
175 #define MIPS32 1
176 #define MIPS64 1
177 #endif
178
179 #if (MIPS1 + MIPS3 + MIPS4 + MIPS32 + MIPS64) == 0
180 #error at least one of MIPS1, MIPS3, MIPS4, MIPS32 or MIPS64 must be specified
181 #endif
182
183 #if (MIPS1 + MIPS3 + MIPS4 + MIPS32 + MIPS64) == 1
184 #ifdef MIPS1
185 # define CPUISMIPS3 0
186 # define CPUIS64BITS 0
187 # define CPUISMIPS32 0
188 # define CPUISMIPS64 0
189 # define CPUISMIPSNN 0
190 # define MIPS_HAS_R4K_MMU 0
191 # define MIPS_HAS_CLOCK 0
192 # define MIPS_HAS_LLSC 0
193 #endif /* MIPS1 */
194
195 #if defined(MIPS3) || defined(MIPS4)
196 # define CPUISMIPS3 1
197 # define CPUIS64BITS 1
198 # define CPUISMIPS32 0
199 # define CPUISMIPS64 0
200 # define CPUISMIPSNN 0
201 # define MIPS_HAS_R4K_MMU 1
202 # define MIPS_HAS_CLOCK 1
203 # define MIPS_HAS_LLSC (mips_has_llsc)
204 #endif /* MIPS3 || MIPS4 */
205
206 #ifdef MIPS32
207 # define CPUISMIPS3 1
208 # define CPUIS64BITS 0
209 # define CPUISMIPS32 1
210 # define CPUISMIPS64 0
211 # define CPUISMIPSNN 1
212 # define MIPS_HAS_R4K_MMU 1
213 # define MIPS_HAS_CLOCK 1
214 # define MIPS_HAS_LLSC 1
215 #endif /* MIPS32 */
216
217 #ifdef MIPS64
218 # define CPUISMIPS3 1
219 # define CPUIS64BITS 1
220 # define CPUISMIPS32 0
221 # define CPUISMIPS64 1
222 # define CPUISMIPSNN 1
223 # define MIPS_HAS_R4K_MMU 1
224 # define MIPS_HAS_CLOCK 1
225 # define MIPS_HAS_LLSC 1
226 #endif /* MIPS64 */
227
228 #else /* run-time test */
229
230 #define MIPS_HAS_R4K_MMU (mips_has_r4k_mmu)
231 #define MIPS_HAS_LLSC (mips_has_llsc)
232
233 /* This test is ... rather bogus */
234 #define CPUISMIPS3 ((cpu_arch & \
235 (CPU_ARCH_MIPS3 | CPU_ARCH_MIPS4 | CPU_ARCH_MIPS32 | CPU_ARCH_MIPS64)) != 0)
236
237 /* And these aren't much better while the previous test exists as is... */
238 #define CPUISMIPS32 ((cpu_arch & CPU_ARCH_MIPS32) != 0)
239 #define CPUISMIPS64 ((cpu_arch & CPU_ARCH_MIPS64) != 0)
240 #define CPUISMIPSNN ((cpu_arch & (CPU_ARCH_MIPS32 | CPU_ARCH_MIPS64)) != 0)
241 #define CPUIS64BITS ((cpu_arch & \
242 (CPU_ARCH_MIPS3 | CPU_ARCH_MIPS4 | CPU_ARCH_MIPS64)) != 0)
243
244 #define MIPS_HAS_CLOCK (cpu_arch >= CPU_ARCH_MIPS3)
245 #endif /* run-time test */
246
247 /* Shortcut for MIPS3 or above defined */
248 #if defined(MIPS3) || defined(MIPS4) || defined(MIPS32) || defined(MIPS64)
249 #define MIPS3_PLUS 1
250 #else
251 #undef MIPS3_PLUS
252 #endif
253
254
255 /*
256 * definitions of cpu-dependent requirements
257 * referenced in generic code
258 */
259 #define cpu_wait(p) /* nothing */
260 #define cpu_swapout(p) panic("cpu_swapout: can't get here");
261
262 void cpu_intr(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
263
264 /*
265 * Arguments to hardclock and gatherstats encapsulate the previous
266 * machine state in an opaque clockframe.
267 */
268 struct clockframe {
269 int pc; /* program counter at time of interrupt */
270 int sr; /* status register at time of interrupt */
271 int ppl; /* previous priority level at time of interrupt */
272 };
273
274 /*
275 * A port must provde CLKF_USERMODE() and CLKF_BASEPRI() for use
276 * in machine-independent code. These differ on r4000 and r3000 systems;
277 * provide them in the port-dependent file that includes this one, using
278 * the macros below.
279 */
280
281 /* mips1 versions */
282 #define MIPS1_CLKF_USERMODE(framep) ((framep)->sr & MIPS_SR_KU_PREV)
283 #define MIPS1_CLKF_BASEPRI(framep) \
284 ((~(framep)->sr & (MIPS_INT_MASK | MIPS_SR_INT_ENA_PREV)) == 0)
285
286 /* mips3 versions */
287 #define MIPS3_CLKF_USERMODE(framep) ((framep)->sr & MIPS_SR_KSU_USER)
288 #define MIPS3_CLKF_BASEPRI(framep) \
289 ((~(framep)->sr & (MIPS_INT_MASK | MIPS_SR_INT_IE)) == 0)
290
291 #ifdef IPL_ICU_MASK
292 #define ICU_CLKF_BASEPRI(framep) ((framep)->ppl == 0)
293 #endif
294
295 #define CLKF_PC(framep) ((framep)->pc)
296 #define CLKF_INTR(framep) (0)
297
298 #if defined(MIPS3_PLUS) && !defined(MIPS1) /* XXX bogus! */
299 #define CLKF_USERMODE(framep) MIPS3_CLKF_USERMODE(framep)
300 #define CLKF_BASEPRI(framep) MIPS3_CLKF_BASEPRI(framep)
301 #endif
302
303 #if !defined(MIPS3_PLUS) && defined(MIPS1) /* XXX bogus! */
304 #define CLKF_USERMODE(framep) MIPS1_CLKF_USERMODE(framep)
305 #define CLKF_BASEPRI(framep) MIPS1_CLKF_BASEPRI(framep)
306 #endif
307
308 #ifdef IPL_ICU_MASK
309 #undef CLKF_BASEPRI
310 #define CLKF_BASEPRI(framep) ICU_CLKF_BASEPRI(framep)
311 #endif
312
313 #if defined(MIPS3_PLUS) && defined(MIPS1) /* XXX bogus! */
314 #define CLKF_USERMODE(framep) \
315 ((CPUISMIPS3) ? MIPS3_CLKF_USERMODE(framep): MIPS1_CLKF_USERMODE(framep))
316 #define CLKF_BASEPRI(framep) \
317 ((CPUISMIPS3) ? MIPS3_CLKF_BASEPRI(framep): MIPS1_CLKF_BASEPRI(framep))
318 #endif
319
320 /*
321 * This is used during profiling to integrate system time. It can safely
322 * assume that the process is resident.
323 */
324 #define PROC_PC(p) \
325 (((struct frame *)(p)->p_md.md_regs)->f_regs[37]) /* XXX PC */
326
327 /*
328 * Preempt the current process if in interrupt from user mode,
329 * or after the current trap/syscall if in system mode.
330 */
331 #define need_resched(ci) \
332 do { \
333 want_resched = 1; \
334 if (curproc != NULL) \
335 aston(curproc); \
336 } while (/*CONSTCOND*/0)
337
338 /*
339 * Give a profiling tick to the current process when the user profiling
340 * buffer pages are invalid. On the MIPS, request an ast to send us
341 * through trap, marking the proc as needing a profiling tick.
342 */
343 #define need_proftick(p) \
344 do { \
345 (p)->p_flag |= P_OWEUPC; \
346 aston(p); \
347 } while (/*CONSTCOND*/0)
348
349 /*
350 * Notify the current process (p) that it has a signal pending,
351 * process as soon as possible.
352 */
353 #define signotify(p) aston(p)
354
355 #define aston(p) ((p)->p_md.md_astpending = 1)
356
357 extern int want_resched; /* resched() was called */
358
359 /*
360 * Misc prototypes and variable declarations.
361 */
362 struct proc;
363 struct user;
364
365 extern struct proc *fpcurproc; /* the current FPU owner */
366 extern struct pcb *curpcb; /* the current running pcb */
367 extern struct segtab *segbase; /* current segtab base */
368
369 /* trap.c */
370 void netintr(void);
371 int kdbpeek(vaddr_t);
372
373 /* mips_machdep.c */
374 void dumpsys(void);
375 int savectx(struct user *);
376 void mips_init_msgbuf(void);
377 void savefpregs(struct proc *);
378 void loadfpregs(struct proc *);
379
380 /* locore*.S */
381 int badaddr(void *, size_t);
382 int badaddr64(uint64_t, size_t);
383
384 /* mips_machdep.c */
385 void cpu_identify(void);
386 void mips_vector_init(void);
387
388 #endif /* ! _LOCORE */
389 #endif /* _KERNEL */
390 #endif /* _CPU_H_ */
391