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