1 /*
2  * Copyright © 2007 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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <pciaccess.h>
33 #include <err.h>
34 #include "intel_chipset.h"
35 #include "intel_io.h"
36 #include "intel_reg.h"
37 
38 static void
print_clock(const char * name,int clock)39 print_clock(const char *name, int clock) {
40 	if (clock == -1)
41 		printf("%s clock: unknown", name);
42 	else
43 		printf("%s clock: %d Mhz", name, clock);
44 }
45 
46 static int
print_clock_info(struct pci_device * pci_dev)47 print_clock_info(struct pci_device *pci_dev)
48 {
49 	uint32_t devid = pci_dev->device_id;
50 	uint16_t gcfgc;
51 
52 	if (IS_GM45(devid)) {
53 		int core_clock = -1;
54 
55 		pci_device_cfg_read_u16(pci_dev, &gcfgc, I915_GCFGC);
56 
57 		switch (gcfgc & 0xf) {
58 		case 8:
59 			core_clock = 266;
60 			break;
61 		case 9:
62 			core_clock = 320;
63 			break;
64 		case 11:
65 			core_clock = 400;
66 			break;
67 		case 13:
68 			core_clock = 533;
69 			break;
70 		}
71 		print_clock("core", core_clock);
72 	} else if (IS_965(devid) && IS_MOBILE(devid)) {
73 		int render_clock = -1, sampler_clock = -1;
74 
75 		pci_device_cfg_read_u16(pci_dev, &gcfgc, I915_GCFGC);
76 
77 		switch (gcfgc & 0xf) {
78 		case 2:
79 			render_clock = 250; sampler_clock = 267;
80 			break;
81 		case 3:
82 			render_clock = 320; sampler_clock = 333;
83 			break;
84 		case 4:
85 			render_clock = 400; sampler_clock = 444;
86 			break;
87 		case 5:
88 			render_clock = 500; sampler_clock = 533;
89 			break;
90 		}
91 
92 		print_clock("render", render_clock);
93 		printf("  ");
94 		print_clock("sampler", sampler_clock);
95 	} else if (IS_945(devid) && IS_MOBILE(devid)) {
96 		int render_clock = -1, display_clock = -1;
97 
98 		pci_device_cfg_read_u16(pci_dev, &gcfgc, I915_GCFGC);
99 
100 		switch (gcfgc & 0x7) {
101 		case 0:
102 			render_clock = 166;
103 			break;
104 		case 1:
105 			render_clock = 200;
106 			break;
107 		case 3:
108 			render_clock = 250;
109 			break;
110 		case 5:
111 			render_clock = 400;
112 			break;
113 		}
114 
115 		switch (gcfgc & 0x70) {
116 		case 0:
117 			display_clock = 200;
118 			break;
119 		case 4:
120 			display_clock = 320;
121 			break;
122 		}
123 		if (gcfgc & (1 << 7))
124 		    display_clock = 133;
125 
126 		print_clock("render", render_clock);
127 		printf("  ");
128 		print_clock("display", display_clock);
129 	} else if (IS_915(devid) && IS_MOBILE(devid)) {
130 		int render_clock = -1, display_clock = -1;
131 
132 		pci_device_cfg_read_u16(pci_dev, &gcfgc, I915_GCFGC);
133 
134 		switch (gcfgc & 0x7) {
135 		case 0:
136 			render_clock = 160;
137 			break;
138 		case 1:
139 			render_clock = 190;
140 			break;
141 		case 4:
142 			render_clock = 333;
143 			break;
144 		}
145 		if (gcfgc & (1 << 13))
146 		    render_clock = 133;
147 
148 		switch (gcfgc & 0x70) {
149 		case 0:
150 			display_clock = 190;
151 			break;
152 		case 4:
153 			display_clock = 333;
154 			break;
155 		}
156 		if (gcfgc & (1 << 7))
157 		    display_clock = 133;
158 
159 		print_clock("render", render_clock);
160 		printf("  ");
161 		print_clock("display", display_clock);
162 	}
163 
164 	printf("\n");
165 	return -1;
166 }
167 
main(int argc,char ** argv)168 int main(int argc, char **argv)
169 {
170 	struct pci_device *dev, *bridge;
171 	int error;
172 	uint8_t stepping;
173 	const char *step_desc = "??";
174 
175 	error = pci_system_init();
176 	if (error != 0) {
177 		fprintf(stderr, "Couldn't initialize PCI system: %s\n",
178 			strerror(error));
179 		exit(1);
180 	}
181 
182 	/* Grab the graphics card */
183 	dev = pci_device_find_by_slot(0, 0, 2, 0);
184 	if (dev == NULL)
185 		errx(1, "Couldn't find graphics card");
186 
187 	error = pci_device_probe(dev);
188 	if (error != 0) {
189 		fprintf(stderr, "Couldn't probe graphics card: %s\n",
190 			strerror(error));
191 		exit(1);
192 	}
193 
194 	if (dev->vendor_id != 0x8086)
195 		errx(1, "Graphics card is non-intel");
196 
197 	bridge = pci_device_find_by_slot(0, 0, 0, 0);
198 	if (dev == NULL)
199 		errx(1, "Couldn't bridge");
200 
201 	error = pci_device_cfg_read_u8(bridge, &stepping, 8);
202 	if (error != 0) {
203 		fprintf(stderr, "Couldn't read revision ID: %s\n",
204 			strerror(error));
205 		exit(1);
206 	}
207 
208 	switch (dev->device_id) {
209 	case PCI_CHIP_I915_G:
210 		if (stepping < 0x04)
211 			step_desc = "<B1";
212 		else if (stepping == 0x04)
213 			step_desc = "B1";
214 		else if (stepping == 0x0e)
215 			step_desc = "C2";
216 		else if (stepping > 0x0e)
217 			step_desc = ">C2";
218 		else
219 			step_desc = ">B1 <C2";
220 		break;
221 	case PCI_CHIP_I915_GM:
222 		if (stepping < 0x03)
223 			step_desc = "<B1";
224 		else if (stepping == 0x03)
225 			step_desc = "B1/C0";
226 		else if (stepping == 0x04)
227 			step_desc = "C1/C2";
228 		else
229 			step_desc = ">C2";
230 		break;
231 	case PCI_CHIP_I945_GM:
232 		if (stepping < 0x03)
233 			step_desc = "<A3";
234 		else if (stepping == 0x03)
235 			step_desc = "A3";
236 		else
237 			step_desc = ">A3";
238 		break;
239 	case PCI_CHIP_I965_G:
240 	case PCI_CHIP_I965_Q:
241 		if (stepping < 0x02)
242 			step_desc = "<C1";
243 		else if (stepping == 0x02)
244 			step_desc = "C1/C2";
245 		else
246 			step_desc = ">C2";
247 		break;
248 	case PCI_CHIP_I965_GM:
249 		if (stepping < 0x03)
250 			step_desc = "<C0";
251 		else if (stepping == 0x03)
252 			step_desc = "C0";
253 		else
254 			step_desc = ">C0";
255 		break;
256 	case PCI_CHIP_I965_G_1:
257 		if (stepping < 0x03)
258 			step_desc = "<E0";
259 		else if (stepping == 0x03)
260 			step_desc = "E0";
261 		else
262 			step_desc = ">E0";
263 		break;
264 	case PCI_CHIP_GM45_GM:
265 		if (stepping < 0x07)
266 			step_desc = "<B3";
267 		else if (stepping == 0x03)
268 			step_desc = "B3";
269 		else
270 			step_desc = ">B3";
271 		break;
272 	case PCI_CHIP_G45_G:
273 	case PCI_CHIP_Q45_G:
274 	case PCI_CHIP_G41_G:
275 		if (stepping < 0x02)
276 			step_desc = "<A2";
277 		else if (stepping == 0x02)
278 			step_desc = "A2";
279 		else if (stepping == 0x03)
280 			step_desc = "A3";
281 		else
282 			step_desc = ">A3";
283 		break;
284 	}
285 
286 	printf("Vendor: 0x%04x, Device: 0x%04x, Revision: 0x%02x (%s)\n",
287 	       dev->vendor_id,
288 	       dev->device_id,
289 	       stepping,
290 	       step_desc);
291 
292 	print_clock_info(dev);
293 
294 	return 0;
295 }
296