blob: a58fed951ddbb4cba1cc6c3aef62bda15fac0879 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
2 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
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 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Joe Perchesa70491c2012-03-18 13:00:11 -070029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Jesse Barnesd1d70672014-05-28 14:39:03 -070031#include <linux/async.h>
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/drm_fb_helper.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/i915_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010038#include "i915_trace.h"
Jordan Crousedcdb1672010-05-27 13:40:25 -060039#include <linux/pci.h>
Daniel Vettera4de0522014-06-05 16:20:46 +020040#include <linux/console.h>
41#include <linux/vt.h>
Dave Airlie28d52042009-09-21 14:33:58 +100042#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080043#include <linux/acpi.h>
44#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100045#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Chris Wilson44834a62010-08-19 16:09:23 +010047#include <acpi/video.h>
Paulo Zanoni8a187452013-12-06 20:32:13 -020048#include <linux/pm.h>
49#include <linux/pm_runtime.h>
Imre Deak4bdc7292014-05-20 19:47:20 +030050#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Daniel Vetter09422b22012-04-26 23:28:10 +020052#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
53
54#define BEGIN_LP_RING(n) \
55 intel_ring_begin(LP_RING(dev_priv), (n))
56
57#define OUT_RING(x) \
58 intel_ring_emit(LP_RING(dev_priv), x)
59
60#define ADVANCE_LP_RING() \
Chris Wilson09246732013-08-10 22:16:32 +010061 __intel_ring_advance(LP_RING(dev_priv))
Daniel Vetter09422b22012-04-26 23:28:10 +020062
63/**
64 * Lock test for when it's just for synchronization of ring access.
65 *
66 * In that case, we don't need to do it when GEM is initialized as nobody else
67 * has access to the ring.
68 */
69#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
Oscar Mateoee1b1e52014-05-22 14:13:35 +010070 if (LP_RING(dev->dev_private)->buffer->obj == NULL) \
Daniel Vetter09422b22012-04-26 23:28:10 +020071 LOCK_TEST_WITH_RETURN(dev, file); \
72} while (0)
73
Daniel Vetter316d3882012-04-26 23:28:15 +020074static inline u32
75intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
76{
77 if (I915_NEED_GFX_HWS(dev_priv->dev))
78 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
79 else
80 return intel_read_status_page(LP_RING(dev_priv), reg);
81}
82
83#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
Daniel Vetter09422b22012-04-26 23:28:10 +020084#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
85#define I915_BREADCRUMB_INDEX 0x21
86
Daniel Vetterd05c6172012-04-26 23:28:09 +020087void i915_update_dri1_breadcrumb(struct drm_device *dev)
88{
Jani Nikula4c8a4be2014-03-31 14:27:15 +030089 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd05c6172012-04-26 23:28:09 +020090 struct drm_i915_master_private *master_priv;
91
Daniel Vetter6c719fa2013-12-10 13:20:59 +010092 /*
93 * The dri breadcrumb update races against the drm master disappearing.
94 * Instead of trying to fix this (this is by far not the only ums issue)
95 * just don't do the update in kms mode.
96 */
97 if (drm_core_check_feature(dev, DRIVER_MODESET))
98 return;
99
Daniel Vetterd05c6172012-04-26 23:28:09 +0200100 if (dev->primary->master) {
101 master_priv = dev->primary->master->driver_priv;
102 if (master_priv->sarea_priv)
103 master_priv->sarea_priv->last_dispatch =
104 READ_BREADCRUMB(dev_priv);
105 }
106}
107
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000108static void i915_write_hws_pga(struct drm_device *dev)
109{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300110 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000111 u32 addr;
112
113 addr = dev_priv->status_page_dmah->busaddr;
114 if (INTEL_INFO(dev)->gen >= 4)
115 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
116 I915_WRITE(HWS_PGA, addr);
117}
118
Keith Packard398c9cb2008-07-30 13:03:43 -0700119/**
Keith Packard398c9cb2008-07-30 13:03:43 -0700120 * Frees the hardware status page, whether it's a physical address or a virtual
121 * address set up by the X Server.
122 */
Eric Anholt3043c602008-10-02 12:24:47 -0700123static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700124{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300125 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100126 struct intel_engine_cs *ring = LP_RING(dev_priv);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000127
Keith Packard398c9cb2008-07-30 13:03:43 -0700128 if (dev_priv->status_page_dmah) {
129 drm_pci_free(dev, dev_priv->status_page_dmah);
130 dev_priv->status_page_dmah = NULL;
131 }
132
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000133 if (ring->status_page.gfx_addr) {
134 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200135 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700136 }
137
138 /* Need to rewrite hardware status page */
139 I915_WRITE(HWS_PGA, 0x1ffff000);
140}
141
Robin Schroer1a5036b2014-06-02 16:59:39 +0200142void i915_kernel_lost_context(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300144 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000145 struct drm_i915_master_private *master_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100146 struct intel_engine_cs *ring = LP_RING(dev_priv);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100147 struct intel_ringbuffer *ringbuf = ring->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jesse Barnes79e53942008-11-07 14:24:08 -0800149 /*
150 * We should never lose context on the ring with modesetting
151 * as we don't expose it to userspace
152 */
153 if (drm_core_check_feature(dev, DRIVER_MODESET))
154 return;
155
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100156 ringbuf->head = I915_READ_HEAD(ring) & HEAD_ADDR;
157 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
158 ringbuf->space = ringbuf->head - (ringbuf->tail + I915_RING_FREE_SPACE);
159 if (ringbuf->space < 0)
160 ringbuf->space += ringbuf->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Dave Airlie7c1c2872008-11-28 14:22:24 +1000162 if (!dev->primary->master)
163 return;
164
165 master_priv = dev->primary->master->driver_priv;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100166 if (ringbuf->head == ringbuf->tail && master_priv->sarea_priv)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000167 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Robin Schroer1a5036b2014-06-02 16:59:39 +0200170static int i915_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300172 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000173 int i;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* Make sure interrupts are disabled here because the uninstall ioctl
176 * may not have been called from userspace and after dev_private
177 * is freed, it's too late.
178 */
Eric Anholted4cb412008-07-29 12:10:39 -0700179 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200182 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000183 for (i = 0; i < I915_NUM_RINGS; i++)
184 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200185 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Keith Packard398c9cb2008-07-30 13:03:43 -0700187 /* Clear the HWS virtual address at teardown */
188 if (I915_NEED_GFX_HWS(dev))
189 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 return 0;
192}
193
Robin Schroer1a5036b2014-06-02 16:59:39 +0200194static int i915_initialize(struct drm_device *dev, drm_i915_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300196 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000197 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000198 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Dave Airlie3a03ac12009-01-11 09:03:49 +1000200 master_priv->sarea = drm_getsarea(dev);
201 if (master_priv->sarea) {
202 master_priv->sarea_priv = (drm_i915_sarea_t *)
203 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
204 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800205 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000206 }
207
Eric Anholt673a3942008-07-30 12:06:12 -0700208 if (init->ring_size != 0) {
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100209 if (LP_RING(dev_priv)->buffer->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700210 i915_dma_cleanup(dev);
211 DRM_ERROR("Client tried to initialize ringbuffer in "
212 "GEM mode\n");
213 return -EINVAL;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Chris Wilsone8616b62011-01-20 09:57:11 +0000216 ret = intel_render_ring_init_dri(dev,
217 init->ring_start,
218 init->ring_size);
219 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700220 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000221 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200225 dev_priv->dri1.cpp = init->cpp;
226 dev_priv->dri1.back_offset = init->back_offset;
227 dev_priv->dri1.front_offset = init->front_offset;
228 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000229 if (master_priv->sarea_priv)
230 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* Allow hardware batchbuffers unless told otherwise.
233 */
Daniel Vetter87813422012-05-02 11:49:32 +0200234 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237}
238
Robin Schroer1a5036b2014-06-02 16:59:39 +0200239static int i915_dma_resume(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300241 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100242 struct intel_engine_cs *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800244 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100246 if (ring->buffer->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 DRM_ERROR("can not ioremap virtual address for"
248 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000249 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800253 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000255 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800257 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800258 ring->status_page.page_addr);
259 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100260 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000261 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000262 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800263
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800264 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 return 0;
267}
268
Eric Anholtc153f452007-09-03 12:06:45 +1000269static int i915_dma_init(struct drm_device *dev, void *data,
270 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Eric Anholtc153f452007-09-03 12:06:45 +1000272 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int retcode = 0;
274
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200275 if (drm_core_check_feature(dev, DRIVER_MODESET))
276 return -ENODEV;
277
Eric Anholtc153f452007-09-03 12:06:45 +1000278 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000280 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 case I915_CLEANUP_DMA:
283 retcode = i915_dma_cleanup(dev);
284 break;
285 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100286 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
288 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000289 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 break;
291 }
292
293 return retcode;
294}
295
296/* Implement basically the same security restrictions as hardware does
297 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
298 *
299 * Most of the calculations below involve calculating the size of a
300 * particular instruction. It's important to get the size right as
301 * that tells us where the next instruction to check is. Any illegal
302 * instruction detected will be given a size of zero, which is a
303 * signal to abort the rest of the buffer.
304 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100305static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 switch (((cmd >> 29) & 0x7)) {
308 case 0x0:
309 switch ((cmd >> 23) & 0x3f) {
310 case 0x0:
311 return 1; /* MI_NOOP */
312 case 0x4:
313 return 1; /* MI_FLUSH */
314 default:
315 return 0; /* disallow everything else */
316 }
317 break;
318 case 0x1:
319 return 0; /* reserved */
320 case 0x2:
321 return (cmd & 0xff) + 2; /* 2d commands */
322 case 0x3:
323 if (((cmd >> 24) & 0x1f) <= 0x18)
324 return 1;
325
326 switch ((cmd >> 24) & 0x1f) {
327 case 0x1c:
328 return 1;
329 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000330 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 case 0x3:
332 return (cmd & 0x1f) + 2;
333 case 0x4:
334 return (cmd & 0xf) + 2;
335 default:
336 return (cmd & 0xffff) + 2;
337 }
338 case 0x1e:
339 if (cmd & (1 << 23))
340 return (cmd & 0xffff) + 1;
341 else
342 return 1;
343 case 0x1f:
344 if ((cmd & (1 << 23)) == 0) /* inline vertices */
345 return (cmd & 0x1ffff) + 2;
346 else if (cmd & (1 << 17)) /* indirect random */
347 if ((cmd & 0xffff) == 0)
348 return 0; /* unknown length, too hard */
349 else
350 return (((cmd & 0xffff) + 1) / 2) + 1;
351 else
352 return 2; /* indirect sequential */
353 default:
354 return 0;
355 }
356 default:
357 return 0;
358 }
359
360 return 0;
361}
362
Robin Schroer1a5036b2014-06-02 16:59:39 +0200363static int i915_emit_cmds(struct drm_device *dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300365 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100366 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100368 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->buffer->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000369 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100372 int sz = validate_cmd(buffer[i]);
Robin Schroer1a5036b2014-06-02 16:59:39 +0200373
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100374 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000375 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100376 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100379 ret = BEGIN_LP_RING((dwords+1)&~1);
380 if (ret)
381 return ret;
382
383 for (i = 0; i < dwords; i++)
384 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100385 if (dwords & 1)
386 OUT_RING(0);
387
388 ADVANCE_LP_RING();
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391}
392
Eric Anholt673a3942008-07-30 12:06:12 -0700393int
394i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000395 struct drm_clip_rect *box,
396 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100398 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100399 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000401 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
402 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000404 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000405 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100408 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100409 ret = BEGIN_LP_RING(4);
410 if (ret)
411 return ret;
412
Alan Hourihanec29b6692006-08-12 16:29:24 +1000413 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000414 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
415 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000416 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000417 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100418 ret = BEGIN_LP_RING(6);
419 if (ret)
420 return ret;
421
Alan Hourihanec29b6692006-08-12 16:29:24 +1000422 OUT_RING(GFX_OP_DRAWRECT_INFO);
423 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000424 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
425 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000426 OUT_RING(DR4);
427 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000428 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100429 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 return 0;
432}
433
Alan Hourihanec29b6692006-08-12 16:29:24 +1000434/* XXX: Emitting the counter should really be moved to part of the IRQ
435 * emit. For now, do it in both places:
436 */
437
Dave Airlie84b1fd12007-07-11 15:53:27 +1000438static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100439{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300440 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000441 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100442
Daniel Vetter231f42a2012-11-02 19:55:05 +0100443 dev_priv->dri1.counter++;
444 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
445 dev_priv->dri1.counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000446 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100447 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Dave Airliede227f52006-01-25 15:31:43 +1100448
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100449 if (BEGIN_LP_RING(4) == 0) {
450 OUT_RING(MI_STORE_DWORD_INDEX);
451 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100452 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100453 OUT_RING(0);
454 ADVANCE_LP_RING();
455 }
Dave Airliede227f52006-01-25 15:31:43 +1100456}
457
Robin Schroer1a5036b2014-06-02 16:59:39 +0200458static int i915_dispatch_cmdbuffer(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700459 drm_i915_cmdbuffer_t *cmd,
460 struct drm_clip_rect *cliprects,
461 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 int nbox = cmd->num_cliprects;
464 int i = 0, count, ret;
465
466 if (cmd->sz & 0x3) {
467 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
471 i915_kernel_lost_context(dev);
472
473 count = nbox ? nbox : 1;
474
475 for (i = 0; i < count; i++) {
476 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000477 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 cmd->DR1, cmd->DR4);
479 if (ret)
480 return ret;
481 }
482
Eric Anholt201361a2009-03-11 12:30:04 -0700483 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (ret)
485 return ret;
486 }
487
Dave Airliede227f52006-01-25 15:31:43 +1100488 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return 0;
490}
491
Robin Schroer1a5036b2014-06-02 16:59:39 +0200492static int i915_dispatch_batchbuffer(struct drm_device *dev,
493 drm_i915_batchbuffer_t *batch,
Eric Anholt201361a2009-03-11 12:30:04 -0700494 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100496 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100498 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if ((batch->start | batch->used) & 0x7) {
501 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
505 i915_kernel_lost_context(dev);
506
507 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 for (i = 0; i < count; i++) {
509 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000510 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100511 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (ret)
513 return ret;
514 }
515
Keith Packard0790d5e2008-07-30 12:28:47 -0700516 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100517 ret = BEGIN_LP_RING(2);
518 if (ret)
519 return ret;
520
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100521 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000522 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
523 OUT_RING(batch->start);
524 } else {
525 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
526 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100529 ret = BEGIN_LP_RING(4);
530 if (ret)
531 return ret;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 OUT_RING(MI_BATCH_BUFFER);
534 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
535 OUT_RING(batch->start + batch->used - 4);
536 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100538 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
Zou Nan hai1cafd342010-06-25 13:40:24 +0800541
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100542 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100543 if (BEGIN_LP_RING(2) == 0) {
544 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
545 OUT_RING(MI_NOOP);
546 ADVANCE_LP_RING();
547 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100550 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
Robin Schroer1a5036b2014-06-02 16:59:39 +0200554static int i915_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300556 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000557 struct drm_i915_master_private *master_priv =
558 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100559 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dave Airlie7c1c2872008-11-28 14:22:24 +1000561 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400562 return -EINVAL;
563
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800564 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800565 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200566 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800567 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dave Airlieaf6061a2008-05-07 12:15:39 +1000569 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100571 ret = BEGIN_LP_RING(10);
572 if (ret)
573 return ret;
574
Jesse Barnes585fb112008-07-29 11:54:06 -0700575 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000576 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Dave Airlieaf6061a2008-05-07 12:15:39 +1000578 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
579 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200580 if (dev_priv->dri1.current_page == 0) {
581 OUT_RING(dev_priv->dri1.back_offset);
582 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000583 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200584 OUT_RING(dev_priv->dri1.front_offset);
585 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000586 }
587 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000588
Dave Airlieaf6061a2008-05-07 12:15:39 +1000589 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
590 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100591
Dave Airlieaf6061a2008-05-07 12:15:39 +1000592 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000593
Daniel Vetter231f42a2012-11-02 19:55:05 +0100594 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000595
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100596 if (BEGIN_LP_RING(4) == 0) {
597 OUT_RING(MI_STORE_DWORD_INDEX);
598 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100599 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100600 OUT_RING(0);
601 ADVANCE_LP_RING();
602 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000603
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200604 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000608static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 i915_kernel_lost_context(dev);
Chris Wilson3e960502012-11-27 16:22:54 +0000611 return intel_ring_idle(LP_RING(dev->dev_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Eric Anholtc153f452007-09-03 12:06:45 +1000614static int i915_flush_ioctl(struct drm_device *dev, void *data,
615 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Eric Anholt546b0972008-09-01 16:45:29 -0700617 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200619 if (drm_core_check_feature(dev, DRIVER_MODESET))
620 return -ENODEV;
621
Eric Anholt546b0972008-09-01 16:45:29 -0700622 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
623
624 mutex_lock(&dev->struct_mutex);
625 ret = i915_quiescent(dev);
626 mutex_unlock(&dev->struct_mutex);
627
628 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Eric Anholtc153f452007-09-03 12:06:45 +1000631static int i915_batchbuffer(struct drm_device *dev, void *data,
632 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300634 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100635 struct drm_i915_master_private *master_priv;
636 drm_i915_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000637 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700639 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200641 if (drm_core_check_feature(dev, DRIVER_MODESET))
642 return -ENODEV;
643
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100644 master_priv = dev->primary->master->driver_priv;
645 sarea_priv = (drm_i915_sarea_t *) master_priv->sarea_priv;
646
Daniel Vetter87813422012-05-02 11:49:32 +0200647 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000649 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800652 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800653 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Eric Anholt546b0972008-09-01 16:45:29 -0700655 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Eric Anholt201361a2009-03-11 12:30:04 -0700657 if (batch->num_cliprects < 0)
658 return -EINVAL;
659
660 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700661 cliprects = kcalloc(batch->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200662 sizeof(*cliprects),
Eric Anholt9a298b22009-03-24 12:23:04 -0700663 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700664 if (cliprects == NULL)
665 return -ENOMEM;
666
667 ret = copy_from_user(cliprects, batch->cliprects,
668 batch->num_cliprects *
669 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200670 if (ret != 0) {
671 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700672 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200673 }
Eric Anholt201361a2009-03-11 12:30:04 -0700674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Eric Anholt546b0972008-09-01 16:45:29 -0700676 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700677 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700678 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400680 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000681 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700682
683fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700684 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return ret;
687}
688
Eric Anholtc153f452007-09-03 12:06:45 +1000689static int i915_cmdbuffer(struct drm_device *dev, void *data,
690 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300692 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100693 struct drm_i915_master_private *master_priv;
694 drm_i915_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000695 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700696 struct drm_clip_rect *cliprects = NULL;
697 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int ret;
699
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800700 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800701 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200703 if (drm_core_check_feature(dev, DRIVER_MODESET))
704 return -ENODEV;
705
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100706 master_priv = dev->primary->master->driver_priv;
707 sarea_priv = (drm_i915_sarea_t *) master_priv->sarea_priv;
708
Eric Anholt546b0972008-09-01 16:45:29 -0700709 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Eric Anholt201361a2009-03-11 12:30:04 -0700711 if (cmdbuf->num_cliprects < 0)
712 return -EINVAL;
713
Eric Anholt9a298b22009-03-24 12:23:04 -0700714 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700715 if (batch_data == NULL)
716 return -ENOMEM;
717
718 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200719 if (ret != 0) {
720 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700721 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200722 }
Eric Anholt201361a2009-03-11 12:30:04 -0700723
724 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700725 cliprects = kcalloc(cmdbuf->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200726 sizeof(*cliprects), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000727 if (cliprects == NULL) {
728 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700729 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000730 }
Eric Anholt201361a2009-03-11 12:30:04 -0700731
732 ret = copy_from_user(cliprects, cmdbuf->cliprects,
733 cmdbuf->num_cliprects *
734 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200735 if (ret != 0) {
736 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700737 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Eric Anholt546b0972008-09-01 16:45:29 -0700741 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700742 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700743 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (ret) {
745 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000746 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400749 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000750 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700751
Eric Anholt201361a2009-03-11 12:30:04 -0700752fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700753 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000754fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700755 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700756
757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Robin Schroer1a5036b2014-06-02 16:59:39 +0200760static int i915_emit_irq(struct drm_device *dev)
Daniel Vetter94888672012-04-26 23:28:08 +0200761{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300762 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200763 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
764
765 i915_kernel_lost_context(dev);
766
767 DRM_DEBUG_DRIVER("\n");
768
Daniel Vetter231f42a2012-11-02 19:55:05 +0100769 dev_priv->dri1.counter++;
770 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
771 dev_priv->dri1.counter = 1;
Daniel Vetter94888672012-04-26 23:28:08 +0200772 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100773 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200774
775 if (BEGIN_LP_RING(4) == 0) {
776 OUT_RING(MI_STORE_DWORD_INDEX);
777 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100778 OUT_RING(dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200779 OUT_RING(MI_USER_INTERRUPT);
780 ADVANCE_LP_RING();
781 }
782
Daniel Vetter231f42a2012-11-02 19:55:05 +0100783 return dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200784}
785
Robin Schroer1a5036b2014-06-02 16:59:39 +0200786static int i915_wait_irq(struct drm_device *dev, int irq_nr)
Daniel Vetter94888672012-04-26 23:28:08 +0200787{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300788 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200789 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
790 int ret = 0;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100791 struct intel_engine_cs *ring = LP_RING(dev_priv);
Daniel Vetter94888672012-04-26 23:28:08 +0200792
793 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
794 READ_BREADCRUMB(dev_priv));
795
796 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
797 if (master_priv->sarea_priv)
798 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
799 return 0;
800 }
801
802 if (master_priv->sarea_priv)
803 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
804
805 if (ring->irq_get(ring)) {
Daniel Vetterbfd83032013-12-11 11:34:41 +0100806 DRM_WAIT_ON(ret, ring->irq_queue, 3 * HZ,
Daniel Vetter94888672012-04-26 23:28:08 +0200807 READ_BREADCRUMB(dev_priv) >= irq_nr);
808 ring->irq_put(ring);
809 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
810 ret = -EBUSY;
811
812 if (ret == -EBUSY) {
813 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Daniel Vetter231f42a2012-11-02 19:55:05 +0100814 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200815 }
816
817 return ret;
818}
819
820/* Needs the lock as it touches the ring.
821 */
822static int i915_irq_emit(struct drm_device *dev, void *data,
823 struct drm_file *file_priv)
824{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300825 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200826 drm_i915_irq_emit_t *emit = data;
827 int result;
828
829 if (drm_core_check_feature(dev, DRIVER_MODESET))
830 return -ENODEV;
831
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100832 if (!dev_priv || !LP_RING(dev_priv)->buffer->virtual_start) {
Daniel Vetter94888672012-04-26 23:28:08 +0200833 DRM_ERROR("called with no initialization\n");
834 return -EINVAL;
835 }
836
837 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
838
839 mutex_lock(&dev->struct_mutex);
840 result = i915_emit_irq(dev);
841 mutex_unlock(&dev->struct_mutex);
842
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100843 if (copy_to_user(emit->irq_seq, &result, sizeof(int))) {
Daniel Vetter94888672012-04-26 23:28:08 +0200844 DRM_ERROR("copy_to_user\n");
845 return -EFAULT;
846 }
847
848 return 0;
849}
850
851/* Doesn't need the hardware lock.
852 */
853static int i915_irq_wait(struct drm_device *dev, void *data,
854 struct drm_file *file_priv)
855{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300856 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200857 drm_i915_irq_wait_t *irqwait = data;
858
859 if (drm_core_check_feature(dev, DRIVER_MODESET))
860 return -ENODEV;
861
862 if (!dev_priv) {
863 DRM_ERROR("called with no initialization\n");
864 return -EINVAL;
865 }
866
867 return i915_wait_irq(dev, irqwait->irq_seq);
868}
869
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200870static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
871 struct drm_file *file_priv)
872{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300873 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200874 drm_i915_vblank_pipe_t *pipe = data;
875
876 if (drm_core_check_feature(dev, DRIVER_MODESET))
877 return -ENODEV;
878
879 if (!dev_priv) {
880 DRM_ERROR("called with no initialization\n");
881 return -EINVAL;
882 }
883
884 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
885
886 return 0;
887}
888
889/**
890 * Schedule buffer swap at given vertical blank.
891 */
892static int i915_vblank_swap(struct drm_device *dev, void *data,
893 struct drm_file *file_priv)
894{
895 /* The delayed swap mechanism was fundamentally racy, and has been
896 * removed. The model was that the client requested a delayed flip/swap
897 * from the kernel, then waited for vblank before continuing to perform
898 * rendering. The problem was that the kernel might wake the client
899 * up before it dispatched the vblank swap (since the lock has to be
900 * held while touching the ringbuffer), in which case the client would
901 * clear and start the next frame before the swap occurred, and
902 * flicker would occur in addition to likely missing the vblank.
903 *
904 * In the absence of this ioctl, userland falls back to a correct path
905 * of waiting for a vblank, then dispatching the swap on its own.
906 * Context switching to userland and back is plenty fast enough for
907 * meeting the requirements of vblank swapping.
908 */
909 return -EINVAL;
910}
911
Eric Anholtc153f452007-09-03 12:06:45 +1000912static int i915_flip_bufs(struct drm_device *dev, void *data,
913 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Eric Anholt546b0972008-09-01 16:45:29 -0700915 int ret;
916
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200917 if (drm_core_check_feature(dev, DRIVER_MODESET))
918 return -ENODEV;
919
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800920 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Eric Anholt546b0972008-09-01 16:45:29 -0700922 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Eric Anholt546b0972008-09-01 16:45:29 -0700924 mutex_lock(&dev->struct_mutex);
925 ret = i915_dispatch_flip(dev);
926 mutex_unlock(&dev->struct_mutex);
927
928 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Eric Anholtc153f452007-09-03 12:06:45 +1000931static int i915_getparam(struct drm_device *dev, void *data,
932 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300934 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000935 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int value;
937
938 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000939 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000940 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
Eric Anholtc153f452007-09-03 12:06:45 +1000943 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700945 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
947 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200948 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100950 case I915_PARAM_LAST_DISPATCH:
951 value = READ_BREADCRUMB(dev_priv);
952 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400953 case I915_PARAM_CHIPSET_ID:
Ville Syrjäläffbab09b2013-10-04 14:53:40 +0300954 value = dev->pdev->device;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400955 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700956 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200957 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700958 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800959 case I915_PARAM_NUM_FENCES_AVAIL:
960 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
961 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200962 case I915_PARAM_HAS_OVERLAY:
963 value = dev_priv->overlay ? 1 : 0;
964 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800965 case I915_PARAM_HAS_PAGEFLIPPING:
966 value = 1;
967 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500968 case I915_PARAM_HAS_EXECBUF2:
969 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200970 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500971 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800972 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100973 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800974 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100975 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100976 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100977 break;
Xiang, Haihaoa1f2cc72013-05-28 19:22:34 -0700978 case I915_PARAM_HAS_VEBOX:
979 value = intel_ring_initialized(&dev_priv->ring[VECS]);
980 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100981 case I915_PARAM_HAS_RELAXED_FENCING:
982 value = 1;
983 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100984 case I915_PARAM_HAS_COHERENT_RINGS:
985 value = 1;
986 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000987 case I915_PARAM_HAS_EXEC_CONSTANTS:
988 value = INTEL_INFO(dev)->gen >= 4;
989 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000990 case I915_PARAM_HAS_RELAXED_DELTA:
991 value = 1;
992 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800993 case I915_PARAM_HAS_GEN7_SOL_RESET:
994 value = 1;
995 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200996 case I915_PARAM_HAS_LLC:
997 value = HAS_LLC(dev);
998 break;
Chris Wilson651d7942013-08-08 14:41:10 +0100999 case I915_PARAM_HAS_WT:
1000 value = HAS_WT(dev);
1001 break;
Daniel Vetter777ee962012-02-15 23:50:25 +01001002 case I915_PARAM_HAS_ALIASING_PPGTT:
Daniel Vetter896ab1a2014-08-06 15:04:51 +02001003 value = USES_PPGTT(dev);
Daniel Vetter777ee962012-02-15 23:50:25 +01001004 break;
Ben Widawsky172cf152012-06-05 15:24:25 -07001005 case I915_PARAM_HAS_WAIT_TIMEOUT:
1006 value = 1;
1007 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +01001008 case I915_PARAM_HAS_SEMAPHORES:
1009 value = i915_semaphore_is_enabled(dev);
1010 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +10001011 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
1012 value = 1;
1013 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001014 case I915_PARAM_HAS_SECURE_BATCHES:
1015 value = capable(CAP_SYS_ADMIN);
1016 break;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001017 case I915_PARAM_HAS_PINNED_BATCHES:
1018 value = 1;
1019 break;
Daniel Vettered5982e2013-01-17 22:23:36 +01001020 case I915_PARAM_HAS_EXEC_NO_RELOC:
1021 value = 1;
1022 break;
Chris Wilsoneef90cc2013-01-08 10:53:17 +00001023 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
1024 value = 1;
1025 break;
Brad Volkind728c8e2014-02-18 10:15:56 -08001026 case I915_PARAM_CMD_PARSER_VERSION:
1027 value = i915_cmd_parser_get_version();
1028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 default:
Ben Widawskye29c32d2013-05-31 11:28:45 -07001030 DRM_DEBUG("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001031 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001034 if (copy_to_user(param->value, &value, sizeof(int))) {
1035 DRM_ERROR("copy_to_user failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001036 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 return 0;
1040}
1041
Eric Anholtc153f452007-09-03 12:06:45 +10001042static int i915_setparam(struct drm_device *dev, void *data,
1043 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001045 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001046 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001049 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001050 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
Eric Anholtc153f452007-09-03 12:06:45 +10001053 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 break;
1058 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001059 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001061 case I915_SETPARAM_NUM_USED_FENCES:
1062 if (param->value > dev_priv->num_fence_regs ||
1063 param->value < 0)
1064 return -EINVAL;
1065 /* Userspace can use first N regs */
1066 dev_priv->fence_reg_start = param->value;
1067 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001069 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001070 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001071 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
1074 return 0;
1075}
1076
Eric Anholtc153f452007-09-03 12:06:45 +10001077static int i915_set_status_page(struct drm_device *dev, void *data,
1078 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001079{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001080 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001081 drm_i915_hws_addr_t *hws = data;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001082 struct intel_engine_cs *ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001083
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001084 if (drm_core_check_feature(dev, DRIVER_MODESET))
1085 return -ENODEV;
1086
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001087 if (!I915_NEED_GFX_HWS(dev))
1088 return -EINVAL;
1089
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001090 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001091 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001092 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001093 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001094
Jesse Barnes79e53942008-11-07 14:24:08 -08001095 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1096 WARN(1, "tried to set status page when mode setting active\n");
1097 return 0;
1098 }
1099
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001100 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001101
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001102 ring = LP_RING(dev_priv);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001103 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001104
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001105 dev_priv->dri1.gfx_hws_cpu_addr =
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001106 ioremap_wc(dev_priv->gtt.mappable_base + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001107 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001108 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001109 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001110 DRM_ERROR("can not ioremap virtual address for"
1111 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001112 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001113 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001114
1115 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001116 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001117
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001118 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001119 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001120 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001121 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001122 return 0;
1123}
1124
Dave Airlieec2a4c32009-08-04 11:43:41 +10001125static int i915_get_bridge_dev(struct drm_device *dev)
1126{
1127 struct drm_i915_private *dev_priv = dev->dev_private;
1128
Akshay Joshi0206e352011-08-16 15:34:10 -04001129 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001130 if (!dev_priv->bridge_dev) {
1131 DRM_ERROR("bridge device not found\n");
1132 return -1;
1133 }
1134 return 0;
1135}
1136
Zhenyu Wangc48044112009-12-17 14:48:43 +08001137#define MCHBAR_I915 0x44
1138#define MCHBAR_I965 0x48
1139#define MCHBAR_SIZE (4*4096)
1140
1141#define DEVEN_REG 0x54
1142#define DEVEN_MCHBAR_EN (1 << 28)
1143
1144/* Allocate space for the MCH regs if needed, return nonzero on error */
1145static int
1146intel_alloc_mchbar_resource(struct drm_device *dev)
1147{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001148 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001149 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001150 u32 temp_lo, temp_hi = 0;
1151 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001152 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001153
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001154 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001155 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1156 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1157 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1158
1159 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1160#ifdef CONFIG_PNP
1161 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001162 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1163 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001164#endif
1165
1166 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001167 dev_priv->mch_res.name = "i915 MCHBAR";
1168 dev_priv->mch_res.flags = IORESOURCE_MEM;
1169 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1170 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001171 MCHBAR_SIZE, MCHBAR_SIZE,
1172 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001173 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001174 dev_priv->bridge_dev);
1175 if (ret) {
1176 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1177 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001178 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001179 }
1180
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001181 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001182 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1183 upper_32_bits(dev_priv->mch_res.start));
1184
1185 pci_write_config_dword(dev_priv->bridge_dev, reg,
1186 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001187 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001188}
1189
1190/* Setup MCHBAR if possible, return true if we should disable it again */
1191static void
1192intel_setup_mchbar(struct drm_device *dev)
1193{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001194 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001195 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001196 u32 temp;
1197 bool enabled;
1198
Jesse Barnes11ea8b72014-03-03 14:27:57 -08001199 if (IS_VALLEYVIEW(dev))
1200 return;
1201
Zhenyu Wangc48044112009-12-17 14:48:43 +08001202 dev_priv->mchbar_need_disable = false;
1203
1204 if (IS_I915G(dev) || IS_I915GM(dev)) {
1205 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1206 enabled = !!(temp & DEVEN_MCHBAR_EN);
1207 } else {
1208 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1209 enabled = temp & 1;
1210 }
1211
1212 /* If it's already enabled, don't have to do anything */
1213 if (enabled)
1214 return;
1215
1216 if (intel_alloc_mchbar_resource(dev))
1217 return;
1218
1219 dev_priv->mchbar_need_disable = true;
1220
1221 /* Space is allocated or reserved, so enable it. */
1222 if (IS_I915G(dev) || IS_I915GM(dev)) {
1223 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1224 temp | DEVEN_MCHBAR_EN);
1225 } else {
1226 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1227 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1228 }
1229}
1230
1231static void
1232intel_teardown_mchbar(struct drm_device *dev)
1233{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001234 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001235 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001236 u32 temp;
1237
1238 if (dev_priv->mchbar_need_disable) {
1239 if (IS_I915G(dev) || IS_I915GM(dev)) {
1240 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1241 temp &= ~DEVEN_MCHBAR_EN;
1242 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1243 } else {
1244 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1245 temp &= ~1;
1246 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1247 }
1248 }
1249
1250 if (dev_priv->mch_res.start)
1251 release_resource(&dev_priv->mch_res);
1252}
1253
Dave Airlie28d52042009-09-21 14:33:58 +10001254/* true = enable decode, false = disable decoder */
1255static unsigned int i915_vga_set_decode(void *cookie, bool state)
1256{
1257 struct drm_device *dev = cookie;
1258
1259 intel_modeset_vga_set_state(dev, state);
1260 if (state)
1261 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1262 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1263 else
1264 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1265}
1266
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001267static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1268{
1269 struct drm_device *dev = pci_get_drvdata(pdev);
1270 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
Robin Schroer1a5036b2014-06-02 16:59:39 +02001271
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001272 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001273 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001274 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001275 /* i915 resume handler doesn't set to D0 */
1276 pci_set_power_state(dev->pdev, PCI_D0);
1277 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001278 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001279 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001280 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001281 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001282 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001283 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001284 }
1285}
1286
1287static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1288{
1289 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001290
Daniel Vetterfc8fd402013-11-03 20:46:34 +01001291 /*
1292 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1293 * locking inversion with the driver load path. And the access here is
1294 * completely racy anyway. So don't bother with locking for now.
1295 */
1296 return dev->open_count == 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001297}
1298
Takashi Iwai26ec6852012-05-11 07:51:17 +02001299static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1300 .set_gpu_state = i915_switcheroo_set_state,
1301 .reprobe = NULL,
1302 .can_switch = i915_switcheroo_can_switch,
1303};
1304
Chris Wilson2c7111d2011-03-29 10:40:27 +01001305static int i915_load_modeset_init(struct drm_device *dev)
1306{
1307 struct drm_i915_private *dev_priv = dev->dev_private;
1308 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001309
Bryan Freed6d139a82010-10-14 09:14:51 +01001310 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001311 if (ret)
1312 DRM_INFO("failed to find VBIOS tables\n");
1313
Chris Wilson934f992c2011-01-20 13:09:12 +00001314 /* If we have > 1 VGA cards, then we need to arbitrate access
1315 * to the common VGA resources.
1316 *
1317 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1318 * then we do not take part in VGA arbitration and the
1319 * vga_client_register() fails with -ENODEV.
1320 */
Dave Airlieebff5fa92013-10-11 15:12:04 +10001321 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1322 if (ret && ret != -ENODEV)
1323 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001324
Jesse Barnes723bfd72010-10-07 16:01:13 -07001325 intel_register_dsm_handler();
1326
Dave Airlie0d697042012-09-10 12:28:36 +10001327 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001328 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001329 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001330
Chris Wilson9797fbf2012-04-24 15:47:39 +01001331 /* Initialise stolen first so that we may reserve preallocated
1332 * objects for the BIOS to KMS transition.
1333 */
1334 ret = i915_gem_init_stolen(dev);
1335 if (ret)
1336 goto cleanup_vga_switcheroo;
1337
Imre Deake13192f2014-02-18 00:02:15 +02001338 intel_power_domains_init_hw(dev_priv);
1339
Daniel Vetterbb0f1b52013-11-03 21:09:27 +01001340 ret = drm_irq_install(dev, dev->pdev->irq);
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001341 if (ret)
1342 goto cleanup_gem_stolen;
1343
Jesse Barnesed2e6df2014-06-20 09:39:36 -07001344 dev_priv->pm._irqs_disabled = false;
1345
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001346 /* Important: The output setup functions called by modeset_init need
1347 * working irqs for e.g. gmbus and dp aux transfers. */
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001348 intel_modeset_init(dev);
1349
Chris Wilson1070a422012-04-24 15:47:41 +01001350 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001351 if (ret)
Imre Deak713028b2014-04-25 17:28:00 +03001352 goto cleanup_irq;
Chris Wilson2c7111d2011-03-29 10:40:27 +01001353
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001354 intel_modeset_gem_init(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001355
Jesse Barnes79e53942008-11-07 14:24:08 -08001356 /* Always safe in the mode setting case. */
1357 /* FIXME: do pre/post-mode set stuff in core KMS code */
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001358 dev->vblank_disable_allowed = true;
Imre Deak713028b2014-04-25 17:28:00 +03001359 if (INTEL_INFO(dev)->num_pipes == 0)
Ben Widawskye3c74752013-04-05 13:12:39 -07001360 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001361
Chris Wilson5a793952010-06-06 10:50:03 +01001362 ret = intel_fbdev_init(dev);
1363 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001364 goto cleanup_gem;
1365
1366 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter20afbda2012-12-11 14:05:07 +01001367 intel_hpd_init(dev);
1368
1369 /*
1370 * Some ports require correctly set-up hpd registers for detection to
1371 * work properly (leading to ghost connected connector status), e.g. VGA
1372 * on gm45. Hence we can only set up the initial fbdev config after hpd
1373 * irqs are fully enabled. Now we should scan for the initial config
1374 * only once hotplug handling is enabled, but due to screwed-up locking
1375 * around kms/fbdev init we can't protect the fdbev initial config
1376 * scanning against hotplug events. Hence do this first and ignore the
1377 * tiny window where we will loose hotplug notifactions.
1378 */
Jesse Barnesd1d70672014-05-28 14:39:03 -07001379 async_schedule(intel_fbdev_initial_config, dev_priv);
Daniel Vetter20afbda2012-12-11 14:05:07 +01001380
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001381 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001382
Jesse Barnes79e53942008-11-07 14:24:08 -08001383 return 0;
1384
Chris Wilson2c7111d2011-03-29 10:40:27 +01001385cleanup_gem:
1386 mutex_lock(&dev->struct_mutex);
1387 i915_gem_cleanup_ringbuffer(dev);
Ben Widawsky55d23282013-05-25 12:26:39 -07001388 i915_gem_context_fini(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001389 mutex_unlock(&dev->struct_mutex);
Imre Deak713028b2014-04-25 17:28:00 +03001390cleanup_irq:
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001391 drm_irq_uninstall(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001392cleanup_gem_stolen:
1393 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001394cleanup_vga_switcheroo:
1395 vga_switcheroo_unregister_client(dev->pdev);
1396cleanup_vga_client:
1397 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001398out:
1399 return ret;
1400}
1401
Dave Airlie7c1c2872008-11-28 14:22:24 +10001402int i915_master_create(struct drm_device *dev, struct drm_master *master)
1403{
1404 struct drm_i915_master_private *master_priv;
1405
Eric Anholt9a298b22009-03-24 12:23:04 -07001406 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001407 if (!master_priv)
1408 return -ENOMEM;
1409
1410 master->driver_priv = master_priv;
1411 return 0;
1412}
1413
1414void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1415{
1416 struct drm_i915_master_private *master_priv = master->driver_priv;
1417
1418 if (!master_priv)
1419 return;
1420
Eric Anholt9a298b22009-03-24 12:23:04 -07001421 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001422
1423 master->driver_priv = NULL;
1424}
1425
Daniel Vetter243eaf32013-12-17 10:00:54 +01001426#if IS_ENABLED(CONFIG_FB)
Chris Wilsonf96de582013-12-16 15:57:40 +00001427static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
Daniel Vettere1887192012-06-12 11:28:17 +02001428{
1429 struct apertures_struct *ap;
1430 struct pci_dev *pdev = dev_priv->dev->pdev;
1431 bool primary;
Chris Wilsonf96de582013-12-16 15:57:40 +00001432 int ret;
Daniel Vettere1887192012-06-12 11:28:17 +02001433
1434 ap = alloc_apertures(1);
1435 if (!ap)
Chris Wilsonf96de582013-12-16 15:57:40 +00001436 return -ENOMEM;
Daniel Vettere1887192012-06-12 11:28:17 +02001437
Ben Widawskydabb7a92013-01-17 12:45:16 -08001438 ap->ranges[0].base = dev_priv->gtt.mappable_base;
Ben Widawskyf64e2922013-05-25 12:26:36 -07001439 ap->ranges[0].size = dev_priv->gtt.mappable_end;
Ben Widawsky93d18792013-01-17 12:45:17 -08001440
Daniel Vettere1887192012-06-12 11:28:17 +02001441 primary =
1442 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1443
Chris Wilsonf96de582013-12-16 15:57:40 +00001444 ret = remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
Daniel Vettere1887192012-06-12 11:28:17 +02001445
1446 kfree(ap);
Chris Wilsonf96de582013-12-16 15:57:40 +00001447
1448 return ret;
Daniel Vettere1887192012-06-12 11:28:17 +02001449}
Daniel Vetter4520f532013-10-09 09:18:51 +02001450#else
Chris Wilsonf96de582013-12-16 15:57:40 +00001451static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
Daniel Vetter4520f532013-10-09 09:18:51 +02001452{
Chris Wilsonf96de582013-12-16 15:57:40 +00001453 return 0;
Daniel Vetter4520f532013-10-09 09:18:51 +02001454}
1455#endif
Daniel Vettere1887192012-06-12 11:28:17 +02001456
Daniel Vettera4de0522014-06-05 16:20:46 +02001457#if !defined(CONFIG_VGA_CONSOLE)
1458static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1459{
1460 return 0;
1461}
1462#elif !defined(CONFIG_DUMMY_CONSOLE)
1463static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1464{
1465 return -ENODEV;
1466}
1467#else
1468static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1469{
Daniel Vetter1bb9e632014-07-08 10:02:43 +02001470 int ret = 0;
Daniel Vettera4de0522014-06-05 16:20:46 +02001471
1472 DRM_INFO("Replacing VGA console driver\n");
1473
1474 console_lock();
Daniel Vetter1bb9e632014-07-08 10:02:43 +02001475 if (con_is_bound(&vga_con))
1476 ret = do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES - 1, 1);
Daniel Vettera4de0522014-06-05 16:20:46 +02001477 if (ret == 0) {
1478 ret = do_unregister_con_driver(&vga_con);
1479
1480 /* Ignore "already unregistered". */
1481 if (ret == -ENODEV)
1482 ret = 0;
1483 }
1484 console_unlock();
1485
1486 return ret;
1487}
1488#endif
1489
Daniel Vetterc96ea642012-08-08 22:01:51 +02001490static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1491{
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001492 const struct intel_device_info *info = &dev_priv->info;
Daniel Vetterc96ea642012-08-08 22:01:51 +02001493
Damien Lespiaue2a58002013-04-23 16:38:34 +01001494#define PRINT_S(name) "%s"
1495#define SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001496#define PRINT_FLAG(name) info->name ? #name "," : ""
1497#define SEP_COMMA ,
Ville Syrjälä19c656a2014-06-13 15:39:56 +03001498 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x rev=0x%02x flags="
Damien Lespiaue2a58002013-04-23 16:38:34 +01001499 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
Daniel Vetterc96ea642012-08-08 22:01:51 +02001500 info->gen,
1501 dev_priv->dev->pdev->device,
Ville Syrjälä19c656a2014-06-13 15:39:56 +03001502 dev_priv->dev->pdev->revision,
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001503 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
Damien Lespiaue2a58002013-04-23 16:38:34 +01001504#undef PRINT_S
1505#undef SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001506#undef PRINT_FLAG
1507#undef SEP_COMMA
Daniel Vetterc96ea642012-08-08 22:01:51 +02001508}
1509
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001510/*
1511 * Determine various intel_device_info fields at runtime.
1512 *
1513 * Use it when either:
1514 * - it's judged too laborious to fill n static structures with the limit
1515 * when a simple if statement does the job,
1516 * - run-time checks (eg read fuse/strap registers) are needed.
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001517 *
1518 * This function needs to be called:
1519 * - after the MMIO has been setup as we are reading registers,
1520 * - after the PCH has been detected,
1521 * - before the first usage of the fields it can tweak.
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001522 */
1523static void intel_device_info_runtime_init(struct drm_device *dev)
1524{
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001525 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001526 struct intel_device_info *info;
Damien Lespiaud615a162014-03-03 17:31:48 +00001527 enum pipe pipe;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001528
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001529 info = (struct intel_device_info *)&dev_priv->info;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001530
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001531 if (IS_VALLEYVIEW(dev))
Damien Lespiau055e3932014-08-18 13:49:10 +01001532 for_each_pipe(dev_priv, pipe)
Damien Lespiaud615a162014-03-03 17:31:48 +00001533 info->num_sprites[pipe] = 2;
1534 else
Damien Lespiau055e3932014-08-18 13:49:10 +01001535 for_each_pipe(dev_priv, pipe)
Damien Lespiaud615a162014-03-03 17:31:48 +00001536 info->num_sprites[pipe] = 1;
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001537
Damien Lespiaua0bae572014-02-10 17:20:55 +00001538 if (i915.disable_display) {
1539 DRM_INFO("Display disabled (module parameter)\n");
1540 info->num_pipes = 0;
1541 } else if (info->num_pipes > 0 &&
1542 (INTEL_INFO(dev)->gen == 7 || INTEL_INFO(dev)->gen == 8) &&
1543 !IS_VALLEYVIEW(dev)) {
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001544 u32 fuse_strap = I915_READ(FUSE_STRAP);
1545 u32 sfuse_strap = I915_READ(SFUSE_STRAP);
1546
1547 /*
1548 * SFUSE_STRAP is supposed to have a bit signalling the display
1549 * is fused off. Unfortunately it seems that, at least in
1550 * certain cases, fused off display means that PCH display
1551 * reads don't land anywhere. In that case, we read 0s.
1552 *
1553 * On CPT/PPT, we can detect this case as SFUSE_STRAP_FUSE_LOCK
1554 * should be set when taking over after the firmware.
1555 */
1556 if (fuse_strap & ILK_INTERNAL_DISPLAY_DISABLE ||
1557 sfuse_strap & SFUSE_STRAP_DISPLAY_DISABLED ||
1558 (dev_priv->pch_type == PCH_CPT &&
1559 !(sfuse_strap & SFUSE_STRAP_FUSE_LOCK))) {
1560 DRM_INFO("Display fused off, disabling\n");
1561 info->num_pipes = 0;
1562 }
1563 }
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001564}
1565
Eric Anholt63ee41d2010-12-20 18:40:06 -08001566/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001567 * i915_driver_load - setup chip and create an initial config
1568 * @dev: DRM device
1569 * @flags: startup flags
1570 *
1571 * The driver load routine has to do several things:
1572 * - drive output discovery via intel_modeset_init()
1573 * - initialize the memory manager
1574 * - allocate initial config memory
1575 * - setup the DRM framebuffer with the allocated memory
1576 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001577int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001578{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001579 struct drm_i915_private *dev_priv;
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001580 struct intel_device_info *info, *device_info;
Chris Wilson934d6082012-09-14 11:57:46 +01001581 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001582 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001583
Daniel Vetter26394d92012-03-26 21:33:18 +02001584 info = (struct intel_device_info *) flags;
1585
1586 /* Refuse to load on gen6+ without kms enabled. */
Jani Nikulae147acc2013-10-10 15:25:37 +03001587 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET)) {
1588 DRM_INFO("Your hardware requires kernel modesetting (KMS)\n");
1589 DRM_INFO("See CONFIG_DRM_I915_KMS, nomodeset, and i915.modeset parameters\n");
Daniel Vetter26394d92012-03-26 21:33:18 +02001590 return -ENODEV;
Jani Nikulae147acc2013-10-10 15:25:37 +03001591 }
Daniel Vetter26394d92012-03-26 21:33:18 +02001592
Daniel Vetter24986ee2013-12-11 11:34:33 +01001593 /* UMS needs agp support. */
1594 if (!drm_core_check_feature(dev, DRIVER_MODESET) && !dev->agp)
1595 return -EINVAL;
1596
Daniel Vetterb14c5672013-09-19 12:18:32 +02001597 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001598 if (dev_priv == NULL)
1599 return -ENOMEM;
1600
Damien Lespiau755f68f2014-07-10 14:52:43 +01001601 dev->dev_private = dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001602 dev_priv->dev = dev;
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001603
Chris Wilson87f1f462014-08-09 19:18:42 +01001604 /* Setup the write-once "constant" device info */
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001605 device_info = (struct intel_device_info *)&dev_priv->info;
Chris Wilson87f1f462014-08-09 19:18:42 +01001606 memcpy(device_info, info, sizeof(dev_priv->info));
1607 device_info->device_id = dev->pdev->device;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001608
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001609 spin_lock_init(&dev_priv->irq_lock);
1610 spin_lock_init(&dev_priv->gpu_error.lock);
Jani Nikula58c68772013-11-08 16:48:54 +02001611 spin_lock_init(&dev_priv->backlight_lock);
Chris Wilson907b28c2013-07-19 20:36:52 +01001612 spin_lock_init(&dev_priv->uncore.lock);
Daniel Vetterc20e8352013-07-24 22:40:23 +02001613 spin_lock_init(&dev_priv->mm.object_stat_lock);
Sourab Gupta84c33a62014-06-02 16:47:17 +05301614 spin_lock_init(&dev_priv->mmio_flip_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001615 mutex_init(&dev_priv->dpio_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001616 mutex_init(&dev_priv->modeset_restore_lock);
1617
Daniel Vetterf742a552013-12-06 10:17:53 +01001618 intel_pm_setup(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03001619
Damien Lespiau07144422013-10-15 18:55:40 +01001620 intel_display_crc_init(dev);
1621
Daniel Vetterc96ea642012-08-08 22:01:51 +02001622 i915_dump_device_info(dev_priv);
1623
Paulo Zanonied1c9e22013-08-12 14:34:08 -03001624 /* Not all pre-production machines fall into this category, only the
1625 * very first ones. Almost everything should work, except for maybe
1626 * suspend/resume. And we don't implement workarounds that affect only
1627 * pre-production machines. */
1628 if (IS_HSW_EARLY_SDV(dev))
1629 DRM_INFO("This is an early pre-production Haswell machine. "
1630 "It may not be fully functional.\n");
1631
Dave Airlieec2a4c32009-08-04 11:43:41 +10001632 if (i915_get_bridge_dev(dev)) {
1633 ret = -EIO;
1634 goto free_priv;
1635 }
1636
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001637 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1638 /* Before gen4, the registers and the GTT are behind different BARs.
1639 * However, from gen4 onwards, the registers and the GTT are shared
1640 * in the same BAR, so we want to restrict this ioremap from
1641 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1642 * the register BAR remains the same size for all the earlier
1643 * generations up to Ironlake.
1644 */
1645 if (info->gen < 5)
1646 mmio_size = 512*1024;
1647 else
1648 mmio_size = 2*1024*1024;
1649
1650 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
1651 if (!dev_priv->regs) {
1652 DRM_ERROR("failed to map registers\n");
1653 ret = -EIO;
1654 goto put_bridge;
1655 }
1656
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001657 /* This must be called before any calls to HAS_PCH_* */
1658 intel_detect_pch(dev);
1659
1660 intel_uncore_init(dev);
1661
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001662 ret = i915_gem_gtt_init(dev);
1663 if (ret)
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001664 goto out_regs;
Daniel Vettere1887192012-06-12 11:28:17 +02001665
Daniel Vettera4de0522014-06-05 16:20:46 +02001666 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1667 ret = i915_kick_out_vgacon(dev_priv);
1668 if (ret) {
1669 DRM_ERROR("failed to remove conflicting VGA console\n");
1670 goto out_gtt;
1671 }
1672
Chris Wilsonf96de582013-12-16 15:57:40 +00001673 ret = i915_kick_out_firmware_fb(dev_priv);
1674 if (ret) {
1675 DRM_ERROR("failed to remove conflicting framebuffer drivers\n");
1676 goto out_gtt;
1677 }
Daniel Vettera4de0522014-06-05 16:20:46 +02001678 }
Daniel Vettere1887192012-06-12 11:28:17 +02001679
Dave Airlie466e69b2011-12-19 11:15:29 +00001680 pci_set_master(dev->pdev);
1681
Daniel Vetter9f82d232010-08-30 21:25:23 +02001682 /* overlay on gen2 is broken and can't address above 1G */
1683 if (IS_GEN2(dev))
1684 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1685
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001686 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1687 * using 32bit addressing, overwriting memory if HWS is located
1688 * above 4GB.
1689 *
1690 * The documentation also mentions an issue with undefined
1691 * behaviour if any general state is accessed within a page above 4GB,
1692 * which also needs to be handled carefully.
1693 */
1694 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1695 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1696
Ben Widawsky93d18792013-01-17 12:45:17 -08001697 aperture_size = dev_priv->gtt.mappable_end;
Chris Wilson71e93392010-10-27 18:46:52 +01001698
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001699 dev_priv->gtt.mappable =
1700 io_mapping_create_wc(dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001701 aperture_size);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001702 if (dev_priv->gtt.mappable == NULL) {
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001703 ret = -EIO;
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001704 goto out_gtt;
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001705 }
1706
Ben Widawsky911bdf02013-06-27 16:30:23 -07001707 dev_priv->gtt.mtrr = arch_phys_wc_add(dev_priv->gtt.mappable_base,
1708 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001709
Chris Wilsone642abb2010-09-09 12:46:34 +01001710 /* The i915 workqueue is primarily used for batched retirement of
1711 * requests (and thus managing bo) once the task has been completed
1712 * by the GPU. i915_gem_retire_requests() is called directly when we
1713 * need high-priority retirement, such as waiting for an explicit
1714 * bo.
1715 *
1716 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001717 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001718 *
1719 * All tasks on the workqueue are expected to acquire the dev mutex
1720 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001721 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001722 */
Tejun Heo53621862012-08-22 16:40:57 -07001723 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001724 if (dev_priv->wq == NULL) {
1725 DRM_ERROR("Failed to create our workqueue.\n");
1726 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001727 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001728 }
1729
Dave Airlie0e32b392014-05-02 14:02:48 +10001730 dev_priv->dp_wq = alloc_ordered_workqueue("i915-dp", 0);
1731 if (dev_priv->dp_wq == NULL) {
1732 DRM_ERROR("Failed to create our dp workqueue.\n");
1733 ret = -ENOMEM;
1734 goto out_freewq;
1735 }
1736
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001737 intel_irq_init(dev);
Ben Widawsky78511f22013-10-04 21:22:49 -07001738 intel_uncore_sanitize(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001739
Zhenyu Wangc48044112009-12-17 14:48:43 +08001740 /* Try to make sure MCHBAR is enabled before poking at it */
1741 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001742 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001743 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001744
Bryan Freed6d139a82010-10-14 09:14:51 +01001745 intel_setup_bios(dev);
1746
Eric Anholt673a3942008-07-30 12:06:12 -07001747 i915_gem_load(dev);
1748
Eric Anholted4cb412008-07-29 12:10:39 -07001749 /* On the 945G/GM, the chipset reports the MSI capability on the
1750 * integrated graphics even though the support isn't actually there
1751 * according to the published specs. It doesn't appear to function
1752 * correctly in testing on 945G.
1753 * This may be a side effect of MSI having been made available for PEG
1754 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001755 *
1756 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001757 * be lost or delayed, but we use them anyways to avoid
1758 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001759 */
Keith Packardb60678a2008-12-08 11:12:28 -08001760 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001761 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001762
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001763 intel_device_info_runtime_init(dev);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001764
Ben Widawskye3c74752013-04-05 13:12:39 -07001765 if (INTEL_INFO(dev)->num_pipes) {
1766 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1767 if (ret)
1768 goto out_gem_unload;
1769 }
Keith Packard52440212008-11-18 09:30:25 -08001770
Imre Deakda7e29b2014-02-18 00:02:02 +02001771 intel_power_domains_init(dev_priv);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001772
Jesse Barnes79e53942008-11-07 14:24:08 -08001773 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001774 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001775 if (ret < 0) {
1776 DRM_ERROR("failed to init modeset\n");
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001777 goto out_power_well;
Jesse Barnes79e53942008-11-07 14:24:08 -08001778 }
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001779 } else {
1780 /* Start out suspended in ums mode. */
1781 dev_priv->ums.mm_suspended = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001782 }
1783
Ben Widawsky0136db52012-04-10 21:17:01 -07001784 i915_setup_sysfs(dev);
1785
Ben Widawskye3c74752013-04-05 13:12:39 -07001786 if (INTEL_INFO(dev)->num_pipes) {
1787 /* Must be done after probing outputs */
1788 intel_opregion_init(dev);
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001789 acpi_video_register();
Ben Widawskye3c74752013-04-05 13:12:39 -07001790 }
Matthew Garrett74a365b2009-03-19 21:35:39 +00001791
Daniel Vettereb48eb02012-04-26 23:28:12 +02001792 if (IS_GEN5(dev))
1793 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001794
Paulo Zanoni8a187452013-12-06 20:32:13 -02001795 intel_init_runtime_pm(dev_priv);
1796
Jesse Barnes79e53942008-11-07 14:24:08 -08001797 return 0;
1798
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001799out_power_well:
Imre Deakda7e29b2014-02-18 00:02:02 +02001800 intel_power_domains_remove(dev_priv);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001801 drm_vblank_cleanup(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001802out_gem_unload:
Imre Deak4bdc7292014-05-20 19:47:20 +03001803 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1804 unregister_shrinker(&dev_priv->mm.shrinker);
Keith Packarda7b85d22011-07-10 13:12:17 -07001805
Chris Wilson56e2ea32010-11-08 17:10:29 +00001806 if (dev->pdev->msi_enabled)
1807 pci_disable_msi(dev->pdev);
1808
1809 intel_teardown_gmbus(dev);
1810 intel_teardown_mchbar(dev);
Stanislaw Gruszka22accca2014-01-25 10:13:37 +01001811 pm_qos_remove_request(&dev_priv->pm_qos);
Dave Airlie0e32b392014-05-02 14:02:48 +10001812 destroy_workqueue(dev_priv->dp_wq);
1813out_freewq:
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001814 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001815out_mtrrfree:
Ben Widawsky911bdf02013-06-27 16:30:23 -07001816 arch_phys_wc_del(dev_priv->gtt.mtrr);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001817 io_mapping_free(dev_priv->gtt.mappable);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001818out_gtt:
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001819 i915_global_gtt_cleanup(dev);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001820out_regs:
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001821 intel_uncore_fini(dev);
Chris Wilson6dda5692010-10-29 21:02:18 +01001822 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001823put_bridge:
1824 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001825free_priv:
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001826 if (dev_priv->slab)
1827 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001828 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001829 return ret;
1830}
1831
1832int i915_driver_unload(struct drm_device *dev)
1833{
1834 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001835 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001836
Chris Wilsonce58c322013-12-02 11:26:07 -02001837 ret = i915_gem_suspend(dev);
1838 if (ret) {
1839 DRM_ERROR("failed to idle hardware: %d\n", ret);
1840 return ret;
1841 }
1842
Paulo Zanoni8a187452013-12-06 20:32:13 -02001843 intel_fini_runtime_pm(dev_priv);
1844
Daniel Vettereb48eb02012-04-26 23:28:12 +02001845 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001846
Imre Deak1c2256d2013-11-25 17:15:34 +02001847 /* The i915.ko module is still not prepared to be loaded when
1848 * the power well is not enabled, so just enable it in case
1849 * we're going to unload/reload. */
Imre Deakda7e29b2014-02-18 00:02:02 +02001850 intel_display_set_init_power(dev_priv, true);
1851 intel_power_domains_remove(dev_priv);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001852
Ben Widawsky0136db52012-04-10 21:17:01 -07001853 i915_teardown_sysfs(dev);
1854
Imre Deak4bdc7292014-05-20 19:47:20 +03001855 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1856 unregister_shrinker(&dev_priv->mm.shrinker);
Chris Wilson17250b72010-10-28 12:51:39 +01001857
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001858 io_mapping_free(dev_priv->gtt.mappable);
Ben Widawsky911bdf02013-06-27 16:30:23 -07001859 arch_phys_wc_del(dev_priv->gtt.mtrr);
Eric Anholtab657db12009-01-23 12:57:47 -08001860
Chris Wilson44834a62010-08-19 16:09:23 +01001861 acpi_video_unregister();
1862
Jesse Barnes79e53942008-11-07 14:24:08 -08001863 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01001864 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001865 intel_modeset_cleanup(dev);
1866
Zhao Yakui6363ee62009-11-24 09:48:44 +08001867 /*
1868 * free the memory space allocated for the child device
1869 * config parsed from VBT
1870 */
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03001871 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1872 kfree(dev_priv->vbt.child_dev);
1873 dev_priv->vbt.child_dev = NULL;
1874 dev_priv->vbt.child_dev_num = 0;
Zhao Yakui6363ee62009-11-24 09:48:44 +08001875 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001876
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001877 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001878 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001879 }
1880
Daniel Vettera8b48992010-08-20 21:25:11 +02001881 /* Free error state after interrupts are fully disabled. */
Daniel Vetter99584db2012-11-14 17:14:04 +01001882 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
1883 cancel_work_sync(&dev_priv->gpu_error.work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001884 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001885
Eric Anholted4cb412008-07-29 12:10:39 -07001886 if (dev->pdev->msi_enabled)
1887 pci_disable_msi(dev->pdev);
1888
Chris Wilson44834a62010-08-19 16:09:23 +01001889 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001890
Jesse Barnes79e53942008-11-07 14:24:08 -08001891 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001892 /* Flush any outstanding unpin_work. */
1893 flush_workqueue(dev_priv->wq);
1894
Jesse Barnes79e53942008-11-07 14:24:08 -08001895 mutex_lock(&dev->struct_mutex);
1896 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001897 i915_gem_context_fini(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001898 mutex_unlock(&dev->struct_mutex);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001899 i915_gem_cleanup_stolen(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001900
1901 if (!I915_NEED_GFX_HWS(dev))
1902 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001903 }
1904
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001905 drm_vblank_cleanup(dev);
1906
Chris Wilsonf899fc62010-07-20 15:44:45 -07001907 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001908 intel_teardown_mchbar(dev);
1909
Dave Airlie0e32b392014-05-02 14:02:48 +10001910 destroy_workqueue(dev_priv->dp_wq);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001911 destroy_workqueue(dev_priv->wq);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001912 pm_qos_remove_request(&dev_priv->pm_qos);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001913
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001914 i915_global_gtt_cleanup(dev);
Imre Deak6640aab2013-05-22 17:47:13 +03001915
Chris Wilsonaec347a2013-08-26 13:46:09 +01001916 intel_uncore_fini(dev);
1917 if (dev_priv->regs != NULL)
1918 pci_iounmap(dev->pdev, dev_priv->regs);
1919
Chris Wilson42dcedd2012-11-15 11:32:30 +00001920 if (dev_priv->slab)
1921 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001922
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001923 pci_dev_put(dev_priv->bridge_dev);
Daniel Vetter2206e6a2014-05-13 22:21:59 +02001924 kfree(dev_priv);
Dave Airlie22eae942005-11-10 22:16:34 +11001925
1926 return 0;
1927}
1928
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001929int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001930{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001931 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001932
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001933 ret = i915_gem_open(dev, file);
1934 if (ret)
1935 return ret;
Ben Widawsky254f9652012-06-04 14:42:42 -07001936
Eric Anholt673a3942008-07-30 12:06:12 -07001937 return 0;
1938}
1939
Jesse Barnes79e53942008-11-07 14:24:08 -08001940/**
1941 * i915_driver_lastclose - clean up after all DRM clients have exited
1942 * @dev: DRM device
1943 *
1944 * Take care of cleaning up after all DRM clients have exited. In the
1945 * mode setting case, we want to restore the kernel's initial mode (just
1946 * in case the last client left us in a bad state).
1947 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001948 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001949 * and DMA structures, since the kernel won't be using them, and clea
1950 * up any GEM state.
1951 */
Robin Schroer1a5036b2014-06-02 16:59:39 +02001952void i915_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001954 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001955
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001956 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1957 * goes right around and calls lastclose. Check for this and don't clean
1958 * up anything. */
1959 if (!dev_priv)
1960 return;
1961
1962 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter0632fef2013-10-08 17:44:49 +02001963 intel_fbdev_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001964 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001965 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001966 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001967
Eric Anholt673a3942008-07-30 12:06:12 -07001968 i915_gem_lastclose(dev);
1969
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001970 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
John Harrison2885f6a2014-06-26 18:23:52 +01001973void i915_driver_preclose(struct drm_device *dev, struct drm_file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Chris Wilson0d1430a2013-12-04 14:52:06 +00001975 mutex_lock(&dev->struct_mutex);
John Harrison2885f6a2014-06-26 18:23:52 +01001976 i915_gem_context_close(dev, file);
1977 i915_gem_release(dev, file);
Chris Wilson0d1430a2013-12-04 14:52:06 +00001978 mutex_unlock(&dev->struct_mutex);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +03001979
1980 if (drm_core_check_feature(dev, DRIVER_MODESET))
1981 intel_modeset_preclose(dev, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001984void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001985{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001986 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001987
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001988 if (file_priv && file_priv->bsd_ring)
1989 file_priv->bsd_ring = NULL;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001990 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001991}
1992
Rob Clarkbaa70942013-08-02 13:27:49 -04001993const struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001994 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1995 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1996 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1997 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1998 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1999 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002000 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002001 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01002002 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
2003 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
2004 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002005 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01002006 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02002007 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002008 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
2009 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
2010 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2011 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2012 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002013 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002014 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2015 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002016 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
2017 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2018 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2019 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002020 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2021 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002022 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2023 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2024 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2025 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2026 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2027 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2028 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2029 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2030 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2031 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002032 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002033 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002034 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2035 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08002036 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2037 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, intel_sprite_get_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002038 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
2039 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2040 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2041 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Mika Kuoppalab6359912013-10-30 15:44:16 +02002042 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_get_reset_stats_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Chris Wilson5cc9ed42014-05-16 14:22:37 +01002043 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airliec94f7022005-07-07 21:03:38 +10002044};
2045
Damien Lespiauf95aeb12014-06-09 14:39:49 +01002046int i915_max_ioctl = ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10002047
Daniel Vetter9021f282012-03-26 09:45:41 +02002048/*
2049 * This is really ugly: Because old userspace abused the linux agp interface to
2050 * manage the gtt, we need to claim that all intel devices are agp. For
2051 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10002052 */
Robin Schroer1a5036b2014-06-02 16:59:39 +02002053int i915_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10002054{
2055 return 1;
2056}