summaryrefslogtreecommitdiff
path: root/util/msrtool/sys.c
blob: f32246ed3492292cc8926bbf64d3ab121b6320b9 (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
/* This file is part of msrtool. */
/* SPDX-License-Identifier: GPL-2.0-only */

#include <pci/pci.h>

#include "msrtool.h"

static struct cpuid_t id;

struct cpuid_t *cpuid(void) {
	uint32_t outeax;
	uint32_t outebx;

/* First, we need determine which vendor we have */
#if defined(__DARWIN__) && !defined(__LP64__)
	asm volatile (
		"pushl %%ebx    \n"
		"cpuid          \n"
		"popl %%ebx     \n"
		: "=b" (outebx) : "a" (0) : "%ecx", "%edx"
	);
#else
	asm ("cpuid" : "=b" (outebx) : "a" (0) : "%ecx", "%edx");
#endif

	id.vendor = outebx;

/* Then, identificate CPU itself */
#if defined(__DARWIN__) && !defined(__LP64__)
	asm volatile (
		"pushl %%ebx    \n"
		"cpuid          \n"
		"popl %%ebx     \n"
		: "=a" (outeax) : "a" (1) : "%ecx", "%edx"
	);
#else
	asm ("cpuid" : "=a" (outeax) : "a" (1) : "%ebx", "%ecx", "%edx");
#endif

	id.stepping = outeax & 0xf;
	outeax >>= 4;
	id.model = outeax & 0xf;
	outeax >>= 4;
	id.family = outeax & 0xf;
	outeax >>= 8;
	id.ext_model = outeax & 0xf;
	outeax >>= 4;
	id.ext_family = outeax & 0xff;
	if ((0xf == id.family) || ((VENDOR_INTEL == id.vendor)
			&& (0x6 == id.family))) {
		/* Intel says always do this, AMD says only for family f */
		id.model |= (id.ext_model << 4);
		id.family += id.ext_family;
	}
	printf_verbose("CPU: family %x, model %x, stepping %x\n",
			id.family, id.model, id.stepping);

	return &id;
}

struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device) {
	struct pci_dev *temp;
	struct pci_filter filter;

	pci_filter_init(NULL, &filter);
	filter.vendor = vendor;
	filter.device = device;

	for (temp = pacc->devices; temp; temp = temp->next)
		if (pci_filter_match(&filter, temp))
			return temp;

	return NULL;
}