xref: /freebsd/sys/arm64/include/cpu.h (revision 5b9c547c)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * Copyright (c) 2014 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Portions of this software were developed by Andrew Turner
10  * under sponsorship from the FreeBSD Foundation
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	from: @(#)cpu.h 5.4 (Berkeley) 5/9/91
37  *	from: FreeBSD: src/sys/i386/include/cpu.h,v 1.62 2001/06/29
38  * $FreeBSD$
39  */
40 
41 #ifndef _MACHINE_CPU_H_
42 #define	_MACHINE_CPU_H_
43 
44 #include <machine/atomic.h>
45 #include <machine/frame.h>
46 
47 #define	TRAPF_PC(tfp)		((tfp)->tf_lr)
48 #define	TRAPF_USERMODE(tfp)	(((tfp)->tf_elr & (1ul << 63)) == 0)
49 
50 #define	cpu_getstack(td)	((td)->td_frame->tf_sp)
51 #define	cpu_setstack(td, sp)	((td)->td_frame->tf_sp = (sp))
52 #define	cpu_spinwait()		/* nothing */
53 
54 /* Extract CPU affinity levels 0-3 */
55 #define	CPU_AFF0(mpidr)	(u_int)(((mpidr) >> 0) & 0xff)
56 #define	CPU_AFF1(mpidr)	(u_int)(((mpidr) >> 8) & 0xff)
57 #define	CPU_AFF2(mpidr)	(u_int)(((mpidr) >> 16) & 0xff)
58 #define	CPU_AFF3(mpidr)	(u_int)(((mpidr) >> 32) & 0xff)
59 #define	CPU_AFF_MASK	0xff00ffffffUL	/* Mask affinity fields in MPIDR_EL1 */
60 
61 #ifdef _KERNEL
62 
63 extern char btext[];
64 extern char etext[];
65 
66 extern uint64_t __cpu_affinity[];
67 
68 void	cpu_halt(void) __dead2;
69 void	cpu_reset(void) __dead2;
70 void	fork_trampoline(void);
71 void	identify_cpu(void);
72 void	swi_vm(void *v);
73 
74 #define	CPU_AFFINITY(cpu)	__cpu_affinity[(cpu)]
75 
76 static __inline uint64_t
77 get_cyclecount(void)
78 {
79 
80 	/* TODO: This is bogus */
81 	return (1);
82 }
83 
84 #define	ADDRESS_TRANSLATE_FUNC(stage)				\
85 static inline uint64_t						\
86 arm64_address_translate_ ##stage (uint64_t addr)		\
87 {								\
88 	uint64_t ret;						\
89 								\
90 	__asm __volatile(					\
91 	    "at " __STRING(stage) ", %1 \n"					\
92 	    "mrs %0, par_el1" : "=r"(ret) : "r"(addr));		\
93 								\
94 	return (ret);						\
95 }
96 
97 ADDRESS_TRANSLATE_FUNC(s1e0r)
98 ADDRESS_TRANSLATE_FUNC(s1e0w)
99 ADDRESS_TRANSLATE_FUNC(s1e1r)
100 ADDRESS_TRANSLATE_FUNC(s1e1w)
101 
102 #endif
103 
104 #endif /* !_MACHINE_CPU_H_ */
105