xref: /netbsd-src/sys/arch/or1k/include/cpu.h (revision 567a3a02e796214c92957e0949056e9002633394)
1 /* $NetBSD: cpu.h,v 1.5 2021/08/14 17:51:19 ryo 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 _OR1K_CPU_H_
33 #define _OR1K_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/intr.h>
49 
50 struct cpu_info {
51 	struct cpu_data ci_data;
52 	device_t ci_dev;
53 	cpuid_t ci_cpuid;
54 	struct lwp *ci_curlwp;
55 	struct lwp *ci_onproc;		/* current user LWP / kthread */
56 	struct lwp *ci_softlwps[SOFTINT_COUNT];
57 
58 	uint64_t ci_lastintr;
59 
60 	int ci_mtx_oldspl;
61 	int ci_mtx_count;
62 
63 	int ci_want_resched;
64 	int ci_cpl;
65 	u_int ci_softints;
66 	volatile u_int ci_intr_depth;
67 #if defined(GPROF) && defined(MULTIPROCESSOR)
68 	struct gmonparam *ci_gmon;	/* MI per-cpu GPROF */
69 #endif
70 };
71 
72 register struct lwp *or1k_curlwp __asm("r10");
73 #define	curlwp		or1k_curlwp
74 
75 static __inline struct cpu_info *
curcpu(void)76 curcpu(void)
77 {
78 	return curlwp->l_cpu;
79 }
80 
81 static __inline cpuid_t
cpu_number(void)82 cpu_number(void)
83 {
84 #ifdef MULTIPROCESSOR
85 	return curcpu()->ci_cpuid;
86 #else
87 	return 0;
88 #endif
89 }
90 
91 void	cpu_proc_fork(struct proc *, struct proc *);
92 void	cpu_signotify(struct lwp *);
93 void	cpu_need_proftick(struct lwp *l);
94 void	cpu_boot_secondary_processors(void);
95 
96 #define CPU_INFO_ITERATOR	cpuid_t
97 #ifdef MULTIPROCESSOR
98 #define CPU_INFO_FOREACH(cii, ci) \
99 	(cii) = 0; ((ci) = cpu_infos[cii]) != NULL; (cii)++
100 #else
101 #define CPU_INFO_FOREACH(cii, ci) \
102 	(cii) = 0; (cii) == 0 && (ci) = curcpu(); (cii)++
103 #endif
104 
105 static __inline void
cpu_dosoftints(void)106 cpu_dosoftints(void)
107 {
108 	extern void dosoftints(void);
109         struct cpu_info * const ci = curcpu();
110         if (ci->ci_intr_depth == 0
111 	    && (ci->ci_data.cpu_softints >> ci->ci_cpl) > 0)
112                 dosoftints();
113 }
114 
115 static __inline bool
cpu_intr_p(void)116 cpu_intr_p(void)
117 {
118 	return curcpu()->ci_intr_depth > 0;
119 }
120 
121 #endif /* _KERNEL || _KMEMUSER */
122 
123 #endif /* _OR1K_CPU_H_ */
124