blob: d44344140627176b493ca354c12665d4183736cf [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
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/drmP.h>
32#include <drm/drm_crtc_helper.h>
33#include <drm/drm_fb_helper.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080034#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/i915_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010037#include "i915_trace.h"
Jordan Crousedcdb1672010-05-27 13:40:25 -060038#include <linux/pci.h>
Daniel Vettera4de0522014-06-05 16:20:46 +020039#include <linux/console.h>
40#include <linux/vt.h>
Dave Airlie28d52042009-09-21 14:33:58 +100041#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080042#include <linux/acpi.h>
43#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100044#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Chris Wilson44834a62010-08-19 16:09:23 +010046#include <acpi/video.h>
Paulo Zanoni8a187452013-12-06 20:32:13 -020047#include <linux/pm.h>
48#include <linux/pm_runtime.h>
Imre Deak4bdc7292014-05-20 19:47:20 +030049#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Daniel Vetter09422b22012-04-26 23:28:10 +020051#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
52
53#define BEGIN_LP_RING(n) \
54 intel_ring_begin(LP_RING(dev_priv), (n))
55
56#define OUT_RING(x) \
57 intel_ring_emit(LP_RING(dev_priv), x)
58
59#define ADVANCE_LP_RING() \
Chris Wilson09246732013-08-10 22:16:32 +010060 __intel_ring_advance(LP_RING(dev_priv))
Daniel Vetter09422b22012-04-26 23:28:10 +020061
62/**
63 * Lock test for when it's just for synchronization of ring access.
64 *
65 * In that case, we don't need to do it when GEM is initialized as nobody else
66 * has access to the ring.
67 */
68#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
Oscar Mateoee1b1e52014-05-22 14:13:35 +010069 if (LP_RING(dev->dev_private)->buffer->obj == NULL) \
Daniel Vetter09422b22012-04-26 23:28:10 +020070 LOCK_TEST_WITH_RETURN(dev, file); \
71} while (0)
72
Daniel Vetter316d3882012-04-26 23:28:15 +020073static inline u32
74intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
75{
76 if (I915_NEED_GFX_HWS(dev_priv->dev))
77 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
78 else
79 return intel_read_status_page(LP_RING(dev_priv), reg);
80}
81
82#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
Daniel Vetter09422b22012-04-26 23:28:10 +020083#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
84#define I915_BREADCRUMB_INDEX 0x21
85
Daniel Vetterd05c6172012-04-26 23:28:09 +020086void i915_update_dri1_breadcrumb(struct drm_device *dev)
87{
Jani Nikula4c8a4be2014-03-31 14:27:15 +030088 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd05c6172012-04-26 23:28:09 +020089 struct drm_i915_master_private *master_priv;
90
Daniel Vetter6c719fa2013-12-10 13:20:59 +010091 /*
92 * The dri breadcrumb update races against the drm master disappearing.
93 * Instead of trying to fix this (this is by far not the only ums issue)
94 * just don't do the update in kms mode.
95 */
96 if (drm_core_check_feature(dev, DRIVER_MODESET))
97 return;
98
Daniel Vetterd05c6172012-04-26 23:28:09 +020099 if (dev->primary->master) {
100 master_priv = dev->primary->master->driver_priv;
101 if (master_priv->sarea_priv)
102 master_priv->sarea_priv->last_dispatch =
103 READ_BREADCRUMB(dev_priv);
104 }
105}
106
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000107static void i915_write_hws_pga(struct drm_device *dev)
108{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300109 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000110 u32 addr;
111
112 addr = dev_priv->status_page_dmah->busaddr;
113 if (INTEL_INFO(dev)->gen >= 4)
114 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
115 I915_WRITE(HWS_PGA, addr);
116}
117
Keith Packard398c9cb2008-07-30 13:03:43 -0700118/**
Keith Packard398c9cb2008-07-30 13:03:43 -0700119 * Frees the hardware status page, whether it's a physical address or a virtual
120 * address set up by the X Server.
121 */
Eric Anholt3043c602008-10-02 12:24:47 -0700122static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700123{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300124 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100125 struct intel_engine_cs *ring = LP_RING(dev_priv);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000126
Keith Packard398c9cb2008-07-30 13:03:43 -0700127 if (dev_priv->status_page_dmah) {
128 drm_pci_free(dev, dev_priv->status_page_dmah);
129 dev_priv->status_page_dmah = NULL;
130 }
131
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000132 if (ring->status_page.gfx_addr) {
133 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200134 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700135 }
136
137 /* Need to rewrite hardware status page */
138 I915_WRITE(HWS_PGA, 0x1ffff000);
139}
140
Dave Airlie84b1fd12007-07-11 15:53:27 +1000141void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300143 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000144 struct drm_i915_master_private *master_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100145 struct intel_engine_cs *ring = LP_RING(dev_priv);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100146 struct intel_ringbuffer *ringbuf = ring->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jesse Barnes79e53942008-11-07 14:24:08 -0800148 /*
149 * We should never lose context on the ring with modesetting
150 * as we don't expose it to userspace
151 */
152 if (drm_core_check_feature(dev, DRIVER_MODESET))
153 return;
154
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100155 ringbuf->head = I915_READ_HEAD(ring) & HEAD_ADDR;
156 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
157 ringbuf->space = ringbuf->head - (ringbuf->tail + I915_RING_FREE_SPACE);
158 if (ringbuf->space < 0)
159 ringbuf->space += ringbuf->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Dave Airlie7c1c2872008-11-28 14:22:24 +1000161 if (!dev->primary->master)
162 return;
163
164 master_priv = dev->primary->master->driver_priv;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100165 if (ringbuf->head == ringbuf->tail && master_priv->sarea_priv)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000166 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Dave Airlie84b1fd12007-07-11 15:53:27 +1000169static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300171 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000172 int i;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* Make sure interrupts are disabled here because the uninstall ioctl
175 * may not have been called from userspace and after dev_private
176 * is freed, it's too late.
177 */
Eric Anholted4cb412008-07-29 12:10:39 -0700178 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200181 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000182 for (i = 0; i < I915_NUM_RINGS; i++)
183 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200184 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Keith Packard398c9cb2008-07-30 13:03:43 -0700186 /* Clear the HWS virtual address at teardown */
187 if (I915_NEED_GFX_HWS(dev))
188 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 return 0;
191}
192
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000193static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300195 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000196 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000197 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Dave Airlie3a03ac12009-01-11 09:03:49 +1000199 master_priv->sarea = drm_getsarea(dev);
200 if (master_priv->sarea) {
201 master_priv->sarea_priv = (drm_i915_sarea_t *)
202 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
203 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800204 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000205 }
206
Eric Anholt673a3942008-07-30 12:06:12 -0700207 if (init->ring_size != 0) {
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100208 if (LP_RING(dev_priv)->buffer->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700209 i915_dma_cleanup(dev);
210 DRM_ERROR("Client tried to initialize ringbuffer in "
211 "GEM mode\n");
212 return -EINVAL;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Chris Wilsone8616b62011-01-20 09:57:11 +0000215 ret = intel_render_ring_init_dri(dev,
216 init->ring_start,
217 init->ring_size);
218 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700219 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000220 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200224 dev_priv->dri1.cpp = init->cpp;
225 dev_priv->dri1.back_offset = init->back_offset;
226 dev_priv->dri1.front_offset = init->front_offset;
227 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000228 if (master_priv->sarea_priv)
229 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Allow hardware batchbuffers unless told otherwise.
232 */
Daniel Vetter87813422012-05-02 11:49:32 +0200233 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236}
237
Dave Airlie84b1fd12007-07-11 15:53:27 +1000238static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300240 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100241 struct intel_engine_cs *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800243 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100245 if (ring->buffer->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 DRM_ERROR("can not ioremap virtual address for"
247 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000248 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
251 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800252 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000254 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800256 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800257 ring->status_page.page_addr);
258 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100259 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000260 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000261 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800262
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800263 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 return 0;
266}
267
Eric Anholtc153f452007-09-03 12:06:45 +1000268static int i915_dma_init(struct drm_device *dev, void *data,
269 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Eric Anholtc153f452007-09-03 12:06:45 +1000271 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int retcode = 0;
273
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200274 if (drm_core_check_feature(dev, DRIVER_MODESET))
275 return -ENODEV;
276
Eric Anholtc153f452007-09-03 12:06:45 +1000277 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000279 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281 case I915_CLEANUP_DMA:
282 retcode = i915_dma_cleanup(dev);
283 break;
284 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100285 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000288 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 break;
290 }
291
292 return retcode;
293}
294
295/* Implement basically the same security restrictions as hardware does
296 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
297 *
298 * Most of the calculations below involve calculating the size of a
299 * particular instruction. It's important to get the size right as
300 * that tells us where the next instruction to check is. Any illegal
301 * instruction detected will be given a size of zero, which is a
302 * signal to abort the rest of the buffer.
303 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100304static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 switch (((cmd >> 29) & 0x7)) {
307 case 0x0:
308 switch ((cmd >> 23) & 0x3f) {
309 case 0x0:
310 return 1; /* MI_NOOP */
311 case 0x4:
312 return 1; /* MI_FLUSH */
313 default:
314 return 0; /* disallow everything else */
315 }
316 break;
317 case 0x1:
318 return 0; /* reserved */
319 case 0x2:
320 return (cmd & 0xff) + 2; /* 2d commands */
321 case 0x3:
322 if (((cmd >> 24) & 0x1f) <= 0x18)
323 return 1;
324
325 switch ((cmd >> 24) & 0x1f) {
326 case 0x1c:
327 return 1;
328 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case 0x3:
331 return (cmd & 0x1f) + 2;
332 case 0x4:
333 return (cmd & 0xf) + 2;
334 default:
335 return (cmd & 0xffff) + 2;
336 }
337 case 0x1e:
338 if (cmd & (1 << 23))
339 return (cmd & 0xffff) + 1;
340 else
341 return 1;
342 case 0x1f:
343 if ((cmd & (1 << 23)) == 0) /* inline vertices */
344 return (cmd & 0x1ffff) + 2;
345 else if (cmd & (1 << 17)) /* indirect random */
346 if ((cmd & 0xffff) == 0)
347 return 0; /* unknown length, too hard */
348 else
349 return (((cmd & 0xffff) + 1) / 2) + 1;
350 else
351 return 2; /* indirect sequential */
352 default:
353 return 0;
354 }
355 default:
356 return 0;
357 }
358
359 return 0;
360}
361
Eric Anholt201361a2009-03-11 12:30:04 -0700362static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300364 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100365 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100367 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->buffer->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000368 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100371 int sz = validate_cmd(buffer[i]);
372 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000373 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100374 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100377 ret = BEGIN_LP_RING((dwords+1)&~1);
378 if (ret)
379 return ret;
380
381 for (i = 0; i < dwords; i++)
382 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100383 if (dwords & 1)
384 OUT_RING(0);
385
386 ADVANCE_LP_RING();
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389}
390
Eric Anholt673a3942008-07-30 12:06:12 -0700391int
392i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000393 struct drm_clip_rect *box,
394 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100396 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100397 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000399 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
400 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000402 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000403 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100406 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100407 ret = BEGIN_LP_RING(4);
408 if (ret)
409 return ret;
410
Alan Hourihanec29b6692006-08-12 16:29:24 +1000411 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000412 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
413 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000414 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000415 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100416 ret = BEGIN_LP_RING(6);
417 if (ret)
418 return ret;
419
Alan Hourihanec29b6692006-08-12 16:29:24 +1000420 OUT_RING(GFX_OP_DRAWRECT_INFO);
421 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000422 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
423 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000424 OUT_RING(DR4);
425 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000426 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100427 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return 0;
430}
431
Alan Hourihanec29b6692006-08-12 16:29:24 +1000432/* XXX: Emitting the counter should really be moved to part of the IRQ
433 * emit. For now, do it in both places:
434 */
435
Dave Airlie84b1fd12007-07-11 15:53:27 +1000436static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100437{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300438 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000439 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100440
Daniel Vetter231f42a2012-11-02 19:55:05 +0100441 dev_priv->dri1.counter++;
442 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
443 dev_priv->dri1.counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000444 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100445 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Dave Airliede227f52006-01-25 15:31:43 +1100446
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100447 if (BEGIN_LP_RING(4) == 0) {
448 OUT_RING(MI_STORE_DWORD_INDEX);
449 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100450 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100451 OUT_RING(0);
452 ADVANCE_LP_RING();
453 }
Dave Airliede227f52006-01-25 15:31:43 +1100454}
455
Dave Airlie84b1fd12007-07-11 15:53:27 +1000456static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700457 drm_i915_cmdbuffer_t *cmd,
458 struct drm_clip_rect *cliprects,
459 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 int nbox = cmd->num_cliprects;
462 int i = 0, count, ret;
463
464 if (cmd->sz & 0x3) {
465 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000466 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 i915_kernel_lost_context(dev);
470
471 count = nbox ? nbox : 1;
472
473 for (i = 0; i < count; i++) {
474 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000475 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 cmd->DR1, cmd->DR4);
477 if (ret)
478 return ret;
479 }
480
Eric Anholt201361a2009-03-11 12:30:04 -0700481 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (ret)
483 return ret;
484 }
485
Dave Airliede227f52006-01-25 15:31:43 +1100486 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488}
489
Dave Airlie84b1fd12007-07-11 15:53:27 +1000490static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700491 drm_i915_batchbuffer_t * batch,
492 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100494 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100496 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if ((batch->start | batch->used) & 0x7) {
499 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 i915_kernel_lost_context(dev);
504
505 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 for (i = 0; i < count; i++) {
507 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000508 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100509 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (ret)
511 return ret;
512 }
513
Keith Packard0790d5e2008-07-30 12:28:47 -0700514 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100515 ret = BEGIN_LP_RING(2);
516 if (ret)
517 return ret;
518
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100519 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000520 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
521 OUT_RING(batch->start);
522 } else {
523 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
524 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100527 ret = BEGIN_LP_RING(4);
528 if (ret)
529 return ret;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 OUT_RING(MI_BATCH_BUFFER);
532 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
533 OUT_RING(batch->start + batch->used - 4);
534 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100536 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
Zou Nan hai1cafd342010-06-25 13:40:24 +0800539
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100540 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100541 if (BEGIN_LP_RING(2) == 0) {
542 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
543 OUT_RING(MI_NOOP);
544 ADVANCE_LP_RING();
545 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100548 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0;
550}
551
Dave Airlieaf6061a2008-05-07 12:15:39 +1000552static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300554 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000555 struct drm_i915_master_private *master_priv =
556 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100557 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Dave Airlie7c1c2872008-11-28 14:22:24 +1000559 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400560 return -EINVAL;
561
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800562 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800563 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200564 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800565 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Dave Airlieaf6061a2008-05-07 12:15:39 +1000567 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100569 ret = BEGIN_LP_RING(10);
570 if (ret)
571 return ret;
572
Jesse Barnes585fb112008-07-29 11:54:06 -0700573 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000574 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dave Airlieaf6061a2008-05-07 12:15:39 +1000576 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
577 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200578 if (dev_priv->dri1.current_page == 0) {
579 OUT_RING(dev_priv->dri1.back_offset);
580 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000581 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200582 OUT_RING(dev_priv->dri1.front_offset);
583 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000584 }
585 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000586
Dave Airlieaf6061a2008-05-07 12:15:39 +1000587 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
588 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100589
Dave Airlieaf6061a2008-05-07 12:15:39 +1000590 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000591
Daniel Vetter231f42a2012-11-02 19:55:05 +0100592 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000593
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100594 if (BEGIN_LP_RING(4) == 0) {
595 OUT_RING(MI_STORE_DWORD_INDEX);
596 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100597 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100598 OUT_RING(0);
599 ADVANCE_LP_RING();
600 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000601
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200602 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000606static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 i915_kernel_lost_context(dev);
Chris Wilson3e960502012-11-27 16:22:54 +0000609 return intel_ring_idle(LP_RING(dev->dev_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Eric Anholtc153f452007-09-03 12:06:45 +1000612static int i915_flush_ioctl(struct drm_device *dev, void *data,
613 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Eric Anholt546b0972008-09-01 16:45:29 -0700615 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200617 if (drm_core_check_feature(dev, DRIVER_MODESET))
618 return -ENODEV;
619
Eric Anholt546b0972008-09-01 16:45:29 -0700620 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
621
622 mutex_lock(&dev->struct_mutex);
623 ret = i915_quiescent(dev);
624 mutex_unlock(&dev->struct_mutex);
625
626 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Eric Anholtc153f452007-09-03 12:06:45 +1000629static int i915_batchbuffer(struct drm_device *dev, void *data,
630 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300632 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100633 struct drm_i915_master_private *master_priv;
634 drm_i915_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000635 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700637 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200639 if (drm_core_check_feature(dev, DRIVER_MODESET))
640 return -ENODEV;
641
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100642 master_priv = dev->primary->master->driver_priv;
643 sarea_priv = (drm_i915_sarea_t *) master_priv->sarea_priv;
644
Daniel Vetter87813422012-05-02 11:49:32 +0200645 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000647 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800650 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800651 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Eric Anholt546b0972008-09-01 16:45:29 -0700653 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Eric Anholt201361a2009-03-11 12:30:04 -0700655 if (batch->num_cliprects < 0)
656 return -EINVAL;
657
658 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700659 cliprects = kcalloc(batch->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200660 sizeof(*cliprects),
Eric Anholt9a298b22009-03-24 12:23:04 -0700661 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700662 if (cliprects == NULL)
663 return -ENOMEM;
664
665 ret = copy_from_user(cliprects, batch->cliprects,
666 batch->num_cliprects *
667 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200668 if (ret != 0) {
669 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700670 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200671 }
Eric Anholt201361a2009-03-11 12:30:04 -0700672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Eric Anholt546b0972008-09-01 16:45:29 -0700674 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700675 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700676 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400678 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000679 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700680
681fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700682 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return ret;
685}
686
Eric Anholtc153f452007-09-03 12:06:45 +1000687static int i915_cmdbuffer(struct drm_device *dev, void *data,
688 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300690 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100691 struct drm_i915_master_private *master_priv;
692 drm_i915_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000693 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700694 struct drm_clip_rect *cliprects = NULL;
695 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int ret;
697
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800698 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800699 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200701 if (drm_core_check_feature(dev, DRIVER_MODESET))
702 return -ENODEV;
703
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100704 master_priv = dev->primary->master->driver_priv;
705 sarea_priv = (drm_i915_sarea_t *) master_priv->sarea_priv;
706
Eric Anholt546b0972008-09-01 16:45:29 -0700707 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Eric Anholt201361a2009-03-11 12:30:04 -0700709 if (cmdbuf->num_cliprects < 0)
710 return -EINVAL;
711
Eric Anholt9a298b22009-03-24 12:23:04 -0700712 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700713 if (batch_data == NULL)
714 return -ENOMEM;
715
716 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200717 if (ret != 0) {
718 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700719 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200720 }
Eric Anholt201361a2009-03-11 12:30:04 -0700721
722 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700723 cliprects = kcalloc(cmdbuf->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200724 sizeof(*cliprects), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000725 if (cliprects == NULL) {
726 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700727 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000728 }
Eric Anholt201361a2009-03-11 12:30:04 -0700729
730 ret = copy_from_user(cliprects, cmdbuf->cliprects,
731 cmdbuf->num_cliprects *
732 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200733 if (ret != 0) {
734 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700735 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Eric Anholt546b0972008-09-01 16:45:29 -0700739 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700740 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700741 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (ret) {
743 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000744 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400747 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000748 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700749
Eric Anholt201361a2009-03-11 12:30:04 -0700750fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700751 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000752fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700753 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700754
755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Daniel Vetter94888672012-04-26 23:28:08 +0200758static int i915_emit_irq(struct drm_device * dev)
759{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300760 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200761 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
762
763 i915_kernel_lost_context(dev);
764
765 DRM_DEBUG_DRIVER("\n");
766
Daniel Vetter231f42a2012-11-02 19:55:05 +0100767 dev_priv->dri1.counter++;
768 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
769 dev_priv->dri1.counter = 1;
Daniel Vetter94888672012-04-26 23:28:08 +0200770 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100771 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200772
773 if (BEGIN_LP_RING(4) == 0) {
774 OUT_RING(MI_STORE_DWORD_INDEX);
775 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100776 OUT_RING(dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200777 OUT_RING(MI_USER_INTERRUPT);
778 ADVANCE_LP_RING();
779 }
780
Daniel Vetter231f42a2012-11-02 19:55:05 +0100781 return dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200782}
783
784static int i915_wait_irq(struct drm_device * dev, int irq_nr)
785{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300786 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200787 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
788 int ret = 0;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100789 struct intel_engine_cs *ring = LP_RING(dev_priv);
Daniel Vetter94888672012-04-26 23:28:08 +0200790
791 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
792 READ_BREADCRUMB(dev_priv));
793
794 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
795 if (master_priv->sarea_priv)
796 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
797 return 0;
798 }
799
800 if (master_priv->sarea_priv)
801 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
802
803 if (ring->irq_get(ring)) {
Daniel Vetterbfd83032013-12-11 11:34:41 +0100804 DRM_WAIT_ON(ret, ring->irq_queue, 3 * HZ,
Daniel Vetter94888672012-04-26 23:28:08 +0200805 READ_BREADCRUMB(dev_priv) >= irq_nr);
806 ring->irq_put(ring);
807 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
808 ret = -EBUSY;
809
810 if (ret == -EBUSY) {
811 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Daniel Vetter231f42a2012-11-02 19:55:05 +0100812 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200813 }
814
815 return ret;
816}
817
818/* Needs the lock as it touches the ring.
819 */
820static int i915_irq_emit(struct drm_device *dev, void *data,
821 struct drm_file *file_priv)
822{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300823 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200824 drm_i915_irq_emit_t *emit = data;
825 int result;
826
827 if (drm_core_check_feature(dev, DRIVER_MODESET))
828 return -ENODEV;
829
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100830 if (!dev_priv || !LP_RING(dev_priv)->buffer->virtual_start) {
Daniel Vetter94888672012-04-26 23:28:08 +0200831 DRM_ERROR("called with no initialization\n");
832 return -EINVAL;
833 }
834
835 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
836
837 mutex_lock(&dev->struct_mutex);
838 result = i915_emit_irq(dev);
839 mutex_unlock(&dev->struct_mutex);
840
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100841 if (copy_to_user(emit->irq_seq, &result, sizeof(int))) {
Daniel Vetter94888672012-04-26 23:28:08 +0200842 DRM_ERROR("copy_to_user\n");
843 return -EFAULT;
844 }
845
846 return 0;
847}
848
849/* Doesn't need the hardware lock.
850 */
851static int i915_irq_wait(struct drm_device *dev, void *data,
852 struct drm_file *file_priv)
853{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300854 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200855 drm_i915_irq_wait_t *irqwait = data;
856
857 if (drm_core_check_feature(dev, DRIVER_MODESET))
858 return -ENODEV;
859
860 if (!dev_priv) {
861 DRM_ERROR("called with no initialization\n");
862 return -EINVAL;
863 }
864
865 return i915_wait_irq(dev, irqwait->irq_seq);
866}
867
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200868static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
869 struct drm_file *file_priv)
870{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300871 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200872 drm_i915_vblank_pipe_t *pipe = data;
873
874 if (drm_core_check_feature(dev, DRIVER_MODESET))
875 return -ENODEV;
876
877 if (!dev_priv) {
878 DRM_ERROR("called with no initialization\n");
879 return -EINVAL;
880 }
881
882 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
883
884 return 0;
885}
886
887/**
888 * Schedule buffer swap at given vertical blank.
889 */
890static int i915_vblank_swap(struct drm_device *dev, void *data,
891 struct drm_file *file_priv)
892{
893 /* The delayed swap mechanism was fundamentally racy, and has been
894 * removed. The model was that the client requested a delayed flip/swap
895 * from the kernel, then waited for vblank before continuing to perform
896 * rendering. The problem was that the kernel might wake the client
897 * up before it dispatched the vblank swap (since the lock has to be
898 * held while touching the ringbuffer), in which case the client would
899 * clear and start the next frame before the swap occurred, and
900 * flicker would occur in addition to likely missing the vblank.
901 *
902 * In the absence of this ioctl, userland falls back to a correct path
903 * of waiting for a vblank, then dispatching the swap on its own.
904 * Context switching to userland and back is plenty fast enough for
905 * meeting the requirements of vblank swapping.
906 */
907 return -EINVAL;
908}
909
Eric Anholtc153f452007-09-03 12:06:45 +1000910static int i915_flip_bufs(struct drm_device *dev, void *data,
911 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Eric Anholt546b0972008-09-01 16:45:29 -0700913 int ret;
914
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200915 if (drm_core_check_feature(dev, DRIVER_MODESET))
916 return -ENODEV;
917
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800918 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Eric Anholt546b0972008-09-01 16:45:29 -0700920 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Eric Anholt546b0972008-09-01 16:45:29 -0700922 mutex_lock(&dev->struct_mutex);
923 ret = i915_dispatch_flip(dev);
924 mutex_unlock(&dev->struct_mutex);
925
926 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Eric Anholtc153f452007-09-03 12:06:45 +1000929static int i915_getparam(struct drm_device *dev, void *data,
930 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300932 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000933 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int value;
935
936 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000937 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000938 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
Eric Anholtc153f452007-09-03 12:06:45 +1000941 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700943 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 break;
945 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200946 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100948 case I915_PARAM_LAST_DISPATCH:
949 value = READ_BREADCRUMB(dev_priv);
950 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400951 case I915_PARAM_CHIPSET_ID:
Ville Syrjäläffbab09b2013-10-04 14:53:40 +0300952 value = dev->pdev->device;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400953 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700954 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200955 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700956 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800957 case I915_PARAM_NUM_FENCES_AVAIL:
958 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
959 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200960 case I915_PARAM_HAS_OVERLAY:
961 value = dev_priv->overlay ? 1 : 0;
962 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800963 case I915_PARAM_HAS_PAGEFLIPPING:
964 value = 1;
965 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500966 case I915_PARAM_HAS_EXECBUF2:
967 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200968 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500969 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800970 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100971 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800972 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100973 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100974 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100975 break;
Xiang, Haihaoa1f2cc72013-05-28 19:22:34 -0700976 case I915_PARAM_HAS_VEBOX:
977 value = intel_ring_initialized(&dev_priv->ring[VECS]);
978 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100979 case I915_PARAM_HAS_RELAXED_FENCING:
980 value = 1;
981 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100982 case I915_PARAM_HAS_COHERENT_RINGS:
983 value = 1;
984 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000985 case I915_PARAM_HAS_EXEC_CONSTANTS:
986 value = INTEL_INFO(dev)->gen >= 4;
987 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000988 case I915_PARAM_HAS_RELAXED_DELTA:
989 value = 1;
990 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800991 case I915_PARAM_HAS_GEN7_SOL_RESET:
992 value = 1;
993 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200994 case I915_PARAM_HAS_LLC:
995 value = HAS_LLC(dev);
996 break;
Chris Wilson651d7942013-08-08 14:41:10 +0100997 case I915_PARAM_HAS_WT:
998 value = HAS_WT(dev);
999 break;
Daniel Vetter777ee962012-02-15 23:50:25 +01001000 case I915_PARAM_HAS_ALIASING_PPGTT:
Daniel Vetter7d9c4772013-12-18 16:32:00 +01001001 value = dev_priv->mm.aliasing_ppgtt || USES_FULL_PPGTT(dev);
Daniel Vetter777ee962012-02-15 23:50:25 +01001002 break;
Ben Widawsky172cf152012-06-05 15:24:25 -07001003 case I915_PARAM_HAS_WAIT_TIMEOUT:
1004 value = 1;
1005 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +01001006 case I915_PARAM_HAS_SEMAPHORES:
1007 value = i915_semaphore_is_enabled(dev);
1008 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +10001009 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
1010 value = 1;
1011 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001012 case I915_PARAM_HAS_SECURE_BATCHES:
1013 value = capable(CAP_SYS_ADMIN);
1014 break;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001015 case I915_PARAM_HAS_PINNED_BATCHES:
1016 value = 1;
1017 break;
Daniel Vettered5982e2013-01-17 22:23:36 +01001018 case I915_PARAM_HAS_EXEC_NO_RELOC:
1019 value = 1;
1020 break;
Chris Wilsoneef90cc2013-01-08 10:53:17 +00001021 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
1022 value = 1;
1023 break;
Brad Volkind728c8e2014-02-18 10:15:56 -08001024 case I915_PARAM_CMD_PARSER_VERSION:
1025 value = i915_cmd_parser_get_version();
1026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 default:
Ben Widawskye29c32d2013-05-31 11:28:45 -07001028 DRM_DEBUG("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001029 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001032 if (copy_to_user(param->value, &value, sizeof(int))) {
1033 DRM_ERROR("copy_to_user failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001034 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037 return 0;
1038}
1039
Eric Anholtc153f452007-09-03 12:06:45 +10001040static int i915_setparam(struct drm_device *dev, void *data,
1041 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001043 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001044 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001047 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001048 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
Eric Anholtc153f452007-09-03 12:06:45 +10001051 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 break;
1054 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001057 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001059 case I915_SETPARAM_NUM_USED_FENCES:
1060 if (param->value > dev_priv->num_fence_regs ||
1061 param->value < 0)
1062 return -EINVAL;
1063 /* Userspace can use first N regs */
1064 dev_priv->fence_reg_start = param->value;
1065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001067 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001068 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001069 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
1072 return 0;
1073}
1074
Eric Anholtc153f452007-09-03 12:06:45 +10001075static int i915_set_status_page(struct drm_device *dev, void *data,
1076 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001077{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001078 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001079 drm_i915_hws_addr_t *hws = data;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001080 struct intel_engine_cs *ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001081
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001082 if (drm_core_check_feature(dev, DRIVER_MODESET))
1083 return -ENODEV;
1084
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001085 if (!I915_NEED_GFX_HWS(dev))
1086 return -EINVAL;
1087
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001088 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001089 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001090 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001091 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001092
Jesse Barnes79e53942008-11-07 14:24:08 -08001093 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1094 WARN(1, "tried to set status page when mode setting active\n");
1095 return 0;
1096 }
1097
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001098 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001099
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001100 ring = LP_RING(dev_priv);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001101 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001102
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001103 dev_priv->dri1.gfx_hws_cpu_addr =
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001104 ioremap_wc(dev_priv->gtt.mappable_base + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001105 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001106 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001107 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001108 DRM_ERROR("can not ioremap virtual address for"
1109 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001110 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001111 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001112
1113 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001114 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001115
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001116 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001117 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001118 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001119 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001120 return 0;
1121}
1122
Dave Airlieec2a4c32009-08-04 11:43:41 +10001123static int i915_get_bridge_dev(struct drm_device *dev)
1124{
1125 struct drm_i915_private *dev_priv = dev->dev_private;
1126
Akshay Joshi0206e352011-08-16 15:34:10 -04001127 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001128 if (!dev_priv->bridge_dev) {
1129 DRM_ERROR("bridge device not found\n");
1130 return -1;
1131 }
1132 return 0;
1133}
1134
Zhenyu Wangc48044112009-12-17 14:48:43 +08001135#define MCHBAR_I915 0x44
1136#define MCHBAR_I965 0x48
1137#define MCHBAR_SIZE (4*4096)
1138
1139#define DEVEN_REG 0x54
1140#define DEVEN_MCHBAR_EN (1 << 28)
1141
1142/* Allocate space for the MCH regs if needed, return nonzero on error */
1143static int
1144intel_alloc_mchbar_resource(struct drm_device *dev)
1145{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001146 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001147 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001148 u32 temp_lo, temp_hi = 0;
1149 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001150 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001151
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001152 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001153 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1154 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1155 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1156
1157 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1158#ifdef CONFIG_PNP
1159 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001160 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1161 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001162#endif
1163
1164 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001165 dev_priv->mch_res.name = "i915 MCHBAR";
1166 dev_priv->mch_res.flags = IORESOURCE_MEM;
1167 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1168 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001169 MCHBAR_SIZE, MCHBAR_SIZE,
1170 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001171 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001172 dev_priv->bridge_dev);
1173 if (ret) {
1174 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1175 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001176 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001177 }
1178
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001179 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001180 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1181 upper_32_bits(dev_priv->mch_res.start));
1182
1183 pci_write_config_dword(dev_priv->bridge_dev, reg,
1184 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001185 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001186}
1187
1188/* Setup MCHBAR if possible, return true if we should disable it again */
1189static void
1190intel_setup_mchbar(struct drm_device *dev)
1191{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001192 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001193 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001194 u32 temp;
1195 bool enabled;
1196
Jesse Barnes11ea8b72014-03-03 14:27:57 -08001197 if (IS_VALLEYVIEW(dev))
1198 return;
1199
Zhenyu Wangc48044112009-12-17 14:48:43 +08001200 dev_priv->mchbar_need_disable = false;
1201
1202 if (IS_I915G(dev) || IS_I915GM(dev)) {
1203 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1204 enabled = !!(temp & DEVEN_MCHBAR_EN);
1205 } else {
1206 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1207 enabled = temp & 1;
1208 }
1209
1210 /* If it's already enabled, don't have to do anything */
1211 if (enabled)
1212 return;
1213
1214 if (intel_alloc_mchbar_resource(dev))
1215 return;
1216
1217 dev_priv->mchbar_need_disable = true;
1218
1219 /* Space is allocated or reserved, so enable it. */
1220 if (IS_I915G(dev) || IS_I915GM(dev)) {
1221 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1222 temp | DEVEN_MCHBAR_EN);
1223 } else {
1224 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1225 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1226 }
1227}
1228
1229static void
1230intel_teardown_mchbar(struct drm_device *dev)
1231{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001232 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001233 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001234 u32 temp;
1235
1236 if (dev_priv->mchbar_need_disable) {
1237 if (IS_I915G(dev) || IS_I915GM(dev)) {
1238 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1239 temp &= ~DEVEN_MCHBAR_EN;
1240 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1241 } else {
1242 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1243 temp &= ~1;
1244 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1245 }
1246 }
1247
1248 if (dev_priv->mch_res.start)
1249 release_resource(&dev_priv->mch_res);
1250}
1251
Dave Airlie28d52042009-09-21 14:33:58 +10001252/* true = enable decode, false = disable decoder */
1253static unsigned int i915_vga_set_decode(void *cookie, bool state)
1254{
1255 struct drm_device *dev = cookie;
1256
1257 intel_modeset_vga_set_state(dev, state);
1258 if (state)
1259 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1260 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1261 else
1262 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1263}
1264
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001265static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1266{
1267 struct drm_device *dev = pci_get_drvdata(pdev);
1268 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1269 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001270 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001271 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001272 /* i915 resume handler doesn't set to D0 */
1273 pci_set_power_state(dev->pdev, PCI_D0);
1274 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001275 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001276 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001277 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001278 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001279 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001280 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001281 }
1282}
1283
1284static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1285{
1286 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001287
Daniel Vetterfc8fd402013-11-03 20:46:34 +01001288 /*
1289 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1290 * locking inversion with the driver load path. And the access here is
1291 * completely racy anyway. So don't bother with locking for now.
1292 */
1293 return dev->open_count == 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001294}
1295
Takashi Iwai26ec6852012-05-11 07:51:17 +02001296static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1297 .set_gpu_state = i915_switcheroo_set_state,
1298 .reprobe = NULL,
1299 .can_switch = i915_switcheroo_can_switch,
1300};
1301
Chris Wilson2c7111d2011-03-29 10:40:27 +01001302static int i915_load_modeset_init(struct drm_device *dev)
1303{
1304 struct drm_i915_private *dev_priv = dev->dev_private;
1305 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001306
Bryan Freed6d139a82010-10-14 09:14:51 +01001307 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001308 if (ret)
1309 DRM_INFO("failed to find VBIOS tables\n");
1310
Chris Wilson934f992c2011-01-20 13:09:12 +00001311 /* If we have > 1 VGA cards, then we need to arbitrate access
1312 * to the common VGA resources.
1313 *
1314 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1315 * then we do not take part in VGA arbitration and the
1316 * vga_client_register() fails with -ENODEV.
1317 */
Dave Airlieebff5fa92013-10-11 15:12:04 +10001318 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1319 if (ret && ret != -ENODEV)
1320 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001321
Jesse Barnes723bfd72010-10-07 16:01:13 -07001322 intel_register_dsm_handler();
1323
Dave Airlie0d697042012-09-10 12:28:36 +10001324 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001325 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001326 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001327
Chris Wilson9797fbf2012-04-24 15:47:39 +01001328 /* Initialise stolen first so that we may reserve preallocated
1329 * objects for the BIOS to KMS transition.
1330 */
1331 ret = i915_gem_init_stolen(dev);
1332 if (ret)
1333 goto cleanup_vga_switcheroo;
1334
Imre Deake13192f2014-02-18 00:02:15 +02001335 intel_power_domains_init_hw(dev_priv);
1336
Daniel Vetterbb0f1b52013-11-03 21:09:27 +01001337 ret = drm_irq_install(dev, dev->pdev->irq);
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001338 if (ret)
1339 goto cleanup_gem_stolen;
1340
1341 /* Important: The output setup functions called by modeset_init need
1342 * working irqs for e.g. gmbus and dp aux transfers. */
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001343 intel_modeset_init(dev);
1344
Chris Wilson1070a422012-04-24 15:47:41 +01001345 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001346 if (ret)
Imre Deak713028b2014-04-25 17:28:00 +03001347 goto cleanup_irq;
Chris Wilson2c7111d2011-03-29 10:40:27 +01001348
Jesse Barnes073f34d2012-11-02 11:13:59 -07001349 INIT_WORK(&dev_priv->console_resume_work, intel_console_resume);
1350
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001351 intel_modeset_gem_init(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001352
Jesse Barnes79e53942008-11-07 14:24:08 -08001353 /* Always safe in the mode setting case. */
1354 /* FIXME: do pre/post-mode set stuff in core KMS code */
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001355 dev->vblank_disable_allowed = true;
Imre Deak713028b2014-04-25 17:28:00 +03001356 if (INTEL_INFO(dev)->num_pipes == 0)
Ben Widawskye3c74752013-04-05 13:12:39 -07001357 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001358
Chris Wilson5a793952010-06-06 10:50:03 +01001359 ret = intel_fbdev_init(dev);
1360 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001361 goto cleanup_gem;
1362
1363 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter20afbda2012-12-11 14:05:07 +01001364 intel_hpd_init(dev);
1365
1366 /*
1367 * Some ports require correctly set-up hpd registers for detection to
1368 * work properly (leading to ghost connected connector status), e.g. VGA
1369 * on gm45. Hence we can only set up the initial fbdev config after hpd
1370 * irqs are fully enabled. Now we should scan for the initial config
1371 * only once hotplug handling is enabled, but due to screwed-up locking
1372 * around kms/fbdev init we can't protect the fdbev initial config
1373 * scanning against hotplug events. Hence do this first and ignore the
1374 * tiny window where we will loose hotplug notifactions.
1375 */
1376 intel_fbdev_initial_config(dev);
1377
1378 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001379 dev_priv->enable_hotplug_processing = true;
Chris Wilson5a793952010-06-06 10:50:03 +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);
Ben Widawskybdf4fd72013-12-06 14:11:18 -08001390 WARN_ON(dev_priv->mm.aliasing_ppgtt);
Imre Deak713028b2014-04-25 17:28:00 +03001391cleanup_irq:
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001392 drm_irq_uninstall(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001393cleanup_gem_stolen:
1394 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001395cleanup_vga_switcheroo:
1396 vga_switcheroo_unregister_client(dev->pdev);
1397cleanup_vga_client:
1398 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001399out:
1400 return ret;
1401}
1402
Dave Airlie7c1c2872008-11-28 14:22:24 +10001403int i915_master_create(struct drm_device *dev, struct drm_master *master)
1404{
1405 struct drm_i915_master_private *master_priv;
1406
Eric Anholt9a298b22009-03-24 12:23:04 -07001407 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001408 if (!master_priv)
1409 return -ENOMEM;
1410
1411 master->driver_priv = master_priv;
1412 return 0;
1413}
1414
1415void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1416{
1417 struct drm_i915_master_private *master_priv = master->driver_priv;
1418
1419 if (!master_priv)
1420 return;
1421
Eric Anholt9a298b22009-03-24 12:23:04 -07001422 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001423
1424 master->driver_priv = NULL;
1425}
1426
Daniel Vetter243eaf32013-12-17 10:00:54 +01001427#if IS_ENABLED(CONFIG_FB)
Daniel Vettere1887192012-06-12 11:28:17 +02001428static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1429{
1430 struct apertures_struct *ap;
1431 struct pci_dev *pdev = dev_priv->dev->pdev;
1432 bool primary;
1433
1434 ap = alloc_apertures(1);
1435 if (!ap)
1436 return;
1437
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
1444 remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
1445
1446 kfree(ap);
1447}
Daniel Vetter4520f532013-10-09 09:18:51 +02001448#else
1449static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1450{
1451}
1452#endif
Daniel Vettere1887192012-06-12 11:28:17 +02001453
Daniel Vettera4de0522014-06-05 16:20:46 +02001454#if !defined(CONFIG_VGA_CONSOLE)
1455static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1456{
1457 return 0;
1458}
1459#elif !defined(CONFIG_DUMMY_CONSOLE)
1460static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1461{
1462 return -ENODEV;
1463}
1464#else
1465static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1466{
Daniel Vetter1bb9e632014-07-08 10:02:43 +02001467 int ret = 0;
Daniel Vettera4de0522014-06-05 16:20:46 +02001468
1469 DRM_INFO("Replacing VGA console driver\n");
1470
1471 console_lock();
Daniel Vetter1bb9e632014-07-08 10:02:43 +02001472 if (con_is_bound(&vga_con))
1473 ret = do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES - 1, 1);
Daniel Vettera4de0522014-06-05 16:20:46 +02001474 if (ret == 0) {
1475 ret = do_unregister_con_driver(&vga_con);
1476
1477 /* Ignore "already unregistered". */
1478 if (ret == -ENODEV)
1479 ret = 0;
1480 }
1481 console_unlock();
1482
1483 return ret;
1484}
1485#endif
1486
Daniel Vetterc96ea642012-08-08 22:01:51 +02001487static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1488{
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001489 const struct intel_device_info *info = &dev_priv->info;
Daniel Vetterc96ea642012-08-08 22:01:51 +02001490
Damien Lespiaue2a58002013-04-23 16:38:34 +01001491#define PRINT_S(name) "%s"
1492#define SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001493#define PRINT_FLAG(name) info->name ? #name "," : ""
1494#define SEP_COMMA ,
Daniel Vetterc96ea642012-08-08 22:01:51 +02001495 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
Damien Lespiaue2a58002013-04-23 16:38:34 +01001496 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
Daniel Vetterc96ea642012-08-08 22:01:51 +02001497 info->gen,
1498 dev_priv->dev->pdev->device,
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001499 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
Damien Lespiaue2a58002013-04-23 16:38:34 +01001500#undef PRINT_S
1501#undef SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001502#undef PRINT_FLAG
1503#undef SEP_COMMA
Daniel Vetterc96ea642012-08-08 22:01:51 +02001504}
1505
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001506/*
1507 * Determine various intel_device_info fields at runtime.
1508 *
1509 * Use it when either:
1510 * - it's judged too laborious to fill n static structures with the limit
1511 * when a simple if statement does the job,
1512 * - run-time checks (eg read fuse/strap registers) are needed.
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001513 *
1514 * This function needs to be called:
1515 * - after the MMIO has been setup as we are reading registers,
1516 * - after the PCH has been detected,
1517 * - before the first usage of the fields it can tweak.
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001518 */
1519static void intel_device_info_runtime_init(struct drm_device *dev)
1520{
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001521 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001522 struct intel_device_info *info;
Damien Lespiaud615a162014-03-03 17:31:48 +00001523 enum pipe pipe;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001524
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001525 info = (struct intel_device_info *)&dev_priv->info;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001526
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001527 if (IS_VALLEYVIEW(dev))
Damien Lespiaud615a162014-03-03 17:31:48 +00001528 for_each_pipe(pipe)
1529 info->num_sprites[pipe] = 2;
1530 else
1531 for_each_pipe(pipe)
1532 info->num_sprites[pipe] = 1;
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001533
Damien Lespiaua0bae572014-02-10 17:20:55 +00001534 if (i915.disable_display) {
1535 DRM_INFO("Display disabled (module parameter)\n");
1536 info->num_pipes = 0;
1537 } else if (info->num_pipes > 0 &&
1538 (INTEL_INFO(dev)->gen == 7 || INTEL_INFO(dev)->gen == 8) &&
1539 !IS_VALLEYVIEW(dev)) {
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001540 u32 fuse_strap = I915_READ(FUSE_STRAP);
1541 u32 sfuse_strap = I915_READ(SFUSE_STRAP);
1542
1543 /*
1544 * SFUSE_STRAP is supposed to have a bit signalling the display
1545 * is fused off. Unfortunately it seems that, at least in
1546 * certain cases, fused off display means that PCH display
1547 * reads don't land anywhere. In that case, we read 0s.
1548 *
1549 * On CPT/PPT, we can detect this case as SFUSE_STRAP_FUSE_LOCK
1550 * should be set when taking over after the firmware.
1551 */
1552 if (fuse_strap & ILK_INTERNAL_DISPLAY_DISABLE ||
1553 sfuse_strap & SFUSE_STRAP_DISPLAY_DISABLED ||
1554 (dev_priv->pch_type == PCH_CPT &&
1555 !(sfuse_strap & SFUSE_STRAP_FUSE_LOCK))) {
1556 DRM_INFO("Display fused off, disabling\n");
1557 info->num_pipes = 0;
1558 }
1559 }
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001560}
1561
Eric Anholt63ee41d2010-12-20 18:40:06 -08001562/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001563 * i915_driver_load - setup chip and create an initial config
1564 * @dev: DRM device
1565 * @flags: startup flags
1566 *
1567 * The driver load routine has to do several things:
1568 * - drive output discovery via intel_modeset_init()
1569 * - initialize the memory manager
1570 * - allocate initial config memory
1571 * - setup the DRM framebuffer with the allocated memory
1572 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001573int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001574{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001575 struct drm_i915_private *dev_priv;
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001576 struct intel_device_info *info, *device_info;
Chris Wilson934d6082012-09-14 11:57:46 +01001577 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001578 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001579
Daniel Vetter26394d92012-03-26 21:33:18 +02001580 info = (struct intel_device_info *) flags;
1581
1582 /* Refuse to load on gen6+ without kms enabled. */
Jani Nikulae147acc2013-10-10 15:25:37 +03001583 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET)) {
1584 DRM_INFO("Your hardware requires kernel modesetting (KMS)\n");
1585 DRM_INFO("See CONFIG_DRM_I915_KMS, nomodeset, and i915.modeset parameters\n");
Daniel Vetter26394d92012-03-26 21:33:18 +02001586 return -ENODEV;
Jani Nikulae147acc2013-10-10 15:25:37 +03001587 }
Daniel Vetter26394d92012-03-26 21:33:18 +02001588
Daniel Vetter24986ee2013-12-11 11:34:33 +01001589 /* UMS needs agp support. */
1590 if (!drm_core_check_feature(dev, DRIVER_MODESET) && !dev->agp)
1591 return -EINVAL;
1592
Daniel Vetterb14c5672013-09-19 12:18:32 +02001593 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001594 if (dev_priv == NULL)
1595 return -ENOMEM;
1596
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001597 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001598 dev_priv->dev = dev;
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001599
1600 /* copy initial configuration to dev_priv->info */
1601 device_info = (struct intel_device_info *)&dev_priv->info;
1602 *device_info = *info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001603
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001604 spin_lock_init(&dev_priv->irq_lock);
1605 spin_lock_init(&dev_priv->gpu_error.lock);
Jani Nikula58c68772013-11-08 16:48:54 +02001606 spin_lock_init(&dev_priv->backlight_lock);
Chris Wilson907b28c2013-07-19 20:36:52 +01001607 spin_lock_init(&dev_priv->uncore.lock);
Daniel Vetterc20e8352013-07-24 22:40:23 +02001608 spin_lock_init(&dev_priv->mm.object_stat_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001609 mutex_init(&dev_priv->dpio_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001610 mutex_init(&dev_priv->modeset_restore_lock);
1611
Daniel Vetterf742a552013-12-06 10:17:53 +01001612 intel_pm_setup(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03001613
Damien Lespiau07144422013-10-15 18:55:40 +01001614 intel_display_crc_init(dev);
1615
Daniel Vetterc96ea642012-08-08 22:01:51 +02001616 i915_dump_device_info(dev_priv);
1617
Paulo Zanonied1c9e22013-08-12 14:34:08 -03001618 /* Not all pre-production machines fall into this category, only the
1619 * very first ones. Almost everything should work, except for maybe
1620 * suspend/resume. And we don't implement workarounds that affect only
1621 * pre-production machines. */
1622 if (IS_HSW_EARLY_SDV(dev))
1623 DRM_INFO("This is an early pre-production Haswell machine. "
1624 "It may not be fully functional.\n");
1625
Dave Airlieec2a4c32009-08-04 11:43:41 +10001626 if (i915_get_bridge_dev(dev)) {
1627 ret = -EIO;
1628 goto free_priv;
1629 }
1630
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001631 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1632 /* Before gen4, the registers and the GTT are behind different BARs.
1633 * However, from gen4 onwards, the registers and the GTT are shared
1634 * in the same BAR, so we want to restrict this ioremap from
1635 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1636 * the register BAR remains the same size for all the earlier
1637 * generations up to Ironlake.
1638 */
1639 if (info->gen < 5)
1640 mmio_size = 512*1024;
1641 else
1642 mmio_size = 2*1024*1024;
1643
1644 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
1645 if (!dev_priv->regs) {
1646 DRM_ERROR("failed to map registers\n");
1647 ret = -EIO;
1648 goto put_bridge;
1649 }
1650
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001651 /* This must be called before any calls to HAS_PCH_* */
1652 intel_detect_pch(dev);
1653
1654 intel_uncore_init(dev);
1655
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001656 ret = i915_gem_gtt_init(dev);
1657 if (ret)
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001658 goto out_regs;
Daniel Vettere1887192012-06-12 11:28:17 +02001659
Daniel Vettera4de0522014-06-05 16:20:46 +02001660 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1661 ret = i915_kick_out_vgacon(dev_priv);
1662 if (ret) {
1663 DRM_ERROR("failed to remove conflicting VGA console\n");
1664 goto out_gtt;
1665 }
1666
Chris Wilson16233922012-10-26 12:06:41 +01001667 i915_kick_out_firmware_fb(dev_priv);
Daniel Vettera4de0522014-06-05 16:20:46 +02001668 }
Daniel Vettere1887192012-06-12 11:28:17 +02001669
Dave Airlie466e69b2011-12-19 11:15:29 +00001670 pci_set_master(dev->pdev);
1671
Daniel Vetter9f82d232010-08-30 21:25:23 +02001672 /* overlay on gen2 is broken and can't address above 1G */
1673 if (IS_GEN2(dev))
1674 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1675
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001676 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1677 * using 32bit addressing, overwriting memory if HWS is located
1678 * above 4GB.
1679 *
1680 * The documentation also mentions an issue with undefined
1681 * behaviour if any general state is accessed within a page above 4GB,
1682 * which also needs to be handled carefully.
1683 */
1684 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1685 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1686
Ben Widawsky93d18792013-01-17 12:45:17 -08001687 aperture_size = dev_priv->gtt.mappable_end;
Chris Wilson71e93392010-10-27 18:46:52 +01001688
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001689 dev_priv->gtt.mappable =
1690 io_mapping_create_wc(dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001691 aperture_size);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001692 if (dev_priv->gtt.mappable == NULL) {
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001693 ret = -EIO;
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001694 goto out_gtt;
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001695 }
1696
Ben Widawsky911bdf02013-06-27 16:30:23 -07001697 dev_priv->gtt.mtrr = arch_phys_wc_add(dev_priv->gtt.mappable_base,
1698 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001699
Chris Wilsone642abb2010-09-09 12:46:34 +01001700 /* The i915 workqueue is primarily used for batched retirement of
1701 * requests (and thus managing bo) once the task has been completed
1702 * by the GPU. i915_gem_retire_requests() is called directly when we
1703 * need high-priority retirement, such as waiting for an explicit
1704 * bo.
1705 *
1706 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001707 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001708 *
1709 * All tasks on the workqueue are expected to acquire the dev mutex
1710 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001711 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001712 */
Tejun Heo53621862012-08-22 16:40:57 -07001713 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001714 if (dev_priv->wq == NULL) {
1715 DRM_ERROR("Failed to create our workqueue.\n");
1716 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001717 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001718 }
1719
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001720 intel_irq_init(dev);
Ben Widawsky78511f22013-10-04 21:22:49 -07001721 intel_uncore_sanitize(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001722
Zhenyu Wangc48044112009-12-17 14:48:43 +08001723 /* Try to make sure MCHBAR is enabled before poking at it */
1724 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001725 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001726 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001727
Bryan Freed6d139a82010-10-14 09:14:51 +01001728 intel_setup_bios(dev);
1729
Eric Anholt673a3942008-07-30 12:06:12 -07001730 i915_gem_load(dev);
1731
Eric Anholted4cb412008-07-29 12:10:39 -07001732 /* On the 945G/GM, the chipset reports the MSI capability on the
1733 * integrated graphics even though the support isn't actually there
1734 * according to the published specs. It doesn't appear to function
1735 * correctly in testing on 945G.
1736 * This may be a side effect of MSI having been made available for PEG
1737 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001738 *
1739 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001740 * be lost or delayed, but we use them anyways to avoid
1741 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001742 */
Keith Packardb60678a2008-12-08 11:12:28 -08001743 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001744 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001745
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001746 intel_device_info_runtime_init(dev);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001747
Ben Widawskye3c74752013-04-05 13:12:39 -07001748 if (INTEL_INFO(dev)->num_pipes) {
1749 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1750 if (ret)
1751 goto out_gem_unload;
1752 }
Keith Packard52440212008-11-18 09:30:25 -08001753
Imre Deakda7e29b2014-02-18 00:02:02 +02001754 intel_power_domains_init(dev_priv);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001755
Jesse Barnes79e53942008-11-07 14:24:08 -08001756 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001757 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001758 if (ret < 0) {
1759 DRM_ERROR("failed to init modeset\n");
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001760 goto out_power_well;
Jesse Barnes79e53942008-11-07 14:24:08 -08001761 }
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001762 } else {
1763 /* Start out suspended in ums mode. */
1764 dev_priv->ums.mm_suspended = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001765 }
1766
Ben Widawsky0136db52012-04-10 21:17:01 -07001767 i915_setup_sysfs(dev);
1768
Ben Widawskye3c74752013-04-05 13:12:39 -07001769 if (INTEL_INFO(dev)->num_pipes) {
1770 /* Must be done after probing outputs */
1771 intel_opregion_init(dev);
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001772 acpi_video_register();
Ben Widawskye3c74752013-04-05 13:12:39 -07001773 }
Matthew Garrett74a365b2009-03-19 21:35:39 +00001774
Daniel Vettereb48eb02012-04-26 23:28:12 +02001775 if (IS_GEN5(dev))
1776 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001777
Paulo Zanoni8a187452013-12-06 20:32:13 -02001778 intel_init_runtime_pm(dev_priv);
1779
Jesse Barnes79e53942008-11-07 14:24:08 -08001780 return 0;
1781
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001782out_power_well:
Imre Deakda7e29b2014-02-18 00:02:02 +02001783 intel_power_domains_remove(dev_priv);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001784 drm_vblank_cleanup(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001785out_gem_unload:
Imre Deak4bdc7292014-05-20 19:47:20 +03001786 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1787 unregister_shrinker(&dev_priv->mm.shrinker);
Keith Packarda7b85d22011-07-10 13:12:17 -07001788
Chris Wilson56e2ea32010-11-08 17:10:29 +00001789 if (dev->pdev->msi_enabled)
1790 pci_disable_msi(dev->pdev);
1791
1792 intel_teardown_gmbus(dev);
1793 intel_teardown_mchbar(dev);
Stanislaw Gruszka22accca2014-01-25 10:13:37 +01001794 pm_qos_remove_request(&dev_priv->pm_qos);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001795 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001796out_mtrrfree:
Ben Widawsky911bdf02013-06-27 16:30:23 -07001797 arch_phys_wc_del(dev_priv->gtt.mtrr);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001798 io_mapping_free(dev_priv->gtt.mappable);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001799out_gtt:
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001800 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001801out_regs:
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001802 intel_uncore_fini(dev);
Chris Wilson6dda5692010-10-29 21:02:18 +01001803 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001804put_bridge:
1805 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001806free_priv:
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001807 if (dev_priv->slab)
1808 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001809 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001810 return ret;
1811}
1812
1813int i915_driver_unload(struct drm_device *dev)
1814{
1815 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001816 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001817
Chris Wilsonce58c322013-12-02 11:26:07 -02001818 ret = i915_gem_suspend(dev);
1819 if (ret) {
1820 DRM_ERROR("failed to idle hardware: %d\n", ret);
1821 return ret;
1822 }
1823
Paulo Zanoni8a187452013-12-06 20:32:13 -02001824 intel_fini_runtime_pm(dev_priv);
1825
Daniel Vettereb48eb02012-04-26 23:28:12 +02001826 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001827
Imre Deak1c2256d2013-11-25 17:15:34 +02001828 /* The i915.ko module is still not prepared to be loaded when
1829 * the power well is not enabled, so just enable it in case
1830 * we're going to unload/reload. */
Imre Deakda7e29b2014-02-18 00:02:02 +02001831 intel_display_set_init_power(dev_priv, true);
1832 intel_power_domains_remove(dev_priv);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001833
Ben Widawsky0136db52012-04-10 21:17:01 -07001834 i915_teardown_sysfs(dev);
1835
Imre Deak4bdc7292014-05-20 19:47:20 +03001836 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1837 unregister_shrinker(&dev_priv->mm.shrinker);
Chris Wilson17250b72010-10-28 12:51:39 +01001838
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001839 io_mapping_free(dev_priv->gtt.mappable);
Ben Widawsky911bdf02013-06-27 16:30:23 -07001840 arch_phys_wc_del(dev_priv->gtt.mtrr);
Eric Anholtab657db12009-01-23 12:57:47 -08001841
Chris Wilson44834a62010-08-19 16:09:23 +01001842 acpi_video_unregister();
1843
Jesse Barnes79e53942008-11-07 14:24:08 -08001844 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01001845 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001846 intel_modeset_cleanup(dev);
Jesse Barnes073f34d2012-11-02 11:13:59 -07001847 cancel_work_sync(&dev_priv->console_resume_work);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001848
Zhao Yakui6363ee62009-11-24 09:48:44 +08001849 /*
1850 * free the memory space allocated for the child device
1851 * config parsed from VBT
1852 */
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03001853 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1854 kfree(dev_priv->vbt.child_dev);
1855 dev_priv->vbt.child_dev = NULL;
1856 dev_priv->vbt.child_dev_num = 0;
Zhao Yakui6363ee62009-11-24 09:48:44 +08001857 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001858
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001859 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001860 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001861 }
1862
Daniel Vettera8b48992010-08-20 21:25:11 +02001863 /* Free error state after interrupts are fully disabled. */
Daniel Vetter99584db2012-11-14 17:14:04 +01001864 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
1865 cancel_work_sync(&dev_priv->gpu_error.work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001866 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001867
Eric Anholted4cb412008-07-29 12:10:39 -07001868 if (dev->pdev->msi_enabled)
1869 pci_disable_msi(dev->pdev);
1870
Chris Wilson44834a62010-08-19 16:09:23 +01001871 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001872
Jesse Barnes79e53942008-11-07 14:24:08 -08001873 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001874 /* Flush any outstanding unpin_work. */
1875 flush_workqueue(dev_priv->wq);
1876
Jesse Barnes79e53942008-11-07 14:24:08 -08001877 mutex_lock(&dev->struct_mutex);
1878 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001879 i915_gem_context_fini(dev);
Ben Widawskybdf4fd72013-12-06 14:11:18 -08001880 WARN_ON(dev_priv->mm.aliasing_ppgtt);
Jesse Barnes79e53942008-11-07 14:24:08 -08001881 mutex_unlock(&dev->struct_mutex);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001882 i915_gem_cleanup_stolen(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001883
1884 if (!I915_NEED_GFX_HWS(dev))
1885 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001886 }
1887
Ben Widawskya7bbbd62013-07-16 16:50:07 -07001888 WARN_ON(!list_empty(&dev_priv->vm_list));
Daniel Vetter701394c2010-10-10 18:54:08 +01001889
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001890 drm_vblank_cleanup(dev);
1891
Chris Wilsonf899fc62010-07-20 15:44:45 -07001892 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001893 intel_teardown_mchbar(dev);
1894
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001895 destroy_workqueue(dev_priv->wq);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001896 pm_qos_remove_request(&dev_priv->pm_qos);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001897
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001898 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
Imre Deak6640aab2013-05-22 17:47:13 +03001899
Chris Wilsonaec347a2013-08-26 13:46:09 +01001900 intel_uncore_fini(dev);
1901 if (dev_priv->regs != NULL)
1902 pci_iounmap(dev->pdev, dev_priv->regs);
1903
Chris Wilson42dcedd2012-11-15 11:32:30 +00001904 if (dev_priv->slab)
1905 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001906
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001907 pci_dev_put(dev_priv->bridge_dev);
Daniel Vetter2206e6a2014-05-13 22:21:59 +02001908 kfree(dev_priv);
Dave Airlie22eae942005-11-10 22:16:34 +11001909
1910 return 0;
1911}
1912
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001913int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001914{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001915 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001916
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001917 ret = i915_gem_open(dev, file);
1918 if (ret)
1919 return ret;
Ben Widawsky254f9652012-06-04 14:42:42 -07001920
Eric Anholt673a3942008-07-30 12:06:12 -07001921 return 0;
1922}
1923
Jesse Barnes79e53942008-11-07 14:24:08 -08001924/**
1925 * i915_driver_lastclose - clean up after all DRM clients have exited
1926 * @dev: DRM device
1927 *
1928 * Take care of cleaning up after all DRM clients have exited. In the
1929 * mode setting case, we want to restore the kernel's initial mode (just
1930 * in case the last client left us in a bad state).
1931 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001932 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001933 * and DMA structures, since the kernel won't be using them, and clea
1934 * up any GEM state.
1935 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001936void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001938 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001939
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001940 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1941 * goes right around and calls lastclose. Check for this and don't clean
1942 * up anything. */
1943 if (!dev_priv)
1944 return;
1945
1946 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter0632fef2013-10-08 17:44:49 +02001947 intel_fbdev_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001948 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001949 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001950 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001951
Eric Anholt673a3942008-07-30 12:06:12 -07001952 i915_gem_lastclose(dev);
1953
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001954 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Eric Anholt6c340ea2007-08-25 20:23:09 +10001957void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Chris Wilson0d1430a2013-12-04 14:52:06 +00001959 mutex_lock(&dev->struct_mutex);
Ben Widawsky254f9652012-06-04 14:42:42 -07001960 i915_gem_context_close(dev, file_priv);
Eric Anholtb9624422009-06-03 07:27:35 +00001961 i915_gem_release(dev, file_priv);
Chris Wilson0d1430a2013-12-04 14:52:06 +00001962 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001965void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001966{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001967 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001968
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001969 if (file_priv && file_priv->bsd_ring)
1970 file_priv->bsd_ring = NULL;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001971 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001972}
1973
Rob Clarkbaa70942013-08-02 13:27:49 -04001974const struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001975 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1976 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1977 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1978 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1979 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1980 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001981 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001982 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001983 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1984 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1985 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001986 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001987 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02001988 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001989 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
1990 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1991 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1992 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1993 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001994 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001995 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1996 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 +02001997 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1998 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1999 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2000 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 +10002001 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2002 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 +02002003 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2004 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2005 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2006 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2007 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2008 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2009 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2010 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2011 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2012 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 +10002013 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 +02002014 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002015 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2016 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08002017 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2018 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 +02002019 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
2020 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2021 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2022 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Mika Kuoppalab6359912013-10-30 15:44:16 +02002023 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 +01002024 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airliec94f7022005-07-07 21:03:38 +10002025};
2026
Damien Lespiauf95aeb12014-06-09 14:39:49 +01002027int i915_max_ioctl = ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10002028
Daniel Vetter9021f282012-03-26 09:45:41 +02002029/*
2030 * This is really ugly: Because old userspace abused the linux agp interface to
2031 * manage the gtt, we need to claim that all intel devices are agp. For
2032 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10002033 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002034int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10002035{
2036 return 1;
2037}