blob: cb300d9b24395490042dc837794902498d5ad4cc [file] [log] [blame]
Peter Stugedad1e302008-11-22 17:13:36 +00001/*
2 * This file is part of msrtool.
3 *
4 * Copyright (c) 2008 Peter Stuge <peter@stuge.se>
Stefan Reinauer37f39352009-09-01 10:03:01 +00005 * Copyright (c) 2009 coresystems GmbH
Peter Stugedad1e302008-11-22 17:13:36 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Peter Stugedad1e302008-11-22 17:13:36 +000015 */
16
Peter Stuge0924dee2008-11-25 02:03:16 +000017#include <pci/pci.h>
18
Peter Stugedad1e302008-11-22 17:13:36 +000019#include "msrtool.h"
20
21static struct cpuid_t id;
22
23struct cpuid_t *cpuid(void) {
24 uint32_t outeax;
Anton Kochkovffbbecc2012-07-04 07:31:37 +040025 uint32_t outebx;
Stefan Reinauer37f39352009-09-01 10:03:01 +000026
Anton Kochkovffbbecc2012-07-04 07:31:37 +040027/* First, we need determine which vendor we have */
28#if defined(__DARWIN__) && !defined(__LP64__)
29 asm volatile (
30 "pushl %%ebx \n"
31 "cpuid \n"
32 "popl %%ebx \n"
33 : "=b" (outebx) : "a" (0) : "%ecx", "%edx"
34 );
35#else
36 asm ("cpuid" : "=b" (outebx) : "a" (0) : "%ecx", "%edx");
37#endif
38
39 id.vendor = (outebx == 0x756e6547) ? VENDOR_INTEL : VENDOR_AMD;
40
41/* Then, identificate CPU itself */
Stefan Reinauer37f39352009-09-01 10:03:01 +000042#if defined(__DARWIN__) && !defined(__LP64__)
43 asm volatile (
44 "pushl %%ebx \n"
45 "cpuid \n"
46 "popl %%ebx \n"
47 : "=a" (outeax) : "a" (1) : "%ecx", "%edx"
48 );
49#else
Peter Stugedad1e302008-11-22 17:13:36 +000050 asm ("cpuid" : "=a" (outeax) : "a" (1) : "%ebx", "%ecx", "%edx");
Stefan Reinauer37f39352009-09-01 10:03:01 +000051#endif
52
Peter Stugedad1e302008-11-22 17:13:36 +000053 id.stepping = outeax & 0xf;
54 outeax >>= 4;
55 id.model = outeax & 0xf;
56 outeax >>= 4;
57 id.family = outeax & 0xf;
58 outeax >>= 8;
59 id.ext_model = outeax & 0xf;
60 outeax >>= 4;
61 id.ext_family = outeax & 0xff;
Anton Kochkovffbbecc2012-07-04 07:31:37 +040062 if ((0xf == id.family) || ((VENDOR_INTEL == id.vendor)
63 && (0x6 == id.family))) {
Peter Stugedad1e302008-11-22 17:13:36 +000064 /* Intel says always do this, AMD says only for family f */
65 id.model |= (id.ext_model << 4);
66 id.family += id.ext_family;
67 }
Stefan Reinauer37f39352009-09-01 10:03:01 +000068 printf_verbose("CPU: family %x, model %x, stepping %x\n",
69 id.family, id.model, id.stepping);
70
Peter Stugedad1e302008-11-22 17:13:36 +000071 return &id;
72}
Peter Stuge0924dee2008-11-25 02:03:16 +000073
74struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device) {
75 struct pci_dev *temp;
76 struct pci_filter filter;
77
78 pci_filter_init(NULL, &filter);
79 filter.vendor = vendor;
80 filter.device = device;
81
82 for (temp = pacc->devices; temp; temp = temp->next)
83 if (pci_filter_match(&filter, temp))
84 return temp;
85
86 return NULL;
87}