blob: 0828e444b7b0c91d9c7eb2d03e1a09d47db9ee7e [file] [log] [blame]
Chris Wilson95374222010-04-08 11:56:57 +01001/*
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>
Sateesh Kavurib39a74b2012-02-06 15:37:04 +053036#include <fcntl.h>
Chris Wilson95374222010-04-08 11:56:57 +010037#include <sys/stat.h>
38#include <sys/mman.h>
Daniel Vetteraed95c32014-03-22 14:54:28 +010039#include "i915_drm.h"
Chris Wilson95374222010-04-08 11:56:57 +010040
Daniel Vetteraed95c32014-03-22 14:54:28 +010041#include "intel_chipset.h"
Daniel Vetter71ac5de2014-08-26 15:13:06 +020042#include "igt_core.h"
Chris Wilson95374222010-04-08 11:56:57 +010043
Daniel Vettera8e86542014-03-22 15:12:57 +010044/**
45 * SECTION:intel_chipset
46 * @short_description: Feature macros and chipset helpers
47 * @title: intel chipset
Daniel Vetter7bb40942014-03-23 14:45:13 +010048 * @include: intel_chipset.h
Daniel Vettercd6d5a62014-03-22 19:35:40 +010049 *
Daniel Vettera8e86542014-03-22 15:12:57 +010050 * This library mostly provides feature macros which use raw pci device ids. It
51 * also provides a few more helper functions to handle pci devices, chipset
52 * detection and related issues.
53 */
54
55/**
56 * intel_pch:
57 *
58 * Global variable to keep track of the pch type. Can either be set manually or
59 * detected at runtime with intel_check_pch().
60 */
Daniel Vetter266b26b2014-03-22 14:59:58 +010061enum pch_type intel_pch;
Zhenyu Wang2b40fc82010-04-15 22:57:02 +080062
Daniel Vettera8e86542014-03-22 15:12:57 +010063/**
64 * intel_get_pci_device:
65 *
66 * Looks up the main graphics pci device using libpciaccess.
67 *
68 * Returns:
69 * The pci_device, exits the program on any failures.
70 */
Chris Wilson95374222010-04-08 11:56:57 +010071struct pci_device *
72intel_get_pci_device(void)
73{
74 struct pci_device *pci_dev;
Daniel Vetter1be3fd72012-01-09 23:42:19 +010075 int error;
Chris Wilson95374222010-04-08 11:56:57 +010076
Daniel Vetter1be3fd72012-01-09 23:42:19 +010077 error = pci_system_init();
Daniel Vetter71ac5de2014-08-26 15:13:06 +020078 igt_fail_on_f(error != 0,
79 "Couldn't initialize PCI system\n");
Chris Wilson95374222010-04-08 11:56:57 +010080
Chris Wilson50fdf322012-12-06 12:16:12 +000081 /* Grab the graphics card. Try the canonical slot first, then
82 * walk the entire PCI bus for a matching device. */
Chris Wilson95374222010-04-08 11:56:57 +010083 pci_dev = pci_device_find_by_slot(0, 0, 2, 0);
Chris Wilson50fdf322012-12-06 12:16:12 +000084 if (pci_dev == NULL || pci_dev->vendor_id != 0x8086) {
85 struct pci_device_iterator *iter;
86 struct pci_id_match match;
87
88 match.vendor_id = 0x8086; /* Intel */
89 match.device_id = PCI_MATCH_ANY;
90 match.subvendor_id = PCI_MATCH_ANY;
91 match.subdevice_id = PCI_MATCH_ANY;
92
93 match.device_class = 0x3 << 16;
94 match.device_class_mask = 0xff << 16;
95
96 match.match_data = 0;
97
98 iter = pci_id_match_iterator_create(&match);
99 pci_dev = pci_device_next(iter);
100 pci_iterator_destroy(iter);
101 }
Chris Wilson95374222010-04-08 11:56:57 +0100102 if (pci_dev == NULL)
103 errx(1, "Couldn't find graphics card");
104
Daniel Vetter1be3fd72012-01-09 23:42:19 +0100105 error = pci_device_probe(pci_dev);
Daniel Vetter71ac5de2014-08-26 15:13:06 +0200106 igt_fail_on_f(error != 0,
107 "Couldn't probe graphics card\n");
Chris Wilson95374222010-04-08 11:56:57 +0100108
109 if (pci_dev->vendor_id != 0x8086)
110 errx(1, "Graphics card is non-intel");
111
112 return pci_dev;
113}
Zhenyu Wang2b40fc82010-04-15 22:57:02 +0800114
Daniel Vettera8e86542014-03-22 15:12:57 +0100115/**
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 */
Daniel Vetteraed95c32014-03-22 14:54:28 +0100125uint32_t
126intel_get_drm_devid(int fd)
127{
Chris Wilsonacca7242014-07-21 07:57:25 +0100128 uint32_t devid = 0;
129 const char *override;
Daniel Vetteraed95c32014-03-22 14:54:28 +0100130
131 override = getenv("INTEL_DEVID_OVERRIDE");
132 if (override) {
133 devid = strtod(override, NULL);
134 } else {
Chris Wilsonacca7242014-07-21 07:57:25 +0100135 struct drm_i915_getparam gp;
136 int ret;
137
138 memset(&gp, 0, sizeof(gp));
Daniel Vetteraed95c32014-03-22 14:54:28 +0100139 gp.param = I915_PARAM_CHIPSET_ID;
140 gp.value = (int *)&devid;
141
142 ret = ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp));
Daniel Vetter71ac5de2014-08-26 15:13:06 +0200143 igt_assert(ret == 0);
Chris Wilsonb918a3b2014-04-25 07:40:34 +0100144 errno = 0;
Daniel Vetteraed95c32014-03-22 14:54:28 +0100145 }
146
147 return devid;
148}
149
Daniel Vettera8e86542014-03-22 15:12:57 +0100150/**
151 * intel_gen:
152 * @devid: pci device id
153 *
154 * Computes the Intel GFX generation for the give device id.
155 *
156 * Returns:
157 * The GFX generation on successful lookup, -1 on failure.
158 */
Daniel Vetteraed95c32014-03-22 14:54:28 +0100159int intel_gen(uint32_t devid)
160{
161 if (IS_GEN2(devid))
162 return 2;
163 if (IS_GEN3(devid))
164 return 3;
165 if (IS_GEN4(devid))
166 return 4;
167 if (IS_GEN5(devid))
168 return 5;
169 if (IS_GEN6(devid))
170 return 6;
171 if (IS_GEN7(devid))
172 return 7;
173 if (IS_GEN8(devid))
174 return 8;
175
176 return -1;
177}
178
Daniel Vettera8e86542014-03-22 15:12:57 +0100179/**
180 * intel_check_pch:
181 *
182 * Detects the PCH chipset type of the running systems and fills in the results
183 * into the global #intel_pch varaible.
184 */
Zhenyu Wang2b40fc82010-04-15 22:57:02 +0800185void
186intel_check_pch(void)
187{
188 struct pci_device *pch_dev;
189
190 pch_dev = pci_device_find_by_slot(0, 0, 31, 0);
191 if (pch_dev == NULL)
192 return;
193
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300194 if (pch_dev->vendor_id != 0x8086)
195 return;
196
197 switch (pch_dev->device_id & 0xff00) {
198 case 0x3b00:
Daniel Vetter266b26b2014-03-22 14:59:58 +0100199 intel_pch = PCH_IBX;
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300200 break;
201 case 0x1c00:
202 case 0x1e00:
Daniel Vetter266b26b2014-03-22 14:59:58 +0100203 intel_pch = PCH_CPT;
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300204 break;
205 case 0x8c00:
206 case 0x9c00:
Daniel Vetter266b26b2014-03-22 14:59:58 +0100207 intel_pch = PCH_LPT;
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300208 break;
209 default:
Daniel Vetter266b26b2014-03-22 14:59:58 +0100210 intel_pch = PCH_NONE;
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300211 return;
212 }
Zhenyu Wang2b40fc82010-04-15 22:57:02 +0800213}