xref: /openbsd/sys/arch/i386/include/cpu.h (revision 097a140d)
1 /*	$OpenBSD: cpu.h,v 1.173 2021/03/11 11:16:57 jsg Exp $	*/
2 /*	$NetBSD: cpu.h,v 1.35 1996/05/05 19:29:26 christos 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 i386 cpu support.
43  */
44 #ifdef _KERNEL
45 #include <machine/frame.h>
46 #include <machine/psl.h>
47 #include <machine/segments.h>
48 #include <machine/intrdefs.h>
49 #include <machine/tss.h>
50 
51 #ifdef MULTIPROCESSOR
52 #include <machine/i82489reg.h>
53 #include <machine/i82489var.h>
54 #endif
55 
56 #endif /* _KERNEL */
57 
58 /*
59  * Arguments to hardclock, softclock and statclock
60  * encapsulate the previous machine state in an opaque
61  * clockframe; for now, use generic intrframe.
62  *
63  * XXX intrframe has a lot of gunk we don't need.
64  */
65 #define clockframe intrframe
66 
67 #include <sys/device.h>
68 #include <sys/sched.h>
69 #include <sys/sensors.h>
70 #include <sys/srp.h>
71 
72 struct intrsource;
73 
74 #ifdef _KERNEL
75 /* XXX stuff to move to cpuvar.h later */
76 struct cpu_info {
77 	u_int32_t	ci_kern_cr3;	/* U+K page table */
78 	u_int32_t	ci_scratch;	/* for U<-->K transition */
79 
80 #define ci_PAGEALIGN	ci_dev
81 	struct device *ci_dev;		/* our device */
82 	struct cpu_info *ci_self;	/* pointer to this structure */
83 	struct schedstate_percpu ci_schedstate; /* scheduler state */
84 	struct cpu_info *ci_next;	/* next cpu */
85 
86 	/*
87 	 * Public members.
88 	 */
89 	struct proc *ci_curproc; 	/* current owner of the processor */
90 	cpuid_t ci_cpuid; 		/* our CPU ID */
91 	u_int ci_apicid;		/* our APIC ID */
92 	u_int ci_acpi_proc_id;
93 	u_int32_t ci_randseed;
94 
95 	u_int32_t ci_kern_esp;		/* kernel-only stack */
96 	u_int32_t ci_intr_esp;		/* U<-->K trampoline stack */
97 	u_int32_t ci_user_cr3;		/* U-K page table */
98 
99 #if defined(MULTIPROCESSOR)
100 	struct srp_hazard ci_srp_hazards[SRP_HAZARD_NUM];
101 #endif
102 
103 	/*
104 	 * Private members.
105 	 */
106 	struct proc *ci_fpcurproc;	/* current owner of the FPU */
107 	struct proc *ci_fpsaveproc;
108 	int ci_fpsaving;		/* save in progress */
109 
110 	struct pcb *ci_curpcb;		/* VA of current HW PCB */
111 	struct pcb *ci_idle_pcb;	/* VA of current PCB */
112 	struct pmap *ci_curpmap;
113 
114 	struct intrsource *ci_isources[MAX_INTR_SOURCES];
115 	u_int32_t	ci_ipending;
116 	int		ci_ilevel;
117 	int		ci_idepth;
118 	u_int32_t	ci_imask[NIPL];
119 	u_int32_t	ci_iunmask[NIPL];
120 #ifdef DIAGNOSTIC
121 	int		ci_mutex_level;
122 #endif
123 
124 	paddr_t		ci_idle_pcb_paddr; /* PA of idle PCB */
125 	volatile u_long	ci_flags;	/* flags; see below */
126 	u_int32_t	ci_ipis; 	/* interprocessor interrupts pending */
127 
128 	u_int32_t	ci_level;
129 	u_int32_t	ci_vendor[4];
130 	u_int32_t	ci_signature;		/* X86 cpuid type */
131 	u_int32_t	ci_family;		/* extended cpuid family */
132 	u_int32_t	ci_model;		/* extended cpuid model */
133 	u_int32_t	ci_feature_flags;	/* X86 CPUID feature bits */
134 	u_int32_t	ci_feature_sefflags_ebx;/* more CPUID feature bits */
135 	u_int32_t	ci_feature_sefflags_ecx;/* more CPUID feature bits */
136 	u_int32_t	ci_feature_sefflags_edx;/* more CPUID feature bits */
137 	u_int32_t	ci_feature_tpmflags;	/* thermal & power bits */
138 	u_int32_t	cpu_class;		/* CPU class */
139 	u_int32_t	ci_cflushsz;		/* clflush cache-line size */
140 	u_int32_t	ci_amdcacheinfo[4];	/* AMD cache info */
141 	u_int32_t	ci_extcacheinfo[4];	/* Intel cache info */
142 
143 	int		ci_inatomic;
144 
145 	struct cpu_functions *ci_func;	/* start/stop functions */
146 	void (*cpu_setup)(struct cpu_info *);	/* proc-dependant init */
147 
148 	struct device	*ci_acpicpudev;
149 	volatile u_int	ci_mwait;
150 #define	MWAIT_IN_IDLE		0x1	/* don't need IPI to wake */
151 #define	MWAIT_KEEP_IDLING	0x2	/* cleared by other cpus to wake me */
152 #define	MWAIT_ONLY		0x4	/* set if all idle states use mwait */
153 #define	MWAIT_IDLING		(MWAIT_IN_IDLE | MWAIT_KEEP_IDLING)
154 
155 	int		ci_want_resched;
156 
157 	union descriptor *ci_gdt;
158 	struct i386tss	*ci_tss;
159 	struct i386tss	*ci_nmi_tss;
160 
161 	volatile int ci_ddb_paused;	/* paused due to other proc in ddb */
162 #define CI_DDB_RUNNING		0
163 #define CI_DDB_SHOULDSTOP	1
164 #define CI_DDB_STOPPED		2
165 #define CI_DDB_ENTERDDB		3
166 #define CI_DDB_INDDB		4
167 
168 	struct ksensordev	ci_sensordev;
169 	struct ksensor		ci_sensor;
170 #if defined(GPROF) || defined(DDBPROF)
171 	struct gmonparam	*ci_gmon;
172 #endif
173 };
174 
175 /*
176  * Processor flag notes: The "primary" CPU has certain MI-defined
177  * roles (mostly relating to hardclock handling); we distinguish
178  * between the processor which booted us, and the processor currently
179  * holding the "primary" role just to give us the flexibility later to
180  * change primaries should we be sufficiently twisted.
181  */
182 
183 #define	CPUF_BSP	0x0001		/* CPU is the original BSP */
184 #define	CPUF_AP		0x0002		/* CPU is an AP */
185 #define	CPUF_SP		0x0004		/* CPU is only processor */
186 #define	CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
187 #define	CPUF_APIC_CD	0x0010		/* CPU has apic configured */
188 #define	CPUF_CONST_TSC	0x0020		/* CPU has constant TSC */
189 
190 #define	CPUF_PRESENT	0x1000		/* CPU is present */
191 #define	CPUF_RUNNING	0x2000		/* CPU is running */
192 #define CPUF_VMM	0x4000		/* CPU is executing in VMM mode */
193 
194 /*
195  * We statically allocate the CPU info for the primary CPU (or,
196  * the only CPU on uniprocessors), and the primary CPU is the
197  * first CPU on the CPU info list.
198  */
199 struct cpu_info_full;
200 extern struct cpu_info_full cpu_info_full_primary;
201 #define cpu_info_primary (*(struct cpu_info *)((char *)&cpu_info_full_primary + PAGE_SIZE*2 - offsetof(struct cpu_info, ci_PAGEALIGN)))
202 
203 extern struct cpu_info *cpu_info_list;
204 
205 #define	CPU_INFO_ITERATOR		int
206 #define	CPU_INFO_FOREACH(cii, ci)	for (cii = 0, ci = cpu_info_list; \
207 					    ci != NULL; ci = ci->ci_next)
208 
209 #define CPU_INFO_UNIT(ci)	((ci)->ci_dev ? (ci)->ci_dev->dv_unit : 0)
210 
211 #ifdef MULTIPROCESSOR
212 
213 #define MAXCPUS			32	/* because we use a bitmask */
214 
215 #define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
216 #define CPU_STOP(_ci)		((_ci)->ci_func->stop(_ci))
217 #define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
218 
219 static struct cpu_info *curcpu(void);
220 
221 __inline static struct cpu_info *
222 curcpu(void)
223 {
224 	struct cpu_info *ci;
225 
226 	/* Can't include sys/param.h for offsetof() since it includes us */
227 	__asm volatile("movl %%fs:%1, %0" :
228 		"=r" (ci) : "m"
229 		(*(struct cpu_info * const *)&((struct cpu_info *)0)->ci_self));
230 	return ci;
231 }
232 #define cpu_number() 		(curcpu()->ci_cpuid)
233 
234 #define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
235 
236 extern struct cpu_info	*cpu_info[MAXCPUS];
237 
238 extern void cpu_boot_secondary_processors(void);
239 extern void cpu_init_idle_pcbs(void);
240 
241 void cpu_kick(struct cpu_info *);
242 void cpu_unidle(struct cpu_info *);
243 
244 #define CPU_BUSY_CYCLE()	__asm volatile("pause": : : "memory")
245 
246 #else /* MULTIPROCESSOR */
247 
248 #define MAXCPUS			1
249 
250 #define cpu_number()		0
251 #define	curcpu()		(&cpu_info_primary)
252 
253 #define CPU_IS_PRIMARY(ci)	1
254 
255 #define cpu_kick(ci)
256 #define cpu_unidle(ci)
257 
258 #define CPU_BUSY_CYCLE()	do {} while (0)
259 
260 #endif
261 
262 #include <machine/cpufunc.h>
263 
264 #define aston(p)	((p)->p_md.md_astpending = 1)
265 
266 #define curpcb			curcpu()->ci_curpcb
267 
268 unsigned int cpu_rnd_messybits(void);
269 
270 /*
271  * Preempt the current process if in interrupt from user mode,
272  * or after the current trap/syscall if in system mode.
273  */
274 extern void need_resched(struct cpu_info *);
275 #define clear_resched(ci) (ci)->ci_want_resched = 0
276 
277 #define	CLKF_USERMODE(frame)	USERMODE((frame)->if_cs, (frame)->if_eflags)
278 #define	CLKF_PC(frame)		((frame)->if_eip)
279 #define	CLKF_INTR(frame)	(IDXSEL((frame)->if_cs) == GICODE_SEL)
280 
281 /*
282  * This is used during profiling to integrate system time.
283  */
284 #define	PROC_PC(p)		((p)->p_md.md_regs->tf_eip)
285 #define	PROC_STACK(p)		((p)->p_md.md_regs->tf_esp)
286 
287 /*
288  * Give a profiling tick to the current process when the user profiling
289  * buffer pages are invalid.  On the i386, request an ast to send us
290  * through trap(), marking the proc as needing a profiling tick.
291  */
292 #define	need_proftick(p)	aston(p)
293 
294 /*
295  * Notify the current process (p) that it has a signal pending,
296  * process as soon as possible.
297  */
298 void signotify(struct proc *);
299 
300 /*
301  * We need a machine-independent name for this.
302  */
303 extern void (*delay_func)(int);
304 struct timeval;
305 
306 #define	DELAY(x)		(*delay_func)(x)
307 #define delay(x)		(*delay_func)(x)
308 
309 /*
310  * High resolution clock support (Pentium only)
311  */
312 void	calibrate_cyclecounter(void);
313 
314 /*
315  * pull in #defines for kinds of processors
316  */
317 #include <machine/cputypes.h>
318 
319 struct cpu_nocpuid_nameclass {
320 	int cpu_vendor;
321 	const char *cpu_vendorname;
322 	const char *cpu_name;
323 	int cpu_class;
324 	void (*cpu_setup)(struct cpu_info *);
325 };
326 
327 struct cpu_cpuid_nameclass {
328 	const char *cpu_id;
329 	int cpu_vendor;
330 	const char *cpu_vendorname;
331 	struct cpu_cpuid_family {
332 		int cpu_class;
333 		const char *cpu_models[CPU_MAXMODEL+2];
334 		void (*cpu_setup)(struct cpu_info *);
335 	} cpu_family[CPU_MAXFAMILY - CPU_MINFAMILY + 1];
336 };
337 
338 struct cpu_cpuid_feature {
339 	int feature_bit;
340 	const char *feature_name;
341 };
342 
343 /* locore.s */
344 extern int cpu;
345 extern int cpu_id;
346 extern char cpu_vendor[]; /* note: NOT nul-terminated */
347 extern char cpu_brandstr[];
348 extern int cpuid_level;
349 extern int cpu_miscinfo;
350 extern int cpu_feature;
351 extern int ecpu_feature;
352 extern int cpu_ecxfeature;
353 extern int ecpu_ecxfeature;
354 extern int cpu_cache_eax;
355 extern int cpu_cache_ebx;
356 extern int cpu_cache_ecx;
357 extern int cpu_cache_edx;
358 extern int cpu_perf_eax;
359 extern int cpu_perf_ebx;
360 extern int cpu_perf_edx;
361 extern int cpu_apmi_edx;
362 extern int cpu_pae;	/* cpu has both PAE and NX features */
363 
364 /* cpu.c */
365 extern u_int cpu_mwait_size;
366 extern u_int cpu_mwait_states;
367 extern void cpu_update_nmi_cr3(vaddr_t);
368 extern void cpu_tsx_disable(struct cpu_info *);
369 
370 /* machdep.c */
371 extern int cpu_apmhalt;
372 extern int cpu_class;
373 extern char cpu_model[];
374 extern const struct cpu_nocpuid_nameclass i386_nocpuid_cpus[];
375 extern const struct cpu_cpuid_nameclass i386_cpuid_cpus[];
376 extern void (*cpu_idle_enter_fcn)(void);
377 extern void (*cpu_idle_cycle_fcn)(void);
378 extern void (*cpu_idle_leave_fcn)(void);
379 
380 extern int cpuspeed;
381 
382 #if !defined(SMALL_KERNEL)
383 #define BUS66  6667
384 #define BUS100 10000
385 #define BUS133 13333
386 #define BUS166 16667
387 #define BUS200 20000
388 #define BUS266 26667
389 #define BUS333 33333
390 extern int bus_clock;
391 #endif
392 
393 /* F00F bug fix stuff for pentium cpu */
394 extern int cpu_f00f_bug;
395 void fix_f00f(void);
396 
397 /* dkcsum.c */
398 void	dkcsumattach(void);
399 
400 extern int i386_use_fxsave;
401 extern int i386_has_sse;
402 extern int i386_has_sse2;
403 
404 extern void (*update_cpuspeed)(void);
405 
406 /* machdep.c */
407 void	dumpconf(void);
408 void	cpu_reset(void);
409 void	i386_proc0_tss_init(void);
410 void	i386_init_pcb_tss(struct cpu_info *);
411 void	cpuid(u_int32_t, u_int32_t *);
412 
413 /* locore.s */
414 struct region_descriptor;
415 void	lgdt(struct region_descriptor *);
416 
417 struct pcb;
418 void	savectx(struct pcb *);
419 void	switch_exit(struct proc *);
420 void	proc_trampoline(void);
421 
422 /* clock.c */
423 extern void (*initclock_func)(void);
424 void	startclocks(void);
425 void	rtcinit(void);
426 void	rtcstart(void);
427 void	rtcstop(void);
428 void	i8254_delay(int);
429 void	i8254_initclocks(void);
430 void	i8254_startclock(void);
431 void	i8254_inittimecounter(void);
432 void	i8254_inittimecounter_simple(void);
433 
434 #if !defined(SMALL_KERNEL)
435 /* est.c */
436 void	est_init(struct cpu_info *, int);
437 void	est_setperf(int);
438 /* longrun.c */
439 void	longrun_init(void);
440 void	longrun_setperf(int);
441 /* p4tcc.c */
442 void	p4tcc_init(int, int);
443 void	p4tcc_setperf(int);
444 /* powernow.c */
445 void	k6_powernow_init(void);
446 void	k6_powernow_setperf(int);
447 /* powernow-k7.c */
448 void	k7_powernow_init(void);
449 void	k7_powernow_setperf(int);
450 /* powernow-k8.c */
451 void 	k8_powernow_init(void);
452 void 	k8_powernow_setperf(int);
453 /* k1x-pstate.c */
454 void k1x_init(struct cpu_info *);
455 void k1x_setperf(int);
456 #endif
457 
458 /* npx.c */
459 void	npxdrop(struct proc *);
460 void	npxsave_proc(struct proc *, int);
461 void	npxsave_cpu(struct cpu_info *, int);
462 
463 /* isa_machdep.c */
464 void	isa_defaultirq(void);
465 int	isa_nmi(void);
466 
467 /* pmap.c */
468 void	pmap_bootstrap(vaddr_t);
469 
470 /* vm_machdep.c */
471 int	kvtop(caddr_t);
472 
473 #ifdef MULTIPROCESSOR
474 /* mp_setperf.c */
475 void	mp_setperf_init(void);
476 #endif
477 
478 int	cpu_paenable(void *);
479 #endif /* _KERNEL */
480 
481 /*
482  * CTL_MACHDEP definitions.
483  */
484 #define	CPU_CONSDEV		1	/* dev_t: console terminal device */
485 #define	CPU_BIOS		2	/* BIOS variables */
486 #define	CPU_BLK2CHR		3	/* convert blk maj into chr one */
487 #define	CPU_CHR2BLK		4	/* convert chr maj into blk one */
488 #define CPU_ALLOWAPERTURE	5	/* allow mmap of /dev/xf86 */
489 #define CPU_CPUVENDOR		6	/* cpuid vendor string */
490 #define CPU_CPUID		7	/* cpuid */
491 #define CPU_CPUFEATURE		8	/* cpuid features */
492 #define CPU_KBDRESET		10	/* keyboard reset under pcvt */
493 #define CPU_OSFXSR		13	/* uses FXSAVE/FXRSTOR */
494 #define CPU_SSE			14	/* supports SSE */
495 #define CPU_SSE2		15	/* supports SSE2 */
496 #define CPU_XCRYPT		16	/* supports VIA xcrypt in userland */
497 #define CPU_LIDACTION		18	/* action caused by lid close */
498 #define CPU_FORCEUKBD		19	/* Force ukbd(4) as console keyboard */
499 #define CPU_MAXID		20	/* number of valid machdep ids */
500 
501 #define	CTL_MACHDEP_NAMES { \
502 	{ 0, 0 }, \
503 	{ "console_device", CTLTYPE_STRUCT }, \
504 	{ "bios", CTLTYPE_INT }, \
505 	{ "blk2chr", CTLTYPE_STRUCT }, \
506 	{ "chr2blk", CTLTYPE_STRUCT }, \
507 	{ "allowaperture", CTLTYPE_INT }, \
508 	{ "cpuvendor", CTLTYPE_STRING }, \
509 	{ "cpuid", CTLTYPE_INT }, \
510 	{ "cpufeature", CTLTYPE_INT }, \
511 	{ 0, 0 }, \
512 	{ "kbdreset", CTLTYPE_INT }, \
513 	{ 0, 0 }, \
514 	{ 0, 0 }, \
515 	{ "osfxsr", CTLTYPE_INT }, \
516 	{ "sse", CTLTYPE_INT }, \
517 	{ "sse2", CTLTYPE_INT }, \
518 	{ "xcrypt", CTLTYPE_INT }, \
519 	{ 0, 0 }, \
520 	{ "lidaction", CTLTYPE_INT }, \
521 	{ "forceukbd", CTLTYPE_INT }, \
522 }
523 
524 /*
525  * This needs to be included late since it relies on definitions higher
526  * up in this file.
527  */
528 #if defined(MULTIPROCESSOR) && defined(_KERNEL)
529 #include <sys/mplock.h>
530 #endif
531 
532 #endif /* !_MACHINE_CPU_H_ */
533