blob: 7300abf4760cf5d9d18922c1735ad23c0563f6af [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Peter Stuge0924dee2008-11-25 02:03:16 +000021#include <pci/pci.h>
22
Peter Stugedad1e302008-11-22 17:13:36 +000023#include "msrtool.h"
24
25static struct cpuid_t id;
26
27struct cpuid_t *cpuid(void) {
28 uint32_t outeax;
Anton Kochkovffbbecc2012-07-04 07:31:37 +040029 uint32_t outebx;
Stefan Reinauer37f39352009-09-01 10:03:01 +000030
Anton Kochkovffbbecc2012-07-04 07:31:37 +040031/* First, we need determine which vendor we have */
32#if defined(__DARWIN__) && !defined(__LP64__)
33 asm volatile (
34 "pushl %%ebx \n"
35 "cpuid \n"
36 "popl %%ebx \n"
37 : "=b" (outebx) : "a" (0) : "%ecx", "%edx"
38 );
39#else
40 asm ("cpuid" : "=b" (outebx) : "a" (0) : "%ecx", "%edx");
41#endif
42
43 id.vendor = (outebx == 0x756e6547) ? VENDOR_INTEL : VENDOR_AMD;
44
45/* Then, identificate CPU itself */
Stefan Reinauer37f39352009-09-01 10:03:01 +000046#if defined(__DARWIN__) && !defined(__LP64__)
47 asm volatile (
48 "pushl %%ebx \n"
49 "cpuid \n"
50 "popl %%ebx \n"
51 : "=a" (outeax) : "a" (1) : "%ecx", "%edx"
52 );
53#else
Peter Stugedad1e302008-11-22 17:13:36 +000054 asm ("cpuid" : "=a" (outeax) : "a" (1) : "%ebx", "%ecx", "%edx");
Stefan Reinauer37f39352009-09-01 10:03:01 +000055#endif
56
Peter Stugedad1e302008-11-22 17:13:36 +000057 id.stepping = outeax & 0xf;
58 outeax >>= 4;
59 id.model = outeax & 0xf;
60 outeax >>= 4;
61 id.family = outeax & 0xf;
62 outeax >>= 8;
63 id.ext_model = outeax & 0xf;
64 outeax >>= 4;
65 id.ext_family = outeax & 0xff;
Anton Kochkovffbbecc2012-07-04 07:31:37 +040066 if ((0xf == id.family) || ((VENDOR_INTEL == id.vendor)
67 && (0x6 == id.family))) {
Peter Stugedad1e302008-11-22 17:13:36 +000068 /* Intel says always do this, AMD says only for family f */
69 id.model |= (id.ext_model << 4);
70 id.family += id.ext_family;
71 }
Stefan Reinauer37f39352009-09-01 10:03:01 +000072 printf_verbose("CPU: family %x, model %x, stepping %x\n",
73 id.family, id.model, id.stepping);
74
Peter Stugedad1e302008-11-22 17:13:36 +000075 return &id;
76}
Peter Stuge0924dee2008-11-25 02:03:16 +000077
78struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device) {
79 struct pci_dev *temp;
80 struct pci_filter filter;
81
82 pci_filter_init(NULL, &filter);
83 filter.vendor = vendor;
84 filter.device = device;
85
86 for (temp = pacc->devices; temp; temp = temp->next)
87 if (pci_filter_match(&filter, temp))
88 return temp;
89
90 return NULL;
91}