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