xref: /netbsd-src/sys/arch/powerpc/include/cpu.h (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: cpu.h,v 1.76 2011/05/04 04:33:37 macallan 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 /*
326  * functions to access the G3's cache throttling register
327  * bits 1 - 9 specify additional waits on cache acess
328  * bit 0 enables cache throttling
329  */
330 
331 static __inline int
332 mfictc(void)
333 {
334 	int reg;
335 
336 	__asm ("mfspr %0,1019" : "=r"(reg));
337 	return reg;
338 }
339 
340 static __inline void
341 mtictc(uint32_t reg)
342 {
343 
344 	__asm ("mtspr 1019,%0" :: "r"(reg));
345 }
346 
347 #define	CLKF_USERMODE(frame)	(((frame)->cf_srr1 & PSL_PR) != 0)
348 #define	CLKF_PC(frame)		((frame)->cf_srr0)
349 #define	CLKF_INTR(frame)	((frame)->cf_idepth > 0)
350 
351 #define	LWP_PC(l)		(trapframe(l)->tf_srr0)
352 
353 #define	cpu_proc_fork(p1, p2)
354 
355 extern int powersave;
356 extern int cpu_timebase;
357 extern int cpu_printfataltraps;
358 extern char cpu_model[];
359 
360 void cpu_uarea_remap(struct lwp *);
361 struct cpu_info *cpu_attach_common(struct device *, int);
362 void cpu_setup(struct device *, struct cpu_info *);
363 void cpu_identify(char *, size_t);
364 int cpu_get_dfs(void);
365 void cpu_set_dfs(int);
366 void delay (unsigned int);
367 void cpu_probe_cache(void);
368 #ifndef PPC_BOOKE
369 void dcache_flush_page(vaddr_t);
370 void icache_flush_page(vaddr_t);
371 void dcache_flush(vaddr_t, vsize_t);
372 void icache_flush(vaddr_t, vsize_t);
373 #else
374 void dcache_wb_page(vaddr_t);
375 void dcache_wbinv_page(vaddr_t);
376 void dcache_inv_page(vaddr_t);
377 void dcache_zero_page(vaddr_t);
378 void icache_inv_page(vaddr_t);
379 void dcache_wb(vaddr_t, vsize_t);
380 void dcache_wbinv(vaddr_t, vsize_t);
381 void dcache_inv(vaddr_t, vsize_t);
382 void icache_inv(vaddr_t, vsize_t);
383 #endif
384 void *mapiodev(paddr_t, psize_t);
385 void unmapiodev(vaddr_t, vsize_t);
386 
387 #ifdef MULTIPROCESSOR
388 int md_setup_trampoline(volatile struct cpu_hatch_data *, struct cpu_info *);
389 void md_presync_timebase(volatile struct cpu_hatch_data *);
390 void md_start_timebase(volatile struct cpu_hatch_data *);
391 void md_sync_timebase(volatile struct cpu_hatch_data *);
392 void md_setup_interrupts(void);
393 int cpu_spinup(struct device *, struct cpu_info *);
394 register_t cpu_hatch(void);
395 void cpu_spinup_trampoline(void);
396 #endif
397 
398 #define	DELAY(n)		delay(n)
399 
400 #define	cpu_need_resched(ci, v)	(ci->ci_want_resched = ci->ci_astpending = 1)
401 #define	cpu_did_resched(l)	((void)(curcpu()->ci_want_resched = 0))
402 #define	cpu_need_proftick(l)	((l)->l_pflag |= LP_OWEUPC, curcpu()->ci_astpending = 1)
403 #define	cpu_signotify(l)	(curcpu()->ci_astpending = 1)	/* XXXSMP */
404 
405 #if !defined(PPC_IBM4XX) && !defined(PPC_BOOKE)
406 void oea_init(void (*)(void));
407 void oea_startup(const char *);
408 void oea_dumpsys(void);
409 void oea_install_extint(void (*)(void));
410 paddr_t kvtop(void *);
411 void softnet(int);
412 
413 extern paddr_t msgbuf_paddr;
414 extern int cpu_altivec;
415 #endif
416 
417 #endif /* _KERNEL */
418 
419 /* XXX The below breaks unified pmap on ppc32 */
420 
421 #if defined(_KERNEL) || defined(_STANDALONE)
422 #if !defined(CACHELINESIZE)
423 #ifdef PPC_IBM403
424 #define	CACHELINESIZE		16
425 #define MAXCACHELINESIZE	16
426 #else
427 #if defined (PPC_OEA64_BRIDGE)
428 #define	CACHELINESIZE		128
429 #define MAXCACHELINESIZE	128
430 #else
431 #define	CACHELINESIZE		32
432 #define MAXCACHELINESIZE	32
433 #endif /* PPC_OEA64_BRIDGE */
434 #endif
435 #endif
436 #endif
437 
438 void __syncicache(void *, size_t);
439 
440 /*
441  * CTL_MACHDEP definitions.
442  */
443 #define	CPU_CACHELINE		1
444 #define	CPU_TIMEBASE		2
445 #define	CPU_CPUTEMP		3
446 #define	CPU_PRINTFATALTRAPS	4
447 #define	CPU_CACHEINFO		5
448 #define	CPU_ALTIVEC		6
449 #define	CPU_MODEL		7
450 #define	CPU_POWERSAVE		8	/* int: use CPU powersave mode */
451 #define	CPU_BOOTED_DEVICE	9	/* string: device we booted from */
452 #define	CPU_BOOTED_KERNEL	10	/* string: kernel we booted */
453 #define	CPU_MAXID		11	/* number of valid machdep ids */
454 
455 #endif	/* _POWERPC_CPU_H_ */
456