xref: /freebsd/sys/arm/include/pcpu.h (revision 2ff63af9)
16fc729afSOlivier Houchard /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3af3dc4a7SPedro F. Giffuni  *
46fc729afSOlivier Houchard  * Copyright (c) 1999 Luoqi Chen <luoqi@freebsd.org>
56fc729afSOlivier Houchard  * All rights reserved.
66fc729afSOlivier Houchard  *
76fc729afSOlivier Houchard  * Redistribution and use in source and binary forms, with or without
86fc729afSOlivier Houchard  * modification, are permitted provided that the following conditions
96fc729afSOlivier Houchard  * are met:
106fc729afSOlivier Houchard  * 1. Redistributions of source code must retain the above copyright
116fc729afSOlivier Houchard  *    notice, this list of conditions and the following disclaimer.
126fc729afSOlivier Houchard  * 2. Redistributions in binary form must reproduce the above copyright
136fc729afSOlivier Houchard  *    notice, this list of conditions and the following disclaimer in the
146fc729afSOlivier Houchard  *    documentation and/or other materials provided with the distribution.
156fc729afSOlivier Houchard  *
166fc729afSOlivier Houchard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
176fc729afSOlivier Houchard  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
186fc729afSOlivier Houchard  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
196fc729afSOlivier Houchard  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
206fc729afSOlivier Houchard  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
216fc729afSOlivier Houchard  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
226fc729afSOlivier Houchard  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
236fc729afSOlivier Houchard  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
246fc729afSOlivier Houchard  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256fc729afSOlivier Houchard  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266fc729afSOlivier Houchard  * SUCH DAMAGE.
276fc729afSOlivier Houchard  *
286fc729afSOlivier Houchard  *	from: FreeBSD: src/sys/i386/include/globaldata.h,v 1.27 2001/04/27
296fc729afSOlivier Houchard  */
306fc729afSOlivier Houchard 
316fc729afSOlivier Houchard #ifndef	_MACHINE_PCPU_H_
326fc729afSOlivier Houchard #define	_MACHINE_PCPU_H_
336fc729afSOlivier Houchard 
346fc729afSOlivier Houchard #ifdef _KERNEL
356fc729afSOlivier Houchard 
36e0b79e66SJason A. Harmening #include <sys/_lock.h>
37e0b79e66SJason A. Harmening #include <sys/_mutex.h>
38e0b79e66SJason A. Harmening 
396fc729afSOlivier Houchard struct vmspace;
406fc729afSOlivier Houchard 
4170d12a18SJohn Baldwin #endif	/* _KERNEL */
4270d12a18SJohn Baldwin 
43a36b6ec0SMichal Meloun /* Branch predictor hardening method */
44a36b6ec0SMichal Meloun #define PCPU_BP_HARDEN_KIND_NONE		0
45a36b6ec0SMichal Meloun #define PCPU_BP_HARDEN_KIND_BPIALL		1
46a36b6ec0SMichal Meloun #define PCPU_BP_HARDEN_KIND_ICIALLU		2
47e0b79e66SJason A. Harmening 
48cf1a573fSOleksandr Tymoshenko #define PCPU_MD_FIELDS							\
49cf1a573fSOleksandr Tymoshenko 	unsigned int pc_vfpsid;						\
50cf1a573fSOleksandr Tymoshenko 	unsigned int pc_vfpmvfr0;					\
51cf1a573fSOleksandr Tymoshenko 	unsigned int pc_vfpmvfr1;					\
5217dece86SGleb Smirnoff 	struct pmap *pc_curpmap;					\
53e0b79e66SJason A. Harmening 	struct mtx pc_cmap_lock;					\
54e0b79e66SJason A. Harmening 	void *pc_cmap1_pte2p;						\
55e0b79e66SJason A. Harmening 	void *pc_cmap2_pte2p;						\
56e0b79e66SJason A. Harmening 	caddr_t pc_cmap1_addr;						\
57e0b79e66SJason A. Harmening 	caddr_t pc_cmap2_addr;						\
58713841afSJason A. Harmening 	vm_offset_t pc_qmap_addr;					\
59fc100b8aSJason A. Harmening 	void *pc_qmap_pte2p;						\
6060ba692cSZbigniew Bodek 	unsigned int pc_dbreg[32];					\
6160ba692cSZbigniew Bodek 	int pc_dbreg_cmd;						\
62a36b6ec0SMichal Meloun 	int pc_bp_harden_kind;						\
63a36b6ec0SMichal Meloun 	uint32_t pc_original_actlr;					\
646185fb0fSEmmanuel Vadot 	uint64_t pc_clock;						\
65bd891015SMichal Meloun 	uint32_t pc_mpidr;						\
66bd891015SMichal Meloun 	char __pad[135]
67cf1a573fSOleksandr Tymoshenko 
6870d12a18SJohn Baldwin #ifdef _KERNEL
6970d12a18SJohn Baldwin 
7060ba692cSZbigniew Bodek #define	PC_DBREG_CMD_NONE	0
7160ba692cSZbigniew Bodek #define	PC_DBREG_CMD_LOAD	1
7260ba692cSZbigniew Bodek 
736fc729afSOlivier Houchard struct pcb;
746fc729afSOlivier Houchard struct pcpu;
756fc729afSOlivier Houchard 
766fc729afSOlivier Houchard extern struct pcpu *pcpup;
776fc729afSOlivier Houchard 
78646b9404SOlivier Houchard #define CPU_MASK (0xf)
79646b9404SOlivier Houchard 
80d5e7c3b7SOlivier Houchard #ifndef SMP
81d5e7c3b7SOlivier Houchard #define get_pcpu() (pcpup)
82d5e7c3b7SOlivier Houchard #else
83646b9404SOlivier Houchard #define get_pcpu() __extension__ ({			  		\
84646b9404SOlivier Houchard     	int id;								\
85646b9404SOlivier Houchard         __asm __volatile("mrc p15, 0, %0, c0, c0, 5" : "=r" (id));	\
86646b9404SOlivier Houchard     	(pcpup + (id & CPU_MASK));					\
87646b9404SOlivier Houchard     })
88d5e7c3b7SOlivier Houchard #endif
89646b9404SOlivier Houchard 
90646b9404SOlivier Houchard static inline struct thread *
get_curthread(void)91646b9404SOlivier Houchard get_curthread(void)
92646b9404SOlivier Houchard {
93646b9404SOlivier Houchard 	void *ret;
94646b9404SOlivier Houchard 
95646b9404SOlivier Houchard 	__asm __volatile("mrc p15, 0, %0, c13, c0, 4" : "=r" (ret));
96646b9404SOlivier Houchard 	return (ret);
97cf1a573fSOleksandr Tymoshenko }
98c640357fSAlan Cox 
99cf1a573fSOleksandr Tymoshenko static inline void
set_curthread(struct thread * td)100646b9404SOlivier Houchard set_curthread(struct thread *td)
101cf1a573fSOleksandr Tymoshenko {
102cf1a573fSOleksandr Tymoshenko 
103646b9404SOlivier Houchard 	__asm __volatile("mcr p15, 0, %0, c13, c0, 4" : : "r" (td));
104cf1a573fSOleksandr Tymoshenko }
105cf1a573fSOleksandr Tymoshenko 
106cf1a573fSOleksandr Tymoshenko static inline void *
get_tls(void)107cf1a573fSOleksandr Tymoshenko get_tls(void)
108cf1a573fSOleksandr Tymoshenko {
109cf1a573fSOleksandr Tymoshenko 	void *tls;
110cf1a573fSOleksandr Tymoshenko 
111fa878ec3SEd Schouten 	/* TPIDRURW contains the authoritative value. */
112fa878ec3SEd Schouten 	__asm __volatile("mrc p15, 0, %0, c13, c0, 2" : "=r" (tls));
113cf1a573fSOleksandr Tymoshenko 	return (tls);
114cf1a573fSOleksandr Tymoshenko }
115cf1a573fSOleksandr Tymoshenko 
116cf1a573fSOleksandr Tymoshenko static inline void
set_tls(void * tls)117cf1a573fSOleksandr Tymoshenko set_tls(void *tls)
118cf1a573fSOleksandr Tymoshenko {
119cf1a573fSOleksandr Tymoshenko 
120fa878ec3SEd Schouten 	/*
121fa878ec3SEd Schouten 	 * Update both TPIDRURW and TPIDRURO. TPIDRURW needs to be written
122fa878ec3SEd Schouten 	 * first to ensure that a context switch between the two writes will
123fa878ec3SEd Schouten 	 * still give the desired result of updating both.
124fa878ec3SEd Schouten 	 */
125fa878ec3SEd Schouten 	__asm __volatile(
126fa878ec3SEd Schouten 	    "mcr p15, 0, %0, c13, c0, 2\n"
127fa878ec3SEd Schouten 	    "mcr p15, 0, %0, c13, c0, 3\n"
128fa878ec3SEd Schouten 	     : : "r" (tls));
129cf1a573fSOleksandr Tymoshenko }
130646b9404SOlivier Houchard 
131646b9404SOlivier Houchard #define curthread get_curthread()
132646b9404SOlivier Houchard 
133cf1a573fSOleksandr Tymoshenko 
134cf1a573fSOleksandr Tymoshenko #define	PCPU_GET(member)	(get_pcpu()->pc_ ## member)
135cf1a573fSOleksandr Tymoshenko #define	PCPU_ADD(member, value)	(get_pcpu()->pc_ ## member += (value))
13600100b0eSOlivier Houchard #define	PCPU_PTR(member)	(&get_pcpu()->pc_ ## member)
13700100b0eSOlivier Houchard #define	PCPU_SET(member,value)	(get_pcpu()->pc_ ## member = (value))
1386fc729afSOlivier Houchard 
139544f047fSAndrew Turner #define	PCPU_GET_MPIDR(pc)	((pc)->pc_mpidr)
140544f047fSAndrew Turner 
141cf1a573fSOleksandr Tymoshenko void pcpu0_init(void);
1426fc729afSOlivier Houchard #endif	/* _KERNEL */
1436fc729afSOlivier Houchard 
1446fc729afSOlivier Houchard #endif	/* !_MACHINE_PCPU_H_ */
145