Home | History | Annotate | Line # | Download | only in include
cpu.h revision 1.115
      1 /*	$NetBSD: cpu.h,v 1.115 2021/02/20 14:51:06 jmcneill Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994-1996 Mark Brinicombe.
      5  * Copyright (c) 1994 Brini.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software written for Brini by Mark Brinicombe
      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 Brini.
     21  * 4. The name of the company nor the name of the author may be used to
     22  *    endorse or promote products derived from this software without specific
     23  *    prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
     26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  * RiscBSD kernel project
     38  *
     39  * cpu.h
     40  *
     41  * CPU specific symbols
     42  *
     43  * Created      : 18/09/94
     44  *
     45  * Based on kate/katelib/arm6.h
     46  */
     47 
     48 #ifndef _ARM_CPU_H_
     49 #define _ARM_CPU_H_
     50 
     51 #ifdef _KERNEL
     52 #ifndef _LOCORE
     53 
     54 typedef unsigned long mpidr_t;
     55 
     56 #ifdef MULTIPROCESSOR
     57 extern u_int arm_cpu_max;
     58 extern mpidr_t cpu_mpidr[];
     59 extern kmutex_t cpu_hatch_lock;
     60 
     61 void cpu_boot_secondary_processors(void);
     62 void cpu_mpstart(void);
     63 bool cpu_hatched_p(u_int);
     64 
     65 void cpu_clr_mbox(int);
     66 void cpu_set_hatched(int);
     67 
     68 #endif
     69 
     70 void	cpu_proc_fork(struct proc *, struct proc *);
     71 
     72 #endif	/* !_LOCORE */
     73 #endif	/* _KERNEL */
     74 
     75 #ifdef __arm__
     76 
     77 /*
     78  * User-visible definitions
     79  */
     80 
     81 /*  CTL_MACHDEP definitions. */
     82 #define	CPU_DEBUG		1	/* int: misc kernel debug control */
     83 #define	CPU_BOOTED_DEVICE	2	/* string: device we booted from */
     84 #define	CPU_BOOTED_KERNEL	3	/* string: kernel we booted */
     85 #define	CPU_CONSDEV		4	/* struct: dev_t of our console */
     86 #define	CPU_POWERSAVE		5	/* int: use CPU powersave mode */
     87 
     88 #if defined(_KERNEL) || defined(_KMEMUSER)
     89 
     90 /*
     91  * Kernel-only definitions
     92  */
     93 
     94 #if !defined(_MODULE) && defined(_KERNEL_OPT)
     95 #include "opt_multiprocessor.h"
     96 #include "opt_cpuoptions.h"
     97 #include "opt_lockdebug.h"
     98 #include "opt_cputypes.h"
     99 #endif /* !_MODULE && _KERNEL_OPT */
    100 
    101 #ifndef _LOCORE
    102 #if defined(TPIDRPRW_IS_CURLWP) || defined(TPIDRPRW_IS_CURCPU)
    103 #include <arm/armreg.h>
    104 #endif /* TPIDRPRW_IS_CURLWP || TPIDRPRW_IS_CURCPU */
    105 
    106 /* 1 == use cpu_sleep(), 0 == don't */
    107 extern int cpu_do_powersave;
    108 extern int cpu_fpu_present;
    109 
    110 /* All the CLKF_* macros take a struct clockframe * as an argument. */
    111 
    112 /*
    113  * CLKF_USERMODE: Return TRUE/FALSE (1/0) depending on whether the
    114  * frame came from USR mode or not.
    115  */
    116 #define CLKF_USERMODE(cf) (((cf)->cf_tf.tf_spsr & PSR_MODE) == PSR_USR32_MODE)
    117 
    118 /*
    119  * CLKF_INTR: True if we took the interrupt from inside another
    120  * interrupt handler.
    121  */
    122 #if !defined(__ARM_EABI__)
    123 /* Hack to treat FPE time as interrupt time so we can measure it */
    124 #define CLKF_INTR(cf)						\
    125 	((curcpu()->ci_intr_depth > 1) ||			\
    126 	    ((cf)->cf_tf.tf_spsr & PSR_MODE) == PSR_UND32_MODE)
    127 #else
    128 #define CLKF_INTR(cf)	((void)(cf), curcpu()->ci_intr_depth > 1)
    129 #endif
    130 
    131 /*
    132  * CLKF_PC: Extract the program counter from a clockframe
    133  */
    134 #define CLKF_PC(frame)		(frame->cf_tf.tf_pc)
    135 
    136 /*
    137  * LWP_PC: Find out the program counter for the given lwp.
    138  */
    139 #define LWP_PC(l)		(lwp_trapframe(l)->tf_pc)
    140 
    141 /*
    142  * Per-CPU information.  For now we assume one CPU.
    143  */
    144 #ifdef _KERNEL
    145 static inline int curcpl(void);
    146 static inline void set_curcpl(int);
    147 static inline void cpu_dosoftints(void);
    148 #endif
    149 
    150 #ifdef _KMEMUSER
    151 #include <sys/intr.h>
    152 #endif
    153 #include <sys/atomic.h>
    154 #include <sys/cpu_data.h>
    155 #include <sys/device_if.h>
    156 #include <sys/evcnt.h>
    157 #include <machine/param.h>
    158 
    159 struct cpu_info {
    160 	struct cpu_data	ci_data;	/* MI per-cpu data */
    161 	device_t	ci_dev;		/* Device corresponding to this CPU */
    162 	cpuid_t		ci_cpuid;
    163 	uint32_t	ci_arm_cpuid;	/* aggregate CPU id */
    164 	uint32_t	ci_arm_cputype;	/* CPU type */
    165 	uint32_t	ci_arm_cpurev;	/* CPU revision */
    166 	uint32_t	ci_ctrl;	/* The CPU control register */
    167 
    168 	/*
    169 	 * the following are in their own cache line, as they are stored to
    170 	 * regularly by remote CPUs; when they were mixed with other fields
    171 	 * we observed frequent cache misses.
    172 	 */
    173 	int		ci_want_resched __aligned(COHERENCY_UNIT);
    174 					/* resched() was called */
    175 	lwp_t *		ci_curlwp __aligned(COHERENCY_UNIT);
    176 					/* current lwp */
    177 	lwp_t *		ci_onproc;	/* current user LWP / kthread */
    178 
    179 	/*
    180 	 * largely CPU-private.
    181 	 */
    182 	lwp_t *		ci_softlwps[SOFTINT_COUNT] __aligned(COHERENCY_UNIT);
    183 
    184 	struct cpu_softc *
    185 			ci_softc;	/* platform softc */
    186 
    187 	int		ci_cpl;		/* current processor level (spl) */
    188 	int		ci_kfpu_spl;
    189 
    190 	volatile u_int	ci_intr_depth;	/* */
    191 	volatile u_int	ci_softints;
    192 	volatile uint32_t ci_blocked_pics;
    193 	volatile uint32_t ci_pending_pics;
    194 	volatile uint32_t ci_pending_ipls;
    195 
    196 	lwp_t *		ci_lastlwp;	/* last lwp */
    197 
    198 	struct evcnt	ci_arm700bugcount;
    199 	int32_t		ci_mtx_count;
    200 	int		ci_mtx_oldspl;
    201 	register_t	ci_undefsave[3];
    202 	uint32_t	ci_vfp_id;
    203 	uint64_t	ci_lastintr;
    204 
    205 	struct pmap_tlb_info *
    206 			ci_tlb_info;
    207 	struct pmap *	ci_pmap_lastuser;
    208 	struct pmap *	ci_pmap_cur;
    209 	tlb_asid_t	ci_pmap_asid_cur;
    210 
    211 	struct trapframe *
    212 			ci_ddb_regs;
    213 
    214 	struct evcnt	ci_abt_evs[16];
    215 	struct evcnt	ci_und_ev;
    216 	struct evcnt	ci_und_cp15_ev;
    217 	struct evcnt	ci_vfp_evs[3];
    218 
    219 	uint32_t	ci_midr;
    220 	uint32_t	ci_mpidr;
    221 	uint32_t	ci_capacity_dmips_mhz;
    222 
    223 	struct arm_cache_info *
    224 			ci_cacheinfo;
    225 };
    226 
    227 extern struct cpu_info cpu_info_store[];
    228 
    229 struct lwp *arm_curlwp(void);
    230 struct cpu_info *arm_curcpu(void);
    231 
    232 #ifdef _KERNEL
    233 #if defined(_MODULE)
    234 
    235 #define	curlwp		arm_curlwp()
    236 #define curcpu()	arm_curcpu()
    237 
    238 #elif defined(TPIDRPRW_IS_CURLWP)
    239 static inline struct lwp *
    240 _curlwp(void)
    241 {
    242 	return (struct lwp *) armreg_tpidrprw_read();
    243 }
    244 
    245 static inline void
    246 _curlwp_set(struct lwp *l)
    247 {
    248 	armreg_tpidrprw_write((uintptr_t)l);
    249 }
    250 
    251 // Also in <sys/lwp.h> but also here if this was included before <sys/lwp.h>
    252 static inline struct cpu_info *lwp_getcpu(struct lwp *);
    253 
    254 #define	curlwp		_curlwp()
    255 // curcpu() expands into two instructions: a mrc and a ldr
    256 #define	curcpu()	lwp_getcpu(_curlwp())
    257 #elif defined(TPIDRPRW_IS_CURCPU)
    258 #ifdef __HAVE_PREEMPTION
    259 #error __HAVE_PREEMPTION requires TPIDRPRW_IS_CURLWP
    260 #endif
    261 static inline struct cpu_info *
    262 curcpu(void)
    263 {
    264 	return (struct cpu_info *) armreg_tpidrprw_read();
    265 }
    266 #elif !defined(MULTIPROCESSOR)
    267 #define	curcpu()	(&cpu_info_store[0])
    268 #elif !defined(__HAVE_PREEMPTION)
    269 #error MULTIPROCESSOR && !__HAVE_PREEMPTION requires TPIDRPRW_IS_CURCPU or TPIDRPRW_IS_CURLWP
    270 #else
    271 #error MULTIPROCESSOR && __HAVE_PREEMPTION requires TPIDRPRW_IS_CURLWP
    272 #endif /* !TPIDRPRW_IS_CURCPU && !TPIDRPRW_IS_CURLWP */
    273 
    274 #ifndef curlwp
    275 #define	curlwp		(curcpu()->ci_curlwp)
    276 #endif
    277 #define curpcb		((struct pcb *)lwp_getpcb(curlwp))
    278 
    279 #define CPU_INFO_ITERATOR	int
    280 #if defined(_MODULE) || defined(MULTIPROCESSOR)
    281 extern struct cpu_info *cpu_info[];
    282 #define cpu_number()		(curcpu()->ci_index)
    283 #define CPU_IS_PRIMARY(ci)	((ci)->ci_index == 0)
    284 #define CPU_INFO_FOREACH(cii, ci)			\
    285 	cii = 0, ci = cpu_info[0]; cii < (ncpu ? ncpu : 1) && (ci = cpu_info[cii]) != NULL; cii++
    286 #else
    287 #define cpu_number()            0
    288 
    289 #define CPU_IS_PRIMARY(ci)	true
    290 #define CPU_INFO_FOREACH(cii, ci)			\
    291 	cii = 0, __USE(cii), ci = curcpu(); ci != NULL; ci = NULL
    292 #endif
    293 
    294 #if defined(MULTIPROCESSOR)
    295 void cpu_init_secondary_processor(int);
    296 #endif
    297 
    298 #define	LWP0_CPU_INFO	(&cpu_info_store[0])
    299 
    300 static inline int
    301 curcpl(void)
    302 {
    303 	return curcpu()->ci_cpl;
    304 }
    305 
    306 static inline void
    307 set_curcpl(int pri)
    308 {
    309 	curcpu()->ci_cpl = pri;
    310 }
    311 
    312 static inline void
    313 cpu_dosoftints(void)
    314 {
    315 #ifdef __HAVE_FAST_SOFTINTS
    316 	void	dosoftints(void);
    317 #ifndef __HAVE_PIC_FAST_SOFTINTS
    318 	struct cpu_info * const ci = curcpu();
    319 	if (ci->ci_intr_depth == 0 && (ci->ci_softints >> ci->ci_cpl) > 0)
    320 		dosoftints();
    321 #endif
    322 #endif
    323 }
    324 
    325 /*
    326  * Scheduling glue
    327  */
    328 void cpu_signotify(struct lwp *);
    329 #define	setsoftast(ci)		(cpu_signotify((ci)->ci_onproc))
    330 
    331 /*
    332  * Give a profiling tick to the current process when the user profiling
    333  * buffer pages are invalid.  On the i386, request an ast to send us
    334  * through trap(), marking the proc as needing a profiling tick.
    335  */
    336 #define	cpu_need_proftick(l)	((l)->l_pflag |= LP_OWEUPC, \
    337 				 setsoftast(lwp_getcpu(l)))
    338 
    339 /*
    340  * We've already preallocated the stack for the idlelwps for additional CPUs.
    341  * This hook allows to return them.
    342  */
    343 vaddr_t cpu_uarea_alloc_idlelwp(struct cpu_info *);
    344 
    345 #ifdef _ARM_ARCH_6
    346 int	cpu_maxproc_hook(int);
    347 #endif
    348 
    349 #endif /* _KERNEL */
    350 
    351 #endif /* !_LOCORE */
    352 
    353 #endif /* _KERNEL || _KMEMUSER */
    354 
    355 #elif defined(__aarch64__)
    356 
    357 #include <aarch64/cpu.h>
    358 
    359 #endif /* __arm__/__aarch64__ */
    360 
    361 #endif /* !_ARM_CPU_H_ */
    362