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