blob: ab35fa70c167c75fa69948d2afbb51b525a08ed5 [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
Tomeu Vizoso644f3642016-02-18 14:23:25 +010041#include "drmtest.h"
Daniel Vetteraed95c32014-03-22 14:54:28 +010042#include "intel_chipset.h"
Daniel Vetter71ac5de2014-08-26 15:13:06 +020043#include "igt_core.h"
Chris Wilson95374222010-04-08 11:56:57 +010044
Daniel Vettera8e86542014-03-22 15:12:57 +010045/**
46 * SECTION:intel_chipset
47 * @short_description: Feature macros and chipset helpers
Damien Lespiau6ebd8c22015-06-26 14:28:41 +010048 * @title: Chipset
Thomas Woodf0381d12015-09-07 09:26:01 +010049 * @include: igt.h
Daniel Vettercd6d5a62014-03-22 19:35:40 +010050 *
Daniel Vettera8e86542014-03-22 15:12:57 +010051 * 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 */
Daniel Vetter266b26b2014-03-22 14:59:58 +010062enum pch_type intel_pch;
Zhenyu Wang2b40fc82010-04-15 22:57:02 +080063
Daniel Vettera8e86542014-03-22 15:12:57 +010064/**
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 */
Chris Wilson95374222010-04-08 11:56:57 +010072struct pci_device *
73intel_get_pci_device(void)
74{
75 struct pci_device *pci_dev;
Daniel Vetter1be3fd72012-01-09 23:42:19 +010076 int error;
Chris Wilson95374222010-04-08 11:56:57 +010077
Daniel Vetter1be3fd72012-01-09 23:42:19 +010078 error = pci_system_init();
Daniel Vetter71ac5de2014-08-26 15:13:06 +020079 igt_fail_on_f(error != 0,
80 "Couldn't initialize PCI system\n");
Chris Wilson95374222010-04-08 11:56:57 +010081
Chris Wilson50fdf322012-12-06 12:16:12 +000082 /* Grab the graphics card. Try the canonical slot first, then
83 * walk the entire PCI bus for a matching device. */
Chris Wilson95374222010-04-08 11:56:57 +010084 pci_dev = pci_device_find_by_slot(0, 0, 2, 0);
Chris Wilson50fdf322012-12-06 12:16:12 +000085 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 }
Daniel Vetterdcab1592016-10-11 11:31:38 +0200103 igt_require_f(pci_dev, "Couldn't find Intel graphics card\n");
Chris Wilson95374222010-04-08 11:56:57 +0100104
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
Chris Wilsoneaa7e612015-02-18 15:58:06 +0000115extern uint16_t __drm_device_id;
116
Daniel Vettera8e86542014-03-22 15:12:57 +0100117/**
118 * intel_get_drm_devid:
119 * @fd: open i915 drm file descriptor
120 *
121 * Queries the kernel for the pci device id corresponding to the drm file
122 * descriptor.
123 *
124 * Returns:
125 * The devid, exits the program on any failures.
126 */
Daniel Vetteraed95c32014-03-22 14:54:28 +0100127uint32_t
128intel_get_drm_devid(int fd)
129{
Chris Wilsonacca7242014-07-21 07:57:25 +0100130 const char *override;
Daniel Vetteraed95c32014-03-22 14:54:28 +0100131
Tomeu Vizoso644f3642016-02-18 14:23:25 +0100132 igt_assert(is_i915_device(fd));
Chris Wilsoneaa7e612015-02-18 15:58:06 +0000133 igt_assert(__drm_device_id);
134
Daniel Vetteraed95c32014-03-22 14:54:28 +0100135 override = getenv("INTEL_DEVID_OVERRIDE");
Chris Wilsoneaa7e612015-02-18 15:58:06 +0000136 if (override)
Chris Wilson5b93daf2015-02-18 16:06:15 +0000137 return strtol(override, NULL, 0);
Chris Wilsoneaa7e612015-02-18 15:58:06 +0000138 else
139 return __drm_device_id;
Daniel Vetteraed95c32014-03-22 14:54:28 +0100140}
141
Daniel Vettera8e86542014-03-22 15:12:57 +0100142/**
Daniel Vettera8e86542014-03-22 15:12:57 +0100143 * intel_check_pch:
144 *
145 * Detects the PCH chipset type of the running systems and fills in the results
Thomas Woodd01ebbd2015-06-29 16:47:14 +0100146 * into the global #intel_pch variable.
Daniel Vettera8e86542014-03-22 15:12:57 +0100147 */
Zhenyu Wang2b40fc82010-04-15 22:57:02 +0800148void
149intel_check_pch(void)
150{
151 struct pci_device *pch_dev;
152
153 pch_dev = pci_device_find_by_slot(0, 0, 31, 0);
154 if (pch_dev == NULL)
155 return;
156
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300157 if (pch_dev->vendor_id != 0x8086)
158 return;
159
160 switch (pch_dev->device_id & 0xff00) {
161 case 0x3b00:
Daniel Vetter266b26b2014-03-22 14:59:58 +0100162 intel_pch = PCH_IBX;
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300163 break;
164 case 0x1c00:
165 case 0x1e00:
Daniel Vetter266b26b2014-03-22 14:59:58 +0100166 intel_pch = PCH_CPT;
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300167 break;
168 case 0x8c00:
169 case 0x9c00:
Daniel Vetter266b26b2014-03-22 14:59:58 +0100170 intel_pch = PCH_LPT;
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300171 break;
172 default:
Daniel Vetter266b26b2014-03-22 14:59:58 +0100173 intel_pch = PCH_NONE;
Paulo Zanoni29abdb92013-03-01 13:30:46 -0300174 return;
175 }
Zhenyu Wang2b40fc82010-04-15 22:57:02 +0800176}