xref: /openbsd-src/sys/arch/amd64/include/cpu.h (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: cpu.h,v 1.73 2012/04/17 16:02:33 guenther 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 
76 	struct proc *ci_fpcurproc;
77 	struct proc *ci_fpsaveproc;
78 	int ci_fpsaving;
79 
80 	struct pcb *ci_curpcb;
81 	struct pcb *ci_idle_pcb;
82 
83 	struct intrsource *ci_isources[MAX_INTR_SOURCES];
84 	u_int64_t	ci_ipending;
85 	int		ci_ilevel;
86 	int		ci_idepth;
87 	u_int64_t	ci_imask[NIPL];
88 	u_int64_t	ci_iunmask[NIPL];
89 #ifdef DIAGNOSTIC
90 	int		ci_mutex_level;
91 #endif
92 
93 	volatile u_int	ci_flags;
94 	u_int32_t	ci_ipis;
95 
96 	u_int32_t	ci_feature_flags;
97 	u_int32_t	ci_feature_eflags;
98 	u_int32_t	ci_signature;
99 	u_int32_t	ci_family;
100 	u_int32_t	ci_model;
101 	u_int32_t	ci_cflushsz;
102 	u_int64_t	ci_tsc_freq;
103 
104 	struct cpu_functions *ci_func;
105 	void (*cpu_setup)(struct cpu_info *);
106 	void (*ci_info)(struct cpu_info *);
107 
108 	int		ci_want_resched;
109 
110 	struct x86_cache_info ci_cinfo[CAI_COUNT];
111 
112 	struct	x86_64_tss *ci_tss;
113 	char		*ci_gdt;
114 
115 	volatile int	ci_ddb_paused;
116 #define CI_DDB_RUNNING		0
117 #define CI_DDB_SHOULDSTOP	1
118 #define CI_DDB_STOPPED		2
119 #define CI_DDB_ENTERDDB		3
120 #define CI_DDB_INDDB		4
121 
122 	volatile int ci_setperf_state;
123 #define CI_SETPERF_READY	0
124 #define CI_SETPERF_SHOULDSTOP	1
125 #define CI_SETPERF_INTRANSIT	2
126 #define CI_SETPERF_DONE		3
127 
128 	struct ksensordev	ci_sensordev;
129 	struct ksensor		ci_sensor;
130 };
131 
132 #define CPUF_BSP	0x0001		/* CPU is the original BSP */
133 #define CPUF_AP		0x0002		/* CPU is an AP */
134 #define CPUF_SP		0x0004		/* CPU is only processor */
135 #define CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
136 
137 #define CPUF_IDENTIFY	0x0010		/* CPU may now identify */
138 #define CPUF_IDENTIFIED	0x0020		/* CPU has been identified */
139 
140 #define CPUF_PRESENT	0x1000		/* CPU is present */
141 #define CPUF_RUNNING	0x2000		/* CPU is running */
142 #define CPUF_PAUSE	0x4000		/* CPU is paused in DDB */
143 #define CPUF_GO		0x8000		/* CPU should start running */
144 
145 #define PROC_PC(p)	((p)->p_md.md_regs->tf_rip)
146 
147 extern struct cpu_info cpu_info_primary;
148 extern struct cpu_info *cpu_info_list;
149 
150 #define CPU_INFO_ITERATOR		int
151 #define CPU_INFO_FOREACH(cii, ci)	for (cii = 0, ci = cpu_info_list; \
152 					    ci != NULL; ci = ci->ci_next)
153 
154 #define CPU_INFO_UNIT(ci)	((ci)->ci_dev ? (ci)->ci_dev->dv_unit : 0)
155 
156 /*
157  * Preempt the current process if in interrupt from user mode,
158  * or after the current trap/syscall if in system mode.
159  */
160 extern void need_resched(struct cpu_info *);
161 #define clear_resched(ci) (ci)->ci_want_resched = 0
162 
163 #if defined(MULTIPROCESSOR)
164 
165 #define MAXCPUS		64	/* bitmask */
166 
167 #define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
168 #define CPU_STOP(_ci)		((_ci)->ci_func->stop(_ci))
169 #define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
170 
171 #define curcpu()	({struct cpu_info *__ci;                  \
172 			asm volatile("movq %%gs:8,%0" : "=r" (__ci)); \
173 			__ci;})
174 #define cpu_number()	(curcpu()->ci_cpuid)
175 
176 #define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
177 
178 extern struct cpu_info *cpu_info[MAXCPUS];
179 
180 void cpu_boot_secondary_processors(void);
181 void cpu_init_idle_pcbs(void);
182 
183 void cpu_unidle(struct cpu_info *);
184 
185 #else /* !MULTIPROCESSOR */
186 
187 #define MAXCPUS		1
188 
189 #ifdef _KERNEL
190 extern struct cpu_info cpu_info_primary;
191 
192 #define curcpu()		(&cpu_info_primary)
193 
194 #define cpu_unidle(ci)
195 
196 #endif
197 
198 /*
199  * definitions of cpu-dependent requirements
200  * referenced in generic code
201  */
202 #define	cpu_number()		0
203 #define CPU_IS_PRIMARY(ci)	1
204 
205 #endif	/* MULTIPROCESSOR */
206 
207 #endif /* _KERNEL */
208 
209 #include <machine/psl.h>
210 
211 #ifdef MULTIPROCESSOR
212 #include <sys/mplock.h>
213 #endif
214 
215 #define aston(p)	((p)->p_md.md_astpending = 1)
216 
217 #define curpcb		curcpu()->ci_curpcb
218 
219 /*
220  * Arguments to hardclock, softclock and statclock
221  * encapsulate the previous machine state in an opaque
222  * clockframe; for now, use generic intrframe.
223  */
224 #define clockframe intrframe
225 
226 #define	CLKF_USERMODE(frame)	USERMODE((frame)->if_cs, (frame)->if_rflags)
227 #define CLKF_PC(frame)		((frame)->if_rip)
228 #define CLKF_INTR(frame)	(curcpu()->ci_idepth > 1)
229 
230 /*
231  * This is used during profiling to integrate system time.
232  */
233 #define	PROC_PC(p)		((p)->p_md.md_regs->tf_rip)
234 
235 /*
236  * Give a profiling tick to the current process when the user profiling
237  * buffer pages are invalid.  On the i386, request an ast to send us
238  * through trap(), marking the proc as needing a profiling tick.
239  */
240 #define	need_proftick(p)	aston(p)
241 
242 void signotify(struct proc *);
243 
244 /*
245  * We need a machine-independent name for this.
246  */
247 extern void (*delay_func)(int);
248 struct timeval;
249 
250 #define DELAY(x)		(*delay_func)(x)
251 #define delay(x)		(*delay_func)(x)
252 
253 
254 #ifdef _KERNEL
255 extern int biosbasemem;
256 extern int biosextmem;
257 extern int cpu;
258 extern int cpu_feature;
259 extern int cpu_ecxfeature;
260 extern int ecpu_ecxfeature;
261 extern int cpu_id;
262 extern char cpu_vendor[];
263 extern int cpuid_level;
264 extern int cpuspeed;
265 
266 /* identcpu.c */
267 void	identifycpu(struct cpu_info *);
268 int	cpu_amd64speed(int *);
269 
270 /* machdep.c */
271 void	dumpconf(void);
272 void	cpu_reset(void);
273 void	x86_64_proc0_tss_ldt_init(void);
274 void	x86_64_bufinit(void);
275 void	x86_64_init_pcb_tss_ldt(struct cpu_info *);
276 void	cpu_proc_fork(struct proc *, struct proc *);
277 int	amd64_pa_used(paddr_t);
278 extern void (*cpu_idle_enter_fcn)(void);
279 extern void (*cpu_idle_cycle_fcn)(void);
280 extern void (*cpu_idle_leave_fcn)(void);
281 
282 struct region_descriptor;
283 void	lgdt(struct region_descriptor *);
284 
285 struct pcb;
286 void	savectx(struct pcb *);
287 void	switch_exit(struct proc *, void (*)(struct proc *));
288 void	proc_trampoline(void);
289 void	child_trampoline(void);
290 
291 /* clock.c */
292 extern void (*initclock_func)(void);
293 void	startclocks(void);
294 void	rtcstart(void);
295 void	rtcstop(void);
296 void	i8254_delay(int);
297 void	i8254_initclocks(void);
298 void	i8254_startclock(void);
299 void	i8254_inittimecounter(void);
300 void	i8254_inittimecounter_simple(void);
301 
302 /* i8259.c */
303 void	i8259_default_setup(void);
304 
305 
306 void cpu_init_msrs(struct cpu_info *);
307 
308 
309 /* trap.c */
310 void	child_return(void *);
311 
312 /* dkcsum.c */
313 void	dkcsumattach(void);
314 
315 /* bus_machdep.c */
316 void x86_bus_space_init(void);
317 void x86_bus_space_mallocok(void);
318 
319 /* powernow-k8.c */
320 void k8_powernow_init(struct cpu_info *);
321 void k8_powernow_setperf(int);
322 
323 /* k1x-pstate.c */
324 void k1x_init(struct cpu_info *);
325 void k1x_setperf(int);
326 
327 void est_init(struct cpu_info *);
328 void est_setperf(int);
329 
330 #ifdef MULTIPROCESSOR
331 /* mp_setperf.c */
332 void mp_setperf_init(void);
333 #endif
334 
335 #endif /* _KERNEL */
336 
337 /*
338  * CTL_MACHDEP definitions.
339  */
340 #define	CPU_CONSDEV		1	/* dev_t: console terminal device */
341 #define	CPU_BIOS		2	/* BIOS variables */
342 #define	CPU_BLK2CHR		3	/* convert blk maj into chr one */
343 #define	CPU_CHR2BLK		4	/* convert chr maj into blk one */
344 #define CPU_ALLOWAPERTURE	5	/* allow mmap of /dev/xf86 */
345 #define CPU_CPUVENDOR		6	/* cpuid vendor string */
346 #define CPU_CPUID		7	/* cpuid */
347 #define CPU_CPUFEATURE		8	/* cpuid features */
348 #define CPU_APMWARN		9	/* APM battery warning percentage */
349 #define CPU_KBDRESET		10	/* keyboard reset under pcvt */
350 #define CPU_APMHALT		11	/* halt -p hack */
351 #define CPU_XCRYPT		12	/* supports VIA xcrypt in userland */
352 #define CPU_LIDSUSPEND		13	/* lid close causes a suspend */
353 #define CPU_MAXID		14	/* number of valid machdep ids */
354 
355 #define	CTL_MACHDEP_NAMES { \
356 	{ 0, 0 }, \
357 	{ "console_device", CTLTYPE_STRUCT }, \
358 	{ "bios", CTLTYPE_INT }, \
359 	{ "blk2chr", CTLTYPE_STRUCT }, \
360 	{ "chr2blk", CTLTYPE_STRUCT }, \
361 	{ "allowaperture", CTLTYPE_INT }, \
362 	{ "cpuvendor", CTLTYPE_STRING }, \
363 	{ "cpuid", CTLTYPE_INT }, \
364 	{ "cpufeature", CTLTYPE_INT }, \
365 	{ "apmwarn", CTLTYPE_INT }, \
366 	{ "kbdreset", CTLTYPE_INT }, \
367 	{ "apmhalt", CTLTYPE_INT }, \
368 	{ "xcrypt", CTLTYPE_INT }, \
369 	{ "lidsuspend", CTLTYPE_INT }, \
370 }
371 
372 /*
373  * Default cr4 flags.
374  * Doesn't really belong here, but doesn't really belong anywhere else
375  * either. Just to avoid painful mismatches of cr4 flags since they are
376  * set in three different places.
377  */
378 #define CR4_DEFAULT (CR4_PAE|CR4_PGE|CR4_PSE|CR4_OSFXSR|CR4_OSXMMEXCPT)
379 
380 #endif /* !_MACHINE_CPU_H_ */
381