1 /* $NetBSD: cpu.h,v 1.9 2022/11/17 09:50:23 simonb Exp $ */ 2 3 /*- 4 * Copyright (c) 2014 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 _RISCV_CPU_H_ 33 #define _RISCV_CPU_H_ 34 35 #if defined(_KERNEL) || defined(_KMEMUSER) 36 37 struct clockframe { 38 vaddr_t cf_epc; 39 register_t cf_status; 40 int cf_intr_depth; 41 }; 42 43 #define CLKF_USERMODE(cf) (((cf)->cf_status & SR_SPP) == 0) 44 #define CLKF_PC(cf) ((cf)->cf_epc) 45 #define CLKF_INTR(cf) ((cf)->cf_intr_depth > 0) 46 47 #include <sys/cpu_data.h> 48 #include <sys/device_if.h> 49 #include <sys/evcnt.h> 50 #include <sys/intr.h> 51 52 struct cpu_info { 53 struct cpu_data ci_data; 54 device_t ci_dev; 55 cpuid_t ci_cpuid; 56 struct lwp *ci_curlwp; 57 struct lwp *ci_onproc; /* current user LWP / kthread */ 58 struct lwp *ci_softlwps[SOFTINT_COUNT]; 59 struct trapframe *ci_ddb_regs; 60 61 uint64_t ci_lastintr; 62 63 int ci_mtx_oldspl; 64 int ci_mtx_count; 65 66 int ci_want_resched; 67 int ci_cpl; 68 u_int ci_softints; 69 volatile u_int ci_intr_depth; 70 71 tlb_asid_t ci_pmap_asid_cur; 72 73 union pmap_segtab *ci_pmap_user_segtab; 74 #ifdef _LP64 75 union pmap_segtab *ci_pmap_user_seg0tab; 76 #endif 77 78 struct evcnt ci_ev_fpu_saves; 79 struct evcnt ci_ev_fpu_loads; 80 struct evcnt ci_ev_fpu_reenables; 81 #if defined(GPROF) && defined(MULTIPROCESSOR) 82 struct gmonparam *ci_gmon; /* MI per-cpu GPROF */ 83 #endif 84 }; 85 86 #endif /* _KERNEL || _KMEMUSER */ 87 88 #ifdef _KERNEL 89 90 extern struct cpu_info cpu_info_store; 91 92 // This is also in <sys/lwp.h> 93 struct lwp; 94 static inline struct cpu_info *lwp_getcpu(struct lwp *); 95 96 register struct lwp *riscv_curlwp __asm("tp"); 97 #define curlwp riscv_curlwp 98 #define curcpu() lwp_getcpu(curlwp) 99 100 static inline cpuid_t 101 cpu_number(void) 102 { 103 #ifdef MULTIPROCESSOR 104 return curcpu()->ci_cpuid; 105 #else 106 return 0; 107 #endif 108 } 109 110 void cpu_proc_fork(struct proc *, struct proc *); 111 void cpu_signotify(struct lwp *); 112 void cpu_need_proftick(struct lwp *l); 113 void cpu_boot_secondary_processors(void); 114 115 #define CPU_INFO_ITERATOR cpuid_t 116 #ifdef MULTIPROCESSOR 117 #define CPU_INFO_FOREACH(cii, ci) \ 118 (cii) = 0; ((ci) = cpu_infos[cii]) != NULL; (cii)++ 119 #else 120 #define CPU_INFO_FOREACH(cii, ci) \ 121 (cii) = 0, (ci) = curcpu(); (cii) == 0; (cii)++ 122 #endif 123 124 #define CPU_INFO_CURPMAP(ci) (curlwp->l_proc->p_vmspace->vm_map.pmap) 125 126 static inline void 127 cpu_dosoftints(void) 128 { 129 extern void dosoftints(void); 130 struct cpu_info * const ci = curcpu(); 131 if (ci->ci_intr_depth == 0 132 && (ci->ci_data.cpu_softints >> ci->ci_cpl) > 0) 133 dosoftints(); 134 } 135 136 static inline bool 137 cpu_intr_p(void) 138 { 139 return curcpu()->ci_intr_depth > 0; 140 } 141 142 #define LWP_PC(l) cpu_lwp_pc(l) 143 144 vaddr_t cpu_lwp_pc(struct lwp *); 145 146 static inline void 147 cpu_idle(void) 148 { 149 } 150 151 #endif /* _KERNEL */ 152 153 #endif /* _RISCV_CPU_H_ */ 154