xref: /netbsd-src/sys/arch/powerpc/include/cpu.h (revision 87d689fb734c654d2486f87f7be32f1b53ecdbec)
1 /*	$NetBSD: cpu.h,v 1.103 2017/12/17 17:18:34 chs 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 
36 #ifndef	_POWERPC_CPU_H_
37 #define	_POWERPC_CPU_H_
38 
39 struct cache_info {
40 	int dcache_size;
41 	int dcache_line_size;
42 	int icache_size;
43 	int icache_line_size;
44 };
45 
46 #if defined(_KERNEL) || defined(_KMEMUSER)
47 #if defined(_KERNEL_OPT)
48 #include "opt_lockdebug.h"
49 #include "opt_modular.h"
50 #include "opt_multiprocessor.h"
51 #include "opt_ppcarch.h"
52 #endif
53 
54 #ifdef _KERNEL
55 #include <sys/intr.h>
56 #include <sys/device_if.h>
57 #include <sys/evcnt.h>
58 #include <sys/param.h>
59 #include <sys/kernel.h>
60 #endif
61 
62 #include <sys/cpu_data.h>
63 
64 struct cpu_info {
65 	struct cpu_data ci_data;	/* MI per-cpu data */
66 #ifdef _KERNEL
67 	device_t ci_dev;		/* device of corresponding cpu */
68 	struct cpu_softc *ci_softc;	/* private cpu info */
69 	struct lwp *ci_curlwp;		/* current owner of the processor */
70 
71 	struct pcb *ci_curpcb;
72 	struct pmap *ci_curpm;
73 	struct lwp *ci_softlwps[SOFTINT_COUNT];
74 	int ci_cpuid;			/* from SPR_PIR */
75 
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 	union {
84 #if !defined(PPC_BOOKE) && !defined(_MODULE)
85 		volatile imask_t un1_ipending;
86 #define	ci_ipending	ci_un1.un1_ipending
87 #endif
88 		uint64_t un1_pad64;
89 	} ci_un1;
90 	volatile uint32_t ci_pending_ipis;
91 	int ci_mtx_oldspl;
92 	int ci_mtx_count;
93 #if defined(PPC_IBM4XX) || defined(MODULAR) || defined(_MODULE)
94 	char *ci_intstk;
95 #endif
96 #define	CI_SAVETEMP	(0*CPUSAVE_LEN)
97 #define	CI_SAVEDDB	(1*CPUSAVE_LEN)
98 #define	CI_SAVEIPKDB	(2*CPUSAVE_LEN)
99 #define	CI_SAVEMMU	(3*CPUSAVE_LEN)
100 #define	CI_SAVEMAX	(4*CPUSAVE_LEN)
101 #define	CPUSAVE_LEN	8
102 #if !defined(PPC_BOOKE) && !defined(MODULAR) && !defined(_MODULE)
103 #define	CPUSAVE_SIZE	(CI_SAVEMAX*CPUSAVE_LEN)
104 #else
105 #define	CPUSAVE_SIZE	128
106 #endif
107 #define	CPUSAVE_R28	0		/* where r28 gets saved */
108 #define	CPUSAVE_R29	1		/* where r29 gets saved */
109 #define	CPUSAVE_R30	2		/* where r30 gets saved */
110 #define	CPUSAVE_R31	3		/* where r31 gets saved */
111 #define	CPUSAVE_DEAR	4		/* where IBM4XX SPR_DEAR gets saved */
112 #define	CPUSAVE_DAR	4		/* where OEA SPR_DAR gets saved */
113 #define	CPUSAVE_ESR	5		/* where IBM4XX SPR_ESR gets saved */
114 #define	CPUSAVE_DSISR	5		/* where OEA SPR_DSISR gets saved */
115 #define	CPUSAVE_SRR0	6		/* where SRR0 gets saved */
116 #define	CPUSAVE_SRR1	7		/* where SRR1 gets saved */
117 	register_t ci_savearea[CPUSAVE_SIZE];
118 #if defined(PPC_BOOKE) || defined(MODULAR) || defined(_MODULE)
119 	uint32_t ci_pmap_asid_cur;
120 	union pmap_segtab *ci_pmap_segtabs[2];
121 #define	ci_pmap_kern_segtab	ci_pmap_segtabs[0]
122 #define	ci_pmap_user_segtab	ci_pmap_segtabs[1]
123 	struct pmap_tlb_info *ci_tlb_info;
124 #endif /* PPC_BOOKE || MODULAR || _MODULE */
125 	struct cache_info ci_ci;
126 	void *ci_sysmon_cookie;
127 	void (*ci_idlespin)(void);
128 	uint32_t ci_khz;
129 	struct evcnt ci_ev_clock;	/* clock intrs */
130 	struct evcnt ci_ev_statclock; 	/* stat clock */
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 #if defined(MULTIPROCESSOR) && !defined(_MODULE)
159 struct cpu_hatch_data {
160 	int hatch_running;
161 	device_t hatch_self;
162 	struct cpu_info *hatch_ci;
163 	uint32_t hatch_tbu;
164 	uint32_t hatch_tbl;
165 	uint32_t hatch_hid0;
166 	uint32_t hatch_pir;
167 #if defined(PPC_OEA) || defined(PPC_OEA64_BRIDGE)
168 	uintptr_t hatch_asr;
169 	uintptr_t hatch_sdr1;
170 	uint32_t hatch_sr[16];
171 	uintptr_t hatch_ibatu[8], hatch_ibatl[8];
172 	uintptr_t hatch_dbatu[8], hatch_dbatl[8];
173 #endif
174 #if defined(PPC_BOOKE)
175 	vaddr_t hatch_sp;
176 	u_int hatch_tlbidx;
177 #endif
178 };
179 
180 struct cpuset_info {
181 	kcpuset_t *cpus_running;
182 	kcpuset_t *cpus_hatched;
183 	kcpuset_t *cpus_paused;
184 	kcpuset_t *cpus_resumed;
185 	kcpuset_t *cpus_halted;
186 };
187 
188 extern struct cpuset_info cpuset_info;
189 #endif /* MULTIPROCESSOR && !_MODULE */
190 
191 #if defined(MULTIPROCESSOR) || defined(_MODULE)
192 #define	cpu_number()		(curcpu()->ci_index + 0)
193 
194 #define CPU_IS_PRIMARY(ci)	((ci)->ci_cpuid == 0)
195 #define CPU_INFO_ITERATOR	int
196 #define CPU_INFO_FOREACH(cii, ci)				\
197 	cii = 0, ci = &cpu_info[0]; cii < (ncpu ? ncpu : 1); cii++, ci++
198 
199 #else
200 #define cpu_number()		0
201 
202 #define CPU_IS_PRIMARY(ci)	true
203 #define CPU_INFO_ITERATOR	int
204 #define CPU_INFO_FOREACH(cii, ci)				\
205 	(void)cii, ci = curcpu(); ci != NULL; ci = NULL
206 
207 #endif /* MULTIPROCESSOR || _MODULE */
208 
209 extern struct cpu_info cpu_info[];
210 
211 static __inline struct cpu_info * curcpu(void) __pure;
212 static __inline struct cpu_info *
213 curcpu(void)
214 {
215 	struct cpu_info *ci;
216 
217 	__asm volatile ("mfsprg0 %0" : "=r"(ci));
218 	return ci;
219 }
220 
221 #ifdef __clang__
222 #define	curlwp			(curcpu()->ci_curlwp)
223 #else
224 register struct lwp *powerpc_curlwp __asm("r13");
225 #define	curlwp			powerpc_curlwp
226 #endif
227 #define curpcb			(curcpu()->ci_curpcb)
228 #define curpm			(curcpu()->ci_curpm)
229 
230 static __inline register_t
231 mfmsr(void)
232 {
233 	register_t msr;
234 
235 	__asm volatile ("mfmsr %0" : "=r"(msr));
236 	return msr;
237 }
238 
239 static __inline void
240 mtmsr(register_t msr)
241 {
242 	//KASSERT(msr & PSL_CE);
243 	//KASSERT(msr & PSL_DE);
244 	__asm volatile ("mtmsr %0" : : "r"(msr));
245 }
246 
247 #if !defined(_MODULE)
248 static __inline uint32_t
249 mftbl(void)
250 {
251 	uint32_t tbl;
252 
253 	__asm volatile (
254 #ifdef PPC_IBM403
255 	"	mftblo %[tbl]"		"\n"
256 #elif defined(PPC_BOOKE)
257 	"	mfspr %[tbl],268"	"\n"
258 #else
259 	"	mftbl %[tbl]"		"\n"
260 #endif
261 	: [tbl] "=r" (tbl));
262 
263 	return tbl;
264 }
265 
266 static __inline uint64_t
267 mftb(void)
268 {
269 	uint64_t tb;
270 
271 #ifdef _ARCH_PPC64
272 	__asm volatile ("mftb %0" : "=r"(tb));
273 #else
274 	int tmp;
275 
276 	__asm volatile (
277 #ifdef PPC_IBM403
278 	"1:	mftbhi %[tb]"		"\n"
279 	"	mftblo %L[tb]"		"\n"
280 	"	mftbhi %[tmp]"		"\n"
281 #elif defined(PPC_BOOKE)
282 	"1:	mfspr %[tb],269"	"\n"
283 	"	mfspr %L[tb],268"	"\n"
284 	"	mfspr %[tmp],269"	"\n"
285 #else
286 	"1:	mftbu %[tb]"		"\n"
287 	"	mftb %L[tb]"		"\n"
288 	"	mftbu %[tmp]"		"\n"
289 #endif
290 	"	cmplw %[tb],%[tmp]"	"\n"
291 	"	bne- 1b"		"\n"
292 	    : [tb] "=r" (tb), [tmp] "=r"(tmp)
293 	    :: "cr0");
294 #endif
295 
296 	return tb;
297 }
298 
299 static __inline uint32_t
300 mfrtcl(void)
301 {
302 	uint32_t rtcl;
303 
304 	__asm volatile ("mfrtcl %0" : "=r"(rtcl));
305 	return rtcl;
306 }
307 
308 static __inline void
309 mfrtc(uint32_t *rtcp)
310 {
311 	uint32_t tmp;
312 
313 	__asm volatile (
314 	"1:	mfrtcu	%[rtcu]"	"\n"
315 	"	mfrtcl	%[rtcl]"	"\n"
316 	"	mfrtcu	%[tmp]"		"\n"
317 	"	cmplw	%[rtcu],%[tmp]"	"\n"
318 	"	bne-	1b"
319 	    : [rtcu] "=r"(rtcp[0]), [rtcl] "=r"(rtcp[1]), [tmp] "=r"(tmp)
320 	    :: "cr0");
321 }
322 
323 static __inline uint64_t
324 rtc_nanosecs(void)
325 {
326     /*
327      * 601 RTC/DEC registers share clock of 7.8125 MHz, 128 ns per tick.
328      * DEC has max of 25 bits, FFFFFF => 2.14748352 seconds.
329      * RTCU is seconds, 32 bits.
330      * RTCL is nano-seconds, 23 bit counter from 0 - 999,999,872 (999,999,999 - 128 ns)
331      */
332     uint64_t cycles;
333     uint32_t tmp[2];
334 
335     mfrtc(tmp);
336 
337     cycles = tmp[0] * 1000000000;
338     cycles += (tmp[1] >> 7);
339 
340     return cycles;
341 }
342 #endif /* !_MODULE */
343 
344 static __inline uint32_t
345 mfpvr(void)
346 {
347 	uint32_t pvr;
348 
349 	__asm volatile ("mfpvr %0" : "=r"(pvr));
350 	return (pvr);
351 }
352 
353 #ifdef _MODULE
354 extern const char __CPU_MAXNUM;
355 /*
356  * Make with 0xffff to force a R_PPC_ADDR16_LO without the
357  * corresponding R_PPC_ADDR16_HI relocation.
358  */
359 #define	CPU_MAXNUM	(((uintptr_t)&__CPU_MAXNUM)&0xffff)
360 #endif /* _MODULE */
361 
362 #if !defined(_MODULE)
363 extern char *booted_kernel;
364 extern int powersave;
365 extern int cpu_timebase;
366 extern int cpu_printfataltraps;
367 
368 struct cpu_info *
369 	cpu_attach_common(device_t, int);
370 void	cpu_setup(device_t, struct cpu_info *);
371 void	cpu_identify(char *, size_t);
372 void	cpu_probe_cache(void);
373 
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 
384 void *	mapiodev(paddr_t, psize_t, bool);
385 void	unmapiodev(vaddr_t, vsize_t);
386 
387 #ifdef MULTIPROCESSOR
388 int	md_setup_trampoline(volatile struct cpu_hatch_data *,
389 	    struct cpu_info *);
390 void	md_presync_timebase(volatile struct cpu_hatch_data *);
391 void	md_start_timebase(volatile struct cpu_hatch_data *);
392 void	md_sync_timebase(volatile struct cpu_hatch_data *);
393 void	md_setup_interrupts(void);
394 int	cpu_spinup(device_t, struct cpu_info *);
395 register_t
396 	cpu_hatch(void);
397 void	cpu_spinup_trampoline(void);
398 void	cpu_boot_secondary_processors(void);
399 void	cpu_halt(void);
400 void	cpu_halt_others(void);
401 void	cpu_pause(struct trapframe *);
402 void	cpu_pause_others(void);
403 void	cpu_resume(cpuid_t);
404 void	cpu_resume_others(void);
405 int	cpu_is_paused(int);
406 void	cpu_debug_dump(void);
407 #endif /* MULTIPROCESSOR */
408 #endif /* !_MODULE */
409 
410 #define	cpu_proc_fork(p1, p2)
411 
412 #define	DELAY(n)		delay(n)
413 void	delay(unsigned int);
414 
415 #define	CLKF_USERMODE(cf)	cpu_clkf_usermode(cf)
416 #define	CLKF_PC(cf)		cpu_clkf_pc(cf)
417 #define	CLKF_INTR(cf)		cpu_clkf_intr(cf)
418 
419 bool	cpu_clkf_usermode(const struct clockframe *);
420 vaddr_t	cpu_clkf_pc(const struct clockframe *);
421 bool	cpu_clkf_intr(const struct clockframe *);
422 
423 #define	LWP_PC(l)		cpu_lwp_pc(l)
424 
425 vaddr_t	cpu_lwp_pc(struct lwp *);
426 
427 void	cpu_ast(struct lwp *, struct cpu_info *);
428 void *	cpu_uarea_alloc(bool);
429 bool	cpu_uarea_free(void *);
430 void	cpu_need_resched(struct cpu_info *, int);
431 void	cpu_signotify(struct lwp *);
432 void	cpu_need_proftick(struct lwp *);
433 #define	cpu_did_resched(l)			((l)->l_md.md_astpending = 0)
434 
435 void	cpu_fixup_stubs(void);
436 
437 #if !defined(PPC_IBM4XX) && !defined(PPC_BOOKE) && !defined(_MODULE)
438 int	cpu_get_dfs(void);
439 void	cpu_set_dfs(int);
440 
441 void	oea_init(void (*)(void));
442 void	oea_startup(const char *);
443 void	oea_dumpsys(void);
444 void	oea_install_extint(void (*)(void));
445 paddr_t	kvtop(void *);
446 
447 extern paddr_t msgbuf_paddr;
448 extern int cpu_altivec;
449 #endif
450 
451 #endif /* _KERNEL */
452 
453 /* XXX The below breaks unified pmap on ppc32 */
454 
455 #if !defined(CACHELINESIZE) && !defined(_MODULE) \
456     && (defined(_KERNEL) || defined(_STANDALONE))
457 #if defined(PPC_IBM403)
458 #define	CACHELINESIZE		16
459 #define MAXCACHELINESIZE	16
460 #elif defined (PPC_OEA64_BRIDGE)
461 #define	CACHELINESIZE		128
462 #define MAXCACHELINESIZE	128
463 #else
464 #define	CACHELINESIZE		32
465 #define MAXCACHELINESIZE	32
466 #endif /* PPC_OEA64_BRIDGE */
467 #endif
468 
469 void	__syncicache(void *, size_t);
470 
471 /*
472  * CTL_MACHDEP definitions.
473  */
474 #define	CPU_CACHELINE		1
475 #define	CPU_TIMEBASE		2
476 #define	CPU_CPUTEMP		3
477 #define	CPU_PRINTFATALTRAPS	4
478 #define	CPU_CACHEINFO		5
479 #define	CPU_ALTIVEC		6
480 #define	CPU_MODEL		7
481 #define	CPU_POWERSAVE		8	/* int: use CPU powersave mode */
482 #define	CPU_BOOTED_DEVICE	9	/* string: device we booted from */
483 #define	CPU_BOOTED_KERNEL	10	/* string: kernel we booted */
484 #define	CPU_EXECPROT		11	/* bool: PROT_EXEC works */
485 #define	CPU_MAXID		12	/* number of valid machdep ids */
486 
487 #endif	/* _POWERPC_CPU_H_ */
488