xref: /netbsd-src/sys/arch/i386/include/cpu.h (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: cpu.h,v 1.129 2006/09/03 09:21:26 xtraeme Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * William Jolitz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)cpu.h	5.4 (Berkeley) 5/9/91
35  */
36 
37 #ifndef _I386_CPU_H_
38 #define _I386_CPU_H_
39 
40 #ifdef _KERNEL
41 #if defined(_KERNEL_OPT)
42 #include "opt_enhanced_speedstep.h"
43 #include "opt_multiprocessor.h"
44 #include "opt_math_emulate.h"
45 #include "opt_user_ldt.h"
46 #include "opt_vm86.h"
47 #endif
48 
49 /*
50  * Definitions unique to i386 cpu support.
51  */
52 #include <machine/frame.h>
53 #include <machine/segments.h>
54 #include <machine/tss.h>
55 #include <machine/intrdefs.h>
56 #include <x86/cacheinfo.h>
57 
58 #include <sys/device.h>
59 #include <sys/lock.h>			/* will also get LOCKDEBUG */
60 #include <sys/cpu_data.h>
61 #include <sys/cc_microtime.h>
62 
63 #include <lib/libkern/libkern.h>	/* offsetof */
64 
65 struct intrsource;
66 struct pmap;
67 
68 /*
69  * a bunch of this belongs in cpuvar.h; move it later..
70  */
71 
72 struct cpu_info {
73 	struct device *ci_dev;		/* pointer to our device */
74 	struct cpu_info *ci_self;	/* self-pointer */
75 	void	*ci_tlog_base;		/* Trap log base */
76 	int32_t ci_tlog_offset;		/* Trap log current offset */
77 	struct cpu_info *ci_next;	/* next cpu */
78 
79 	/*
80 	 * Public members.
81 	 */
82 	struct lwp *ci_curlwp;		/* current owner of the processor */
83 	struct simplelock ci_slock;	/* lock on this data structure */
84 	cpuid_t ci_cpuid;		/* our CPU ID */
85 	u_int ci_apicid;		/* our APIC ID */
86 	struct cpu_data ci_data;	/* MI per-cpu data */
87 	struct cc_microtime_state ci_cc;/* cc_microtime state */
88 
89 	/*
90 	 * Private members.
91 	 */
92 	struct lwp *ci_fpcurlwp;	/* current owner of the FPU */
93 	int	ci_fpsaving;		/* save in progress */
94 
95 	volatile uint32_t	ci_tlb_ipi_mask;
96 
97 	struct pmap *ci_pmap;		/* current pmap */
98 	int ci_want_pmapload;		/* pmap_load() is needed */
99 	int ci_tlbstate;		/* one of TLBSTATE_ states. see below */
100 #define	TLBSTATE_VALID	0	/* all user tlbs are valid */
101 #define	TLBSTATE_LAZY	1	/* tlbs are valid but won't be kept uptodate */
102 #define	TLBSTATE_STALE	2	/* we might have stale user tlbs */
103 
104 	struct pcb *ci_curpcb;		/* VA of current HW PCB */
105 	struct pcb *ci_idle_pcb;	/* VA of current PCB */
106 	int ci_idle_tss_sel;		/* TSS selector of idle PCB */
107 
108 	struct intrsource *ci_isources[MAX_INTR_SOURCES];
109 	uint32_t	ci_ipending;
110 	int		ci_ilevel;
111 	int		ci_idepth;
112 	uint32_t	ci_imask[NIPL];
113 	uint32_t	ci_iunmask[NIPL];
114 
115 	paddr_t ci_idle_pcb_paddr;	/* PA of idle PCB */
116 	uint32_t ci_flags;		/* flags; see below */
117 	uint32_t ci_ipis;		/* interprocessor interrupts pending */
118 	int sc_apic_version;		/* local APIC version */
119 
120 	int32_t		ci_cpuid_level;
121 	uint32_t	ci_signature;	 /* X86 cpuid type */
122 	uint32_t	ci_feature_flags;/* X86 %edx CPUID feature bits */
123 	uint32_t	ci_feature2_flags;/* X86 %ecx CPUID feature bits */
124 	uint32_t	ci_feature3_flags;/* X86 extended feature bits */
125 	uint32_t	ci_cpu_class;	 /* CPU class */
126 	uint32_t	ci_brand_id;	 /* Intel brand id */
127 	uint32_t	ci_vendor[4];	 /* vendor string */
128 	uint32_t	ci_cpu_serial[3]; /* PIII serial number */
129 	uint64_t	ci_tsc_freq;	 /* cpu cycles/second */
130 
131 	struct cpu_functions *ci_func;  /* start/stop functions */
132 	void (*cpu_setup)(struct cpu_info *);
133  					/* proc-dependant init */
134 	void (*ci_info)(struct cpu_info *);
135 
136 	int		ci_want_resched;
137 	int		ci_astpending;
138 	struct trapframe *ci_ddb_regs;
139 
140 	u_int ci_cflush_lsize;	/* CFLUSH insn line size */
141 	struct x86_cache_info ci_cinfo[CAI_COUNT];
142 
143 	union descriptor *ci_gdt;
144 
145 	struct i386tss	ci_doubleflt_tss;
146 	struct i386tss	ci_ddbipi_tss;
147 
148 	char *ci_doubleflt_stack;
149 	char *ci_ddbipi_stack;
150 
151 	struct evcnt ci_ipi_events[X86_NIPI];
152 };
153 
154 /*
155  * Processor flag notes: The "primary" CPU has certain MI-defined
156  * roles (mostly relating to hardclock handling); we distinguish
157  * betwen the processor which booted us, and the processor currently
158  * holding the "primary" role just to give us the flexibility later to
159  * change primaries should we be sufficiently twisted.
160  */
161 
162 #define	CPUF_BSP	0x0001		/* CPU is the original BSP */
163 #define	CPUF_AP		0x0002		/* CPU is an AP */
164 #define	CPUF_SP		0x0004		/* CPU is only processor */
165 #define	CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
166 
167 #define CPUF_APIC_CD    0x0010		/* CPU has apic configured */
168 
169 #define	CPUF_PRESENT	0x1000		/* CPU is present */
170 #define	CPUF_RUNNING	0x2000		/* CPU is running */
171 #define	CPUF_PAUSE	0x4000		/* CPU is paused in DDB */
172 #define	CPUF_GO		0x8000		/* CPU should start running */
173 
174 /*
175  * We statically allocate the CPU info for the primary CPU (or,
176  * the only CPU on uniprocessors), and the primary CPU is the
177  * first CPU on the CPU info list.
178  */
179 extern struct cpu_info cpu_info_primary;
180 extern struct cpu_info *cpu_info_list;
181 
182 #define	CPU_INFO_ITERATOR		int
183 #define	CPU_INFO_FOREACH(cii, ci)	cii = 0, ci = cpu_info_list; \
184 					ci != NULL; ci = ci->ci_next
185 
186 #if defined(MULTIPROCESSOR)
187 
188 #define X86_MAXPROCS		32	/* because we use a bitmask */
189 
190 #define CPU_STARTUP(_ci)	((_ci)->ci_func->start(_ci))
191 #define CPU_STOP(_ci)	        ((_ci)->ci_func->stop(_ci))
192 #define CPU_START_CLEANUP(_ci)	((_ci)->ci_func->cleanup(_ci))
193 
194 static struct cpu_info *curcpu(void);
195 
196 __inline static struct cpu_info * __attribute__((__unused__))
197 curcpu()
198 {
199 	struct cpu_info *ci;
200 
201 	__asm volatile("movl %%fs:%1, %0" :
202 	    "=r" (ci) :
203 	    "m"
204 	    (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_self)));
205 	return ci;
206 }
207 
208 #define cpu_number() 		(curcpu()->ci_cpuid)
209 
210 #define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
211 
212 #define aston(p)		((p)->p_md.md_astpending = 1)
213 
214 extern	struct cpu_info *cpu_info[X86_MAXPROCS];
215 
216 void cpu_boot_secondary_processors(void);
217 void cpu_init_idle_pcbs(void);
218 
219 /*
220  * Preempt the current process if in interrupt from user mode,
221  * or after the current trap/syscall if in system mode.
222  */
223 extern void need_resched(struct cpu_info *);
224 
225 #else /* !MULTIPROCESSOR */
226 
227 #define	X86_MAXPROCS		1
228 #define	curcpu()		(&cpu_info_primary)
229 
230 /*
231  * definitions of cpu-dependent requirements
232  * referenced in generic code
233  */
234 #define	cpu_number()		0
235 #define CPU_IS_PRIMARY(ci)	1
236 
237 /*
238  * Preempt the current process if in interrupt from user mode,
239  * or after the current trap/syscall if in system mode.
240  */
241 #define	need_resched(ci)						\
242 do {									\
243 	struct cpu_info *__ci = (ci);					\
244 	__ci->ci_want_resched = 1;					\
245 	if (__ci->ci_curlwp != NULL)					\
246 		aston(__ci->ci_curlwp->l_proc);       			\
247 } while (/*CONSTCOND*/0)
248 
249 #define aston(p)		((p)->p_md.md_astpending = 1)
250 
251 #endif /* MULTIPROCESSOR */
252 
253 extern uint32_t cpus_attached;
254 
255 #define	curpcb			curcpu()->ci_curpcb
256 #define	curlwp			curcpu()->ci_curlwp
257 
258 /*
259  * Arguments to hardclock, softclock and statclock
260  * encapsulate the previous machine state in an opaque
261  * clockframe; for now, use generic intrframe.
262  *
263  * Note: Since spllowersoftclock() does not actually unmask the currently
264  * running (hardclock) interrupt, CLKF_BASEPRI() *must* always be 0; otherwise
265  * we could stall hardclock ticks if another interrupt takes too long.
266  */
267 struct clockframe {
268 	struct intrframe cf_if;
269 };
270 
271 #define	CLKF_USERMODE(frame)	USERMODE((frame)->cf_if.if_cs, (frame)->cf_if.if_eflags)
272 #define	CLKF_BASEPRI(frame)	(0)
273 #define	CLKF_PC(frame)		((frame)->cf_if.if_eip)
274 #define	CLKF_INTR(frame)	(curcpu()->ci_idepth > 1)
275 
276 /*
277  * This is used during profiling to integrate system time.  It can safely
278  * assume that the process is resident.
279  */
280 #define	LWP_PC(l)		((l)->l_md.md_regs->tf_eip)
281 
282 /*
283  * Give a profiling tick to the current process when the user profiling
284  * buffer pages are invalid.  On the i386, request an ast to send us
285  * through trap(), marking the proc as needing a profiling tick.
286  */
287 #define	need_proftick(p)	((p)->p_flag |= P_OWEUPC, aston(p))
288 
289 /*
290  * Notify the current process (p) that it has a signal pending,
291  * process as soon as possible.
292  */
293 #define	signotify(p)		aston(p)
294 
295 /*
296  * We need a machine-independent name for this.
297  */
298 extern void (*delay_func)(int);
299 struct timeval;
300 
301 #define	DELAY(x)		(*delay_func)(x)
302 #define delay(x)		(*delay_func)(x)
303 
304 /*
305  * pull in #defines for kinds of processors
306  */
307 #include <machine/cputypes.h>
308 
309 struct cpu_nocpuid_nameclass {
310 	int cpu_vendor;
311 	const char *cpu_vendorname;
312 	const char *cpu_name;
313 	int cpu_class;
314 	void (*cpu_setup)(struct cpu_info *);
315 	void (*cpu_cacheinfo)(struct cpu_info *);
316 	void (*cpu_info)(struct cpu_info *);
317 };
318 
319 
320 struct cpu_cpuid_nameclass {
321 	const char *cpu_id;
322 	int cpu_vendor;
323 	const char *cpu_vendorname;
324 	struct cpu_cpuid_family {
325 		int cpu_class;
326 		const char *cpu_models[CPU_MAXMODEL+2];
327 		void (*cpu_setup)(struct cpu_info *);
328 		void (*cpu_probe)(struct cpu_info *);
329 		void (*cpu_info)(struct cpu_info *);
330 	} cpu_family[CPU_MAXFAMILY - CPU_MINFAMILY + 1];
331 };
332 
333 extern int biosbasemem;
334 extern int biosextmem;
335 extern unsigned int cpu_feature;
336 extern unsigned int cpu_feature2;
337 extern int cpu;
338 extern int cpu_class;
339 extern char cpu_brand_string[];
340 extern const struct cpu_nocpuid_nameclass i386_nocpuid_cpus[];
341 extern const struct cpu_cpuid_nameclass i386_cpuid_cpus[];
342 
343 extern int i386_use_fxsave;
344 extern int i386_has_sse;
345 extern int i386_has_sse2;
346 
347 /* machdep.c */
348 void	dumpconf(void);
349 int	cpu_maxproc(void);
350 void	cpu_reset(void);
351 void	i386_init_pcb_tss_ldt(struct cpu_info *);
352 void	i386_proc0_tss_ldt_init(void);
353 
354 /* identcpu.c */
355 #ifdef ENHANCED_SPEEDSTEP
356 extern int bus_clock;
357 #endif
358 extern int tmx86_has_longrun;
359 extern u_int crusoe_longrun;
360 extern u_int crusoe_frequency;
361 extern u_int crusoe_voltage;
362 extern u_int crusoe_percentage;
363 extern u_int tmx86_set_longrun_mode(u_int);
364 void tmx86_get_longrun_status_all(void);
365 u_int tmx86_get_longrun_mode(void);
366 void identifycpu(struct cpu_info *);
367 
368 /* vm_machdep.c */
369 void	cpu_proc_fork(struct proc *, struct proc *);
370 
371 /* locore.s */
372 struct region_descriptor;
373 void	lgdt(struct region_descriptor *);
374 void	fillw(short, void *, size_t);
375 
376 struct pcb;
377 void	savectx(struct pcb *);
378 void	proc_trampoline(void);
379 
380 /* clock.c */
381 void	initrtclock(u_long);
382 void	startrtclock(void);
383 void	i8254_delay(int);
384 void	i8254_microtime(struct timeval *);
385 void	i8254_initclocks(void);
386 
387 /* cpu.c */
388 
389 void	cpu_probe_features(struct cpu_info *);
390 
391 /* npx.c */
392 void	npxsave_lwp(struct lwp *, int);
393 void	npxsave_cpu(struct cpu_info *, int);
394 
395 /* vm_machdep.c */
396 int kvtop(caddr_t);
397 
398 #ifdef MATH_EMULATE
399 /* math_emulate.c */
400 int	math_emulate(struct trapframe *, ksiginfo_t *);
401 #endif
402 
403 #ifdef USER_LDT
404 /* sys_machdep.h */
405 int	i386_get_ldt(struct lwp *, void *, register_t *);
406 int	i386_set_ldt(struct lwp *, void *, register_t *);
407 #endif
408 
409 /* isa_machdep.c */
410 void	isa_defaultirq(void);
411 int	isa_nmi(void);
412 
413 #ifdef VM86
414 /* vm86.c */
415 void	vm86_gpfault(struct lwp *, int);
416 #endif /* VM86 */
417 
418 /* consinit.c */
419 void kgdb_port_init(void);
420 
421 /* bus_machdep.c */
422 void x86_bus_space_init(void);
423 void x86_bus_space_mallocok(void);
424 
425 #include <machine/psl.h>	/* Must be after struct cpu_info declaration */
426 
427 /* est.c */
428 void	est_init(struct cpu_info *, int);
429 
430 #endif /* _KERNEL */
431 
432 /*
433  * CTL_MACHDEP definitions.
434  */
435 #define	CPU_CONSDEV		1	/* dev_t: console terminal device */
436 #define	CPU_BIOSBASEMEM		2	/* int: bios-reported base mem (K) */
437 #define	CPU_BIOSEXTMEM		3	/* int: bios-reported ext. mem (K) */
438 #define	CPU_NKPDE		4	/* int: number of kernel PDEs */
439 #define	CPU_BOOTED_KERNEL	5	/* string: booted kernel name */
440 #define CPU_DISKINFO		6	/* struct disklist *:
441 					 * disk geometry information */
442 #define CPU_FPU_PRESENT		7	/* int: FPU is present */
443 #define	CPU_OSFXSR		8	/* int: OS uses FXSAVE/FXRSTOR */
444 #define	CPU_SSE			9	/* int: OS/CPU supports SSE */
445 #define	CPU_SSE2		10	/* int: OS/CPU supports SSE2 */
446 #define CPU_TMLR_MODE		11 	/* int: longrun mode
447 					 * 0: minimum frequency
448 					 * 1: economy
449 					 * 2: performance
450 					 * 3: maximum frequency
451 					 */
452 #define CPU_TMLR_FREQUENCY	12 	/* int: current frequency */
453 #define CPU_TMLR_VOLTAGE	13 	/* int: curret voltage */
454 #define CPU_TMLR_PERCENTAGE	14	/* int: current clock percentage */
455 #define	CPU_MAXID		15	/* number of valid machdep ids */
456 
457 #define	CTL_MACHDEP_NAMES { \
458 	{ 0, 0 }, \
459 	{ "console_device", CTLTYPE_STRUCT }, \
460 	{ "biosbasemem", CTLTYPE_INT }, \
461 	{ "biosextmem", CTLTYPE_INT }, \
462 	{ "nkpde", CTLTYPE_INT }, \
463 	{ "booted_kernel", CTLTYPE_STRING }, \
464 	{ "diskinfo", CTLTYPE_STRUCT }, \
465 	{ "fpu_present", CTLTYPE_INT }, \
466 	{ "osfxsr", CTLTYPE_INT }, \
467 	{ "sse", CTLTYPE_INT }, \
468 	{ "sse2", CTLTYPE_INT }, \
469 	{ "tm_longrun_mode", CTLTYPE_INT }, \
470 	{ "tm_longrun_frequency", CTLTYPE_INT }, \
471 	{ "tm_longrun_voltage", CTLTYPE_INT }, \
472 	{ "tm_longrun_percentage", CTLTYPE_INT }, \
473 }
474 
475 /*
476  * Structure for CPU_DISKINFO sysctl call.
477  * XXX this should be somewhere else.
478  */
479 #define MAX_BIOSDISKS	16
480 
481 struct disklist {
482 	int dl_nbiosdisks;			   /* number of bios disks */
483 	struct biosdisk_info {
484 		int bi_dev;			   /* BIOS device # (0x80 ..) */
485 		int bi_cyl;			   /* cylinders on disk */
486 		int bi_head;			   /* heads per track */
487 		int bi_sec;			   /* sectors per track */
488 		uint64_t bi_lbasecs;		   /* total sec. (iff ext13) */
489 #define BIFLAG_INVALID		0x01
490 #define BIFLAG_EXTINT13		0x02
491 		int bi_flags;
492 	} dl_biosdisks[MAX_BIOSDISKS];
493 
494 	int dl_nnativedisks;			   /* number of native disks */
495 	struct nativedisk_info {
496 		char ni_devname[16];		   /* native device name */
497 		int ni_nmatches; 		   /* # of matches w/ BIOS */
498 		int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */
499 	} dl_nativedisks[1];			   /* actually longer */
500 };
501 #endif /* !_I386_CPU_H_ */
502