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