Home | History | Annotate | Line # | Download | only in include
cpu.h revision 1.36
      1 /* $NetBSD: cpu.h,v 1.36 2021/05/29 06:54:20 skrll Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014, 2020 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas of 3am Software Foundry.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _AARCH64_CPU_H_
     33 #define _AARCH64_CPU_H_
     34 
     35 #include <arm/cpu.h>
     36 
     37 #ifdef __aarch64__
     38 
     39 #ifdef _KERNEL_OPT
     40 #include "opt_multiprocessor.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 
     45 #if defined(_KERNEL) || defined(_KMEMUSER)
     46 #include <sys/evcnt.h>
     47 
     48 #include <aarch64/armreg.h>
     49 #include <aarch64/frame.h>
     50 
     51 struct clockframe {
     52 	struct trapframe cf_tf;
     53 };
     54 
     55 /* (spsr & 15) == SPSR_M_EL0T(64bit,0) or USER(32bit,0) */
     56 #define CLKF_USERMODE(cf)	((((cf)->cf_tf.tf_spsr) & 0x0f) == 0)
     57 #define CLKF_PC(cf)		((cf)->cf_tf.tf_pc)
     58 #define CLKF_INTR(cf)		((void)(cf), curcpu()->ci_intr_depth > 1)
     59 
     60 /*
     61  * LWP_PC: Find out the program counter for the given lwp.
     62  */
     63 #define LWP_PC(l)		((l)->l_md.md_utf->tf_pc)
     64 
     65 #include <sys/cpu_data.h>
     66 #include <sys/device_if.h>
     67 #include <sys/intr.h>
     68 
     69 struct aarch64_cpufuncs {
     70 	void (*cf_set_ttbr0)(uint64_t);
     71 	void (*cf_icache_sync_range)(vaddr_t, vsize_t);
     72 };
     73 
     74 struct cpu_info {
     75 	struct cpu_data ci_data;
     76 	device_t ci_dev;
     77 	cpuid_t ci_cpuid;
     78 
     79 	/*
     80 	 * the following are in their own cache line, as they are stored to
     81 	 * regularly by remote CPUs; when they were mixed with other fields
     82 	 * we observed frequent cache misses.
     83 	 */
     84 	int ci_want_resched __aligned(COHERENCY_UNIT);
     85 	/* XXX pending IPIs? */
     86 
     87 	/*
     88 	 * this is stored frequently, and is fetched by remote CPUs.
     89 	 */
     90 	struct lwp *ci_curlwp __aligned(COHERENCY_UNIT);
     91 	struct lwp *ci_onproc;
     92 
     93 	/*
     94 	 * largely CPU-private.
     95 	 */
     96 	struct lwp *ci_softlwps[SOFTINT_COUNT] __aligned(COHERENCY_UNIT);
     97 
     98 	uint64_t ci_lastintr;
     99 
    100 	int ci_mtx_oldspl;
    101 	int ci_mtx_count;
    102 
    103 	int ci_cpl;		/* current processor level (spl) */
    104 	int ci_hwpl;		/* current hardware priority */
    105 	volatile u_int ci_softints;
    106 	volatile u_int ci_intr_depth;
    107 
    108 	int ci_kfpu_spl;
    109 
    110 	/* event counters */
    111 	struct evcnt ci_vfp_use;
    112 	struct evcnt ci_vfp_reuse;
    113 	struct evcnt ci_vfp_save;
    114 	struct evcnt ci_vfp_release;
    115 	struct evcnt ci_uct_trap;
    116 	struct evcnt ci_intr_preempt;
    117 
    118 	/* FDT or similar supplied "cpu capacity" */
    119 	uint32_t ci_capacity_dmips_mhz;
    120 
    121 	/* interrupt controller */
    122 	u_int ci_gic_redist;	/* GICv3 redistributor index */
    123 	uint64_t ci_gic_sgir;	/* GICv3 SGIR target */
    124 
    125 	/* ACPI */
    126 	uint32_t ci_acpiid;	/* ACPI Processor Unique ID */
    127 
    128 	struct aarch64_sysctl_cpu_id ci_id;
    129 
    130 	struct aarch64_cache_info *ci_cacheinfo;
    131 	struct aarch64_cpufuncs ci_cpufuncs;
    132 
    133 } __aligned(COHERENCY_UNIT);
    134 
    135 #ifdef _KERNEL
    136 static inline struct lwp * __attribute__ ((const))
    137 aarch64_curlwp(void)
    138 {
    139 	struct lwp *l;
    140 	__asm("mrs %0, tpidr_el1" : "=r"(l));
    141 	return l;
    142 }
    143 
    144 /* forward declaration; defined in sys/lwp.h. */
    145 static __inline struct cpu_info *lwp_getcpu(struct lwp *);
    146 
    147 #define	curcpu()		(lwp_getcpu(aarch64_curlwp()))
    148 #define	setsoftast(ci)		(cpu_signotify((ci)->ci_onproc))
    149 #undef curlwp
    150 #define	curlwp			(aarch64_curlwp())
    151 
    152 static inline int
    153 cpu_maxproc(void)
    154 {
    155 	/*
    156 	 * the pmap uses PID for ASID.
    157 	 */
    158 	switch (__SHIFTOUT(reg_id_aa64mmfr0_el1_read(), ID_AA64MMFR0_EL1_ASIDBITS)) {
    159 	case ID_AA64MMFR0_EL1_ASIDBITS_8BIT:
    160 		return (1U << 8) - 1;
    161 	case ID_AA64MMFR0_EL1_ASIDBITS_16BIT:
    162 		return (1U << 16) - 1;
    163 	default:
    164 		return 0;
    165 	}
    166 }
    167 
    168 void	cpu_signotify(struct lwp *l);
    169 void	cpu_need_proftick(struct lwp *l);
    170 
    171 void	cpu_hatch(struct cpu_info *);
    172 
    173 extern struct cpu_info *cpu_info[];
    174 extern struct cpu_info cpu_info_store[];
    175 
    176 #define CPU_INFO_ITERATOR	int
    177 #if defined(MULTIPROCESSOR) || defined(_MODULE)
    178 #define cpu_number()		(curcpu()->ci_index)
    179 #define CPU_IS_PRIMARY(ci)	((ci)->ci_index == 0)
    180 #define CPU_INFO_FOREACH(cii, ci)					\
    181 	cii = 0, ci = cpu_info[0];					\
    182 	cii < (ncpu ? ncpu : 1) && (ci = cpu_info[cii]) != NULL;	\
    183 	cii++
    184 #else /* MULTIPROCESSOR */
    185 #define cpu_number()		0
    186 #define CPU_IS_PRIMARY(ci)	true
    187 #define CPU_INFO_FOREACH(cii, ci)					\
    188 	cii = 0, __USE(cii), ci = curcpu(); ci != NULL; ci = NULL
    189 #endif /* MULTIPROCESSOR */
    190 
    191 #define	LWP0_CPU_INFO	(&cpu_info_store[0])
    192 
    193 #define	__HAVE_CPU_DOSOFTINTS_CI
    194 
    195 static inline void
    196 cpu_dosoftints_ci(struct cpu_info *ci)
    197 {
    198 #if defined(__HAVE_FAST_SOFTINTS) && !defined(__HAVE_PIC_FAST_SOFTINTS)
    199 	void dosoftints(void);
    200 
    201 	if (ci->ci_intr_depth == 0 && (ci->ci_softints >> ci->ci_cpl) > 0) {
    202 		dosoftints();
    203 	}
    204 #endif
    205 }
    206 
    207 static inline void
    208 cpu_dosoftints(void)
    209 {
    210 #if defined(__HAVE_FAST_SOFTINTS) && !defined(__HAVE_PIC_FAST_SOFTINTS)
    211 	cpu_dosoftints_ci(curcpu());
    212 #endif
    213 }
    214 
    215 
    216 #endif /* _KERNEL */
    217 
    218 #endif /* _KERNEL || _KMEMUSER */
    219 
    220 #endif
    221 
    222 #endif /* _AARCH64_CPU_H_ */
    223