summaryrefslogtreecommitdiff
path: root/com32/modules/cpuid.c
blob: 4d6d46e494538ed882ab69dd1d7a9fcaa51521de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2006 Erwan Velu - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

#include <stdio.h>
#include <string.h>
#include "cpuid.h"

struct cpu_dev * cpu_devs[X86_VENDOR_NUM] = {};

/*
* CPUID functions returning a single datum
*/

/* Probe for the CPUID instruction */
static int have_cpuid_p(void)
{
        return cpu_has_eflag(X86_EFLAGS_ID);
}

static struct cpu_dev amd_cpu_dev = {
        .c_vendor       = "AMD",
        .c_ident        = { "AuthenticAMD" }
};

static struct cpu_dev intel_cpu_dev = {
        .c_vendor       = "Intel",
        .c_ident        = { "GenuineIntel" }
};

static struct cpu_dev cyrix_cpu_dev = {
        .c_vendor       = "Cyrix",
        .c_ident        = { "CyrixInstead" }
};

static struct cpu_dev umc_cpu_dev = {
        .c_vendor       = "UMC",
	.c_ident        = { "UMC UMC UMC" }

};

static struct cpu_dev nexgen_cpu_dev = {
        .c_vendor       = "Nexgen",
        .c_ident        = { "NexGenDriven" }
};

static struct cpu_dev centaur_cpu_dev = {
        .c_vendor       = "Centaur",
        .c_ident        = { "CentaurHauls" }
};

static struct cpu_dev rise_cpu_dev = {
        .c_vendor       = "Rise",
        .c_ident        = { "RiseRiseRise" }
};

static struct cpu_dev transmeta_cpu_dev = {
        .c_vendor       = "Transmeta",
        .c_ident        = { "GenuineTMx86", "TransmetaCPU" }
};

void init_cpu_devs(void)
{
	cpu_devs[X86_VENDOR_INTEL] = &intel_cpu_dev;
	cpu_devs[X86_VENDOR_CYRIX] = &cyrix_cpu_dev;
	cpu_devs[X86_VENDOR_AMD] = &amd_cpu_dev;
	cpu_devs[X86_VENDOR_UMC] = &umc_cpu_dev;
	cpu_devs[X86_VENDOR_NEXGEN] = &nexgen_cpu_dev;
	cpu_devs[X86_VENDOR_CENTAUR] = &centaur_cpu_dev;
	cpu_devs[X86_VENDOR_RISE] = &rise_cpu_dev;
	cpu_devs[X86_VENDOR_TRANSMETA] = &transmeta_cpu_dev;
}

void get_cpu_vendor(struct cpuinfo_x86 *c)
{
        char *v = c->x86_vendor_id;
        int i;
	init_cpu_devs();
        for (i = 0; i < X86_VENDOR_NUM; i++) {
                if (cpu_devs[i]) {
                        if (!strcmp(v,cpu_devs[i]->c_ident[0]) ||
                            (cpu_devs[i]->c_ident[1] &&
                             !strcmp(v,cpu_devs[i]->c_ident[1]))) {
                                c->x86_vendor = i;
                                return;
                        }
                }
        }

        c->x86_vendor = X86_VENDOR_UNKNOWN;
}

int get_model_name(struct cpuinfo_x86 *c)
{
        unsigned int *v;
        char *p, *q;

        if (cpuid_eax(0x80000000) < 0x80000004)
                return 0;

        v = (unsigned int *) c->x86_model_id;
        cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
        cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
        cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
        c->x86_model_id[48] = 0;

        /* Intel chips right-justify this string for some dumb reason;
           undo that brain damage */
        p = q = &c->x86_model_id[0];
        while ( *p == ' ' )
             p++;
        if ( p != q ) {
             while ( *p )
                  *q++ = *p++;
             while ( q <= &c->x86_model_id[48] )
                  *q++ = '\0';  /* Zero-pad the rest */
        }

        return 1;
}

