]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/i915/i915_dma.c
drm/i915: refactor stepping info retrieval
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / i915 / i915_dma.c
CommitLineData
1da177e4
LT
1/* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
2 */
0d6aa60b 3/*
1da177e4
LT
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
bc54fd1a
DA
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
0d6aa60b 27 */
1da177e4 28
a70491cc
JP
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
760285e7
DH
31#include <drm/drmP.h>
32#include <drm/drm_crtc_helper.h>
33#include <drm/drm_fb_helper.h>
4f03b1fc 34#include <drm/drm_legacy.h>
79e53945 35#include "intel_drv.h"
760285e7 36#include <drm/i915_drm.h>
1da177e4 37#include "i915_drv.h"
e21fd552 38#include "i915_vgpu.h"
1c5d22f7 39#include "i915_trace.h"
dcdb1674 40#include <linux/pci.h>
a4de0526
DV
41#include <linux/console.h>
42#include <linux/vt.h>
28d52043 43#include <linux/vgaarb.h>
c4804411
ZW
44#include <linux/acpi.h>
45#include <linux/pnp.h>
6a9ee8af 46#include <linux/vga_switcheroo.h>
5a0e3ad6 47#include <linux/slab.h>
44834a67 48#include <acpi/video.h>
8a187455
PZ
49#include <linux/pm.h>
50#include <linux/pm_runtime.h>
4bdc7293 51#include <linux/oom.h>
1da177e4 52
1da177e4 53
c153f45f
EA
54static int i915_getparam(struct drm_device *dev, void *data,
55 struct drm_file *file_priv)
1da177e4 56{
4c8a4be9 57 struct drm_i915_private *dev_priv = dev->dev_private;
c153f45f 58 drm_i915_getparam_t *param = data;
1da177e4
LT
59 int value;
60
c153f45f 61 switch (param->param) {
1da177e4 62 case I915_PARAM_IRQ_ACTIVE:
1da177e4 63 case I915_PARAM_ALLOW_BATCHBUFFER:
0d6aa60b 64 case I915_PARAM_LAST_DISPATCH:
ac883c84 65 /* Reject all old ums/dri params. */
5c6c6003 66 return -ENODEV;
ed4c9c4a 67 case I915_PARAM_CHIPSET_ID:
ffbab09b 68 value = dev->pdev->device;
ed4c9c4a 69 break;
27cd4461
NR
70 case I915_PARAM_REVISION:
71 value = dev->pdev->revision;
72 break;
673a394b 73 case I915_PARAM_HAS_GEM:
2e895b17 74 value = 1;
673a394b 75 break;
0f973f27
JB
76 case I915_PARAM_NUM_FENCES_AVAIL:
77 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
78 break;
02e792fb
DV
79 case I915_PARAM_HAS_OVERLAY:
80 value = dev_priv->overlay ? 1 : 0;
81 break;
e9560f7c
JB
82 case I915_PARAM_HAS_PAGEFLIPPING:
83 value = 1;
84 break;
76446cac
JB
85 case I915_PARAM_HAS_EXECBUF2:
86 /* depends on GEM */
2e895b17 87 value = 1;
76446cac 88 break;
e3a815fc 89 case I915_PARAM_HAS_BSD:
edc912f5 90 value = intel_ring_initialized(&dev_priv->ring[VCS]);
e3a815fc 91 break;
549f7365 92 case I915_PARAM_HAS_BLT:
edc912f5 93 value = intel_ring_initialized(&dev_priv->ring[BCS]);
549f7365 94 break;
a1f2cc73
XH
95 case I915_PARAM_HAS_VEBOX:
96 value = intel_ring_initialized(&dev_priv->ring[VECS]);
97 break;
08e16dc8
ZG
98 case I915_PARAM_HAS_BSD2:
99 value = intel_ring_initialized(&dev_priv->ring[VCS2]);
100 break;
a00b10c3
CW
101 case I915_PARAM_HAS_RELAXED_FENCING:
102 value = 1;
103 break;
bbf0c6b3
DV
104 case I915_PARAM_HAS_COHERENT_RINGS:
105 value = 1;
106 break;
72bfa19c
CW
107 case I915_PARAM_HAS_EXEC_CONSTANTS:
108 value = INTEL_INFO(dev)->gen >= 4;
109 break;
271d81b8
CW
110 case I915_PARAM_HAS_RELAXED_DELTA:
111 value = 1;
112 break;
ae662d31
EA
113 case I915_PARAM_HAS_GEN7_SOL_RESET:
114 value = 1;
115 break;
3d29b842
ED
116 case I915_PARAM_HAS_LLC:
117 value = HAS_LLC(dev);
118 break;
651d794f
CW
119 case I915_PARAM_HAS_WT:
120 value = HAS_WT(dev);
121 break;
777ee96f 122 case I915_PARAM_HAS_ALIASING_PPGTT:
896ab1a5 123 value = USES_PPGTT(dev);
777ee96f 124 break;
172cf15d
BW
125 case I915_PARAM_HAS_WAIT_TIMEOUT:
126 value = 1;
127 break;
2fedbff9
CW
128 case I915_PARAM_HAS_SEMAPHORES:
129 value = i915_semaphore_is_enabled(dev);
130 break;
ec6f1bb9
DA
131 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
132 value = 1;
133 break;
d7d4eedd
CW
134 case I915_PARAM_HAS_SECURE_BATCHES:
135 value = capable(CAP_SYS_ADMIN);
136 break;
b45305fc
DV
137 case I915_PARAM_HAS_PINNED_BATCHES:
138 value = 1;
139 break;
ed5982e6
DV
140 case I915_PARAM_HAS_EXEC_NO_RELOC:
141 value = 1;
142 break;
eef90ccb
CW
143 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
144 value = 1;
145 break;
d728c8ef
BV
146 case I915_PARAM_CMD_PARSER_VERSION:
147 value = i915_cmd_parser_get_version();
148 break;
6a2c4232
CW
149 case I915_PARAM_HAS_COHERENT_PHYS_GTT:
150 value = 1;
1816f923
AG
151 break;
152 case I915_PARAM_MMAP_VERSION:
153 value = 1;
6a2c4232 154 break;
a1559ffe
JM
155 case I915_PARAM_SUBSLICE_TOTAL:
156 value = INTEL_INFO(dev)->subslice_total;
157 if (!value)
158 return -ENODEV;
159 break;
160 case I915_PARAM_EU_TOTAL:
161 value = INTEL_INFO(dev)->eu_total;
162 if (!value)
163 return -ENODEV;
164 break;
49e4d842
CW
165 case I915_PARAM_HAS_GPU_RESET:
166 value = i915.enable_hangcheck &&
49e4d842
CW
167 intel_has_gpu_reset(dev);
168 break;
a9ed33ca
AJ
169 case I915_PARAM_HAS_RESOURCE_STREAMER:
170 value = HAS_RESOURCE_STREAMER(dev);
171 break;
1da177e4 172 default:
e29c32da 173 DRM_DEBUG("Unknown parameter %d\n", param->param);
20caafa6 174 return -EINVAL;
1da177e4
LT
175 }
176
1d6ac185
DV
177 if (copy_to_user(param->value, &value, sizeof(int))) {
178 DRM_ERROR("copy_to_user failed\n");
20caafa6 179 return -EFAULT;
1da177e4
LT
180 }
181
182 return 0;
183}
184
c153f45f
EA
185static int i915_setparam(struct drm_device *dev, void *data,
186 struct drm_file *file_priv)
1da177e4 187{
4c8a4be9 188 struct drm_i915_private *dev_priv = dev->dev_private;
c153f45f 189 drm_i915_setparam_t *param = data;
1da177e4 190
c153f45f 191 switch (param->param) {
1da177e4 192 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
1da177e4 193 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
1da177e4 194 case I915_SETPARAM_ALLOW_BATCHBUFFER:
ac883c84 195 /* Reject all old ums/dri params. */
5c6c6003
CW
196 return -ENODEV;
197
0f973f27
JB
198 case I915_SETPARAM_NUM_USED_FENCES:
199 if (param->value > dev_priv->num_fence_regs ||
200 param->value < 0)
201 return -EINVAL;
202 /* Userspace can use first N regs */
203 dev_priv->fence_reg_start = param->value;
204 break;
1da177e4 205 default:
8a4c47f3 206 DRM_DEBUG_DRIVER("unknown parameter %d\n",
be25ed9c 207 param->param);
20caafa6 208 return -EINVAL;
1da177e4
LT
209 }
210
211 return 0;
212}
213
ec2a4c3f
DA
214static int i915_get_bridge_dev(struct drm_device *dev)
215{
216 struct drm_i915_private *dev_priv = dev->dev_private;
217
0206e353 218 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
ec2a4c3f
DA
219 if (!dev_priv->bridge_dev) {
220 DRM_ERROR("bridge device not found\n");
221 return -1;
222 }
223 return 0;
224}
225
c4804411
ZW
226#define MCHBAR_I915 0x44
227#define MCHBAR_I965 0x48
228#define MCHBAR_SIZE (4*4096)
229
230#define DEVEN_REG 0x54
231#define DEVEN_MCHBAR_EN (1 << 28)
232
233/* Allocate space for the MCH regs if needed, return nonzero on error */
234static int
235intel_alloc_mchbar_resource(struct drm_device *dev)
236{
4c8a4be9 237 struct drm_i915_private *dev_priv = dev->dev_private;
a6c45cf0 238 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
c4804411
ZW
239 u32 temp_lo, temp_hi = 0;
240 u64 mchbar_addr;
a25c25c2 241 int ret;
c4804411 242
a6c45cf0 243 if (INTEL_INFO(dev)->gen >= 4)
c4804411
ZW
244 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
245 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
246 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
247
248 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
249#ifdef CONFIG_PNP
250 if (mchbar_addr &&
a25c25c2
CW
251 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
252 return 0;
c4804411
ZW
253#endif
254
255 /* Get some space for it */
a25c25c2
CW
256 dev_priv->mch_res.name = "i915 MCHBAR";
257 dev_priv->mch_res.flags = IORESOURCE_MEM;
258 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
259 &dev_priv->mch_res,
c4804411
ZW
260 MCHBAR_SIZE, MCHBAR_SIZE,
261 PCIBIOS_MIN_MEM,
a25c25c2 262 0, pcibios_align_resource,
c4804411
ZW
263 dev_priv->bridge_dev);
264 if (ret) {
265 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
266 dev_priv->mch_res.start = 0;
a25c25c2 267 return ret;
c4804411
ZW
268 }
269
a6c45cf0 270 if (INTEL_INFO(dev)->gen >= 4)
c4804411
ZW
271 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
272 upper_32_bits(dev_priv->mch_res.start));
273
274 pci_write_config_dword(dev_priv->bridge_dev, reg,
275 lower_32_bits(dev_priv->mch_res.start));
a25c25c2 276 return 0;
c4804411
ZW
277}
278
279/* Setup MCHBAR if possible, return true if we should disable it again */
280static void
281intel_setup_mchbar(struct drm_device *dev)
282{
4c8a4be9 283 struct drm_i915_private *dev_priv = dev->dev_private;
a6c45cf0 284 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
c4804411
ZW
285 u32 temp;
286 bool enabled;
287
11ea8b7d
JB
288 if (IS_VALLEYVIEW(dev))
289 return;
290
c4804411
ZW
291 dev_priv->mchbar_need_disable = false;
292
293 if (IS_I915G(dev) || IS_I915GM(dev)) {
294 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
295 enabled = !!(temp & DEVEN_MCHBAR_EN);
296 } else {
297 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
298 enabled = temp & 1;
299 }
300
301 /* If it's already enabled, don't have to do anything */
302 if (enabled)
303 return;
304
305 if (intel_alloc_mchbar_resource(dev))
306 return;
307
308 dev_priv->mchbar_need_disable = true;
309
310 /* Space is allocated or reserved, so enable it. */
311 if (IS_I915G(dev) || IS_I915GM(dev)) {
312 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
313 temp | DEVEN_MCHBAR_EN);
314 } else {
315 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
316 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
317 }
318}
319
320static void
321intel_teardown_mchbar(struct drm_device *dev)
322{
4c8a4be9 323 struct drm_i915_private *dev_priv = dev->dev_private;
a6c45cf0 324 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
c4804411
ZW
325 u32 temp;
326
327 if (dev_priv->mchbar_need_disable) {
328 if (IS_I915G(dev) || IS_I915GM(dev)) {
329 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
330 temp &= ~DEVEN_MCHBAR_EN;
331 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
332 } else {
333 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
334 temp &= ~1;
335 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
336 }
337 }
338
339 if (dev_priv->mch_res.start)
340 release_resource(&dev_priv->mch_res);
341}
342
28d52043
DA
343/* true = enable decode, false = disable decoder */
344static unsigned int i915_vga_set_decode(void *cookie, bool state)
345{
346 struct drm_device *dev = cookie;
347
348 intel_modeset_vga_set_state(dev, state);
349 if (state)
350 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
351 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
352 else
353 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
354}
355
6a9ee8af
DA
356static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
357{
358 struct drm_device *dev = pci_get_drvdata(pdev);
359 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1a5036bf 360
6a9ee8af 361 if (state == VGA_SWITCHEROO_ON) {
a70491cc 362 pr_info("switched on\n");
5bcf719b 363 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
6a9ee8af
DA
364 /* i915 resume handler doesn't set to D0 */
365 pci_set_power_state(dev->pdev, PCI_D0);
1751fcf9 366 i915_resume_switcheroo(dev);
5bcf719b 367 dev->switch_power_state = DRM_SWITCH_POWER_ON;
6a9ee8af 368 } else {
fa9d6078 369 pr_info("switched off\n");
5bcf719b 370 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1751fcf9 371 i915_suspend_switcheroo(dev, pmm);
5bcf719b 372 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
6a9ee8af
DA
373 }
374}
375
376static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
377{
378 struct drm_device *dev = pci_get_drvdata(pdev);
6a9ee8af 379
fc8fd40e
DV
380 /*
381 * FIXME: open_count is protected by drm_global_mutex but that would lead to
382 * locking inversion with the driver load path. And the access here is
383 * completely racy anyway. So don't bother with locking for now.
384 */
385 return dev->open_count == 0;
6a9ee8af
DA
386}
387
26ec685f
TI
388static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
389 .set_gpu_state = i915_switcheroo_set_state,
390 .reprobe = NULL,
391 .can_switch = i915_switcheroo_can_switch,
392};
393
2c7111db
CW
394static int i915_load_modeset_init(struct drm_device *dev)
395{
396 struct drm_i915_private *dev_priv = dev->dev_private;
397 int ret;
79e53945 398
6d139a87 399 ret = intel_parse_bios(dev);
79e53945
JB
400 if (ret)
401 DRM_INFO("failed to find VBIOS tables\n");
402
934f992c
CW
403 /* If we have > 1 VGA cards, then we need to arbitrate access
404 * to the common VGA resources.
405 *
406 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
407 * then we do not take part in VGA arbitration and the
408 * vga_client_register() fails with -ENODEV.
409 */
ebff5fa9
DA
410 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
411 if (ret && ret != -ENODEV)
412 goto out;
28d52043 413
723bfd70
JB
414 intel_register_dsm_handler();
415
0d69704a 416 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
6a9ee8af 417 if (ret)
5a79395b 418 goto cleanup_vga_client;
6a9ee8af 419
9797fbfb
CW
420 /* Initialise stolen first so that we may reserve preallocated
421 * objects for the BIOS to KMS transition.
422 */
423 ret = i915_gem_init_stolen(dev);
424 if (ret)
425 goto cleanup_vga_switcheroo;
426
e13192f6
ID
427 intel_power_domains_init_hw(dev_priv);
428
2aeb7d3a 429 ret = intel_irq_install(dev_priv);
52d7eced
DV
430 if (ret)
431 goto cleanup_gem_stolen;
432
433 /* Important: The output setup functions called by modeset_init need
434 * working irqs for e.g. gmbus and dp aux transfers. */
b01f2c3a
JB
435 intel_modeset_init(dev);
436
33a732f4
AD
437 /* intel_guc_ucode_init() needs the mutex to allocate GEM objects */
438 mutex_lock(&dev->struct_mutex);
439 intel_guc_ucode_init(dev);
440 mutex_unlock(&dev->struct_mutex);
441
1070a42b 442 ret = i915_gem_init(dev);
79e53945 443 if (ret)
713028b3 444 goto cleanup_irq;
2c7111db 445
52d7eced 446 intel_modeset_gem_init(dev);
2c7111db 447
79e53945
JB
448 /* Always safe in the mode setting case. */
449 /* FIXME: do pre/post-mode set stuff in core KMS code */
ba0bf120 450 dev->vblank_disable_allowed = true;
713028b3 451 if (INTEL_INFO(dev)->num_pipes == 0)
e3c74757 452 return 0;
79e53945 453
5a79395b
CW
454 ret = intel_fbdev_init(dev);
455 if (ret)
52d7eced
DV
456 goto cleanup_gem;
457
20afbda2 458 /* Only enable hotplug handling once the fbdev is fully set up. */
b963291c 459 intel_hpd_init(dev_priv);
20afbda2
DV
460
461 /*
462 * Some ports require correctly set-up hpd registers for detection to
463 * work properly (leading to ghost connected connector status), e.g. VGA
464 * on gm45. Hence we can only set up the initial fbdev config after hpd
465 * irqs are fully enabled. Now we should scan for the initial config
466 * only once hotplug handling is enabled, but due to screwed-up locking
467 * around kms/fbdev init we can't protect the fdbev initial config
468 * scanning against hotplug events. Hence do this first and ignore the
469 * tiny window where we will loose hotplug notifactions.
470 */
e00bf696 471 intel_fbdev_initial_config_async(dev);
20afbda2 472
eb1f8e4f 473 drm_kms_helper_poll_init(dev);
87acb0a5 474
79e53945
JB
475 return 0;
476
2c7111db
CW
477cleanup_gem:
478 mutex_lock(&dev->struct_mutex);
479 i915_gem_cleanup_ringbuffer(dev);
55d23285 480 i915_gem_context_fini(dev);
2c7111db 481 mutex_unlock(&dev->struct_mutex);
713028b3 482cleanup_irq:
33a732f4
AD
483 mutex_lock(&dev->struct_mutex);
484 intel_guc_ucode_fini(dev);
485 mutex_unlock(&dev->struct_mutex);
52d7eced 486 drm_irq_uninstall(dev);
9797fbfb
CW
487cleanup_gem_stolen:
488 i915_gem_cleanup_stolen(dev);
5a79395b
CW
489cleanup_vga_switcheroo:
490 vga_switcheroo_unregister_client(dev->pdev);
491cleanup_vga_client:
492 vga_client_register(dev->pdev, NULL, NULL, NULL);
79e53945
JB
493out:
494 return ret;
495}
496
243eaf38 497#if IS_ENABLED(CONFIG_FB)
f96de58f 498static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
e188719a
DV
499{
500 struct apertures_struct *ap;
501 struct pci_dev *pdev = dev_priv->dev->pdev;
502 bool primary;
f96de58f 503 int ret;
e188719a
DV
504
505 ap = alloc_apertures(1);
506 if (!ap)
f96de58f 507 return -ENOMEM;
e188719a 508
dabb7a91 509 ap->ranges[0].base = dev_priv->gtt.mappable_base;
f64e2922 510 ap->ranges[0].size = dev_priv->gtt.mappable_end;
93d18799 511
e188719a
DV
512 primary =
513 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
514
f96de58f 515 ret = remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
e188719a
DV
516
517 kfree(ap);
f96de58f
CW
518
519 return ret;
e188719a 520}
4520f53a 521#else
f96de58f 522static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
4520f53a 523{
f96de58f 524 return 0;
4520f53a
DV
525}
526#endif
e188719a 527
a4de0526
DV
528#if !defined(CONFIG_VGA_CONSOLE)
529static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
530{
531 return 0;
532}
533#elif !defined(CONFIG_DUMMY_CONSOLE)
534static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
535{
536 return -ENODEV;
537}
538#else
539static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
540{
1bb9e632 541 int ret = 0;
a4de0526
DV
542
543 DRM_INFO("Replacing VGA console driver\n");
544
545 console_lock();
1bb9e632
DV
546 if (con_is_bound(&vga_con))
547 ret = do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES - 1, 1);
a4de0526
DV
548 if (ret == 0) {
549 ret = do_unregister_con_driver(&vga_con);
550
551 /* Ignore "already unregistered". */
552 if (ret == -ENODEV)
553 ret = 0;
554 }
555 console_unlock();
556
557 return ret;
558}
559#endif
560
c96ea64e
DV
561static void i915_dump_device_info(struct drm_i915_private *dev_priv)
562{
5c969aa7 563 const struct intel_device_info *info = &dev_priv->info;
c96ea64e 564
e2a5800a
DL
565#define PRINT_S(name) "%s"
566#define SEP_EMPTY
79fc46df
DL
567#define PRINT_FLAG(name) info->name ? #name "," : ""
568#define SEP_COMMA ,
19c656a1 569 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x rev=0x%02x flags="
e2a5800a 570 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
c96ea64e
DV
571 info->gen,
572 dev_priv->dev->pdev->device,
19c656a1 573 dev_priv->dev->pdev->revision,
79fc46df 574 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
e2a5800a
DL
575#undef PRINT_S
576#undef SEP_EMPTY
79fc46df
DL
577#undef PRINT_FLAG
578#undef SEP_COMMA
c96ea64e
DV
579}
580
9705ad8a
JM
581static void cherryview_sseu_info_init(struct drm_device *dev)
582{
583 struct drm_i915_private *dev_priv = dev->dev_private;
584 struct intel_device_info *info;
585 u32 fuse, eu_dis;
586
587 info = (struct intel_device_info *)&dev_priv->info;
588 fuse = I915_READ(CHV_FUSE_GT);
589
590 info->slice_total = 1;
591
592 if (!(fuse & CHV_FGT_DISABLE_SS0)) {
593 info->subslice_per_slice++;
594 eu_dis = fuse & (CHV_FGT_EU_DIS_SS0_R0_MASK |
595 CHV_FGT_EU_DIS_SS0_R1_MASK);
596 info->eu_total += 8 - hweight32(eu_dis);
597 }
598
599 if (!(fuse & CHV_FGT_DISABLE_SS1)) {
600 info->subslice_per_slice++;
601 eu_dis = fuse & (CHV_FGT_EU_DIS_SS1_R0_MASK |
602 CHV_FGT_EU_DIS_SS1_R1_MASK);
603 info->eu_total += 8 - hweight32(eu_dis);
604 }
605
606 info->subslice_total = info->subslice_per_slice;
607 /*
608 * CHV expected to always have a uniform distribution of EU
609 * across subslices.
610 */
611 info->eu_per_subslice = info->subslice_total ?
612 info->eu_total / info->subslice_total :
613 0;
614 /*
615 * CHV supports subslice power gating on devices with more than
616 * one subslice, and supports EU power gating on devices with
617 * more than one EU pair per subslice.
618 */
619 info->has_slice_pg = 0;
620 info->has_subslice_pg = (info->subslice_total > 1);
621 info->has_eu_pg = (info->eu_per_subslice > 2);
622}
623
624static void gen9_sseu_info_init(struct drm_device *dev)
625{
626 struct drm_i915_private *dev_priv = dev->dev_private;
627 struct intel_device_info *info;
dead16e2 628 int s_max = 3, ss_max = 4, eu_max = 8;
9705ad8a 629 int s, ss;
dead16e2
JM
630 u32 fuse2, s_enable, ss_disable, eu_disable;
631 u8 eu_mask = 0xff;
632
9705ad8a
JM
633 info = (struct intel_device_info *)&dev_priv->info;
634 fuse2 = I915_READ(GEN8_FUSE2);
635 s_enable = (fuse2 & GEN8_F2_S_ENA_MASK) >>
636 GEN8_F2_S_ENA_SHIFT;
637 ss_disable = (fuse2 & GEN9_F2_SS_DIS_MASK) >>
638 GEN9_F2_SS_DIS_SHIFT;
639
9705ad8a
JM
640 info->slice_total = hweight32(s_enable);
641 /*
642 * The subslice disable field is global, i.e. it applies
643 * to each of the enabled slices.
644 */
645 info->subslice_per_slice = ss_max - hweight32(ss_disable);
646 info->subslice_total = info->slice_total *
647 info->subslice_per_slice;
648
649 /*
650 * Iterate through enabled slices and subslices to
651 * count the total enabled EU.
652 */
653 for (s = 0; s < s_max; s++) {
654 if (!(s_enable & (0x1 << s)))
655 /* skip disabled slice */
656 continue;
657
dead16e2 658 eu_disable = I915_READ(GEN9_EU_DISABLE(s));
9705ad8a 659 for (ss = 0; ss < ss_max; ss++) {
dead16e2 660 int eu_per_ss;
9705ad8a
JM
661
662 if (ss_disable & (0x1 << ss))
663 /* skip disabled subslice */
664 continue;
665
dead16e2
JM
666 eu_per_ss = eu_max - hweight8((eu_disable >> (ss*8)) &
667 eu_mask);
9705ad8a
JM
668
669 /*
670 * Record which subslice(s) has(have) 7 EUs. we
671 * can tune the hash used to spread work among
672 * subslices if they are unbalanced.
673 */
dead16e2 674 if (eu_per_ss == 7)
9705ad8a
JM
675 info->subslice_7eu[s] |= 1 << ss;
676
dead16e2 677 info->eu_total += eu_per_ss;
9705ad8a
JM
678 }
679 }
680
681 /*
682 * SKL is expected to always have a uniform distribution
683 * of EU across subslices with the exception that any one
684 * EU in any one subslice may be fused off for die
dead16e2
JM
685 * recovery. BXT is expected to be perfectly uniform in EU
686 * distribution.
9705ad8a
JM
687 */
688 info->eu_per_subslice = info->subslice_total ?
689 DIV_ROUND_UP(info->eu_total,
690 info->subslice_total) : 0;
691 /*
692 * SKL supports slice power gating on devices with more than
693 * one slice, and supports EU power gating on devices with
dead16e2
JM
694 * more than one EU pair per subslice. BXT supports subslice
695 * power gating on devices with more than one subslice, and
696 * supports EU power gating on devices with more than one EU
697 * pair per subslice.
9705ad8a 698 */
ef11bdb3
RV
699 info->has_slice_pg = ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
700 (info->slice_total > 1));
dead16e2
JM
701 info->has_subslice_pg = (IS_BROXTON(dev) && (info->subslice_total > 1));
702 info->has_eu_pg = (info->eu_per_subslice > 2);
9705ad8a
JM
703}
704
91bedd34
ŁD
705static void broadwell_sseu_info_init(struct drm_device *dev)
706{
707 struct drm_i915_private *dev_priv = dev->dev_private;
708 struct intel_device_info *info;
709 const int s_max = 3, ss_max = 3, eu_max = 8;
710 int s, ss;
711 u32 fuse2, eu_disable[s_max], s_enable, ss_disable;
712
713 fuse2 = I915_READ(GEN8_FUSE2);
714 s_enable = (fuse2 & GEN8_F2_S_ENA_MASK) >> GEN8_F2_S_ENA_SHIFT;
715 ss_disable = (fuse2 & GEN8_F2_SS_DIS_MASK) >> GEN8_F2_SS_DIS_SHIFT;
716
717 eu_disable[0] = I915_READ(GEN8_EU_DISABLE0) & GEN8_EU_DIS0_S0_MASK;
718 eu_disable[1] = (I915_READ(GEN8_EU_DISABLE0) >> GEN8_EU_DIS0_S1_SHIFT) |
719 ((I915_READ(GEN8_EU_DISABLE1) & GEN8_EU_DIS1_S1_MASK) <<
720 (32 - GEN8_EU_DIS0_S1_SHIFT));
721 eu_disable[2] = (I915_READ(GEN8_EU_DISABLE1) >> GEN8_EU_DIS1_S2_SHIFT) |
722 ((I915_READ(GEN8_EU_DISABLE2) & GEN8_EU_DIS2_S2_MASK) <<
723 (32 - GEN8_EU_DIS1_S2_SHIFT));
724
725
726 info = (struct intel_device_info *)&dev_priv->info;
727 info->slice_total = hweight32(s_enable);
728
729 /*
730 * The subslice disable field is global, i.e. it applies
731 * to each of the enabled slices.
732 */
733 info->subslice_per_slice = ss_max - hweight32(ss_disable);
734 info->subslice_total = info->slice_total * info->subslice_per_slice;
735
736 /*
737 * Iterate through enabled slices and subslices to
738 * count the total enabled EU.
739 */
740 for (s = 0; s < s_max; s++) {
741 if (!(s_enable & (0x1 << s)))
742 /* skip disabled slice */
743 continue;
744
745 for (ss = 0; ss < ss_max; ss++) {
746 u32 n_disabled;
747
748 if (ss_disable & (0x1 << ss))
749 /* skip disabled subslice */
750 continue;
751
752 n_disabled = hweight8(eu_disable[s] >> (ss * eu_max));
753
754 /*
755 * Record which subslices have 7 EUs.
756 */
757 if (eu_max - n_disabled == 7)
758 info->subslice_7eu[s] |= 1 << ss;
759
760 info->eu_total += eu_max - n_disabled;
761 }
762 }
763
764 /*
765 * BDW is expected to always have a uniform distribution of EU across
766 * subslices with the exception that any one EU in any one subslice may
767 * be fused off for die recovery.
768 */
769 info->eu_per_subslice = info->subslice_total ?
770 DIV_ROUND_UP(info->eu_total, info->subslice_total) : 0;
771
772 /*
773 * BDW supports slice power gating on devices with more than
774 * one slice.
775 */
776 info->has_slice_pg = (info->slice_total > 1);
777 info->has_subslice_pg = 0;
778 info->has_eu_pg = 0;
779}
780
22d3fd46
DL
781/*
782 * Determine various intel_device_info fields at runtime.
783 *
784 * Use it when either:
785 * - it's judged too laborious to fill n static structures with the limit
786 * when a simple if statement does the job,
787 * - run-time checks (eg read fuse/strap registers) are needed.
658ac4c6
DL
788 *
789 * This function needs to be called:
790 * - after the MMIO has been setup as we are reading registers,
791 * - after the PCH has been detected,
792 * - before the first usage of the fields it can tweak.
22d3fd46
DL
793 */
794static void intel_device_info_runtime_init(struct drm_device *dev)
795{
658ac4c6 796 struct drm_i915_private *dev_priv = dev->dev_private;
22d3fd46 797 struct intel_device_info *info;
d615a166 798 enum pipe pipe;
22d3fd46 799
658ac4c6 800 info = (struct intel_device_info *)&dev_priv->info;
22d3fd46 801
edd43ed8
DL
802 /*
803 * Skylake and Broxton currently don't expose the topmost plane as its
804 * use is exclusive with the legacy cursor and we only want to expose
805 * one of those, not both. Until we can safely expose the topmost plane
806 * as a DRM_PLANE_TYPE_CURSOR with all the features exposed/supported,
807 * we don't expose the topmost plane at all to prevent ABI breakage
808 * down the line.
809 */
8fb9397d 810 if (IS_BROXTON(dev)) {
edd43ed8
DL
811 info->num_sprites[PIPE_A] = 2;
812 info->num_sprites[PIPE_B] = 2;
813 info->num_sprites[PIPE_C] = 1;
814 } else if (IS_VALLEYVIEW(dev))
055e393f 815 for_each_pipe(dev_priv, pipe)
d615a166
DL
816 info->num_sprites[pipe] = 2;
817 else
055e393f 818 for_each_pipe(dev_priv, pipe)
d615a166 819 info->num_sprites[pipe] = 1;
658ac4c6 820
a0bae57f
DL
821 if (i915.disable_display) {
822 DRM_INFO("Display disabled (module parameter)\n");
823 info->num_pipes = 0;
824 } else if (info->num_pipes > 0 &&
825 (INTEL_INFO(dev)->gen == 7 || INTEL_INFO(dev)->gen == 8) &&
826 !IS_VALLEYVIEW(dev)) {
658ac4c6
DL
827 u32 fuse_strap = I915_READ(FUSE_STRAP);
828 u32 sfuse_strap = I915_READ(SFUSE_STRAP);
829
830 /*
831 * SFUSE_STRAP is supposed to have a bit signalling the display
832 * is fused off. Unfortunately it seems that, at least in
833 * certain cases, fused off display means that PCH display
834 * reads don't land anywhere. In that case, we read 0s.
835 *
836 * On CPT/PPT, we can detect this case as SFUSE_STRAP_FUSE_LOCK
837 * should be set when taking over after the firmware.
838 */
839 if (fuse_strap & ILK_INTERNAL_DISPLAY_DISABLE ||
840 sfuse_strap & SFUSE_STRAP_DISPLAY_DISABLED ||
841 (dev_priv->pch_type == PCH_CPT &&
842 !(sfuse_strap & SFUSE_STRAP_FUSE_LOCK))) {
843 DRM_INFO("Display fused off, disabling\n");
844 info->num_pipes = 0;
845 }
846 }
693d11c3 847
3873218f 848 /* Initialize slice/subslice/EU info */
9705ad8a
JM
849 if (IS_CHERRYVIEW(dev))
850 cherryview_sseu_info_init(dev);
91bedd34
ŁD
851 else if (IS_BROADWELL(dev))
852 broadwell_sseu_info_init(dev);
dead16e2 853 else if (INTEL_INFO(dev)->gen >= 9)
9705ad8a 854 gen9_sseu_info_init(dev);
3873218f 855
3873218f
JM
856 DRM_DEBUG_DRIVER("slice total: %u\n", info->slice_total);
857 DRM_DEBUG_DRIVER("subslice total: %u\n", info->subslice_total);
858 DRM_DEBUG_DRIVER("subslice per slice: %u\n", info->subslice_per_slice);
859 DRM_DEBUG_DRIVER("EU total: %u\n", info->eu_total);
860 DRM_DEBUG_DRIVER("EU per subslice: %u\n", info->eu_per_subslice);
861 DRM_DEBUG_DRIVER("has slice power gating: %s\n",
862 info->has_slice_pg ? "y" : "n");
863 DRM_DEBUG_DRIVER("has subslice power gating: %s\n",
864 info->has_subslice_pg ? "y" : "n");
865 DRM_DEBUG_DRIVER("has EU power gating: %s\n",
866 info->has_eu_pg ? "y" : "n");
22d3fd46
DL
867}
868
e27f299e
VS
869static void intel_init_dpio(struct drm_i915_private *dev_priv)
870{
871 if (!IS_VALLEYVIEW(dev_priv))
872 return;
873
874 /*
875 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
876 * CHV x1 PHY (DP/HDMI D)
877 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
878 */
879 if (IS_CHERRYVIEW(dev_priv)) {
880 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
881 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
882 } else {
883 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
884 }
885}
886
79e53945
JB
887/**
888 * i915_driver_load - setup chip and create an initial config
889 * @dev: DRM device
890 * @flags: startup flags
891 *
892 * The driver load routine has to do several things:
893 * - drive output discovery via intel_modeset_init()
894 * - initialize the memory manager
895 * - allocate initial config memory
896 * - setup the DRM framebuffer with the allocated memory
897 */
84b1fd10 898int i915_driver_load(struct drm_device *dev, unsigned long flags)
22eae947 899{
ea059a1e 900 struct drm_i915_private *dev_priv;
5c969aa7 901 struct intel_device_info *info, *device_info;
934d6086 902 int ret = 0, mmio_bar, mmio_size;
9021f284 903 uint32_t aperture_size;
fe669bf8 904
26394d92
DV
905 info = (struct intel_device_info *) flags;
906
b14c5679 907 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
ba8bbcf6
JB
908 if (dev_priv == NULL)
909 return -ENOMEM;
910
755f68f4 911 dev->dev_private = dev_priv;
673a394b 912 dev_priv->dev = dev;
5c969aa7 913
87f1f465 914 /* Setup the write-once "constant" device info */
5c969aa7 915 device_info = (struct intel_device_info *)&dev_priv->info;
87f1f465
CW
916 memcpy(device_info, info, sizeof(dev_priv->info));
917 device_info->device_id = dev->pdev->device;
ba8bbcf6 918
7dcd2677
KK
919 spin_lock_init(&dev_priv->irq_lock);
920 spin_lock_init(&dev_priv->gpu_error.lock);
07f11d49 921 mutex_init(&dev_priv->backlight_lock);
907b28c5 922 spin_lock_init(&dev_priv->uncore.lock);
c20e8355 923 spin_lock_init(&dev_priv->mm.object_stat_lock);
84c33a64 924 spin_lock_init(&dev_priv->mmio_flip_lock);
a580516d 925 mutex_init(&dev_priv->sb_lock);
7dcd2677 926 mutex_init(&dev_priv->modeset_restore_lock);
eb805623 927 mutex_init(&dev_priv->csr_lock);
4a21ef7d 928 mutex_init(&dev_priv->av_mutex);
7dcd2677 929
f742a552 930 intel_pm_setup(dev);
c67a470b 931
07144428
DL
932 intel_display_crc_init(dev);
933
c96ea64e
DV
934 i915_dump_device_info(dev_priv);
935
ed1c9e2c
PZ
936 /* Not all pre-production machines fall into this category, only the
937 * very first ones. Almost everything should work, except for maybe
938 * suspend/resume. And we don't implement workarounds that affect only
939 * pre-production machines. */
940 if (IS_HSW_EARLY_SDV(dev))
941 DRM_INFO("This is an early pre-production Haswell machine. "
942 "It may not be fully functional.\n");
943
ec2a4c3f
DA
944 if (i915_get_bridge_dev(dev)) {
945 ret = -EIO;
946 goto free_priv;
947 }
948
1e1bd0fd
BW
949 mmio_bar = IS_GEN2(dev) ? 1 : 0;
950 /* Before gen4, the registers and the GTT are behind different BARs.
951 * However, from gen4 onwards, the registers and the GTT are shared
952 * in the same BAR, so we want to restrict this ioremap from
953 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
954 * the register BAR remains the same size for all the earlier
955 * generations up to Ironlake.
956 */
957 if (info->gen < 5)
958 mmio_size = 512*1024;
959 else
960 mmio_size = 2*1024*1024;
961
962 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
963 if (!dev_priv->regs) {
964 DRM_ERROR("failed to map registers\n");
965 ret = -EIO;
966 goto put_bridge;
967 }
968
c3d685a7
BW
969 /* This must be called before any calls to HAS_PCH_* */
970 intel_detect_pch(dev);
971
972 intel_uncore_init(dev);
973
eb805623
DV
974 /* Load CSR Firmware for SKL */
975 intel_csr_ucode_init(dev);
976
e76e9aeb
BW
977 ret = i915_gem_gtt_init(dev);
978 if (ret)
eb805623 979 goto out_freecsr;
e188719a 980
17fa6463
DV
981 /* WARNING: Apparently we must kick fbdev drivers before vgacon,
982 * otherwise the vga fbdev driver falls over. */
983 ret = i915_kick_out_firmware_fb(dev_priv);
984 if (ret) {
985 DRM_ERROR("failed to remove conflicting framebuffer drivers\n");
986 goto out_gtt;
987 }
a4de0526 988
17fa6463
DV
989 ret = i915_kick_out_vgacon(dev_priv);
990 if (ret) {
991 DRM_ERROR("failed to remove conflicting VGA console\n");
992 goto out_gtt;
a4de0526 993 }
e188719a 994
466e69b8
DA
995 pci_set_master(dev->pdev);
996
9f82d238
DV
997 /* overlay on gen2 is broken and can't address above 1G */
998 if (IS_GEN2(dev))
999 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1000
6927faf3
JN
1001 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1002 * using 32bit addressing, overwriting memory if HWS is located
1003 * above 4GB.
1004 *
1005 * The documentation also mentions an issue with undefined
1006 * behaviour if any general state is accessed within a page above 4GB,
1007 * which also needs to be handled carefully.
1008 */
1009 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1010 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1011
93d18799 1012 aperture_size = dev_priv->gtt.mappable_end;
71e9339c 1013
5d4545ae
BW
1014 dev_priv->gtt.mappable =
1015 io_mapping_create_wc(dev_priv->gtt.mappable_base,
dd2757f8 1016 aperture_size);
5d4545ae 1017 if (dev_priv->gtt.mappable == NULL) {
6644107d 1018 ret = -EIO;
cbb47d17 1019 goto out_gtt;
6644107d
VP
1020 }
1021
911bdf0a
BW
1022 dev_priv->gtt.mtrr = arch_phys_wc_add(dev_priv->gtt.mappable_base,
1023 aperture_size);
19966754 1024
e642abbf
CW
1025 /* The i915 workqueue is primarily used for batched retirement of
1026 * requests (and thus managing bo) once the task has been completed
1027 * by the GPU. i915_gem_retire_requests() is called directly when we
1028 * need high-priority retirement, such as waiting for an explicit
1029 * bo.
1030 *
1031 * It is also used for periodic low-priority events, such as
df9c2042 1032 * idle-timers and recording error state.
e642abbf
CW
1033 *
1034 * All tasks on the workqueue are expected to acquire the dev mutex
1035 * so there is no point in running more than one instance of the
53621860 1036 * workqueue at any time. Use an ordered one.
e642abbf 1037 */
53621860 1038 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
9c9fe1f8
EA
1039 if (dev_priv->wq == NULL) {
1040 DRM_ERROR("Failed to create our workqueue.\n");
1041 ret = -ENOMEM;
a7b85d2a 1042 goto out_mtrrfree;
9c9fe1f8
EA
1043 }
1044
5fcece80
JN
1045 dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
1046 if (dev_priv->hotplug.dp_wq == NULL) {
0e32b39c
DA
1047 DRM_ERROR("Failed to create our dp workqueue.\n");
1048 ret = -ENOMEM;
1049 goto out_freewq;
1050 }
1051
737b1506
CW
1052 dev_priv->gpu_error.hangcheck_wq =
1053 alloc_ordered_workqueue("i915-hangcheck", 0);
1054 if (dev_priv->gpu_error.hangcheck_wq == NULL) {
1055 DRM_ERROR("Failed to create our hangcheck workqueue.\n");
1056 ret = -ENOMEM;
1057 goto out_freedpwq;
1058 }
1059
b963291c 1060 intel_irq_init(dev_priv);
78511f2a 1061 intel_uncore_sanitize(dev);
9880b7a5 1062
c4804411
ZW
1063 /* Try to make sure MCHBAR is enabled before poking at it */
1064 intel_setup_mchbar(dev);
f899fc64 1065 intel_setup_gmbus(dev);
44834a67 1066 intel_opregion_setup(dev);
c4804411 1067
673a394b
EA
1068 i915_gem_load(dev);
1069
ed4cb414
EA
1070 /* On the 945G/GM, the chipset reports the MSI capability on the
1071 * integrated graphics even though the support isn't actually there
1072 * according to the published specs. It doesn't appear to function
1073 * correctly in testing on 945G.
1074 * This may be a side effect of MSI having been made available for PEG
1075 * and the registers being closely associated.
d1ed629f
KP
1076 *
1077 * According to chipset errata, on the 965GM, MSI interrupts may
b60678a7
KP
1078 * be lost or delayed, but we use them anyways to avoid
1079 * stuck interrupts on some machines.
ed4cb414 1080 */
b60678a7 1081 if (!IS_I945G(dev) && !IS_I945GM(dev))
d3e74d02 1082 pci_enable_msi(dev->pdev);
ed4cb414 1083
22d3fd46 1084 intel_device_info_runtime_init(dev);
7f1f3851 1085
e27f299e
VS
1086 intel_init_dpio(dev_priv);
1087
e3c74757
BW
1088 if (INTEL_INFO(dev)->num_pipes) {
1089 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1090 if (ret)
1091 goto out_gem_unload;
1092 }
52440211 1093
da7e29bd 1094 intel_power_domains_init(dev_priv);
a38911a3 1095
17fa6463
DV
1096 ret = i915_load_modeset_init(dev);
1097 if (ret < 0) {
1098 DRM_ERROR("failed to init modeset\n");
1099 goto out_power_well;
79e53945
JB
1100 }
1101
e21fd552
YZ
1102 /*
1103 * Notify a valid surface after modesetting,
1104 * when running inside a VM.
1105 */
1106 if (intel_vgpu_active(dev))
1107 I915_WRITE(vgtif_reg(display_ready), VGT_DRV_DISPLAY_READY);
1108
0136db58
BW
1109 i915_setup_sysfs(dev);
1110
e3c74757
BW
1111 if (INTEL_INFO(dev)->num_pipes) {
1112 /* Must be done after probing outputs */
1113 intel_opregion_init(dev);
8e5c2b77 1114 acpi_video_register();
e3c74757 1115 }
74a365b3 1116
eb48eb00
DV
1117 if (IS_GEN5(dev))
1118 intel_gpu_ips_init(dev_priv);
63ee41d7 1119
f458ebbc 1120 intel_runtime_pm_enable(dev_priv);
8a187455 1121
58fddc28
ID
1122 i915_audio_component_init(dev_priv);
1123
79e53945
JB
1124 return 0;
1125
cbb47d17 1126out_power_well:
f458ebbc 1127 intel_power_domains_fini(dev_priv);
cbb47d17 1128 drm_vblank_cleanup(dev);
56e2ea34 1129out_gem_unload:
4bdc7293
ID
1130 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1131 unregister_shrinker(&dev_priv->mm.shrinker);
a7b85d2a 1132
56e2ea34
CW
1133 if (dev->pdev->msi_enabled)
1134 pci_disable_msi(dev->pdev);
1135
1136 intel_teardown_gmbus(dev);
1137 intel_teardown_mchbar(dev);
22accca0 1138 pm_qos_remove_request(&dev_priv->pm_qos);
737b1506
CW
1139 destroy_workqueue(dev_priv->gpu_error.hangcheck_wq);
1140out_freedpwq:
5fcece80 1141 destroy_workqueue(dev_priv->hotplug.dp_wq);
0e32b39c 1142out_freewq:
9c9fe1f8 1143 destroy_workqueue(dev_priv->wq);
a7b85d2a 1144out_mtrrfree:
911bdf0a 1145 arch_phys_wc_del(dev_priv->gtt.mtrr);
5d4545ae 1146 io_mapping_free(dev_priv->gtt.mappable);
cbb47d17 1147out_gtt:
90d0a0e8 1148 i915_global_gtt_cleanup(dev);
eb805623
DV
1149out_freecsr:
1150 intel_csr_ucode_fini(dev);
c3d685a7 1151 intel_uncore_fini(dev);
6dda569f 1152 pci_iounmap(dev->pdev, dev_priv->regs);
ec2a4c3f
DA
1153put_bridge:
1154 pci_dev_put(dev_priv->bridge_dev);
79e53945 1155free_priv:
76b1cf21
JL
1156 kmem_cache_destroy(dev_priv->requests);
1157 kmem_cache_destroy(dev_priv->vmas);
1158 kmem_cache_destroy(dev_priv->objects);
9a298b2a 1159 kfree(dev_priv);
ba8bbcf6
JB
1160 return ret;
1161}
1162
1163int i915_driver_unload(struct drm_device *dev)
1164{
1165 struct drm_i915_private *dev_priv = dev->dev_private;
c911fc1c 1166 int ret;
ba8bbcf6 1167
2013bfc0
VS
1168 intel_fbdev_fini(dev);
1169
58fddc28
ID
1170 i915_audio_component_cleanup(dev_priv);
1171
ce58c32b
CW
1172 ret = i915_gem_suspend(dev);
1173 if (ret) {
1174 DRM_ERROR("failed to idle hardware: %d\n", ret);
1175 return ret;
1176 }
1177
41373cd5 1178 intel_power_domains_fini(dev_priv);
8a187455 1179
eb48eb00 1180 intel_gpu_ips_teardown();
7648fa99 1181
0136db58
BW
1182 i915_teardown_sysfs(dev);
1183
4bdc7293
ID
1184 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1185 unregister_shrinker(&dev_priv->mm.shrinker);
17250b71 1186
5d4545ae 1187 io_mapping_free(dev_priv->gtt.mappable);
911bdf0a 1188 arch_phys_wc_del(dev_priv->gtt.mtrr);
ab657db1 1189
44834a67
CW
1190 acpi_video_unregister();
1191
2ebfaf5f
PZ
1192 drm_vblank_cleanup(dev);
1193
17fa6463 1194 intel_modeset_cleanup(dev);
6c0d9350 1195
17fa6463
DV
1196 /*
1197 * free the memory space allocated for the child device
1198 * config parsed from VBT
1199 */
1200 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1201 kfree(dev_priv->vbt.child_dev);
1202 dev_priv->vbt.child_dev = NULL;
1203 dev_priv->vbt.child_dev_num = 0;
79e53945 1204 }
9aa61142
MR
1205 kfree(dev_priv->vbt.sdvo_lvds_vbt_mode);
1206 dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
1207 kfree(dev_priv->vbt.lfp_lvds_vbt_mode);
1208 dev_priv->vbt.lfp_lvds_vbt_mode = NULL;
79e53945 1209
17fa6463
DV
1210 vga_switcheroo_unregister_client(dev->pdev);
1211 vga_client_register(dev->pdev, NULL, NULL, NULL);
1212
a8b4899e 1213 /* Free error state after interrupts are fully disabled. */
737b1506 1214 cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
a8b4899e 1215 i915_destroy_error_state(dev);
bc0c7f14 1216
ed4cb414
EA
1217 if (dev->pdev->msi_enabled)
1218 pci_disable_msi(dev->pdev);
1219
44834a67 1220 intel_opregion_fini(dev);
8ee1c3db 1221
17fa6463
DV
1222 /* Flush any outstanding unpin_work. */
1223 flush_workqueue(dev_priv->wq);
67e77c5a 1224
17fa6463 1225 mutex_lock(&dev->struct_mutex);
33a732f4 1226 intel_guc_ucode_fini(dev);
17fa6463 1227 i915_gem_cleanup_ringbuffer(dev);
17fa6463
DV
1228 i915_gem_context_fini(dev);
1229 mutex_unlock(&dev->struct_mutex);
7733b49b 1230 intel_fbc_cleanup_cfb(dev_priv);
17fa6463 1231 i915_gem_cleanup_stolen(dev);
79e53945 1232
eb805623
DV
1233 intel_csr_ucode_fini(dev);
1234
f899fc64 1235 intel_teardown_gmbus(dev);
c4804411
ZW
1236 intel_teardown_mchbar(dev);
1237
5fcece80 1238 destroy_workqueue(dev_priv->hotplug.dp_wq);
bc0c7f14 1239 destroy_workqueue(dev_priv->wq);
737b1506 1240 destroy_workqueue(dev_priv->gpu_error.hangcheck_wq);
9ee32fea 1241 pm_qos_remove_request(&dev_priv->pm_qos);
bc0c7f14 1242
90d0a0e8 1243 i915_global_gtt_cleanup(dev);
6640aab6 1244
aec347ab
CW
1245 intel_uncore_fini(dev);
1246 if (dev_priv->regs != NULL)
1247 pci_iounmap(dev->pdev, dev_priv->regs);
1248
76b1cf21
JL
1249 kmem_cache_destroy(dev_priv->requests);
1250 kmem_cache_destroy(dev_priv->vmas);
1251 kmem_cache_destroy(dev_priv->objects);
ec2a4c3f 1252 pci_dev_put(dev_priv->bridge_dev);
2206e6a1 1253 kfree(dev_priv);
ba8bbcf6 1254
22eae947
DA
1255 return 0;
1256}
1257
f787a5f5 1258int i915_driver_open(struct drm_device *dev, struct drm_file *file)
673a394b 1259{
b29c19b6 1260 int ret;
673a394b 1261
b29c19b6
CW
1262 ret = i915_gem_open(dev, file);
1263 if (ret)
1264 return ret;
254f965c 1265
673a394b
EA
1266 return 0;
1267}
1268
79e53945
JB
1269/**
1270 * i915_driver_lastclose - clean up after all DRM clients have exited
1271 * @dev: DRM device
1272 *
1273 * Take care of cleaning up after all DRM clients have exited. In the
1274 * mode setting case, we want to restore the kernel's initial mode (just
1275 * in case the last client left us in a bad state).
1276 *
9021f284 1277 * Additionally, in the non-mode setting case, we'll tear down the GTT
79e53945
JB
1278 * and DMA structures, since the kernel won't be using them, and clea
1279 * up any GEM state.
1280 */
1a5036bf 1281void i915_driver_lastclose(struct drm_device *dev)
1da177e4 1282{
377e91b2
DV
1283 intel_fbdev_restore_mode(dev);
1284 vga_switcheroo_process_delayed_switch();
1da177e4
LT
1285}
1286
2885f6ac 1287void i915_driver_preclose(struct drm_device *dev, struct drm_file *file)
1da177e4 1288{
0d1430a3 1289 mutex_lock(&dev->struct_mutex);
2885f6ac
JH
1290 i915_gem_context_close(dev, file);
1291 i915_gem_release(dev, file);
0d1430a3 1292 mutex_unlock(&dev->struct_mutex);
e2fcdaa9 1293
17fa6463 1294 intel_modeset_preclose(dev, file);
1da177e4
LT
1295}
1296
f787a5f5 1297void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
673a394b 1298{
f787a5f5 1299 struct drm_i915_file_private *file_priv = file->driver_priv;
673a394b 1300
a8ebba75
ZY
1301 if (file_priv && file_priv->bsd_ring)
1302 file_priv->bsd_ring = NULL;
f787a5f5 1303 kfree(file_priv);
673a394b
EA
1304}
1305
4feb7659
DV
1306static int
1307i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
1308 struct drm_file *file)
1309{
1310 return -ENODEV;
1311}
1312
baa70943 1313const struct drm_ioctl_desc i915_ioctls[] = {
77f31815
DV
1314 DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1315 DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
1316 DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
1317 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
1318 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
1319 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
10ba5012 1320 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
1b2f1489 1321 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
b2c606fe
DV
1322 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1323 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1324 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
77f31815 1325 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
b2c606fe 1326 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
d1c1edbc 1327 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
77f31815
DV
1328 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH),
1329 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
1330 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
f548c0e9 1331 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1b2f1489 1332 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
10ba5012 1333 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
4feb7659
DV
1334 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1335 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
10ba5012
KH
1336 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1337 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1338 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1339 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
71b14ab6
DV
1340 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1341 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
10ba5012
KH
1342 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1343 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1344 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1345 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1346 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1347 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1348 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1349 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1350 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1351 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1b2f1489 1352 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
10ba5012 1353 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1b2f1489
DA
1354 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1355 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
8ea30864 1356 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
a8265c59 1357 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
10ba5012
KH
1358 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1359 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1360 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1361 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
b6359918 1362 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_get_reset_stats_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
5cc9ed4b 1363 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
c9dc0f35
CW
1364 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1365 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
c94f7029
DA
1366};
1367
f95aeb17 1368int i915_max_ioctl = ARRAY_SIZE(i915_ioctls);