xref: /openbsd-src/sys/arch/amd64/include/cpu.h (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: cpu.h,v 1.68 2011/06/16 19:46:40 kettenis Exp $	*/
2 /*	$NetBSD: cpu.h,v 1.1 2003/04/26 18:39:39 fvdl Exp $	*/
3 
4 /*-
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * William Jolitz.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)cpu.h	5.4 (Berkeley) 5/9/91
36  */
37 
38 #ifndef _MACHINE_CPU_H_
39 #define _MACHINE_CPU_H_
40 
41 /*
42  * Definitions unique to x86-64 cpu support.
43  */
44 #include <machine/frame.h>
45 #include <machine/segments.h>
46 #include <machine/intrdefs.h>
47 #include <machine/cacheinfo.h>
48 
49 #ifdef MULTIPROCESSOR
50 #include <machine/i82489reg.h>
51 #include <machine/i82489var.h>
52 #endif
53 
54 #include <sys/device.h>
55 #include <sys/lock.h>
56 #include <sys/sched.h>
57 #include <sys/sensors.h>
58 
59 #ifdef _KERNEL
60 
61 struct x86_64_tss;
62 struct cpu_info {
63 	struct device *ci_dev;
64 	struct cpu_info *ci_self;
65 	struct schedstate_percpu ci_schedstate; /* scheduler state */
66 	struct cpu_info *ci_next;
67 
68 	struct proc *ci_curproc;
69 	struct simplelock ci_slock;
70 	u_int ci_cpuid;
71 	u_int ci_apicid;
72 	u_int32_t ci_randseed;
73 
74 	u_int64_t ci_scratch;
75 	u_int64_t ci_cur_fsbase;
76 
77 	struct proc *ci_fpcurproc;
78 	struct proc *ci_fpsaveproc;
79 	int ci_fpsaving;
80 
81 	struct pcb *ci_curpcb;
82 	struct pcb *ci_idle_pcb;
83 
84 	struct intrsource *ci_isources[MAX_INTR_SOURCES];
85 	u_int64_t	ci_ipending;
86 	int		ci_ilevel;
87 	int		ci_idepth;
88 	u_int64_t	ci_imask[NIPL];
89 	u_int64_t	ci_iunmask[NIPL];
90 #ifdef DIAGNOSTIC
91 	int		ci_mutex_level;
92 #endif
93 
94 	volatile u_int	ci_flags;
95 	u_int32_t	ci_ipis;
96 
97 	u_int32_t	ci_feature_flags;
98 	u_int32_t	ci_feature_eflags;
99 	u_int32_t	ci_signature;
100 	u_int32_t	ci_family;
101 	u_int32_t	ci_model;
102 	u_int32_t	ci_cflushsz;
103 	u_int64_t	ci_tsc_freq;
104 
105 	struct cpu_functions *ci_func;
106 	void (*cpu_setup)(struct cpu_info *);
107 	void (*ci_info)(struct cpu_info *);
108 
109 	int		ci_want_resched;
110 
111 	struct x86_cache_info ci_cinfo[CAI_COUNT];
112 
113 	struct	x86_64_tss *ci_tss;
114 	char		*ci_gdt;
115 
116 	volatile int	ci_ddb_paused;
117 #define CI_DDB_RUNNING		0
118 #define CI_DDB_SHOULDSTOP	1
119 #define CI_DDB_STOPPED		2
120 #define CI_DDB_ENTERDDB		3
121 #define CI_DDB_INDDB		4
122 
123 	volatile int ci_setperf_state;
124 #define CI_SETPERF_READY	0
125 #define CI_SETPERF_SHOULDSTOP	1
126 #define CI_SETPERF_INTRANSIT	2
127 #define CI_SETPERF_DONE		3
128 
129 	struct ksensordev	ci_sensordev;
130 	struct ksensor		ci_sensor;
131 };
132 
133 #define CPUF_BSP	0x0001		/* CPU is the original BSP */
134 #define CPUF_AP		0x0002		/* CPU is an AP */
135 #define CPUF_SP		0x0004		/* CPU is only processor */
136 #define CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
137 
138 #define CPUF_PRESENT	0x1000		/* CPU is present */
139 #define CPUF_RUNNING	0x2000		/* CPU is running */
140 #define CPUF_PAUSE	0x4000		/* CPU is paused in DDB */
141 #define CPUF_GO		0x8000		/* CPU should start running */
142 
143 #define PROC_PC(p)	((p)->p_md.md_regs->tf_rip)
144 
145 extern struct cpu_info cpu_info_primary;
146 extern struct cpu_info *cpu_info_list;
147 
148 #define CPU_INFO_ITERATOR		int
149 #define CPU_INFO_FOREACH(cii, ci)	for (cii = 0, ci = cpu_info_list; \
150 					    ci != NULL; ci = ci->ci_next)
151 
152 #define CPU_INFO_UNIT(ci)	((ci)->ci_dev ? (ci)->ci_dev->dv_unit : 0)
153 
154 /*
155  * Preempt the current process if in interrupt from user mode,
156  * or after the current trap/syscall if in system mode.
157  */
158 extern void need_resched(struct cpu_info *);
159 #define clear_resched(ci) (ci)->ci_want_resched = 0
160 
161 #if defined(MULTIPROCESSOR)
162 
163 #define MAXCPUS		64	/* bitmask; can be bumped to 64 */
164 
165 #define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
166 #define CPU_STOP(_ci)		((_ci)->ci_func->stop(_ci))
167 #define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
168 
169 #define curcpu()	({struct cpu_info *__ci;                  \
170 			asm volatile("movq %%gs:8,%0" : "=r" (__ci)); \
171 			__ci;})
172 #define cpu_number()	(curcpu()->ci_cpuid)
173 
174 #define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
175 
176 extern struct cpu_info *cpu_info[MAXCPUS];
177 
178 void cpu_boot_secondary_processors(void);
179 void cpu_init_idle_pcbs(void);
180 
181 void cpu_unidle(struct cpu_info *);
182 
183 #else /* !MULTIPROCESSOR */
184 
185 #define MAXCPUS		1
186 
187 #ifdef _KERNEL
188 extern struct cpu_info cpu_info_primary;
189 
190 #define curcpu()		(&cpu_info_primary)
191 
192 #define cpu_unidle(ci)
193 
194 #endif
195 
196 /*
197  * definitions of cpu-dependent requirements
198  * referenced in generic code
199  */
200 #define	cpu_number()		0
201 #define CPU_IS_PRIMARY(ci)	1
202 
203 #endif	/* MULTIPROCESSOR */
204 
205 #endif /* _KERNEL */
206 
207 #include <machine/psl.h>
208 
209 #ifdef MULTIPROCESSOR
210 #include <sys/mplock.h>
211 #endif
212 
213 #define aston(p)	((p)->p_md.md_astpending = 1)
214 
215 #define curpcb		curcpu()->ci_curpcb
216 
217 /*
218  * Arguments to hardclock, softclock and statclock
219  * encapsulate the previous machine state in an opaque
220  * clockframe; for now, use generic intrframe.
221  */
222 #define clockframe intrframe
223 
224 #define	CLKF_USERMODE(frame)	USERMODE((frame)->if_cs, (frame)->if_rflags)
225 #define CLKF_PC(frame)		((frame)->if_rip)
226 #define CLKF_INTR(frame)	(curcpu()->ci_idepth > 1)
227 
228 /*
229  * This is used during profiling to integrate system time.
230  */
231 #define	PROC_PC(p)		((p)->p_md.md_regs->tf_rip)
232 
233 /*
234  * Give a profiling tick to the current process when the user profiling
235  * buffer pages are invalid.  On the i386, request an ast to send us
236  * through trap(), marking the proc as needing a profiling tick.
237  */
238 #define	need_proftick(p)	aston(p)
239 
240 void signotify(struct proc *);
241 
242 /*
243  * We need a machine-independent name for this.
244  */
245 extern void (*delay_func)(int);
246 struct timeval;
247 
248 #define DELAY(x)		(*delay_func)(x)
249 #define delay(x)		(*delay_func)(x)
250 
251 
252 #ifdef _KERNEL
253 extern int biosbasemem;
254 extern int biosextmem;
255 extern int cpu;
256 extern int cpu_feature;
257 extern int cpu_ecxfeature;
258 extern int cpu_id;
259 extern char cpu_vendor[];
260 extern int cpuid_level;
261 extern int cpuspeed;
262 
263 /* identcpu.c */
264 void	identifycpu(struct cpu_info *);
265 int	cpu_amd64speed(int *);
266 void cpu_probe_features(struct cpu_info *);
267 
268 /* machdep.c */
269 void	dumpconf(void);
270 void	cpu_reset(void);
271 void	x86_64_proc0_tss_ldt_init(void);
272 void	x86_64_bufinit(void);
273 void	x86_64_init_pcb_tss_ldt(struct cpu_info *);
274 void	cpu_proc_fork(struct proc *, struct proc *);
275 int	amd64_pa_used(paddr_t);
276 extern void (*cpu_idle_enter_fcn)(void);
277 extern void (*cpu_idle_cycle_fcn)(void);
278 extern void (*cpu_idle_leave_fcn)(void);
279 
280 struct region_descriptor;
281 void	lgdt(struct region_descriptor *);
282 
283 struct pcb;
284 void	savectx(struct pcb *);
285 void	switch_exit(struct proc *, void (*)(struct proc *));
286 void	proc_trampoline(void);
287 void	child_trampoline(void);
288 
289 /* clock.c */
290 extern void (*initclock_func)(void);
291 void	startclocks(void);
292 void	rtcstart(void);
293 void	rtcstop(void);
294 void	i8254_delay(int);
295 void	i8254_initclocks(void);
296 void	i8254_startclock(void);
297 void	i8254_inittimecounter(void);
298 void	i8254_inittimecounter_simple(void);
299 
300 /* i8259.c */
301 void	i8259_default_setup(void);
302 
303 
304 void cpu_init_msrs(struct cpu_info *);
305 
306 
307 /* trap.c */
308 void	child_return(void *);
309 
310 /* dkcsum.c */
311 void	dkcsumattach(void);
312 
313 /* bus_machdep.c */
314 void x86_bus_space_init(void);
315 void x86_bus_space_mallocok(void);
316 
317 /* powernow-k8.c */
318 void k8_powernow_init(struct cpu_info *);
319 void k8_powernow_setperf(int);
320 
321 /* k1x-pstate.c */
322 void k1x_init(struct cpu_info *);
323 void k1x_setperf(int);
324 
325 void est_init(struct cpu_info *);
326 void est_setperf(int);
327 
328 #ifdef MULTIPROCESSOR
329 /* mp_setperf.c */
330 void mp_setperf_init(void);
331 #endif
332 
333 #endif /* _KERNEL */
334 
335 /*
336  * CTL_MACHDEP definitions.
337  */
338 #define	CPU_CONSDEV		1	/* dev_t: console terminal device */
339 #define	CPU_BIOS		2	/* BIOS variables */
340 #define	CPU_BLK2CHR		3	/* convert blk maj into chr one */
341 #define	CPU_CHR2BLK		4	/* convert chr maj into blk one */
342 #define CPU_ALLOWAPERTURE	5	/* allow mmap of /dev/xf86 */
343 #define CPU_CPUVENDOR		6	/* cpuid vendor string */
344 #define CPU_CPUID		7	/* cpuid */
345 #define CPU_CPUFEATURE		8	/* cpuid features */
346 #define CPU_APMWARN		9	/* APM battery warning percentage */
347 #define CPU_KBDRESET		10	/* keyboard reset under pcvt */
348 #define CPU_APMHALT		11	/* halt -p hack */
349 #define CPU_XCRYPT		12	/* supports VIA xcrypt in userland */
350 #define CPU_LIDSUSPEND		13	/* lid close causes a suspend */
351 #define CPU_MAXID		14	/* number of valid machdep ids */
352 
353 #define	CTL_MACHDEP_NAMES { \
354 	{ 0, 0 }, \
355 	{ "console_device", CTLTYPE_STRUCT }, \
356 	{ "bios", CTLTYPE_INT }, \
357 	{ "blk2chr", CTLTYPE_STRUCT }, \
358 	{ "chr2blk", CTLTYPE_STRUCT }, \
359 	{ "allowaperture", CTLTYPE_INT }, \
360 	{ "cpuvendor", CTLTYPE_STRING }, \
361 	{ "cpuid", CTLTYPE_INT }, \
362 	{ "cpufeature", CTLTYPE_INT }, \
363 	{ "apmwarn", CTLTYPE_INT }, \
364 	{ "kbdreset", CTLTYPE_INT }, \
365 	{ "apmhalt", CTLTYPE_INT }, \
366 	{ "xcrypt", CTLTYPE_INT }, \
367 	{ "lidsuspend", CTLTYPE_INT }, \
368 }
369 
370 /*
371  * Default cr4 flags.
372  * Doesn't really belong here, but doesn't really belong anywhere else
373  * either. Just to avoid painful mismatches of cr4 flags since they are
374  * set in three different places.
375  */
376 #define CR4_DEFAULT (CR4_PAE|CR4_PGE|CR4_PSE|CR4_OSFXSR|CR4_OSXMMEXCPT)
377 
378 #endif /* !_MACHINE_CPU_H_ */
379