1 /*
2  * Copyright © 2008 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <err.h>
34 #include <assert.h>
35 #include <sys/ioctl.h>
36 #include <fcntl.h>
37 #include <sys/stat.h>
38 #include <sys/mman.h>
39 
40 #include "intel_gpu_tools.h"
41 
42 enum pch_type pch;
43 
44 struct pci_device *
intel_get_pci_device(void)45 intel_get_pci_device(void)
46 {
47 	struct pci_device *pci_dev;
48 	int error;
49 
50 	error = pci_system_init();
51 	if (error != 0) {
52 		fprintf(stderr, "Couldn't initialize PCI system: %s\n",
53 			strerror(error));
54 		exit(1);
55 	}
56 
57 	/* Grab the graphics card. Try the canonical slot first, then
58 	 * walk the entire PCI bus for a matching device. */
59 	pci_dev = pci_device_find_by_slot(0, 0, 2, 0);
60 	if (pci_dev == NULL || pci_dev->vendor_id != 0x8086) {
61 		struct pci_device_iterator *iter;
62 		struct pci_id_match match;
63 
64 		match.vendor_id = 0x8086; /* Intel */
65 		match.device_id = PCI_MATCH_ANY;
66 		match.subvendor_id = PCI_MATCH_ANY;
67 		match.subdevice_id = PCI_MATCH_ANY;
68 
69 		match.device_class = 0x3 << 16;
70 		match.device_class_mask = 0xff << 16;
71 
72 		match.match_data = 0;
73 
74 		iter = pci_id_match_iterator_create(&match);
75 		pci_dev = pci_device_next(iter);
76 		pci_iterator_destroy(iter);
77 	}
78 	if (pci_dev == NULL)
79 		errx(1, "Couldn't find graphics card");
80 
81 	error = pci_device_probe(pci_dev);
82 	if (error != 0) {
83 		fprintf(stderr, "Couldn't probe graphics card: %s\n",
84 			strerror(error));
85 		exit(1);
86 	}
87 
88 	if (pci_dev->vendor_id != 0x8086)
89 		errx(1, "Graphics card is non-intel");
90 
91 	return pci_dev;
92 }
93 
94 void
intel_check_pch(void)95 intel_check_pch(void)
96 {
97 	struct pci_device *pch_dev;
98 
99 	pch_dev = pci_device_find_by_slot(0, 0, 31, 0);
100 	if (pch_dev == NULL)
101 		return;
102 
103 	if (pch_dev->vendor_id != 0x8086)
104 		return;
105 
106 	switch (pch_dev->device_id & 0xff00) {
107 	case 0x3b00:
108 		pch = PCH_IBX;
109 		break;
110 	case 0x1c00:
111 	case 0x1e00:
112 		pch = PCH_CPT;
113 		break;
114 	case 0x8c00:
115 	case 0x9c00:
116 		pch = PCH_LPT;
117 		break;
118 	case 0xA100:
119 	case 0x9D00:
120 		pch = PCH_SPT;
121 		break;
122 	case 0xA200:
123 		pch = PCH_KBP;
124 		break;
125 	default:
126 		pch = PCH_NONE;
127 		return;
128 	}
129 }
130