void generic_identify(struct cpuinfo_x86 *c)
{
	uint32_t tfms, xlvl;
	unsigned int ebx;

	/* Get vendor name */
	cpuid(0x00000000,
	      (uint32_t *)&c->cpuid_level,
              (uint32_t *)&c->x86_vendor_id[0],
              (uint32_t *)&c->x86_vendor_id[8],
              (uint32_t *)&c->x86_vendor_id[4]);

        get_cpu_vendor(c);
        /* Intel-defined flags: level 0x00000001 */
        if ( c->cpuid_level >= 0x00000001 ) {
		uint32_t capability, excap;
                cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
                c->x86_capability[0] = capability;
                c->x86_capability[4] = excap;
                c->x86 = (tfms >> 8) & 15;
                c->x86_model = (tfms >> 4) & 15;
                if (c->x86 == 0xf)
                        c->x86 += (tfms >> 20) & 0xff;
                if (c->x86 >= 0x6)
                        c->x86_model += ((tfms >> 16) & 0xF) << 4;
                c->x86_mask = tfms & 15;
                if (cpu_has(c, X86_FEATURE_CLFLSH))
                        c->x86_clflush_size = ((ebx >> 8) & 0xff) * 8;
              } else {
                      /* Have CPUID level 0 only - unheard of */
                      c->x86 = 4;
	}

        /* AMD-defined flags: level 0x80000001 */
        xlvl = cpuid_eax(0x80000000);
        if ( (xlvl & 0xffff0000) == 0x80000000 ) {
               if ( xlvl >= 0x80000001 ) {
                     c->x86_capability[1] = cpuid_edx(0x80000001);
                     c->x86_capability[6] = cpuid_ecx(0x80000001);
               }
               if ( xlvl >= 0x80000004 )
                     get_model_name(c); /* Default name */
       }
}

/*
 * Checksum an MP configuration block.
 */

static int mpf_checksum(unsigned char *mp, int len)
{
        int sum = 0;

        while (len--)
                sum += *mp++;

        return sum & 0xFF;
}

static int smp_scan_config (unsigned long base, unsigned long length)
{
        unsigned long *bp = (unsigned long *)base;
        struct intel_mp_floating *mpf;

//        printf("Scan SMP from %p for %ld bytes.\n", bp,length);
        if (sizeof(*mpf) != 16) {
                printf("Error: MPF size\n");
		return 0;
	}

        while (length > 0) {
                mpf = (struct intel_mp_floating *)bp;
                if ((*bp == SMP_MAGIC_IDENT) &&
                        (mpf->mpf_length == 1) &&
                        !mpf_checksum((unsigned char *)bp, 16) &&
                        ((mpf->mpf_specification == 1)
                                || (mpf->mpf_specification == 4)) ) {
                        return 1;
                }
                bp += 4;
                length -= 16;
        }
        return 0;
}

int find_smp_config (void)
{
//        unsigned int address;

        /*
         * FIXME: Linux assumes you have 640K of base ram..
         * this continues the error...
         *
         * 1) Scan the bottom 1K for a signature
         * 2) Scan the top 1K of base RAM
         * 3) Scan the 64K of bios
         */
        if (smp_scan_config(0x0,0x400) ||
                smp_scan_config(639*0x400,0x400) ||
                        smp_scan_config(0xF0000,0x10000))
                return 1;
        /*
         * If it is an SMP machine we should know now, unless the
         * configuration is in an EISA/MCA bus machine with an
         * extended bios data area.
         *
         * there is a real-mode segmented pointer pointing to the
         * 4K EBDA area at 0x40E, calculate and scan it here.
         *
         * NOTE! There are Linux loaders that will corrupt the EBDA
         * area, and as such this kind of SMP config may be less
         * trustworthy, simply because the SMP table may have been
         * stomped on during early boot. These loaders are buggy and
         * should be fixed.
         *
         * MP1.4 SPEC states to only scan first 1K of 4K EBDA.
         */

//        address = get_bios_ebda();
//        if (address)
//                smp_scan_config(address, 0x400);
	 return 0;
}


