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