xref: /openbsd-src/sys/arch/i386/include/cpu.h (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /*	$OpenBSD: cpu.h,v 1.176 2022/07/12 05:45:49 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 
141 	int		ci_inatomic;
142 
143 	struct cpu_functions *ci_func;	/* start/stop functions */
144 	void (*cpu_setup)(struct cpu_info *);	/* proc-dependant init */
145 
146 	struct device	*ci_acpicpudev;
147 	volatile u_int	ci_mwait;
148 #define	MWAIT_IN_IDLE		0x1	/* don't need IPI to wake */
149 #define	MWAIT_KEEP_IDLING	0x2	/* cleared by other cpus to wake me */
150 #define	MWAIT_ONLY		0x4	/* set if all idle states use mwait */
151 #define	MWAIT_IDLING		(MWAIT_IN_IDLE | MWAIT_KEEP_IDLING)
152 
153 	int		ci_want_resched;
154 
155 	union descriptor *ci_gdt;
156 	struct i386tss	*ci_tss;
157 	struct i386tss	*ci_nmi_tss;
158 
159 	volatile int ci_ddb_paused;	/* paused due to other proc in ddb */
160 #define CI_DDB_RUNNING		0
161 #define CI_DDB_SHOULDSTOP	1
162 #define CI_DDB_STOPPED		2
163 #define CI_DDB_ENTERDDB		3
164 #define CI_DDB_INDDB		4
165 
166 	struct ksensordev	ci_sensordev;
167 	struct ksensor		ci_sensor;
168 #if defined(GPROF) || defined(DDBPROF)
169 	struct gmonparam	*ci_gmon;
170 #endif
171 	char			ci_panicbuf[512];
172 };
173 
174 /*
175  * Processor flag notes: The "primary" CPU has certain MI-defined
176  * roles (mostly relating to hardclock handling); we distinguish
177  * between the processor which booted us, and the processor currently
178  * holding the "primary" role just to give us the flexibility later to
179  * change primaries should we be sufficiently twisted.
180  */
181 
182 #define	CPUF_BSP	0x0001		/* CPU is the original BSP */
183 #define	CPUF_AP		0x0002		/* CPU is an AP */
184 #define	CPUF_SP		0x0004		/* CPU is only processor */
185 #define	CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
186 #define	CPUF_APIC_CD	0x0010		/* CPU has apic configured */
187 #define	CPUF_CONST_TSC	0x0020		/* CPU has constant TSC */
188 
189 #define	CPUF_PRESENT	0x1000		/* CPU is present */
190 #define	CPUF_RUNNING	0x2000		/* CPU is running */
191 #define CPUF_VMM	0x4000		/* CPU is executing in VMM mode */
192 
193 /*
194  * We statically allocate the CPU info for the primary CPU (or,
195  * the only CPU on uniprocessors), and the primary CPU is the
196  * first CPU on the CPU info list.
197  */
198 struct cpu_info_full;
199 extern struct cpu_info_full cpu_info_full_primary;
200 #define cpu_info_primary (*(struct cpu_info *)((char *)&cpu_info_full_primary + PAGE_SIZE*2 - offsetof(struct cpu_info, ci_PAGEALIGN)))
201 
202 extern struct cpu_info *cpu_info_list;
203 
204 #define	CPU_INFO_ITERATOR		int
205 #define	CPU_INFO_FOREACH(cii, ci)	for (cii = 0, ci = cpu_info_list; \
206 					    ci != NULL; ci = ci->ci_next)
207 
208 #define CPU_INFO_UNIT(ci)	((ci)->ci_dev ? (ci)->ci_dev->dv_unit : 0)
209 
210 #ifdef MULTIPROCESSOR
211 
212 #define MAXCPUS			32	/* because we use a bitmask */
213 
214 #define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
215 #define CPU_STOP(_ci)		((_ci)->ci_func->stop(_ci))
216 #define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
217 
218 static struct cpu_info *curcpu(void);
219 
220 __inline static struct cpu_info *
221 curcpu(void)
222 {
223 	struct cpu_info *ci;
224 
225 	/* Can't include sys/param.h for offsetof() since it includes us */
226 	__asm volatile("movl %%fs:%1, %0" :
227 		"=r" (ci) : "m"
228 		(*(struct cpu_info * const *)&((struct cpu_info *)0)->ci_self));
229 	return ci;
230 }
231 #define cpu_number() 		(curcpu()->ci_cpuid)
232 
233 #define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
234 #define CPU_IS_RUNNING(ci)	((ci)->ci_flags & CPUF_RUNNING)
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 #define CPU_IS_RUNNING(ci)	1
255 
256 #define cpu_kick(ci)
257 #define cpu_unidle(ci)
258 
259 #define CPU_BUSY_CYCLE()	do {} while (0)
260 
261 #endif
262 
263 #include <machine/cpufunc.h>
264 
265 #define aston(p)	((p)->p_md.md_astpending = 1)
266 
267 #define curpcb			curcpu()->ci_curpcb
268 
269 unsigned int cpu_rnd_messybits(void);
270 
271 /*
272  * Preempt the current process if in interrupt from user mode,
273  * or after the current trap/syscall if in system mode.
274  */
275 extern void need_resched(struct cpu_info *);
276 #define clear_resched(ci) (ci)->ci_want_resched = 0
277 
278 #define	CLKF_USERMODE(frame)	USERMODE((frame)->if_cs, (frame)->if_eflags)
279 #define	CLKF_PC(frame)		((frame)->if_eip)
280 #define	CLKF_INTR(frame)	(IDXSEL((frame)->if_cs) == GICODE_SEL)
281 
282 /*
283  * This is used during profiling to integrate system time.
284  */
285 #define	PROC_PC(p)		((p)->p_md.md_regs->tf_eip)
286 #define	PROC_STACK(p)		((p)->p_md.md_regs->tf_esp)
287 
288 /*
289  * Give a profiling tick to the current process when the user profiling
290  * buffer pages are invalid.  On the i386, request an ast to send us
291  * through trap(), marking the proc as needing a profiling tick.
292  */
293 #define	need_proftick(p)	aston(p)
294 
295 /*
296  * Notify the current process (p) that it has a signal pending,
297  * process as soon as possible.
298  */
299 void signotify(struct proc *);
300 
301 /*
302  * We need a machine-independent name for this.
303  */
304 extern void (*delay_func)(int);
305 struct timeval;
306 
307 #define	DELAY(x)		(*delay_func)(x)
308 #define delay(x)		(*delay_func)(x)
309 
310 /*
311  * High resolution clock support (Pentium only)
312  */
313 void	calibrate_cyclecounter(void);
314 
315 /*
316  * pull in #defines for kinds of processors
317  */
318 #include <machine/cputypes.h>
319 
320 struct cpu_nocpuid_nameclass {
321 	int cpu_vendor;
322 	const char *cpu_vendorname;
323 	const char *cpu_name;
324 	int cpu_class;
325 	void (*cpu_setup)(struct cpu_info *);
326 };
327 
328 struct cpu_cpuid_nameclass {
329 	const char *cpu_id;
330 	int cpu_vendor;
331 	const char *cpu_vendorname;
332 	struct cpu_cpuid_family {
333 		int cpu_class;
334 		const char *cpu_models[CPU_MAXMODEL+2];
335 		void (*cpu_setup)(struct cpu_info *);
336 	} cpu_family[CPU_MAXFAMILY - CPU_MINFAMILY + 1];
337 };
338 
339 struct cpu_cpuid_feature {
340 	int feature_bit;
341 	const char *feature_name;
342 };
343 
344 /* locore.s */
345 extern int cpu;
346 extern int cpu_id;
347 extern char cpu_vendor[]; /* note: NOT nul-terminated */
348 extern char cpu_brandstr[];
349 extern int cpuid_level;
350 extern int cpu_miscinfo;
351 extern int cpu_feature;
352 extern int ecpu_feature;
353 extern int cpu_ecxfeature;
354 extern int ecpu_ecxfeature;
355 extern int cpu_cache_eax;
356 extern int cpu_cache_ebx;
357 extern int cpu_cache_ecx;
358 extern int cpu_cache_edx;
359 extern int cpu_perf_eax;
360 extern int cpu_perf_ebx;
361 extern int cpu_perf_edx;
362 extern int cpu_apmi_edx;
363 extern int cpu_pae;	/* cpu has both PAE and NX features */
364 
365 /* cpu.c */
366 extern u_int cpu_mwait_size;
367 extern u_int cpu_mwait_states;
368 extern void cpu_update_nmi_cr3(vaddr_t);
369 extern void cpu_tsx_disable(struct cpu_info *);
370 
371 /* machdep.c */
372 extern int cpu_apmhalt;
373 extern int cpu_class;
374 extern char cpu_model[];
375 extern const struct cpu_nocpuid_nameclass i386_nocpuid_cpus[];
376 extern const struct cpu_cpuid_nameclass i386_cpuid_cpus[];
377 extern void (*cpu_idle_enter_fcn)(void);
378 extern void (*cpu_idle_cycle_fcn)(void);
379 extern void (*cpu_idle_leave_fcn)(void);
380 
381 extern int cpuspeed;
382 
383 #if !defined(SMALL_KERNEL)
384 #define BUS66  6667
385 #define BUS100 10000
386 #define BUS133 13333
387 #define BUS166 16667
388 #define BUS200 20000
389 #define BUS266 26667
390 #define BUS333 33333
391 extern int bus_clock;
392 #endif
393 
394 /* F00F bug fix stuff for pentium cpu */
395 extern int cpu_f00f_bug;
396 void fix_f00f(void);
397 
398 /* dkcsum.c */
399 void	dkcsumattach(void);
400 
401 extern int i386_use_fxsave;
402 extern int i386_has_sse;
403 extern int i386_has_sse2;
404 
405 extern void (*update_cpuspeed)(void);
406 
407 /* machdep.c */
408 void	dumpconf(void);
409 void	cpu_reset(void);
410 void	i386_proc0_tss_init(void);
411 void	i386_init_pcb_tss(struct cpu_info *);
412 void	cpuid(u_int32_t, u_int32_t *);
413 
414 /* locore.s */
415 struct region_descriptor;
416 void	lgdt(struct region_descriptor *);
417 
418 struct pcb;
419 void	savectx(struct pcb *);
420 void	switch_exit(struct proc *);
421 void	proc_trampoline(void);
422 
423 /* clock.c */
424 extern void (*initclock_func)(void);
425 void	startclocks(void);
426 void	rtcinit(void);
427 void	rtcstart(void);
428 void	rtcstop(void);
429 void	i8254_delay(int);
430 void	i8254_initclocks(void);
431 void	i8254_startclock(void);
432 void	i8254_inittimecounter(void);
433 void	i8254_inittimecounter_simple(void);
434 
435 #if !defined(SMALL_KERNEL)
436 /* est.c */
437 void	est_init(struct cpu_info *, int);
438 void	est_setperf(int);
439 /* longrun.c */
440 void	longrun_init(void);
441 void	longrun_setperf(int);
442 /* p4tcc.c */
443 void	p4tcc_init(int, int);
444 void	p4tcc_setperf(int);
445 /* powernow.c */
446 void	k6_powernow_init(void);
447 void	k6_powernow_setperf(int);
448 /* powernow-k7.c */
449 void	k7_powernow_init(void);
450 void	k7_powernow_setperf(int);
451 /* powernow-k8.c */
452 void 	k8_powernow_init(void);
453 void 	k8_powernow_setperf(int);
454 /* k1x-pstate.c */
455 void k1x_init(struct cpu_info *);
456 void k1x_setperf(int);
457 #endif
458 
459 /* npx.c */
460 void	npxdrop(struct proc *);
461 void	npxsave_proc(struct proc *, int);
462 void	npxsave_cpu(struct cpu_info *, int);
463 
464 /* isa_machdep.c */
465 void	isa_defaultirq(void);
466 int	isa_nmi(void);
467 
468 /* pmap.c */
469 void	pmap_bootstrap(vaddr_t);
470 
471 /* vm_machdep.c */
472 int	kvtop(caddr_t);
473 
474 #ifdef MULTIPROCESSOR
475 /* mp_setperf.c */
476 void	mp_setperf_init(void);
477 #endif
478 
479 int	cpu_paenable(void *);
480 #endif /* _KERNEL */
481 
482 /*
483  * CTL_MACHDEP definitions.
484  */
485 #define	CPU_CONSDEV		1	/* dev_t: console terminal device */
486 #define	CPU_BIOS		2	/* BIOS variables */
487 #define	CPU_BLK2CHR		3	/* convert blk maj into chr one */
488 #define	CPU_CHR2BLK		4	/* convert chr maj into blk one */
489 #define CPU_ALLOWAPERTURE	5	/* allow mmap of /dev/xf86 */
490 #define CPU_CPUVENDOR		6	/* cpuid vendor string */
491 #define CPU_CPUID		7	/* cpuid */
492 #define CPU_CPUFEATURE		8	/* cpuid features */
493 #define CPU_KBDRESET		10	/* keyboard reset under pcvt */
494 #define CPU_OSFXSR		13	/* uses FXSAVE/FXRSTOR */
495 #define CPU_SSE			14	/* supports SSE */
496 #define CPU_SSE2		15	/* supports SSE2 */
497 #define CPU_XCRYPT		16	/* supports VIA xcrypt in userland */
498 #define CPU_LIDACTION		18	/* action caused by lid close */
499 #define CPU_FORCEUKBD		19	/* Force ukbd(4) as console keyboard */
500 #define CPU_MAXID		20	/* number of valid machdep ids */
501 
502 #define	CTL_MACHDEP_NAMES { \
503 	{ 0, 0 }, \
504 	{ "console_device", CTLTYPE_STRUCT }, \
505 	{ "bios", CTLTYPE_INT }, \
506 	{ "blk2chr", CTLTYPE_STRUCT }, \
507 	{ "chr2blk", CTLTYPE_STRUCT }, \
508 	{ "allowaperture", CTLTYPE_INT }, \
509 	{ "cpuvendor", CTLTYPE_STRING }, \
510 	{ "cpuid", CTLTYPE_INT }, \
511 	{ "cpufeature", CTLTYPE_INT }, \
512 	{ 0, 0 }, \
513 	{ "kbdreset", CTLTYPE_INT }, \
514 	{ 0, 0 }, \
515 	{ 0, 0 }, \
516 	{ "osfxsr", CTLTYPE_INT }, \
517 	{ "sse", CTLTYPE_INT }, \
518 	{ "sse2", CTLTYPE_INT }, \
519 	{ "xcrypt", CTLTYPE_INT }, \
520 	{ 0, 0 }, \
521 	{ "lidaction", CTLTYPE_INT }, \
522 	{ "forceukbd", CTLTYPE_INT }, \
523 }
524 
525 /*
526  * This needs to be included late since it relies on definitions higher
527  * up in this file.
528  */
529 #if defined(MULTIPROCESSOR) && defined(_KERNEL)
530 #include <sys/mplock.h>
531 #endif
532 
533 #endif /* !_MACHINE_CPU_H_ */
534