xref: /netbsd-src/sys/arch/powerpc/include/cpu.h (revision daf6c4152fcddc27c445489775ed1f66ab4ea9a9)
1 /*	$NetBSD: cpu.h,v 1.75 2011/02/16 18:42:33 matt Exp $	*/
2 
3 /*
4  * Copyright (C) 1999 Wolfgang Solfrank.
5  * Copyright (C) 1999 TooLs GmbH.
6  * Copyright (C) 1995-1997 Wolfgang Solfrank.
7  * Copyright (C) 1995-1997 TooLs GmbH.
8  * All rights reserved.
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 TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #ifndef	_POWERPC_CPU_H_
36 #define	_POWERPC_CPU_H_
37 
38 struct cache_info {
39 	int dcache_size;
40 	int dcache_line_size;
41 	int icache_size;
42 	int icache_line_size;
43 };
44 
45 #if defined(_KERNEL) || defined(_KMEMUSER)
46 #if defined(_KERNEL_OPT)
47 #include "opt_lockdebug.h"
48 #include "opt_multiprocessor.h"
49 #include "opt_ppcarch.h"
50 #endif
51 
52 #ifdef _KERNEL
53 #include <machine/frame.h>
54 #include <machine/psl.h>
55 #include <machine/intr.h>
56 #include <sys/device_if.h>
57 #include <sys/evcnt.h>
58 #endif
59 
60 #include <sys/cpu_data.h>
61 
62 struct cpu_info {
63 	struct cpu_data ci_data;	/* MI per-cpu data */
64 #ifdef _KERNEL
65 	device_t ci_dev;		/* device of corresponding cpu */
66 	struct cpu_softc *ci_softc;	/* private cpu info */
67 	struct lwp *ci_curlwp;		/* current owner of the processor */
68 
69 	struct pcb *ci_curpcb;
70 	struct pmap *ci_curpm;
71 	struct lwp * volatile ci_fpulwp;
72 	struct lwp * volatile ci_veclwp;
73 	int ci_cpuid;
74 
75 	volatile int ci_astpending;
76 	int ci_want_resched;
77 	volatile uint64_t ci_lastintr;
78 	volatile u_long ci_lasttb;
79 	volatile int ci_tickspending;
80 	volatile int ci_cpl;
81 	volatile int ci_iactive;
82 	volatile int ci_idepth;
83 #ifndef PPC_BOOKE
84 	volatile imask_t ci_ipending;
85 #endif
86 	int ci_mtx_oldspl;
87 	int ci_mtx_count;
88 #ifdef PPC_IBM4XX
89 	char *ci_intstk;
90 #endif
91 #ifndef PPC_BOOKE
92 #define	CPUSAVE_LEN	8
93 	register_t ci_tempsave[CPUSAVE_LEN];
94 	register_t ci_ddbsave[CPUSAVE_LEN];
95 	register_t ci_ipkdbsave[CPUSAVE_LEN];
96 #define	CPUSAVE_R28	0		/* where r28 gets saved */
97 #define	CPUSAVE_R29	1		/* where r29 gets saved */
98 #define	CPUSAVE_R30	2		/* where r30 gets saved */
99 #define	CPUSAVE_R31	3		/* where r31 gets saved */
100 #if defined(PPC_IBM4XX)
101 #define	CPUSAVE_DEAR	4		/* where SPR_DEAR gets saved */
102 #define	CPUSAVE_ESR	5		/* where SPR_ESR gets saved */
103 	register_t ci_tlbmisssave[CPUSAVE_LEN];
104 #else
105 #define	CPUSAVE_DAR	4		/* where SPR_DAR gets saved */
106 #define	CPUSAVE_DSISR	5		/* where SPR_DSISR gets saved */
107 #define	DISISAVE_LEN	4
108 	register_t ci_disisave[DISISAVE_LEN];
109 #endif
110 #define	CPUSAVE_SRR0	6		/* where SRR0 gets saved */
111 #define	CPUSAVE_SRR1	7		/* where SRR1 gets saved */
112 #else /* PPC_BOOKE */
113 #define	CPUSAVE_LEN	128
114 	register_t ci_savelifo[CPUSAVE_LEN];
115 	struct pmap_segtab *ci_pmap_segtabs[2];
116 #define	ci_pmap_kern_segtab	ci_pmap_segtabs[0]
117 #define	ci_pmap_user_segtab	ci_pmap_segtabs[1]
118 	struct pmap_tlb_info *ci_tlb_info;
119 #endif /* PPC_BOOKE */
120 	struct cache_info ci_ci;
121 	void *ci_sysmon_cookie;
122 	void (*ci_idlespin)(void);
123 	uint32_t ci_khz;
124 	struct evcnt ci_ev_clock;	/* clock intrs */
125 	struct evcnt ci_ev_statclock; 	/* stat clock */
126 #ifndef PPC_BOOKE
127 	struct evcnt ci_ev_softclock;	/* softclock intrs */
128 	struct evcnt ci_ev_softnet;	/* softnet intrs */
129 	struct evcnt ci_ev_softserial;	/* softserial intrs */
130 #endif
131 	struct evcnt ci_ev_traps;	/* calls to trap() */
132 	struct evcnt ci_ev_kdsi;	/* kernel DSI traps */
133 	struct evcnt ci_ev_udsi;	/* user DSI traps */
134 	struct evcnt ci_ev_udsi_fatal;	/* user DSI trap failures */
135 	struct evcnt ci_ev_kisi;	/* kernel ISI traps */
136 	struct evcnt ci_ev_isi;		/* user ISI traps */
137 	struct evcnt ci_ev_isi_fatal;	/* user ISI trap failures */
138 	struct evcnt ci_ev_pgm;		/* user PGM traps */
139 	struct evcnt ci_ev_debug;	/* user debug traps */
140 	struct evcnt ci_ev_fpu;		/* FPU traps */
141 	struct evcnt ci_ev_fpusw;	/* FPU context switch */
142 	struct evcnt ci_ev_ali;		/* Alignment traps */
143 	struct evcnt ci_ev_ali_fatal;	/* Alignment fatal trap */
144 	struct evcnt ci_ev_scalls;	/* system call traps */
145 	struct evcnt ci_ev_vec;		/* Altivec traps */
146 	struct evcnt ci_ev_vecsw;	/* Altivec context switches */
147 	struct evcnt ci_ev_umchk;	/* user MCHK events */
148 	struct evcnt ci_ev_ipi;		/* IPIs received */
149 	struct evcnt ci_ev_tlbmiss_soft; /* tlb miss (no trap) */
150 	struct evcnt ci_ev_dtlbmiss_hard; /* data tlb miss (trap) */
151 	struct evcnt ci_ev_itlbmiss_hard; /* instruction tlb miss (trap) */
152 #endif /* _KERNEL */
153 };
154 #endif /* _KERNEL || _KMEMUSER */
155 
156 #ifdef _KERNEL
157 
158 #ifdef MULTIPROCESSOR
159 
160 struct cpu_hatch_data {
161 	struct device *self;
162 	struct cpu_info *ci;
163 	int running;
164 	int pir;
165 	int asr;
166 	int hid0;
167 	int sdr1;
168 	int sr[16];
169 	int batu[4], batl[4];
170 	int tbu, tbl;
171 };
172 
173 static __inline int
174 cpu_number(void)
175 {
176 	int pir;
177 
178 	__asm ("mfspr %0,1023" : "=r"(pir));
179 	return pir;
180 }
181 
182 void	cpu_boot_secondary_processors(void);
183 
184 
185 #define CPU_IS_PRIMARY(ci)	((ci)->ci_cpuid == 0)
186 #define CPU_INFO_ITERATOR		int
187 #define CPU_INFO_FOREACH(cii, ci)					\
188 	cii = 0, ci = &cpu_info[0]; cii < ncpu; cii++, ci++
189 
190 #else
191 
192 #define cpu_number()		0
193 
194 #define CPU_INFO_ITERATOR		int
195 #define CPU_INFO_FOREACH(cii, ci)					\
196 	cii = 0, ci = curcpu(); ci != NULL; ci = NULL
197 
198 #endif /* MULTIPROCESSOR */
199 
200 extern struct cpu_info cpu_info[];
201 
202 static __inline struct cpu_info *
203 curcpu(void)
204 {
205 	struct cpu_info *ci;
206 
207 	__asm volatile ("mfsprg %0,0" : "=r"(ci));
208 	return ci;
209 }
210 
211 #define curlwp			(curcpu()->ci_curlwp)
212 #define curpcb			(curcpu()->ci_curpcb)
213 #define curpm			(curcpu()->ci_curpm)
214 
215 static __inline register_t
216 mfmsr(void)
217 {
218 	register_t msr;
219 
220 	__asm volatile ("mfmsr %0" : "=r"(msr));
221 	return msr;
222 }
223 
224 static __inline void
225 mtmsr(register_t msr)
226 {
227 	//KASSERT(msr & PSL_CE);
228 	//KASSERT(msr & PSL_DE);
229 	__asm volatile ("mtmsr %0" : : "r"(msr));
230 }
231 
232 static __inline uint32_t
233 mftbl(void)
234 {
235 	uint32_t tbl;
236 
237 	__asm volatile (
238 #ifdef PPC_IBM403
239 	"	mftblo %[tbl]"		"\n"
240 #elif defined(PPC_BOOKE)
241 	"	mfspr %[tbl],268"	"\n"
242 #else
243 	"	mftbl %[tbl]"		"\n"
244 #endif
245 	: [tbl] "=r" (tbl));
246 
247 	return tbl;
248 }
249 
250 static __inline uint64_t
251 mftb(void)
252 {
253 	uint64_t tb;
254 
255 #ifdef _LP64
256 	__asm volatile ("mftb %0" : "=r"(tb));
257 #else
258 	int tmp;
259 
260 	__asm volatile (
261 #ifdef PPC_IBM403
262 	"1:	mftbhi %[tb]"		"\n"
263 	"	mftblo %L[tb]"		"\n"
264 	"	mftbhi %[tmp]"		"\n"
265 #elif defined(PPC_BOOKE)
266 	"1:	mfspr %[tb],269"	"\n"
267 	"	mfspr %L[tb],268"	"\n"
268 	"	mfspr %[tmp],269"	"\n"
269 #else
270 	"1:	mftbu %[tb]"		"\n"
271 	"	mftb %L[tb]"		"\n"
272 	"	mftbu %[tmp]"		"\n"
273 #endif
274 	"	cmplw %[tb],%[tmp]"	"\n"
275 	"	bne- 1b"		"\n"
276 	    : [tb] "=r" (tb), [tmp] "=r"(tmp)
277 	    :: "cr0");
278 #endif
279 
280 	return tb;
281 }
282 
283 static __inline uint32_t
284 mfrtcl(void)
285 {
286 	uint32_t rtcl;
287 
288 	__asm volatile ("mfrtcl %0" : "=r"(rtcl));
289 	return rtcl;
290 }
291 
292 static __inline void
293 mfrtc(uint32_t *rtcp)
294 {
295 	uint32_t tmp;
296 
297 	__asm volatile (
298 	"1:	mfrtcu	%[rtcu]"	"\n"
299 	"	mfrtcl	%[rtcl]"	"\n"
300 	"	mfrtcu	%[tmp]"		"\n"
301 	"	cmplw	%[rtcu],%[tmp]"	"\n"
302 	"	bne-	1b"
303 	    : [rtcu] "=r"(rtcp[0]), [rtcl] "=r"(rtcp[1]), [tmp] "=r"(tmp)
304 	    :: "cr0");
305 }
306 
307 static __inline uint32_t
308 mfpvr(void)
309 {
310 	uint32_t pvr;
311 
312 	__asm volatile ("mfpvr %0" : "=r"(pvr));
313 	return (pvr);
314 }
315 
316 static __inline int
317 cntlzw(uint32_t val)
318 {
319 	int 			cnt;
320 
321 	__asm volatile ("cntlzw %0,%1" : "=r"(cnt) : "r"(val));
322 	return (cnt);
323 }
324 
325 #define	CLKF_USERMODE(frame)	(((frame)->cf_srr1 & PSL_PR) != 0)
326 #define	CLKF_PC(frame)		((frame)->cf_srr0)
327 #define	CLKF_INTR(frame)	((frame)->cf_idepth > 0)
328 
329 #define	LWP_PC(l)		(trapframe(l)->tf_srr0)
330 
331 #define	cpu_proc_fork(p1, p2)
332 
333 extern int powersave;
334 extern int cpu_timebase;
335 extern int cpu_printfataltraps;
336 extern char cpu_model[];
337 
338 void cpu_uarea_remap(struct lwp *);
339 struct cpu_info *cpu_attach_common(struct device *, int);
340 void cpu_setup(struct device *, struct cpu_info *);
341 void cpu_identify(char *, size_t);
342 int cpu_get_dfs(void);
343 void cpu_set_dfs(int);
344 void delay (unsigned int);
345 void cpu_probe_cache(void);
346 #ifndef PPC_BOOKE
347 void dcache_flush_page(vaddr_t);
348 void icache_flush_page(vaddr_t);
349 void dcache_flush(vaddr_t, vsize_t);
350 void icache_flush(vaddr_t, vsize_t);
351 #else
352 void dcache_wb_page(vaddr_t);
353 void dcache_wbinv_page(vaddr_t);
354 void dcache_inv_page(vaddr_t);
355 void dcache_zero_page(vaddr_t);
356 void icache_inv_page(vaddr_t);
357 void dcache_wb(vaddr_t, vsize_t);
358 void dcache_wbinv(vaddr_t, vsize_t);
359 void dcache_inv(vaddr_t, vsize_t);
360 void icache_inv(vaddr_t, vsize_t);
361 #endif
362 void *mapiodev(paddr_t, psize_t);
363 void unmapiodev(vaddr_t, vsize_t);
364 
365 #ifdef MULTIPROCESSOR
366 int md_setup_trampoline(volatile struct cpu_hatch_data *, struct cpu_info *);
367 void md_presync_timebase(volatile struct cpu_hatch_data *);
368 void md_start_timebase(volatile struct cpu_hatch_data *);
369 void md_sync_timebase(volatile struct cpu_hatch_data *);
370 void md_setup_interrupts(void);
371 int cpu_spinup(struct device *, struct cpu_info *);
372 register_t cpu_hatch(void);
373 void cpu_spinup_trampoline(void);
374 #endif
375 
376 #define	DELAY(n)		delay(n)
377 
378 #define	cpu_need_resched(ci, v)	(ci->ci_want_resched = ci->ci_astpending = 1)
379 #define	cpu_did_resched(l)	((void)(curcpu()->ci_want_resched = 0))
380 #define	cpu_need_proftick(l)	((l)->l_pflag |= LP_OWEUPC, curcpu()->ci_astpending = 1)
381 #define	cpu_signotify(l)	(curcpu()->ci_astpending = 1)	/* XXXSMP */
382 
383 #if !defined(PPC_IBM4XX) && !defined(PPC_BOOKE)
384 void oea_init(void (*)(void));
385 void oea_startup(const char *);
386 void oea_dumpsys(void);
387 void oea_install_extint(void (*)(void));
388 paddr_t kvtop(void *);
389 void softnet(int);
390 
391 extern paddr_t msgbuf_paddr;
392 extern int cpu_altivec;
393 #endif
394 
395 #endif /* _KERNEL */
396 
397 /* XXX The below breaks unified pmap on ppc32 */
398 
399 #if defined(_KERNEL) || defined(_STANDALONE)
400 #if !defined(CACHELINESIZE)
401 #ifdef PPC_IBM403
402 #define	CACHELINESIZE		16
403 #define MAXCACHELINESIZE	16
404 #else
405 #if defined (PPC_OEA64_BRIDGE)
406 #define	CACHELINESIZE		128
407 #define MAXCACHELINESIZE	128
408 #else
409 #define	CACHELINESIZE		32
410 #define MAXCACHELINESIZE	32
411 #endif /* PPC_OEA64_BRIDGE */
412 #endif
413 #endif
414 #endif
415 
416 void __syncicache(void *, size_t);
417 
418 /*
419  * CTL_MACHDEP definitions.
420  */
421 #define	CPU_CACHELINE		1
422 #define	CPU_TIMEBASE		2
423 #define	CPU_CPUTEMP		3
424 #define	CPU_PRINTFATALTRAPS	4
425 #define	CPU_CACHEINFO		5
426 #define	CPU_ALTIVEC		6
427 #define	CPU_MODEL		7
428 #define	CPU_POWERSAVE		8	/* int: use CPU powersave mode */
429 #define	CPU_BOOTED_DEVICE	9	/* string: device we booted from */
430 #define	CPU_BOOTED_KERNEL	10	/* string: kernel we booted */
431 #define	CPU_MAXID		11	/* number of valid machdep ids */
432 
433 #endif	/* _POWERPC_CPU_H_ */
434