xref: /openbsd-src/sys/arch/arm/include/cpu.h (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: cpu.h,v 1.57 2020/01/12 16:55:00 kettenis Exp $	*/
2 /*	$NetBSD: cpu.h,v 1.34 2003/06/23 11:01:08 martin Exp $	*/
3 
4 /*
5  * Copyright (c) 1994-1996 Mark Brinicombe.
6  * Copyright (c) 1994 Brini.
7  * All rights reserved.
8  *
9  * This code is derived from software written for Brini by Mark Brinicombe
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Brini.
22  * 4. The name of the company nor the name of the author may be used to
23  *    endorse or promote products derived from this software without specific
24  *    prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
27  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * RiscBSD kernel project
39  *
40  * cpu.h
41  *
42  * CPU specific symbols
43  *
44  * Created      : 18/09/94
45  *
46  * Based on kate/katelib/arm6.h
47  */
48 
49 #ifndef _ARM_CPU_H_
50 #define _ARM_CPU_H_
51 
52 /*
53  * User-visible definitions
54  */
55 
56 /*  CTL_MACHDEP definitions. */
57 		/*		1	   formerly int: CPU_DEBUG */
58 		/*		2	   formerly string: CPU_BOOTED_DEVICE */
59 		/*		3	   formerly string: CPU_BOOTED_KERNEL */
60 #define	CPU_CONSDEV		4	/* struct: dev_t of our console */
61 #define	CPU_POWERSAVE		5	/* int: use CPU powersave mode */
62 #define	CPU_ALLOWAPERTURE	6	/* int: allow mmap of /dev/xf86 */
63 		/*		7	   formerly int: apmwarn */
64 		/*		8	   formerly int: keyboard reset */
65 		/*		9	   formerly int: CPU_ZTSRAWMODE */
66 		/*		10	   formerly struct: CPU_ZTSSCALE */
67 #define	CPU_MAXSPEED		11	/* int: number of valid machdep ids */
68 		/*		12	   formerly int: CPU_LIDSUSPEND */
69 #define CPU_LIDACTION		13	/* action caused by lid close */
70 #define	CPU_MAXID		14	/* number of valid machdep ids */
71 
72 #define	CTL_MACHDEP_NAMES { \
73 	{ 0, 0 }, \
74 	{ 0, 0 }, \
75 	{ 0, 0 }, \
76 	{ 0, 0 }, \
77 	{ "console_device", CTLTYPE_STRUCT }, \
78 	{ "powersave", CTLTYPE_INT }, \
79 	{ "allowaperture", CTLTYPE_INT }, \
80 	{ 0, 0 }, \
81 	{ 0, 0 }, \
82 	{ 0, 0 }, \
83 	{ 0, 0 }, \
84 	{ "maxspeed", CTLTYPE_INT }, \
85 	{ 0, 0 }, \
86 	{ "lidaction", CTLTYPE_INT }, \
87 }
88 
89 #ifdef _KERNEL
90 
91 /*
92  * Kernel-only definitions
93  */
94 
95 #include <arm/cpuconf.h>
96 
97 #include <machine/intr.h>
98 #include <machine/frame.h>
99 #include <machine/pcb.h>
100 #include <arm/armreg.h>
101 
102 /* 1 == use cpu_sleep(), 0 == don't */
103 extern int cpu_do_powersave;
104 
105 /* All the CLKF_* macros take a struct clockframe * as an argument. */
106 
107 /*
108  * CLKF_USERMODE: Return TRUE/FALSE (1/0) depending on whether the
109  * frame came from USR mode or not.
110  */
111 #define CLKF_USERMODE(frame)	((frame->if_spsr & PSR_MODE) == PSR_USR32_MODE)
112 
113 /*
114  * CLKF_INTR: True if we took the interrupt from inside another
115  * interrupt handler.
116  */
117 #define CLKF_INTR(frame)	(curcpu()->ci_idepth > 1)
118 
119 /*
120  * CLKF_PC: Extract the program counter from a clockframe
121  */
122 #define CLKF_PC(frame)		(frame->if_pc)
123 
124 /*
125  * PROC_PC: Find out the program counter for the given process.
126  */
127 #define PROC_PC(p)	((p)->p_addr->u_pcb.pcb_tf->tf_pc)
128 #define PROC_STACK(p)	((p)->p_addr->u_pcb.pcb_tf->tf_usr_sp)
129 
130 /* The address of the vector page. */
131 extern vaddr_t vector_page;
132 void	arm32_vector_init(vaddr_t, int);
133 
134 #define	ARM_VEC_RESET			(1 << 0)
135 #define	ARM_VEC_UNDEFINED		(1 << 1)
136 #define	ARM_VEC_SWI			(1 << 2)
137 #define	ARM_VEC_PREFETCH_ABORT		(1 << 3)
138 #define	ARM_VEC_DATA_ABORT		(1 << 4)
139 #define	ARM_VEC_ADDRESS_EXCEPTION	(1 << 5)
140 #define	ARM_VEC_IRQ			(1 << 6)
141 #define	ARM_VEC_FIQ			(1 << 7)
142 
143 #define	ARM_NVEC			8
144 #define	ARM_VEC_ALL			0xffffffff
145 
146 /*
147  * Per-CPU information.  For now we assume one CPU.
148  */
149 
150 #include <sys/device.h>
151 #include <sys/sched.h>
152 #include <sys/srp.h>
153 
154 struct cpu_info {
155 	struct device		*ci_dev; /* Device corresponding to this CPU */
156 	struct cpu_info		*ci_next;
157 	struct schedstate_percpu ci_schedstate; /* scheduler state */
158 
159 	u_int32_t		ci_cpuid;
160 	uint64_t		ci_mpidr;
161 	int			ci_node;
162 	struct cpu_info		*ci_self;
163 
164 	struct proc		*ci_curproc;
165 	struct proc		*ci_fpuproc;
166 	u_int32_t		ci_randseed;
167 
168 	struct pcb		*ci_curpcb;
169 	struct pcb		*ci_idle_pcb;
170 
171 	uint32_t		ci_cpl;
172 	uint32_t		ci_ipending;
173 	uint32_t		ci_idepth;
174 #ifdef DIAGNOSTIC
175 	int			ci_mutex_level;
176 #endif
177 	int			ci_want_resched;
178 
179 	void			(*ci_flush_bp)(void);
180 
181 	struct opp_table	*ci_opp_table;
182 	volatile int		ci_opp_idx;
183 	volatile int		ci_opp_max;
184 	uint32_t		ci_cpu_supply;
185 
186 #ifdef MULTIPROCESSOR
187 	struct srp_hazard	ci_srp_hazards[SRP_HAZARD_NUM];
188 	volatile int		ci_flags;
189 	uint32_t		ci_ttbr0;
190 	vaddr_t			ci_pl1_stkend;
191 	vaddr_t			ci_irq_stkend;
192 	vaddr_t			ci_abt_stkend;
193 	vaddr_t			ci_und_stkend;
194 #endif
195 
196 #ifdef GPROF
197 	struct gmonparam *ci_gmon;
198 #endif
199 };
200 
201 #define CPUF_PRIMARY 		(1<<0)
202 #define CPUF_AP	 		(1<<1)
203 #define CPUF_IDENTIFY		(1<<2)
204 #define CPUF_IDENTIFIED		(1<<3)
205 #define CPUF_PRESENT		(1<<4)
206 #define CPUF_GO			(1<<5)
207 #define CPUF_RUNNING		(1<<6)
208 
209 static inline struct cpu_info *
210 curcpu(void)
211 {
212 	struct cpu_info *__ci;
213 	__asm volatile("mrc	p15, 0, %0, c13, c0, 4" : "=r" (__ci));
214 	return (__ci);
215 }
216 
217 extern struct cpu_info cpu_info_primary;
218 extern struct cpu_info *cpu_info_list;
219 
220 #ifndef MULTIPROCESSOR
221 #define cpu_number()	0
222 #define CPU_IS_PRIMARY(ci)	1
223 #define CPU_INFO_ITERATOR	int
224 #define CPU_INFO_FOREACH(cii, ci) \
225 	for (cii = 0, ci = curcpu(); ci != NULL; ci = NULL)
226 #define CPU_INFO_UNIT(ci)	0
227 #define MAXCPUS	1
228 #define cpu_kick(ci)
229 #define cpu_unidle(ci)
230 #else
231 #define cpu_number()		(curcpu()->ci_cpuid)
232 #define CPU_IS_PRIMARY(ci)	((ci) == &cpu_info_primary)
233 #define CPU_INFO_ITERATOR		int
234 #define CPU_INFO_FOREACH(cii, ci)	for (cii = 0, ci = cpu_info_list; \
235 					    ci != NULL; ci = ci->ci_next)
236 #define CPU_INFO_UNIT(ci)	((ci)->ci_dev ? (ci)->ci_dev->dv_unit : 0)
237 #define MAXCPUS	4
238 void cpu_kick(struct cpu_info *);
239 void cpu_unidle(struct cpu_info *ci);
240 
241 extern struct cpu_info *cpu_info[MAXCPUS];
242 
243 void cpu_boot_secondary_processors(void);
244 #endif /* !MULTIPROCESSOR */
245 
246 #define CPU_BUSY_CYCLE()	do {} while (0)
247 
248 #define curpcb		curcpu()->ci_curpcb
249 
250 /*
251  * Scheduling glue
252  */
253 
254 extern int astpending;
255 #define setsoftast() (astpending = 1)
256 
257 /*
258  * Notify the current process (p) that it has a signal pending,
259  * process as soon as possible.
260  */
261 
262 #define signotify(p)            setsoftast()
263 
264 /*
265  * Preempt the current process if in interrupt from user mode,
266  * or after the current trap/syscall if in system mode.
267  */
268 extern int want_resched;	/* resched() was called */
269 #define	need_resched(ci)	(want_resched = 1, setsoftast())
270 #define clear_resched(ci) 	want_resched = 0
271 
272 /*
273  * Give a profiling tick to the current process when the user profiling
274  * buffer pages are invalid.  On the i386, request an ast to send us
275  * through trap(), marking the proc as needing a profiling tick.
276  */
277 #define	need_proftick(p)	setsoftast()
278 
279 /*
280  * cpu device glue (belongs in cpuvar.h)
281  */
282 
283 int	cpu_alloc_idle_pcb(struct cpu_info *);
284 
285 /*
286  * Random cruft
287  */
288 
289 /* cpuswitch.S */
290 struct pcb;
291 void	savectx		(struct pcb *pcb);
292 
293 /* machdep.h */
294 void bootsync		(int);
295 
296 /* fault.c */
297 int badaddr_read	(void *, size_t, void *);
298 
299 /* syscall.c */
300 void swi_handler	(trapframe_t *);
301 
302 /* machine_machdep.c */
303 void board_startup(void);
304 
305 static inline u_long
306 intr_disable(void)
307 {
308 	uint32_t cpsr;
309 
310 	__asm volatile ("mrs %0, cpsr" : "=r"(cpsr));
311 	__asm volatile ("msr cpsr_c, %0" :: "r"(cpsr | PSR_I));
312 
313 	return cpsr;
314 }
315 
316 static inline void
317 intr_restore(u_long cpsr)
318 {
319 	__asm volatile ("msr cpsr_c, %0" :: "r"(cpsr));
320 }
321 
322 void	cpu_startclock(void);
323 
324 #endif /* _KERNEL */
325 
326 #ifdef MULTIPROCESSOR
327 #include <sys/mplock.h>
328 #endif /* MULTIPROCESSOR */
329 
330 #endif /* !_ARM_CPU_H_ */
331