void set_cpu_flags(struct cpuinfo_x86 *c, s_cpu *cpu) {
cpu->flags.fpu=cpu_has(c, X86_FEATURE_FPU);
cpu->flags.vme=cpu_has(c, X86_FEATURE_VME);
cpu->flags.de=cpu_has(c, X86_FEATURE_DE);
cpu->flags.pse=cpu_has(c, X86_FEATURE_PSE);
cpu->flags.tsc=cpu_has(c, X86_FEATURE_TSC);
cpu->flags.msr=cpu_has(c, X86_FEATURE_MSR);
cpu->flags.pae=cpu_has(c, X86_FEATURE_PAE);
cpu->flags.mce=cpu_has(c, X86_FEATURE_MCE);
cpu->flags.cx8=cpu_has(c, X86_FEATURE_CX8);
cpu->flags.apic=cpu_has(c, X86_FEATURE_APIC);
cpu->flags.sep=cpu_has(c, X86_FEATURE_SEP);
cpu->flags.mtrr=cpu_has(c, X86_FEATURE_MTRR);
cpu->flags.pge=cpu_has(c, X86_FEATURE_PGE);
cpu->flags.mca=cpu_has(c, X86_FEATURE_MCA);
cpu->flags.cmov=cpu_has(c, X86_FEATURE_CMOV);
cpu->flags.pat=cpu_has(c, X86_FEATURE_PAT);
cpu->flags.pse_36=cpu_has(c, X86_FEATURE_PSE36);
cpu->flags.psn=cpu_has(c, X86_FEATURE_PN);
cpu->flags.clflsh=cpu_has(c, X86_FEATURE_CLFLSH);
cpu->flags.dts=cpu_has(c, X86_FEATURE_DTES);
cpu->flags.acpi=cpu_has(c, X86_FEATURE_ACPI);
cpu->flags.mmx=cpu_has(c, X86_FEATURE_MMX);
cpu->flags.fxsr=cpu_has(c, X86_FEATURE_FXSR);
cpu->flags.sse=cpu_has(c, X86_FEATURE_XMM);
cpu->flags.sse2=cpu_has(c, X86_FEATURE_XMM2);
cpu->flags.ss=cpu_has(c, X86_FEATURE_SELFSNOOP);
cpu->flags.htt=cpu_has(c, X86_FEATURE_HT);
cpu->flags.acc=cpu_has(c, X86_FEATURE_ACC);
cpu->flags.syscall=cpu_has(c, X86_FEATURE_SYSCALL);
cpu->flags.mp=cpu_has(c, X86_FEATURE_MP);
cpu->flags.nx=cpu_has(c, X86_FEATURE_NX);
cpu->flags.mmxext=cpu_has(c, X86_FEATURE_MMXEXT);
cpu->flags.lm=cpu_has(c, X86_FEATURE_LM);
cpu->flags.nowext=cpu_has(c, X86_FEATURE_3DNOWEXT);
cpu->flags.now=cpu_has(c, X86_FEATURE_3DNOW);
cpu->flags.smp = find_smp_config();
}

void set_generic_info(struct cpuinfo_x86 *c,s_cpu *cpu) {
	cpu->family=c->x86;
	cpu->vendor_id=c->x86_vendor;
	cpu->model_id=c->x86_model;
	cpu->stepping=c->x86_mask;
	strncpy(cpu->vendor,cpu_devs[c->x86_vendor]->c_vendor,sizeof(cpu->vendor));
	strncpy(cpu->model,c->x86_model_id,sizeof(cpu->model));
}

void detect_cpu(s_cpu *cpu)
{
         struct cpuinfo_x86 c;
	 c.x86_clflush_size = 32;
         c.x86_cache_size = -1;
         c.x86_vendor = X86_VENDOR_UNKNOWN;
         c.cpuid_level = -1;    /* CPUID not detected */
         c.x86_model = c.x86_mask = 0; /* So far unknown... */
	 c.x86_max_cores = 1;
	 memset(&c.x86_capability, 0, sizeof(c.x86_capability));
	 memset(&c.x86_vendor_id,0,sizeof(c.x86_vendor_id));
	 memset(&c.x86_model_id,0,sizeof(c.x86_model_id));

         if (!have_cpuid_p())
                 return;

	generic_identify(&c);
	set_generic_info(&c,cpu);
	set_cpu_flags(&c,cpu);
}