Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of msrtool. |
| 3 | * |
| 4 | * Copyright (c) 2008 Peter Stuge <peter@stuge.se> |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 5 | * Copyright (c) 2009 coresystems GmbH |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 6 | * |
| 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 Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 17 | #include <pci/pci.h> |
| 18 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 19 | #include "msrtool.h" |
| 20 | |
| 21 | static struct cpuid_t id; |
| 22 | |
| 23 | struct cpuid_t *cpuid(void) { |
| 24 | uint32_t outeax; |
Anton Kochkov | ffbbecc | 2012-07-04 07:31:37 +0400 | [diff] [blame] | 25 | uint32_t outebx; |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 26 | |
Anton Kochkov | ffbbecc | 2012-07-04 07:31:37 +0400 | [diff] [blame] | 27 | /* First, we need determine which vendor we have */ |
| 28 | #if defined(__DARWIN__) && !defined(__LP64__) |
Elyes HAOUAS | 3baa7e7 | 2016-10-15 10:01:21 +0200 | [diff] [blame] | 29 | asm volatile ( |
| 30 | "pushl %%ebx \n" |
| 31 | "cpuid \n" |
| 32 | "popl %%ebx \n" |
| 33 | : "=b" (outebx) : "a" (0) : "%ecx", "%edx" |
| 34 | ); |
Anton Kochkov | ffbbecc | 2012-07-04 07:31:37 +0400 | [diff] [blame] | 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 Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 42 | #if defined(__DARWIN__) && !defined(__LP64__) |
Elyes HAOUAS | 3baa7e7 | 2016-10-15 10:01:21 +0200 | [diff] [blame] | 43 | asm volatile ( |
| 44 | "pushl %%ebx \n" |
| 45 | "cpuid \n" |
| 46 | "popl %%ebx \n" |
| 47 | : "=a" (outeax) : "a" (1) : "%ecx", "%edx" |
| 48 | ); |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 49 | #else |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 50 | asm ("cpuid" : "=a" (outeax) : "a" (1) : "%ebx", "%ecx", "%edx"); |
Stefan Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 53 | 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 Kochkov | ffbbecc | 2012-07-04 07:31:37 +0400 | [diff] [blame] | 62 | if ((0xf == id.family) || ((VENDOR_INTEL == id.vendor) |
| 63 | && (0x6 == id.family))) { |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 64 | /* 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 Reinauer | 37f3935 | 2009-09-01 10:03:01 +0000 | [diff] [blame] | 68 | printf_verbose("CPU: family %x, model %x, stepping %x\n", |
| 69 | id.family, id.model, id.stepping); |
| 70 | |
Peter Stuge | dad1e30 | 2008-11-22 17:13:36 +0000 | [diff] [blame] | 71 | return &id; |
| 72 | } |
Peter Stuge | 0924dee | 2008-11-25 02:03:16 +0000 | [diff] [blame] | 73 | |
| 74 | struct 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 | } |