cpu.h revision 1.73 1 1.73 phx /* $NetBSD: cpu.h,v 1.73 2010/12/21 20:39:53 phx Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.5 ws * Copyright (C) 1999 Wolfgang Solfrank.
5 1.5 ws * Copyright (C) 1999 TooLs GmbH.
6 1.9 matt * Copyright (C) 1995-1997 Wolfgang Solfrank.
7 1.9 matt * Copyright (C) 1995-1997 TooLs GmbH.
8 1.1 ws * All rights reserved.
9 1.1 ws *
10 1.1 ws * Redistribution and use in source and binary forms, with or without
11 1.1 ws * modification, are permitted provided that the following conditions
12 1.1 ws * are met:
13 1.1 ws * 1. Redistributions of source code must retain the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer.
15 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ws * notice, this list of conditions and the following disclaimer in the
17 1.1 ws * documentation and/or other materials provided with the distribution.
18 1.1 ws * 3. All advertising materials mentioning features or use of this software
19 1.1 ws * must display the following acknowledgement:
20 1.1 ws * This product includes software developed by TooLs GmbH.
21 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 1.1 ws * derived from this software without specific prior written permission.
23 1.1 ws *
24 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 ws */
35 1.5 ws #ifndef _POWERPC_CPU_H_
36 1.5 ws #define _POWERPC_CPU_H_
37 1.1 ws
38 1.27 matt struct cache_info {
39 1.27 matt int dcache_size;
40 1.27 matt int dcache_line_size;
41 1.27 matt int icache_size;
42 1.27 matt int icache_line_size;
43 1.27 matt };
44 1.27 matt
45 1.73 phx #if defined(_KERNEL) || defined(_KMEMUSER)
46 1.9 matt #if defined(_KERNEL_OPT)
47 1.9 matt #include "opt_lockdebug.h"
48 1.9 matt #include "opt_multiprocessor.h"
49 1.16 matt #include "opt_ppcarch.h"
50 1.9 matt #endif
51 1.9 matt
52 1.73 phx #ifdef _KERNEL
53 1.9 matt #include <machine/frame.h>
54 1.9 matt #include <machine/psl.h>
55 1.9 matt #include <machine/intr.h>
56 1.72 uebayasi #include <sys/device_if.h>
57 1.72 uebayasi #include <sys/evcnt.h>
58 1.73 phx #endif
59 1.9 matt
60 1.42 yamt #include <sys/cpu_data.h>
61 1.14 eeh
62 1.9 matt struct cpu_info {
63 1.42 yamt struct cpu_data ci_data; /* MI per-cpu data */
64 1.73 phx #ifdef _KERNEL
65 1.9 matt struct device *ci_dev; /* device of corresponding cpu */
66 1.23 thorpej struct lwp *ci_curlwp; /* current owner of the processor */
67 1.9 matt
68 1.9 matt struct pcb *ci_curpcb;
69 1.38 matt struct pmap *ci_curpm;
70 1.23 thorpej struct lwp *ci_fpulwp;
71 1.23 thorpej struct lwp *ci_veclwp;
72 1.9 matt int ci_cpuid;
73 1.9 matt
74 1.28 matt volatile int ci_astpending;
75 1.57 rjs int ci_want_resched;
76 1.28 matt volatile u_long ci_lasttb;
77 1.28 matt volatile int ci_tickspending;
78 1.50 freza volatile int ci_cpl;
79 1.50 freza volatile int ci_iactive;
80 1.60 ad volatile int ci_idepth;
81 1.70 kiyohara volatile imask_t ci_ipending;
82 1.9 matt int ci_intrdepth;
83 1.53 ad int ci_mtx_oldspl;
84 1.53 ad int ci_mtx_count;
85 1.68 matt #ifndef PPC_BOOKE
86 1.9 matt char *ci_intstk;
87 1.68 matt #endif
88 1.32 matt #define CPUSAVE_LEN 8
89 1.32 matt register_t ci_tempsave[CPUSAVE_LEN];
90 1.32 matt register_t ci_ddbsave[CPUSAVE_LEN];
91 1.32 matt register_t ci_ipkdbsave[CPUSAVE_LEN];
92 1.68 matt #ifndef PPC_BOOKE
93 1.32 matt #define CPUSAVE_R28 0 /* where r28 gets saved */
94 1.32 matt #define CPUSAVE_R29 1 /* where r29 gets saved */
95 1.32 matt #define CPUSAVE_R30 2 /* where r30 gets saved */
96 1.32 matt #define CPUSAVE_R31 3 /* where r31 gets saved */
97 1.32 matt #define CPUSAVE_DAR 4 /* where SPR_DAR gets saved */
98 1.32 matt #define CPUSAVE_DSISR 5 /* where SPR_DSISR gets saved */
99 1.32 matt #define CPUSAVE_SRR0 6 /* where SRR0 gets saved */
100 1.32 matt #define CPUSAVE_SRR1 7 /* where SRR1 gets saved */
101 1.32 matt #define DISISAVE_LEN 4
102 1.32 matt register_t ci_disisave[DISISAVE_LEN];
103 1.68 matt #else
104 1.68 matt #define CPUSAVE_R26 0 /* where r26 gets saved */
105 1.68 matt #define CPUSAVE_R27 1 /* where r27 gets saved */
106 1.68 matt #define CPUSAVE_R28 2 /* where r28 gets saved */
107 1.68 matt #define CPUSAVE_R29 3 /* where r29 gets saved */
108 1.68 matt #define CPUSAVE_R30 4 /* where r30 gets saved */
109 1.68 matt #define CPUSAVE_R31 5 /* where r31 gets saved */
110 1.68 matt register_t ci_critsave[CPUSAVE_LEN];
111 1.68 matt register_t ci_mchksave[CPUSAVE_LEN];
112 1.68 matt struct pmap_segtab *ci_pmap_kern_segtab;
113 1.68 matt struct pmap_segtab *ci_pmap_user_segtab;
114 1.68 matt #endif
115 1.14 eeh struct cache_info ci_ci;
116 1.40 matt void *ci_sysmon_cookie;
117 1.43 matt void (*ci_idlespin)(void);
118 1.44 briggs uint32_t ci_khz;
119 1.25 matt struct evcnt ci_ev_clock; /* clock intrs */
120 1.50 freza struct evcnt ci_ev_statclock; /* stat clock */
121 1.25 matt struct evcnt ci_ev_softclock; /* softclock intrs */
122 1.25 matt struct evcnt ci_ev_softnet; /* softnet intrs */
123 1.25 matt struct evcnt ci_ev_softserial; /* softserial intrs */
124 1.9 matt struct evcnt ci_ev_traps; /* calls to trap() */
125 1.9 matt struct evcnt ci_ev_kdsi; /* kernel DSI traps */
126 1.9 matt struct evcnt ci_ev_udsi; /* user DSI traps */
127 1.9 matt struct evcnt ci_ev_udsi_fatal; /* user DSI trap failures */
128 1.33 matt struct evcnt ci_ev_kisi; /* kernel ISI traps */
129 1.9 matt struct evcnt ci_ev_isi; /* user ISI traps */
130 1.9 matt struct evcnt ci_ev_isi_fatal; /* user ISI trap failures */
131 1.9 matt struct evcnt ci_ev_pgm; /* user PGM traps */
132 1.9 matt struct evcnt ci_ev_fpu; /* FPU traps */
133 1.9 matt struct evcnt ci_ev_fpusw; /* FPU context switch */
134 1.9 matt struct evcnt ci_ev_ali; /* Alignment traps */
135 1.9 matt struct evcnt ci_ev_ali_fatal; /* Alignment fatal trap */
136 1.9 matt struct evcnt ci_ev_scalls; /* system call traps */
137 1.9 matt struct evcnt ci_ev_vec; /* Altivec traps */
138 1.9 matt struct evcnt ci_ev_vecsw; /* Altivec context switches */
139 1.16 matt struct evcnt ci_ev_umchk; /* user MCHK events */
140 1.59 garbled struct evcnt ci_ev_ipi; /* IPIs received */
141 1.73 phx #endif /* _KERNEL */
142 1.9 matt };
143 1.73 phx #endif /* _KERNEL || _KMEMUSER */
144 1.73 phx
145 1.73 phx #ifdef _KERNEL
146 1.9 matt
147 1.9 matt #ifdef MULTIPROCESSOR
148 1.59 garbled
149 1.59 garbled struct cpu_hatch_data {
150 1.59 garbled struct device *self;
151 1.59 garbled struct cpu_info *ci;
152 1.59 garbled int running;
153 1.59 garbled int pir;
154 1.64 garbled int asr;
155 1.59 garbled int hid0;
156 1.59 garbled int sdr1;
157 1.59 garbled int sr[16];
158 1.59 garbled int batu[4], batl[4];
159 1.59 garbled int tbu, tbl;
160 1.59 garbled };
161 1.59 garbled
162 1.47 perry static __inline int
163 1.11 chs cpu_number(void)
164 1.9 matt {
165 1.9 matt int pir;
166 1.9 matt
167 1.30 matt __asm ("mfspr %0,1023" : "=r"(pir));
168 1.9 matt return pir;
169 1.9 matt }
170 1.9 matt
171 1.11 chs void cpu_boot_secondary_processors(void);
172 1.9 matt
173 1.9 matt
174 1.9 matt #define CPU_IS_PRIMARY(ci) ((ci)->ci_cpuid == 0)
175 1.18 chs #define CPU_INFO_ITERATOR int
176 1.18 chs #define CPU_INFO_FOREACH(cii, ci) \
177 1.64 garbled cii = 0, ci = &cpu_info[0]; cii < ncpu; cii++, ci++
178 1.18 chs
179 1.9 matt #else
180 1.9 matt
181 1.9 matt #define cpu_number() 0
182 1.9 matt
183 1.18 chs #define CPU_INFO_ITERATOR int
184 1.18 chs #define CPU_INFO_FOREACH(cii, ci) \
185 1.18 chs cii = 0, ci = curcpu(); ci != NULL; ci = NULL
186 1.18 chs
187 1.9 matt #endif /* MULTIPROCESSOR */
188 1.9 matt
189 1.25 matt extern struct cpu_info cpu_info[];
190 1.25 matt
191 1.47 perry static __inline struct cpu_info *
192 1.25 matt curcpu(void)
193 1.25 matt {
194 1.25 matt struct cpu_info *ci;
195 1.25 matt
196 1.46 perry __asm volatile ("mfsprg %0,0" : "=r"(ci));
197 1.25 matt return ci;
198 1.25 matt }
199 1.25 matt
200 1.25 matt #define curlwp (curcpu()->ci_curlwp)
201 1.25 matt #define curpcb (curcpu()->ci_curpcb)
202 1.38 matt #define curpm (curcpu()->ci_curpm)
203 1.25 matt
204 1.47 perry static __inline register_t
205 1.18 chs mfmsr(void)
206 1.18 chs {
207 1.20 matt register_t msr;
208 1.18 chs
209 1.46 perry __asm volatile ("mfmsr %0" : "=r"(msr));
210 1.18 chs return msr;
211 1.18 chs }
212 1.18 chs
213 1.47 perry static __inline void
214 1.20 matt mtmsr(register_t msr)
215 1.18 chs {
216 1.18 chs
217 1.46 perry __asm volatile ("mtmsr %0" : : "r"(msr));
218 1.19 chs }
219 1.19 chs
220 1.47 perry static __inline uint32_t
221 1.19 chs mftbl(void)
222 1.19 chs {
223 1.19 chs uint32_t tbl;
224 1.19 chs
225 1.46 perry __asm volatile (
226 1.29 hannken #ifdef PPC_IBM403
227 1.29 hannken " mftblo %0 \n"
228 1.29 hannken #else
229 1.29 hannken " mftbl %0 \n"
230 1.29 hannken #endif
231 1.29 hannken : "=r" (tbl));
232 1.29 hannken
233 1.19 chs return tbl;
234 1.19 chs }
235 1.19 chs
236 1.47 perry static __inline uint64_t
237 1.19 chs mftb(void)
238 1.19 chs {
239 1.19 chs uint64_t tb;
240 1.32 matt
241 1.32 matt #ifdef _LP64
242 1.46 perry __asm volatile ("mftb %0" : "=r"(tb));
243 1.32 matt #else
244 1.19 chs int tmp;
245 1.19 chs
246 1.46 perry __asm volatile (
247 1.29 hannken #ifdef PPC_IBM403
248 1.29 hannken "1: mftbhi %0 \n"
249 1.29 hannken " mftblo %0+1 \n"
250 1.29 hannken " mftbhi %1 \n"
251 1.29 hannken #else
252 1.22 thorpej "1: mftbu %0 \n"
253 1.22 thorpej " mftb %0+1 \n"
254 1.22 thorpej " mftbu %1 \n"
255 1.29 hannken #endif
256 1.22 thorpej " cmplw %0,%1 \n"
257 1.29 hannken " bne- 1b \n"
258 1.29 hannken : "=r" (tb), "=r"(tmp) :: "cr0");
259 1.32 matt #endif
260 1.29 hannken
261 1.19 chs return tb;
262 1.24 kleink }
263 1.24 kleink
264 1.47 perry static __inline uint32_t
265 1.24 kleink mfrtcl(void)
266 1.24 kleink {
267 1.24 kleink uint32_t rtcl;
268 1.24 kleink
269 1.46 perry __asm volatile ("mfrtcl %0" : "=r"(rtcl));
270 1.24 kleink return rtcl;
271 1.24 kleink }
272 1.24 kleink
273 1.47 perry static __inline void
274 1.24 kleink mfrtc(uint32_t *rtcp)
275 1.24 kleink {
276 1.24 kleink uint32_t tmp;
277 1.24 kleink
278 1.46 perry __asm volatile (
279 1.24 kleink "1: mfrtcu %0 \n"
280 1.24 kleink " mfrtcl %1 \n"
281 1.24 kleink " mfrtcu %2 \n"
282 1.24 kleink " cmplw %0,%2 \n"
283 1.24 kleink " bne- 1b"
284 1.41 kleink : "=r"(*rtcp), "=r"(*(rtcp + 1)), "=r"(tmp) :: "cr0");
285 1.19 chs }
286 1.19 chs
287 1.47 perry static __inline uint32_t
288 1.19 chs mfpvr(void)
289 1.19 chs {
290 1.19 chs uint32_t pvr;
291 1.19 chs
292 1.46 perry __asm volatile ("mfpvr %0" : "=r"(pvr));
293 1.19 chs return (pvr);
294 1.18 chs }
295 1.18 chs
296 1.49 freza static __inline int
297 1.49 freza cntlzw(uint32_t val)
298 1.49 freza {
299 1.49 freza int cnt;
300 1.49 freza
301 1.49 freza __asm volatile ("cntlzw %0,%1" : "=r"(cnt) : "r"(val));
302 1.49 freza return (cnt);
303 1.49 freza }
304 1.49 freza
305 1.48 freza #if defined(PPC_IBM4XX) || defined(PPC_IBM403)
306 1.48 freza /*
307 1.48 freza * DCR (Device Control Register) access. These have to be
308 1.48 freza * macros because register address is encoded as immediate
309 1.48 freza * operand.
310 1.48 freza */
311 1.48 freza #define mtdcr(reg, val) \
312 1.48 freza __asm volatile("mtdcr %0,%1" : : "K"(reg), "r"(val))
313 1.48 freza
314 1.48 freza #define mfdcr(reg) \
315 1.48 freza ({ \
316 1.48 freza uint32_t __val; \
317 1.48 freza \
318 1.48 freza __asm volatile("mfdcr %0,%1" : "=r"(__val) : "K"(reg)); \
319 1.48 freza __val; \
320 1.48 freza })
321 1.69 kiyohara
322 1.69 kiyohara #define mtcpr(reg, val) \
323 1.69 kiyohara do { \
324 1.69 kiyohara mtdcr(DCR_CPR0_CFGADDR, reg); \
325 1.69 kiyohara mtdcr(DCR_CPR0_CFGDATA, val); \
326 1.69 kiyohara } while (0/*CONSTCOND*/)
327 1.69 kiyohara
328 1.69 kiyohara #define mfcpr(reg) \
329 1.69 kiyohara ({ \
330 1.69 kiyohara mtdcr(DCR_CPR0_CFGADDR, reg); \
331 1.69 kiyohara mfdcr(DCR_CPR0_CFGDATA); \
332 1.69 kiyohara })
333 1.69 kiyohara
334 1.69 kiyohara #define mtsdr(reg, val) \
335 1.69 kiyohara do { \
336 1.69 kiyohara mtdcr(DCR_SDR0_CFGADDR, reg); \
337 1.69 kiyohara mtdcr(DCR_SDR0_CFGDATA, val); \
338 1.69 kiyohara } while (0/*CONSTCOND*/)
339 1.69 kiyohara
340 1.69 kiyohara #define mfsdr(reg) \
341 1.69 kiyohara ({ \
342 1.69 kiyohara mtdcr(DCR_SDR0_CFGADDR, reg); \
343 1.69 kiyohara mfdcr(DCR_SDR0_CFGDATA); \
344 1.69 kiyohara })
345 1.48 freza #endif /* PPC_IBM4XX || PPC_IBM403 */
346 1.48 freza
347 1.9 matt #define CLKF_USERMODE(frame) (((frame)->srr1 & PSL_PR) != 0)
348 1.9 matt #define CLKF_PC(frame) ((frame)->srr0)
349 1.9 matt #define CLKF_INTR(frame) ((frame)->depth > 0)
350 1.9 matt
351 1.23 thorpej #define LWP_PC(l) (trapframe(l)->srr0)
352 1.9 matt
353 1.23 thorpej #define cpu_proc_fork(p1, p2)
354 1.56 yamt #define cpu_idle() (curcpu()->ci_idlespin())
355 1.9 matt
356 1.9 matt extern int powersave;
357 1.9 matt extern int cpu_timebase;
358 1.9 matt extern int cpu_printfataltraps;
359 1.16 matt extern char cpu_model[];
360 1.16 matt
361 1.16 matt struct cpu_info *cpu_attach_common(struct device *, int);
362 1.18 chs void cpu_setup(struct device *, struct cpu_info *);
363 1.16 matt void cpu_identify(char *, size_t);
364 1.71 phx int cpu_get_dfs(void);
365 1.71 phx void cpu_set_dfs(int);
366 1.16 matt void delay (unsigned int);
367 1.16 matt void cpu_probe_cache(void);
368 1.16 matt void dcache_flush_page(vaddr_t);
369 1.16 matt void icache_flush_page(vaddr_t);
370 1.16 matt void dcache_flush(vaddr_t, vsize_t);
371 1.16 matt void icache_flush(vaddr_t, vsize_t);
372 1.31 scw void *mapiodev(paddr_t, psize_t);
373 1.52 matt void unmapiodev(vaddr_t, vsize_t);
374 1.9 matt
375 1.59 garbled #ifdef MULTIPROCESSOR
376 1.59 garbled int md_setup_trampoline(volatile struct cpu_hatch_data *, struct cpu_info *);
377 1.59 garbled void md_presync_timebase(volatile struct cpu_hatch_data *);
378 1.59 garbled void md_start_timebase(volatile struct cpu_hatch_data *);
379 1.59 garbled void md_sync_timebase(volatile struct cpu_hatch_data *);
380 1.59 garbled void md_setup_interrupts(void);
381 1.59 garbled int cpu_spinup(struct device *, struct cpu_info *);
382 1.64 garbled register_t cpu_hatch(void);
383 1.59 garbled void cpu_spinup_trampoline(void);
384 1.59 garbled #endif
385 1.59 garbled
386 1.9 matt #define DELAY(n) delay(n)
387 1.9 matt
388 1.57 rjs #define cpu_need_resched(ci, v) (ci->ci_want_resched = ci->ci_astpending = 1)
389 1.65 macallan #define cpu_did_resched(l) ((void)(curcpu()->ci_want_resched = 0))
390 1.56 yamt #define cpu_need_proftick(l) ((l)->l_pflag |= LP_OWEUPC, curcpu()->ci_astpending = 1)
391 1.53 ad #define cpu_signotify(l) (curcpu()->ci_astpending = 1) /* XXXSMP */
392 1.9 matt
393 1.61 garbled #if !defined(PPC_IBM4XX)
394 1.26 matt void oea_init(void (*)(void));
395 1.26 matt void oea_startup(const char *);
396 1.26 matt void oea_dumpsys(void);
397 1.26 matt void oea_install_extint(void (*)(void));
398 1.55 christos paddr_t kvtop(void *);
399 1.16 matt void softnet(int);
400 1.16 matt
401 1.16 matt extern paddr_t msgbuf_paddr;
402 1.16 matt extern int cpu_altivec;
403 1.16 matt #endif
404 1.16 matt
405 1.9 matt #endif /* _KERNEL */
406 1.9 matt
407 1.61 garbled /* XXX The below breaks unified pmap on ppc32 */
408 1.61 garbled
409 1.9 matt #if defined(_KERNEL) || defined(_STANDALONE)
410 1.9 matt #if !defined(CACHELINESIZE)
411 1.29 hannken #ifdef PPC_IBM403
412 1.62 garbled #define CACHELINESIZE 16
413 1.62 garbled #define MAXCACHELINESIZE 16
414 1.29 hannken #else
415 1.51 sanjayl #if defined (PPC_OEA64_BRIDGE)
416 1.62 garbled #define CACHELINESIZE 128
417 1.62 garbled #define MAXCACHELINESIZE 128
418 1.51 sanjayl #else
419 1.62 garbled #define CACHELINESIZE 32
420 1.62 garbled #define MAXCACHELINESIZE 32
421 1.51 sanjayl #endif /* PPC_OEA64_BRIDGE */
422 1.29 hannken #endif
423 1.9 matt #endif
424 1.10 matt #endif
425 1.10 matt
426 1.15 matt void __syncicache(void *, size_t);
427 1.14 eeh
428 1.5 ws /*
429 1.5 ws * CTL_MACHDEP definitions.
430 1.5 ws */
431 1.9 matt #define CPU_CACHELINE 1
432 1.9 matt #define CPU_TIMEBASE 2
433 1.9 matt #define CPU_CPUTEMP 3
434 1.9 matt #define CPU_PRINTFATALTRAPS 4
435 1.14 eeh #define CPU_CACHEINFO 5
436 1.16 matt #define CPU_ALTIVEC 6
437 1.16 matt #define CPU_MODEL 7
438 1.58 nisimura #define CPU_POWERSAVE 8 /* int: use CPU powersave mode */
439 1.58 nisimura #define CPU_BOOTED_DEVICE 9 /* string: device we booted from */
440 1.58 nisimura #define CPU_BOOTED_KERNEL 10 /* string: kernel we booted */
441 1.58 nisimura #define CPU_MAXID 11 /* number of valid machdep ids */
442 1.1 ws
443 1.5 ws #endif /* _POWERPC_CPU_H_ */
444