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 #include "i915_drm.h"
40
41 #include "drmtest.h"
42 #include "intel_chipset.h"
43 #include "igt_core.h"
44
45 /**
46 * SECTION:intel_chipset
47 * @short_description: Feature macros and chipset helpers
48 * @title: Chipset
49 * @include: igt.h
50 *
51 * This library mostly provides feature macros which use raw pci device ids. It
52 * also provides a few more helper functions to handle pci devices, chipset
53 * detection and related issues.
54 */
55
56 /**
57 * intel_pch:
58 *
59 * Global variable to keep track of the pch type. Can either be set manually or
60 * detected at runtime with intel_check_pch().
61 */
62 enum pch_type intel_pch;
63
64 /**
65 * intel_get_pci_device:
66 *
67 * Looks up the main graphics pci device using libpciaccess.
68 *
69 * Returns:
70 * The pci_device, exits the program on any failures.
71 */
72 struct pci_device *
intel_get_pci_device(void)73 intel_get_pci_device(void)
74 {
75 struct pci_device *pci_dev;
76 int error;
77
78 error = pci_system_init();
79 igt_fail_on_f(error != 0,
80 "Couldn't initialize PCI system\n");
81
82 /* Grab the graphics card. Try the canonical slot first, then
83 * walk the entire PCI bus for a matching device. */
84 pci_dev = pci_device_find_by_slot(0, 0, 2, 0);
85 if (pci_dev == NULL || pci_dev->vendor_id != 0x8086) {
86 struct pci_device_iterator *iter;
87 struct pci_id_match match;
88
89 match.vendor_id = 0x8086; /* Intel */
90 match.device_id = PCI_MATCH_ANY;
91 match.subvendor_id = PCI_MATCH_ANY;
92 match.subdevice_id = PCI_MATCH_ANY;
93
94 match.device_class = 0x3 << 16;
95 match.device_class_mask = 0xff << 16;
96
97 match.match_data = 0;
98
99 iter = pci_id_match_iterator_create(&match);
100 pci_dev = pci_device_next(iter);
101 pci_iterator_destroy(iter);
102 }
103 igt_require_f(pci_dev, "Couldn't find Intel graphics card\n");
104
105 error = pci_device_probe(pci_dev);
106 igt_fail_on_f(error != 0,
107 "Couldn't probe graphics card\n");
108
109 if (pci_dev->vendor_id != 0x8086)
110 errx(1, "Graphics card is non-intel");
111
112 return pci_dev;
113 }
114
115 /**
116 * intel_get_drm_devid:
117 * @fd: open i915 drm file descriptor
118 *
119 * Queries the kernel for the pci device id corresponding to the drm file
120 * descriptor.
121 *
122 * Returns:
123 * The devid, exits the program on any failures.
124 */
125 uint32_t
intel_get_drm_devid(int fd)126 intel_get_drm_devid(int fd)
127 {
128 struct drm_i915_getparam gp;
129 const char *override;
130 int devid = 0;
131
132 igt_assert(is_i915_device(fd));
133
134 override = getenv("INTEL_DEVID_OVERRIDE");
135 if (override)
136 return strtol(override, NULL, 0);
137
138 memset(&gp, 0, sizeof(gp));
139 gp.param = I915_PARAM_CHIPSET_ID;
140 gp.value = &devid;
141 ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp));
142
143 return devid;
144 }
145
146 /**
147 * intel_check_pch:
148 *
149 * Detects the PCH chipset type of the running systems and fills in the results
150 * into the global #intel_pch variable.
151 */
152 void
intel_check_pch(void)153 intel_check_pch(void)
154 {
155 struct pci_device *pch_dev;
156
157 pch_dev = pci_device_find_by_slot(0, 0, 31, 0);
158 if (pch_dev == NULL)
159 return;
160
161 if (pch_dev->vendor_id != 0x8086)
162 return;
163
164 switch (pch_dev->device_id & 0xff00) {
165 case 0x3b00:
166 intel_pch = PCH_IBX;
167 break;
168 case 0x1c00:
169 case 0x1e00:
170 intel_pch = PCH_CPT;
171 break;
172 case 0x8c00:
173 case 0x9c00:
174 intel_pch = PCH_LPT;
175 break;
176 default:
177 intel_pch = PCH_NONE;
178 return;
179 }
180 }
181