cpu.h revision 1.141 1 /* $NetBSD: cpu.h,v 1.141 2007/05/17 14:51:22 yamt 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 #include <x86/via_padlock.h>
58
59 #include <sys/device.h>
60 #include <sys/simplelock.h> /* will also get LOCKDEBUG */
61 #include <sys/cpu_data.h>
62 #include <sys/cc_microtime.h>
63
64 #include <lib/libkern/libkern.h> /* offsetof */
65
66 struct intrsource;
67 struct pmap;
68
69 /*
70 * a bunch of this belongs in cpuvar.h; move it later..
71 */
72
73 struct cpu_info {
74 struct device *ci_dev; /* pointer to our device */
75 struct cpu_info *ci_self; /* self-pointer */
76 void *ci_self150; /* self + 0x150, see lock_stubs.S */
77 void *ci_tlog_base; /* Trap log base */
78 int32_t ci_tlog_offset; /* Trap log current offset */
79 struct cpu_info *ci_next; /* next cpu */
80
81 /*
82 * Public members.
83 */
84 struct lwp *ci_curlwp; /* current owner of the processor */
85 struct simplelock ci_slock; /* lock on this data structure */
86 cpuid_t ci_cpuid; /* our CPU ID */
87 u_int ci_apicid; /* our APIC ID */
88 struct cpu_data ci_data; /* MI per-cpu data */
89 struct cc_microtime_state ci_cc;/* cc_microtime state */
90
91 /*
92 * Private members.
93 */
94 struct lwp *ci_fpcurlwp; /* current owner of the FPU */
95 int ci_fpsaving; /* save in progress */
96
97 volatile uint32_t ci_tlb_ipi_mask;
98
99 struct pmap *ci_pmap; /* current pmap */
100 int ci_want_pmapload; /* pmap_load() is needed */
101 int ci_tlbstate; /* one of TLBSTATE_ states. see below */
102 #define TLBSTATE_VALID 0 /* all user tlbs are valid */
103 #define TLBSTATE_LAZY 1 /* tlbs are valid but won't be kept uptodate */
104 #define TLBSTATE_STALE 2 /* we might have stale user tlbs */
105
106 struct intrsource *ci_isources[MAX_INTR_SOURCES];
107 volatile int ci_mtx_count; /* Negative count of spin mutexes */
108 volatile int ci_mtx_oldspl; /* Old SPL at this ci_idepth */
109
110 /* The following must be aligned for cmpxchg8b. */
111 struct {
112 uint32_t ipending;
113 int ilevel;
114 } ci_istate __aligned(8);
115 #define ci_ipending ci_istate.ipending
116 #define ci_ilevel ci_istate.ilevel
117
118 int ci_idepth;
119 uint32_t ci_imask[NIPL];
120 uint32_t ci_iunmask[NIPL];
121 void * ci_intrstack;
122
123 paddr_t ci_idle_pcb_paddr; /* PA of idle PCB */
124 uint32_t ci_flags; /* flags; see below */
125 uint32_t ci_ipis; /* interprocessor interrupts pending */
126 int sc_apic_version; /* local APIC version */
127
128 int32_t ci_cpuid_level;
129 uint32_t ci_signature; /* X86 cpuid type */
130 uint32_t ci_feature_flags;/* X86 %edx CPUID feature bits */
131 uint32_t ci_feature2_flags;/* X86 %ecx CPUID feature bits */
132 uint32_t ci_feature3_flags;/* X86 extended feature bits */
133 uint32_t ci_padlock_flags;/* VIA PadLock feature bits */
134 uint32_t ci_cpu_class; /* CPU class */
135 uint32_t ci_brand_id; /* Intel brand id */
136 uint32_t ci_vendor[4]; /* vendor string */
137 uint32_t ci_cpu_serial[3]; /* PIII serial number */
138 uint64_t ci_tsc_freq; /* cpu cycles/second */
139
140 const struct cpu_functions *ci_func; /* start/stop functions */
141 void (*cpu_setup)(struct cpu_info *);
142 /* proc-dependant init */
143 void (*ci_info)(struct cpu_info *);
144
145 int ci_want_resched;
146 int ci_astpending;
147 struct trapframe *ci_ddb_regs;
148
149 u_int ci_cflush_lsize; /* CFLUSH insn line size */
150 struct x86_cache_info ci_cinfo[CAI_COUNT];
151
152 union descriptor *ci_gdt;
153
154 struct i386tss ci_doubleflt_tss;
155 struct i386tss ci_ddbipi_tss;
156
157 char *ci_doubleflt_stack;
158 char *ci_ddbipi_stack;
159
160 struct evcnt ci_ipi_events[X86_NIPI];
161
162 struct via_padlock ci_vp; /* VIA PadLock private storage */
163 };
164
165 /*
166 * Processor flag notes: The "primary" CPU has certain MI-defined
167 * roles (mostly relating to hardclock handling); we distinguish
168 * betwen the processor which booted us, and the processor currently
169 * holding the "primary" role just to give us the flexibility later to
170 * change primaries should we be sufficiently twisted.
171 */
172
173 #define CPUF_BSP 0x0001 /* CPU is the original BSP */
174 #define CPUF_AP 0x0002 /* CPU is an AP */
175 #define CPUF_SP 0x0004 /* CPU is only processor */
176 #define CPUF_PRIMARY 0x0008 /* CPU is active primary processor */
177
178 #define CPUF_APIC_CD 0x0010 /* CPU has apic configured */
179
180 #define CPUF_PRESENT 0x1000 /* CPU is present */
181 #define CPUF_RUNNING 0x2000 /* CPU is running */
182 #define CPUF_PAUSE 0x4000 /* CPU is paused in DDB */
183 #define CPUF_GO 0x8000 /* CPU should start running */
184
185 /*
186 * We statically allocate the CPU info for the primary CPU (or,
187 * the only CPU on uniprocessors), and the primary CPU is the
188 * first CPU on the CPU info list.
189 */
190 extern struct cpu_info cpu_info_primary;
191 extern struct cpu_info *cpu_info_list;
192
193 #define CPU_INFO_ITERATOR int
194 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = cpu_info_list; \
195 ci != NULL; ci = ci->ci_next
196
197 #if defined(MULTIPROCESSOR)
198
199 #define X86_MAXPROCS 32 /* because we use a bitmask */
200
201 #define CPU_STARTUP(_ci) ((_ci)->ci_func->start(_ci))
202 #define CPU_STOP(_ci) ((_ci)->ci_func->stop(_ci))
203 #define CPU_START_CLEANUP(_ci) ((_ci)->ci_func->cleanup(_ci))
204
205 static struct cpu_info *curcpu(void);
206
207 __inline static struct cpu_info * __attribute__((__unused__))
208 curcpu()
209 {
210 struct cpu_info *ci;
211
212 __asm volatile("movl %%fs:%1, %0" :
213 "=r" (ci) :
214 "m"
215 (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_self)));
216 return ci;
217 }
218
219 #define cpu_number() (curcpu()->ci_cpuid)
220
221 #define CPU_IS_PRIMARY(ci) ((ci)->ci_flags & CPUF_PRIMARY)
222
223 #define aston(l) ((l)->l_md.md_astpending = 1)
224
225 extern struct cpu_info *cpu_info[X86_MAXPROCS];
226
227 void cpu_boot_secondary_processors(void);
228 void cpu_init_idle_lwps(void);
229
230 #else /* !MULTIPROCESSOR */
231
232 #define X86_MAXPROCS 1
233 #define curcpu() (&cpu_info_primary)
234
235 /*
236 * definitions of cpu-dependent requirements
237 * referenced in generic code
238 */
239 #define cpu_number() 0
240 #define CPU_IS_PRIMARY(ci) 1
241
242 #define aston(l) ((l)->l_md.md_astpending = 1)
243
244 #endif /* MULTIPROCESSOR */
245
246 extern uint32_t cpus_attached;
247
248 #define curlwp curcpu()->ci_curlwp
249 #define curpcb (&curlwp->l_addr->u_pcb)
250
251 /*
252 * Arguments to hardclock, softclock and statclock
253 * encapsulate the previous machine state in an opaque
254 * clockframe; for now, use generic intrframe.
255 */
256 struct clockframe {
257 struct intrframe cf_if;
258 };
259
260 #define CLKF_USERMODE(frame) USERMODE((frame)->cf_if.if_cs, (frame)->cf_if.if_eflags)
261 #define CLKF_PC(frame) ((frame)->cf_if.if_eip)
262 #define CLKF_INTR(frame) (curcpu()->ci_idepth > 0)
263
264 /*
265 * This is used during profiling to integrate system time. It can safely
266 * assume that the process is resident.
267 */
268 #define LWP_PC(l) ((l)->l_md.md_regs->tf_eip)
269
270 /*
271 * Give a profiling tick to the current process when the user profiling
272 * buffer pages are invalid. On the i386, request an ast to send us
273 * through trap(), marking the proc as needing a profiling tick.
274 */
275 extern void cpu_need_proftick(struct lwp *l);
276
277 /*
278 * Notify the LWP l that it has a signal pending, process as soon as
279 * possible.
280 */
281 extern void cpu_signotify(struct lwp *);
282
283 /*
284 * We need a machine-independent name for this.
285 */
286 extern void (*delay_func)(int);
287 struct timeval;
288
289 #define DELAY(x) (*delay_func)(x)
290 #define delay(x) (*delay_func)(x)
291
292 /*
293 * pull in #defines for kinds of processors
294 */
295 #include <machine/cputypes.h>
296
297 struct cpu_nocpuid_nameclass {
298 int cpu_vendor;
299 const char *cpu_vendorname;
300 const char *cpu_name;
301 int cpu_class;
302 void (*cpu_setup)(struct cpu_info *);
303 void (*cpu_cacheinfo)(struct cpu_info *);
304 void (*cpu_info)(struct cpu_info *);
305 };
306
307
308 struct cpu_cpuid_nameclass {
309 const char *cpu_id;
310 int cpu_vendor;
311 const char *cpu_vendorname;
312 struct cpu_cpuid_family {
313 int cpu_class;
314 const char *cpu_models[CPU_MAXMODEL+2];
315 void (*cpu_setup)(struct cpu_info *);
316 void (*cpu_probe)(struct cpu_info *);
317 void (*cpu_info)(struct cpu_info *);
318 } cpu_family[CPU_MAXFAMILY - CPU_MINFAMILY + 1];
319 };
320
321 extern int biosbasemem;
322 extern int biosextmem;
323 extern unsigned int cpu_feature;
324 extern unsigned int cpu_feature2;
325 extern unsigned int cpu_feature_padlock;
326 extern int cpu;
327 extern int cpu_class;
328 extern char cpu_brand_string[];
329 extern const struct cpu_nocpuid_nameclass i386_nocpuid_cpus[];
330 extern const struct cpu_cpuid_nameclass i386_cpuid_cpus[];
331
332 extern int i386_use_fxsave;
333 extern int i386_has_sse;
334 extern int i386_has_sse2;
335
336 /* machdep.c */
337 void dumpconf(void);
338 int cpu_maxproc(void);
339 void cpu_reset(void);
340 void i386_proc0_tss_ldt_init(void);
341
342 /* identcpu.c */
343 #ifdef ENHANCED_SPEEDSTEP
344 extern int bus_clock;
345 #endif
346 extern int tmx86_has_longrun;
347 extern u_int crusoe_longrun;
348 extern u_int crusoe_frequency;
349 extern u_int crusoe_voltage;
350 extern u_int crusoe_percentage;
351 extern u_int tmx86_set_longrun_mode(u_int);
352 void tmx86_get_longrun_status_all(void);
353 u_int tmx86_get_longrun_mode(void);
354 void identifycpu(struct cpu_info *);
355
356 /* vm_machdep.c */
357 void cpu_proc_fork(struct proc *, struct proc *);
358
359 /* locore.s */
360 struct region_descriptor;
361 void lgdt(struct region_descriptor *);
362 void fillw(short, void *, size_t);
363
364 struct pcb;
365 void savectx(struct pcb *);
366 void lwp_trampoline(void);
367
368 /* clock.c */
369 void initrtclock(u_long);
370 void startrtclock(void);
371 void i8254_delay(int);
372 void i8254_microtime(struct timeval *);
373 void i8254_initclocks(void);
374
375 /* cpu.c */
376
377 void cpu_probe_features(struct cpu_info *);
378
379 /* npx.c */
380 void npxsave_lwp(struct lwp *, int);
381 void npxsave_cpu(struct cpu_info *, int);
382
383 /* vm_machdep.c */
384 int kvtop(void *);
385
386 #ifdef MATH_EMULATE
387 /* math_emulate.c */
388 int math_emulate(struct trapframe *, ksiginfo_t *);
389 #endif
390
391 #ifdef USER_LDT
392 /* sys_machdep.h */
393 int x86_get_ldt(struct lwp *, void *, register_t *);
394 int x86_set_ldt(struct lwp *, void *, register_t *);
395 #endif
396
397 /* isa_machdep.c */
398 void isa_defaultirq(void);
399 int isa_nmi(void);
400
401 #ifdef VM86
402 /* vm86.c */
403 void vm86_gpfault(struct lwp *, int);
404 #endif /* VM86 */
405
406 /* consinit.c */
407 void kgdb_port_init(void);
408
409 /* bus_machdep.c */
410 void x86_bus_space_init(void);
411 void x86_bus_space_mallocok(void);
412
413 #include <machine/psl.h> /* Must be after struct cpu_info declaration */
414
415 /* est.c */
416 void est_init(int);
417
418 #endif /* _KERNEL */
419
420 /*
421 * CTL_MACHDEP definitions.
422 */
423 #define CPU_CONSDEV 1 /* dev_t: console terminal device */
424 #define CPU_BIOSBASEMEM 2 /* int: bios-reported base mem (K) */
425 #define CPU_BIOSEXTMEM 3 /* int: bios-reported ext. mem (K) */
426 #define CPU_NKPDE 4 /* int: number of kernel PDEs */
427 #define CPU_BOOTED_KERNEL 5 /* string: booted kernel name */
428 #define CPU_DISKINFO 6 /* struct disklist *:
429 * disk geometry information */
430 #define CPU_FPU_PRESENT 7 /* int: FPU is present */
431 #define CPU_OSFXSR 8 /* int: OS uses FXSAVE/FXRSTOR */
432 #define CPU_SSE 9 /* int: OS/CPU supports SSE */
433 #define CPU_SSE2 10 /* int: OS/CPU supports SSE2 */
434 #define CPU_TMLR_MODE 11 /* int: longrun mode
435 * 0: minimum frequency
436 * 1: economy
437 * 2: performance
438 * 3: maximum frequency
439 */
440 #define CPU_TMLR_FREQUENCY 12 /* int: current frequency */
441 #define CPU_TMLR_VOLTAGE 13 /* int: curret voltage */
442 #define CPU_TMLR_PERCENTAGE 14 /* int: current clock percentage */
443 #define CPU_MAXID 15 /* number of valid machdep ids */
444
445 #define CTL_MACHDEP_NAMES { \
446 { 0, 0 }, \
447 { "console_device", CTLTYPE_STRUCT }, \
448 { "biosbasemem", CTLTYPE_INT }, \
449 { "biosextmem", CTLTYPE_INT }, \
450 { "nkpde", CTLTYPE_INT }, \
451 { "booted_kernel", CTLTYPE_STRING }, \
452 { "diskinfo", CTLTYPE_STRUCT }, \
453 { "fpu_present", CTLTYPE_INT }, \
454 { "osfxsr", CTLTYPE_INT }, \
455 { "sse", CTLTYPE_INT }, \
456 { "sse2", CTLTYPE_INT }, \
457 { "tm_longrun_mode", CTLTYPE_INT }, \
458 { "tm_longrun_frequency", CTLTYPE_INT }, \
459 { "tm_longrun_voltage", CTLTYPE_INT }, \
460 { "tm_longrun_percentage", CTLTYPE_INT }, \
461 }
462
463 /*
464 * Structure for CPU_DISKINFO sysctl call.
465 * XXX this should be somewhere else.
466 */
467 #define MAX_BIOSDISKS 16
468
469 struct disklist {
470 int dl_nbiosdisks; /* number of bios disks */
471 struct biosdisk_info {
472 int bi_dev; /* BIOS device # (0x80 ..) */
473 int bi_cyl; /* cylinders on disk */
474 int bi_head; /* heads per track */
475 int bi_sec; /* sectors per track */
476 uint64_t bi_lbasecs; /* total sec. (iff ext13) */
477 #define BIFLAG_INVALID 0x01
478 #define BIFLAG_EXTINT13 0x02
479 int bi_flags;
480 } dl_biosdisks[MAX_BIOSDISKS];
481
482 int dl_nnativedisks; /* number of native disks */
483 struct nativedisk_info {
484 char ni_devname[16]; /* native device name */
485 int ni_nmatches; /* # of matches w/ BIOS */
486 int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */
487 } dl_nativedisks[1]; /* actually longer */
488 };
489 #endif /* !_I386_CPU_H_ */
490