xref: /netbsd-src/sys/arch/powerpc/include/cpu.h (revision 8e6ab8837d8d6b9198e67c1c445300b483e2f304)
1 /*	$NetBSD: cpu.h,v 1.33 2003/08/04 22:26:59 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 #ifdef _KERNEL
46 #if defined(_KERNEL_OPT)
47 #include "opt_lockdebug.h"
48 #include "opt_multiprocessor.h"
49 #include "opt_ppcarch.h"
50 #endif
51 
52 #include <machine/frame.h>
53 #include <machine/psl.h>
54 #include <machine/intr.h>
55 #include <sys/device.h>
56 
57 #include <sys/sched.h>
58 #include <dev/sysmon/sysmonvar.h>
59 
60 struct cpu_info {
61 	struct schedstate_percpu ci_schedstate; /* scheduler state */
62 	struct device *ci_dev;		/* device of corresponding cpu */
63 	struct lwp *ci_curlwp;		/* current owner of the processor */
64 
65 	struct pcb *ci_curpcb;
66 	struct pmap *ci_curpm;
67 	struct lwp *ci_fpulwp;
68 	struct lwp *ci_veclwp;
69 	struct pcb *ci_idle_pcb;	/* PA of our idle pcb */
70 	int ci_cpuid;
71 
72 	volatile int ci_astpending;
73 	int ci_want_resched;
74 	volatile u_long ci_lasttb;
75 	volatile int ci_tickspending;
76 	int ci_cpl;
77 	int ci_iactive;
78 	int ci_ipending;
79 	int ci_intrdepth;
80 	char *ci_intstk;
81 	char *ci_spillstk;
82 #define	CPUSAVE_LEN	8
83 	register_t ci_tempsave[CPUSAVE_LEN];
84 	register_t ci_ddbsave[CPUSAVE_LEN];
85 	register_t ci_ipkdbsave[CPUSAVE_LEN];
86 #define	CPUSAVE_R28	0		/* where r28 gets saved */
87 #define	CPUSAVE_R29	1		/* where r29 gets saved */
88 #define	CPUSAVE_R30	2		/* where r30 gets saved */
89 #define	CPUSAVE_R31	3		/* where r31 gets saved */
90 #define	CPUSAVE_DAR	4		/* where SPR_DAR gets saved */
91 #define	CPUSAVE_DSISR	5		/* where SPR_DSISR gets saved */
92 #define	CPUSAVE_SRR0	6		/* where SRR0 gets saved */
93 #define	CPUSAVE_SRR1	7		/* where SRR1 gets saved */
94 #define	DISISAVE_LEN	4
95 	register_t ci_disisave[DISISAVE_LEN];
96 	struct cache_info ci_ci;
97 	struct sysmon_envsys ci_sysmon;
98 	struct envsys_tre_data ci_tau_info;
99 	struct evcnt ci_ev_clock;	/* clock intrs */
100 	struct evcnt ci_ev_softclock;	/* softclock intrs */
101 	struct evcnt ci_ev_softnet;	/* softnet intrs */
102 	struct evcnt ci_ev_softserial;	/* softserial intrs */
103 	struct evcnt ci_ev_traps;	/* calls to trap() */
104 	struct evcnt ci_ev_kdsi;	/* kernel DSI traps */
105 	struct evcnt ci_ev_udsi;	/* user DSI traps */
106 	struct evcnt ci_ev_udsi_fatal;	/* user DSI trap failures */
107 	struct evcnt ci_ev_kisi;	/* kernel ISI traps */
108 	struct evcnt ci_ev_isi;		/* user ISI traps */
109 	struct evcnt ci_ev_isi_fatal;	/* user ISI trap failures */
110 	struct evcnt ci_ev_pgm;		/* user PGM traps */
111 	struct evcnt ci_ev_fpu;		/* FPU traps */
112 	struct evcnt ci_ev_fpusw;	/* FPU context switch */
113 	struct evcnt ci_ev_ali;		/* Alignment traps */
114 	struct evcnt ci_ev_ali_fatal;	/* Alignment fatal trap */
115 	struct evcnt ci_ev_scalls;	/* system call traps */
116 	struct evcnt ci_ev_vec;		/* Altivec traps */
117 	struct evcnt ci_ev_vecsw;	/* Altivec context switches */
118 	struct evcnt ci_ev_umchk;	/* user MCHK events */
119 #if defined(DIAGNOSTIC) || defined(LOCKDEBUG)
120 	u_long ci_spin_locks;		/* # of spin locks held */
121 	u_long ci_simple_locks;		/* # of simple locks held */
122 #endif
123 };
124 
125 #ifdef MULTIPROCESSOR
126 static __inline int
127 cpu_number(void)
128 {
129 	int pir;
130 
131 	__asm ("mfspr %0,1023" : "=r"(pir));
132 	return pir;
133 }
134 
135 void	cpu_boot_secondary_processors(void);
136 
137 
138 #define CPU_IS_PRIMARY(ci)	((ci)->ci_cpuid == 0)
139 #define CPU_INFO_ITERATOR		int
140 #define CPU_INFO_FOREACH(cii, ci)					\
141 	cii = 0, ci = &cpu_info[0]; cii < CPU_MAXNUM; cii++, ci++
142 
143 #else
144 
145 #define cpu_number()		0
146 
147 #define CPU_INFO_ITERATOR		int
148 #define CPU_INFO_FOREACH(cii, ci)					\
149 	cii = 0, ci = curcpu(); ci != NULL; ci = NULL
150 
151 #endif /* MULTIPROCESSOR */
152 
153 extern struct cpu_info cpu_info[];
154 
155 static __inline struct cpu_info *
156 curcpu(void)
157 {
158 	struct cpu_info *ci;
159 
160 	__asm __volatile ("mfsprg %0,0" : "=r"(ci));
161 	return ci;
162 }
163 
164 #define curlwp			(curcpu()->ci_curlwp)
165 #define curpcb			(curcpu()->ci_curpcb)
166 #define curpm			(curcpu()->ci_curpm)
167 
168 static __inline register_t
169 mfmsr(void)
170 {
171 	register_t msr;
172 
173 	__asm __volatile ("mfmsr %0" : "=r"(msr));
174 	return msr;
175 }
176 
177 static __inline void
178 mtmsr(register_t msr)
179 {
180 
181 	__asm __volatile ("mtmsr %0" : : "r"(msr));
182 }
183 
184 static __inline uint32_t
185 mftbl(void)
186 {
187 	uint32_t tbl;
188 
189 	__asm __volatile (
190 #ifdef PPC_IBM403
191 "	mftblo %0	\n"
192 #else
193 "	mftbl %0	\n"
194 #endif
195 	: "=r" (tbl));
196 
197 	return tbl;
198 }
199 
200 static __inline uint64_t
201 mftb(void)
202 {
203 	uint64_t tb;
204 
205 #ifdef _LP64
206 	__asm __volatile ("mftb %0" : "=r"(tb));
207 #else
208 	int tmp;
209 
210 	__asm __volatile (
211 #ifdef PPC_IBM403
212 "1:	mftbhi %0	\n"
213 "	mftblo %0+1	\n"
214 "	mftbhi %1	\n"
215 #else
216 "1:	mftbu %0	\n"
217 "	mftb %0+1	\n"
218 "	mftbu %1	\n"
219 #endif
220 "	cmplw %0,%1	\n"
221 "	bne- 1b		\n"
222 	: "=r" (tb), "=r"(tmp) :: "cr0");
223 #endif
224 
225 	return tb;
226 }
227 
228 static __inline uint32_t
229 mfrtcl(void)
230 {
231 	uint32_t rtcl;
232 
233 	__asm __volatile ("mfrtcl %0" : "=r"(rtcl));
234 	return rtcl;
235 }
236 
237 static __inline void
238 mfrtc(uint32_t *rtcp)
239 {
240 	uint32_t tmp;
241 
242 	__asm __volatile (
243 "1:	mfrtcu	%0	\n"
244 "	mfrtcl	%1	\n"
245 "	mfrtcu	%2	\n"
246 "	cmplw	%0,%2	\n"
247 "	bne-	1b"
248 	    : "=r"(*rtcp), "=r"(*(rtcp + 1)), "=r"(tmp));
249 }
250 
251 static __inline uint32_t
252 mfpvr(void)
253 {
254 	uint32_t pvr;
255 
256 	__asm __volatile ("mfpvr %0" : "=r"(pvr));
257 	return (pvr);
258 }
259 
260 #define	CLKF_USERMODE(frame)	(((frame)->srr1 & PSL_PR) != 0)
261 #define	CLKF_BASEPRI(frame)	((frame)->pri == 0)
262 #define	CLKF_PC(frame)		((frame)->srr0)
263 #define	CLKF_INTR(frame)	((frame)->depth > 0)
264 
265 #define	LWP_PC(l)		(trapframe(l)->srr0)
266 
267 #define	cpu_swapout(p)
268 #define cpu_wait(p)
269 #define	cpu_proc_fork(p1, p2)
270 
271 extern int powersave;
272 extern int cpu_timebase;
273 extern int cpu_printfataltraps;
274 extern char cpu_model[];
275 
276 struct cpu_info *cpu_attach_common(struct device *, int);
277 void cpu_setup(struct device *, struct cpu_info *);
278 void cpu_identify(char *, size_t);
279 void delay (unsigned int);
280 void cpu_probe_cache(void);
281 void dcache_flush_page(vaddr_t);
282 void icache_flush_page(vaddr_t);
283 void dcache_flush(vaddr_t, vsize_t);
284 void icache_flush(vaddr_t, vsize_t);
285 void *mapiodev(paddr_t, psize_t);
286 
287 #define	DELAY(n)		delay(n)
288 
289 #define	need_resched(ci)	(ci->ci_want_resched = 1, ci->ci_astpending = 1)
290 #define	need_proftick(p)	((p)->p_flag |= P_OWEUPC, curcpu()->ci_astpending = 1)
291 #define	signotify(p)		(curcpu()->ci_astpending = 1)
292 
293 #ifdef PPC_OEA
294 void oea_init(void (*)(void));
295 void oea_startup(const char *);
296 void oea_dumpsys(void);
297 void oea_install_extint(void (*)(void));
298 paddr_t kvtop(caddr_t);
299 void softnet(int);
300 
301 extern paddr_t msgbuf_paddr;
302 extern int cpu_altivec;
303 #endif
304 
305 #endif /* _KERNEL */
306 
307 #if defined(_KERNEL) || defined(_STANDALONE)
308 #if !defined(CACHELINESIZE)
309 #ifdef PPC_IBM403
310 #define	CACHELINESIZE	16
311 #else
312 #define	CACHELINESIZE	32
313 #endif
314 #endif
315 #endif
316 
317 void __syncicache(void *, size_t);
318 
319 /*
320  * CTL_MACHDEP definitions.
321  */
322 #define	CPU_CACHELINE		1
323 #define	CPU_TIMEBASE		2
324 #define	CPU_CPUTEMP		3
325 #define	CPU_PRINTFATALTRAPS	4
326 #define	CPU_CACHEINFO		5
327 #define	CPU_ALTIVEC		6
328 #define	CPU_MODEL		7
329 #define	CPU_POWERSAVE		8
330 #define	CPU_MAXID		9
331 
332 #define	CTL_MACHDEP_NAMES { \
333 	{ 0, 0 }, \
334 	{ "cachelinesize", CTLTYPE_INT }, \
335 	{ "timebase", CTLTYPE_INT }, \
336 	{ "cputempature", CTLTYPE_INT }, \
337 	{ "printfataltraps", CTLTYPE_INT }, \
338 	{ "cacheinfo", CTLTYPE_STRUCT }, \
339 	{ "altivec", CTLTYPE_INT }, \
340 	{ "model", CTLTYPE_STRING }, \
341 	{ "powersave", CTLTYPE_INT }, \
342 }
343 
344 #endif	/* _POWERPC_CPU_H_ */
345