blob: f14c47a715a4c9851ab97e49b06609fe2d036ea7 [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>
Dave Airlie28d52042009-09-21 14:33:58 +100039#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080040#include <linux/acpi.h>
41#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100042#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Chris Wilson44834a62010-08-19 16:09:23 +010044#include <acpi/video.h>
Paulo Zanoni8a187452013-12-06 20:32:13 -020045#include <linux/pm.h>
46#include <linux/pm_runtime.h>
Imre Deak4bdc7292014-05-20 19:47:20 +030047#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Daniel Vetter09422b22012-04-26 23:28:10 +020049#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
50
51#define BEGIN_LP_RING(n) \
52 intel_ring_begin(LP_RING(dev_priv), (n))
53
54#define OUT_RING(x) \
55 intel_ring_emit(LP_RING(dev_priv), x)
56
57#define ADVANCE_LP_RING() \
Chris Wilson09246732013-08-10 22:16:32 +010058 __intel_ring_advance(LP_RING(dev_priv))
Daniel Vetter09422b22012-04-26 23:28:10 +020059
60/**
61 * Lock test for when it's just for synchronization of ring access.
62 *
63 * In that case, we don't need to do it when GEM is initialized as nobody else
64 * has access to the ring.
65 */
66#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
67 if (LP_RING(dev->dev_private)->obj == NULL) \
68 LOCK_TEST_WITH_RETURN(dev, file); \
69} while (0)
70
Daniel Vetter316d3882012-04-26 23:28:15 +020071static inline u32
72intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
73{
74 if (I915_NEED_GFX_HWS(dev_priv->dev))
75 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
76 else
77 return intel_read_status_page(LP_RING(dev_priv), reg);
78}
79
80#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
Daniel Vetter09422b22012-04-26 23:28:10 +020081#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
82#define I915_BREADCRUMB_INDEX 0x21
83
Daniel Vetterd05c6172012-04-26 23:28:09 +020084void i915_update_dri1_breadcrumb(struct drm_device *dev)
85{
Jani Nikula4c8a4be2014-03-31 14:27:15 +030086 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd05c6172012-04-26 23:28:09 +020087 struct drm_i915_master_private *master_priv;
88
Daniel Vetter6c719fa2013-12-10 13:20:59 +010089 /*
90 * The dri breadcrumb update races against the drm master disappearing.
91 * Instead of trying to fix this (this is by far not the only ums issue)
92 * just don't do the update in kms mode.
93 */
94 if (drm_core_check_feature(dev, DRIVER_MODESET))
95 return;
96
Daniel Vetterd05c6172012-04-26 23:28:09 +020097 if (dev->primary->master) {
98 master_priv = dev->primary->master->driver_priv;
99 if (master_priv->sarea_priv)
100 master_priv->sarea_priv->last_dispatch =
101 READ_BREADCRUMB(dev_priv);
102 }
103}
104
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000105static void i915_write_hws_pga(struct drm_device *dev)
106{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300107 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000108 u32 addr;
109
110 addr = dev_priv->status_page_dmah->busaddr;
111 if (INTEL_INFO(dev)->gen >= 4)
112 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
113 I915_WRITE(HWS_PGA, addr);
114}
115
Keith Packard398c9cb2008-07-30 13:03:43 -0700116/**
Keith Packard398c9cb2008-07-30 13:03:43 -0700117 * Frees the hardware status page, whether it's a physical address or a virtual
118 * address set up by the X Server.
119 */
Eric Anholt3043c602008-10-02 12:24:47 -0700120static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700121{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300122 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000123 struct intel_ring_buffer *ring = LP_RING(dev_priv);
124
Keith Packard398c9cb2008-07-30 13:03:43 -0700125 if (dev_priv->status_page_dmah) {
126 drm_pci_free(dev, dev_priv->status_page_dmah);
127 dev_priv->status_page_dmah = NULL;
128 }
129
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000130 if (ring->status_page.gfx_addr) {
131 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200132 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700133 }
134
135 /* Need to rewrite hardware status page */
136 I915_WRITE(HWS_PGA, 0x1ffff000);
137}
138
Dave Airlie84b1fd12007-07-11 15:53:27 +1000139void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300141 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000142 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000143 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jesse Barnes79e53942008-11-07 14:24:08 -0800145 /*
146 * We should never lose context on the ring with modesetting
147 * as we don't expose it to userspace
148 */
149 if (drm_core_check_feature(dev, DRIVER_MODESET))
150 return;
151
Chris Wilson8168bd42010-11-11 17:54:52 +0000152 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
153 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Ville Syrjälä633cf8f2012-12-03 18:43:32 +0200154 ring->space = ring->head - (ring->tail + I915_RING_FREE_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800156 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dave Airlie7c1c2872008-11-28 14:22:24 +1000158 if (!dev->primary->master)
159 return;
160
161 master_priv = dev->primary->master->driver_priv;
162 if (ring->head == ring->tail && master_priv->sarea_priv)
163 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Dave Airlie84b1fd12007-07-11 15:53:27 +1000166static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300168 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000169 int i;
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* Make sure interrupts are disabled here because the uninstall ioctl
172 * may not have been called from userspace and after dev_private
173 * is freed, it's too late.
174 */
Eric Anholted4cb412008-07-29 12:10:39 -0700175 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200178 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000179 for (i = 0; i < I915_NUM_RINGS; i++)
180 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200181 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Keith Packard398c9cb2008-07-30 13:03:43 -0700183 /* Clear the HWS virtual address at teardown */
184 if (I915_NEED_GFX_HWS(dev))
185 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 return 0;
188}
189
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000190static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300192 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000193 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000194 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dave Airlie3a03ac12009-01-11 09:03:49 +1000196 master_priv->sarea = drm_getsarea(dev);
197 if (master_priv->sarea) {
198 master_priv->sarea_priv = (drm_i915_sarea_t *)
199 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
200 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800201 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000202 }
203
Eric Anholt673a3942008-07-30 12:06:12 -0700204 if (init->ring_size != 0) {
Chris Wilsone8616b62011-01-20 09:57:11 +0000205 if (LP_RING(dev_priv)->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700206 i915_dma_cleanup(dev);
207 DRM_ERROR("Client tried to initialize ringbuffer in "
208 "GEM mode\n");
209 return -EINVAL;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Chris Wilsone8616b62011-01-20 09:57:11 +0000212 ret = intel_render_ring_init_dri(dev,
213 init->ring_start,
214 init->ring_size);
215 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700216 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000217 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200221 dev_priv->dri1.cpp = init->cpp;
222 dev_priv->dri1.back_offset = init->back_offset;
223 dev_priv->dri1.front_offset = init->front_offset;
224 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000225 if (master_priv->sarea_priv)
226 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* Allow hardware batchbuffers unless told otherwise.
229 */
Daniel Vetter87813422012-05-02 11:49:32 +0200230 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233}
234
Dave Airlie84b1fd12007-07-11 15:53:27 +1000235static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300237 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000238 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800240 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Daniel Vetter4225d0f2012-04-26 23:28:16 +0200242 if (ring->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 DRM_ERROR("can not ioremap virtual address for"
244 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000245 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
248 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800249 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000251 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800253 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800254 ring->status_page.page_addr);
255 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100256 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000257 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000258 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800259
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800260 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 return 0;
263}
264
Eric Anholtc153f452007-09-03 12:06:45 +1000265static int i915_dma_init(struct drm_device *dev, void *data,
266 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Eric Anholtc153f452007-09-03 12:06:45 +1000268 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int retcode = 0;
270
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200271 if (drm_core_check_feature(dev, DRIVER_MODESET))
272 return -ENODEV;
273
Eric Anholtc153f452007-09-03 12:06:45 +1000274 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000276 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 case I915_CLEANUP_DMA:
279 retcode = i915_dma_cleanup(dev);
280 break;
281 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100282 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
284 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000285 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287 }
288
289 return retcode;
290}
291
292/* Implement basically the same security restrictions as hardware does
293 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
294 *
295 * Most of the calculations below involve calculating the size of a
296 * particular instruction. It's important to get the size right as
297 * that tells us where the next instruction to check is. Any illegal
298 * instruction detected will be given a size of zero, which is a
299 * signal to abort the rest of the buffer.
300 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100301static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 switch (((cmd >> 29) & 0x7)) {
304 case 0x0:
305 switch ((cmd >> 23) & 0x3f) {
306 case 0x0:
307 return 1; /* MI_NOOP */
308 case 0x4:
309 return 1; /* MI_FLUSH */
310 default:
311 return 0; /* disallow everything else */
312 }
313 break;
314 case 0x1:
315 return 0; /* reserved */
316 case 0x2:
317 return (cmd & 0xff) + 2; /* 2d commands */
318 case 0x3:
319 if (((cmd >> 24) & 0x1f) <= 0x18)
320 return 1;
321
322 switch ((cmd >> 24) & 0x1f) {
323 case 0x1c:
324 return 1;
325 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 case 0x3:
328 return (cmd & 0x1f) + 2;
329 case 0x4:
330 return (cmd & 0xf) + 2;
331 default:
332 return (cmd & 0xffff) + 2;
333 }
334 case 0x1e:
335 if (cmd & (1 << 23))
336 return (cmd & 0xffff) + 1;
337 else
338 return 1;
339 case 0x1f:
340 if ((cmd & (1 << 23)) == 0) /* inline vertices */
341 return (cmd & 0x1ffff) + 2;
342 else if (cmd & (1 << 17)) /* indirect random */
343 if ((cmd & 0xffff) == 0)
344 return 0; /* unknown length, too hard */
345 else
346 return (((cmd & 0xffff) + 1) / 2) + 1;
347 else
348 return 2; /* indirect sequential */
349 default:
350 return 0;
351 }
352 default:
353 return 0;
354 }
355
356 return 0;
357}
358
Eric Anholt201361a2009-03-11 12:30:04 -0700359static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300361 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100362 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000364 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000365 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100368 int sz = validate_cmd(buffer[i]);
369 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000370 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100371 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100374 ret = BEGIN_LP_RING((dwords+1)&~1);
375 if (ret)
376 return ret;
377
378 for (i = 0; i < dwords; i++)
379 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100380 if (dwords & 1)
381 OUT_RING(0);
382
383 ADVANCE_LP_RING();
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386}
387
Eric Anholt673a3942008-07-30 12:06:12 -0700388int
389i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000390 struct drm_clip_rect *box,
391 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100393 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100394 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000396 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
397 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000399 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100403 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100404 ret = BEGIN_LP_RING(4);
405 if (ret)
406 return ret;
407
Alan Hourihanec29b6692006-08-12 16:29:24 +1000408 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000409 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
410 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000411 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000412 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100413 ret = BEGIN_LP_RING(6);
414 if (ret)
415 return ret;
416
Alan Hourihanec29b6692006-08-12 16:29:24 +1000417 OUT_RING(GFX_OP_DRAWRECT_INFO);
418 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000419 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
420 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000421 OUT_RING(DR4);
422 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000423 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100424 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return 0;
427}
428
Alan Hourihanec29b6692006-08-12 16:29:24 +1000429/* XXX: Emitting the counter should really be moved to part of the IRQ
430 * emit. For now, do it in both places:
431 */
432
Dave Airlie84b1fd12007-07-11 15:53:27 +1000433static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100434{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300435 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000436 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100437
Daniel Vetter231f42a2012-11-02 19:55:05 +0100438 dev_priv->dri1.counter++;
439 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
440 dev_priv->dri1.counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000441 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100442 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Dave Airliede227f52006-01-25 15:31:43 +1100443
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100444 if (BEGIN_LP_RING(4) == 0) {
445 OUT_RING(MI_STORE_DWORD_INDEX);
446 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100447 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100448 OUT_RING(0);
449 ADVANCE_LP_RING();
450 }
Dave Airliede227f52006-01-25 15:31:43 +1100451}
452
Dave Airlie84b1fd12007-07-11 15:53:27 +1000453static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700454 drm_i915_cmdbuffer_t *cmd,
455 struct drm_clip_rect *cliprects,
456 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 int nbox = cmd->num_cliprects;
459 int i = 0, count, ret;
460
461 if (cmd->sz & 0x3) {
462 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
466 i915_kernel_lost_context(dev);
467
468 count = nbox ? nbox : 1;
469
470 for (i = 0; i < count; i++) {
471 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000472 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 cmd->DR1, cmd->DR4);
474 if (ret)
475 return ret;
476 }
477
Eric Anholt201361a2009-03-11 12:30:04 -0700478 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (ret)
480 return ret;
481 }
482
Dave Airliede227f52006-01-25 15:31:43 +1100483 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
486
Dave Airlie84b1fd12007-07-11 15:53:27 +1000487static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700488 drm_i915_batchbuffer_t * batch,
489 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100491 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100493 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if ((batch->start | batch->used) & 0x7) {
496 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000497 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
500 i915_kernel_lost_context(dev);
501
502 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 for (i = 0; i < count; i++) {
504 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000505 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100506 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (ret)
508 return ret;
509 }
510
Keith Packard0790d5e2008-07-30 12:28:47 -0700511 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100512 ret = BEGIN_LP_RING(2);
513 if (ret)
514 return ret;
515
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100516 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000517 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
518 OUT_RING(batch->start);
519 } else {
520 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
521 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100524 ret = BEGIN_LP_RING(4);
525 if (ret)
526 return ret;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 OUT_RING(MI_BATCH_BUFFER);
529 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
530 OUT_RING(batch->start + batch->used - 4);
531 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100533 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
Zou Nan hai1cafd342010-06-25 13:40:24 +0800536
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100537 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100538 if (BEGIN_LP_RING(2) == 0) {
539 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
540 OUT_RING(MI_NOOP);
541 ADVANCE_LP_RING();
542 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100545 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
547}
548
Dave Airlieaf6061a2008-05-07 12:15:39 +1000549static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300551 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000552 struct drm_i915_master_private *master_priv =
553 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100554 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dave Airlie7c1c2872008-11-28 14:22:24 +1000556 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400557 return -EINVAL;
558
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800559 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800560 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200561 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800562 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Dave Airlieaf6061a2008-05-07 12:15:39 +1000564 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100566 ret = BEGIN_LP_RING(10);
567 if (ret)
568 return ret;
569
Jesse Barnes585fb112008-07-29 11:54:06 -0700570 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000571 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dave Airlieaf6061a2008-05-07 12:15:39 +1000573 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
574 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200575 if (dev_priv->dri1.current_page == 0) {
576 OUT_RING(dev_priv->dri1.back_offset);
577 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000578 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200579 OUT_RING(dev_priv->dri1.front_offset);
580 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000581 }
582 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000583
Dave Airlieaf6061a2008-05-07 12:15:39 +1000584 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
585 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100586
Dave Airlieaf6061a2008-05-07 12:15:39 +1000587 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000588
Daniel Vetter231f42a2012-11-02 19:55:05 +0100589 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000590
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100591 if (BEGIN_LP_RING(4) == 0) {
592 OUT_RING(MI_STORE_DWORD_INDEX);
593 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100594 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100595 OUT_RING(0);
596 ADVANCE_LP_RING();
597 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000598
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200599 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000603static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 i915_kernel_lost_context(dev);
Chris Wilson3e960502012-11-27 16:22:54 +0000606 return intel_ring_idle(LP_RING(dev->dev_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Eric Anholtc153f452007-09-03 12:06:45 +1000609static int i915_flush_ioctl(struct drm_device *dev, void *data,
610 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Eric Anholt546b0972008-09-01 16:45:29 -0700612 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200614 if (drm_core_check_feature(dev, DRIVER_MODESET))
615 return -ENODEV;
616
Eric Anholt546b0972008-09-01 16:45:29 -0700617 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
618
619 mutex_lock(&dev->struct_mutex);
620 ret = i915_quiescent(dev);
621 mutex_unlock(&dev->struct_mutex);
622
623 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Eric Anholtc153f452007-09-03 12:06:45 +1000626static int i915_batchbuffer(struct drm_device *dev, void *data,
627 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300629 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100630 struct drm_i915_master_private *master_priv;
631 drm_i915_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000632 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700634 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200636 if (drm_core_check_feature(dev, DRIVER_MODESET))
637 return -ENODEV;
638
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100639 master_priv = dev->primary->master->driver_priv;
640 sarea_priv = (drm_i915_sarea_t *) master_priv->sarea_priv;
641
Daniel Vetter87813422012-05-02 11:49:32 +0200642 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000644 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800647 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800648 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric Anholt546b0972008-09-01 16:45:29 -0700650 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric Anholt201361a2009-03-11 12:30:04 -0700652 if (batch->num_cliprects < 0)
653 return -EINVAL;
654
655 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700656 cliprects = kcalloc(batch->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200657 sizeof(*cliprects),
Eric Anholt9a298b22009-03-24 12:23:04 -0700658 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700659 if (cliprects == NULL)
660 return -ENOMEM;
661
662 ret = copy_from_user(cliprects, batch->cliprects,
663 batch->num_cliprects *
664 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200665 if (ret != 0) {
666 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700667 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200668 }
Eric Anholt201361a2009-03-11 12:30:04 -0700669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Eric Anholt546b0972008-09-01 16:45:29 -0700671 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700672 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700673 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400675 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000676 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700677
678fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700679 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return ret;
682}
683
Eric Anholtc153f452007-09-03 12:06:45 +1000684static int i915_cmdbuffer(struct drm_device *dev, void *data,
685 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300687 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100688 struct drm_i915_master_private *master_priv;
689 drm_i915_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000690 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700691 struct drm_clip_rect *cliprects = NULL;
692 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int ret;
694
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800695 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800696 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200698 if (drm_core_check_feature(dev, DRIVER_MODESET))
699 return -ENODEV;
700
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100701 master_priv = dev->primary->master->driver_priv;
702 sarea_priv = (drm_i915_sarea_t *) master_priv->sarea_priv;
703
Eric Anholt546b0972008-09-01 16:45:29 -0700704 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Eric Anholt201361a2009-03-11 12:30:04 -0700706 if (cmdbuf->num_cliprects < 0)
707 return -EINVAL;
708
Eric Anholt9a298b22009-03-24 12:23:04 -0700709 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700710 if (batch_data == NULL)
711 return -ENOMEM;
712
713 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200714 if (ret != 0) {
715 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700716 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200717 }
Eric Anholt201361a2009-03-11 12:30:04 -0700718
719 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700720 cliprects = kcalloc(cmdbuf->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200721 sizeof(*cliprects), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000722 if (cliprects == NULL) {
723 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700724 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000725 }
Eric Anholt201361a2009-03-11 12:30:04 -0700726
727 ret = copy_from_user(cliprects, cmdbuf->cliprects,
728 cmdbuf->num_cliprects *
729 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200730 if (ret != 0) {
731 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700732 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Eric Anholt546b0972008-09-01 16:45:29 -0700736 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700737 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700738 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (ret) {
740 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000741 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400744 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000745 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700746
Eric Anholt201361a2009-03-11 12:30:04 -0700747fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700748 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000749fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700750 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700751
752 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Daniel Vetter94888672012-04-26 23:28:08 +0200755static int i915_emit_irq(struct drm_device * dev)
756{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300757 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200758 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
759
760 i915_kernel_lost_context(dev);
761
762 DRM_DEBUG_DRIVER("\n");
763
Daniel Vetter231f42a2012-11-02 19:55:05 +0100764 dev_priv->dri1.counter++;
765 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
766 dev_priv->dri1.counter = 1;
Daniel Vetter94888672012-04-26 23:28:08 +0200767 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100768 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200769
770 if (BEGIN_LP_RING(4) == 0) {
771 OUT_RING(MI_STORE_DWORD_INDEX);
772 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100773 OUT_RING(dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200774 OUT_RING(MI_USER_INTERRUPT);
775 ADVANCE_LP_RING();
776 }
777
Daniel Vetter231f42a2012-11-02 19:55:05 +0100778 return dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200779}
780
781static int i915_wait_irq(struct drm_device * dev, int irq_nr)
782{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300783 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200784 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
785 int ret = 0;
786 struct intel_ring_buffer *ring = LP_RING(dev_priv);
787
788 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
789 READ_BREADCRUMB(dev_priv));
790
791 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
792 if (master_priv->sarea_priv)
793 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
794 return 0;
795 }
796
797 if (master_priv->sarea_priv)
798 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
799
800 if (ring->irq_get(ring)) {
Daniel Vetterbfd83032013-12-11 11:34:41 +0100801 DRM_WAIT_ON(ret, ring->irq_queue, 3 * HZ,
Daniel Vetter94888672012-04-26 23:28:08 +0200802 READ_BREADCRUMB(dev_priv) >= irq_nr);
803 ring->irq_put(ring);
804 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
805 ret = -EBUSY;
806
807 if (ret == -EBUSY) {
808 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Daniel Vetter231f42a2012-11-02 19:55:05 +0100809 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200810 }
811
812 return ret;
813}
814
815/* Needs the lock as it touches the ring.
816 */
817static int i915_irq_emit(struct drm_device *dev, void *data,
818 struct drm_file *file_priv)
819{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300820 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200821 drm_i915_irq_emit_t *emit = data;
822 int result;
823
824 if (drm_core_check_feature(dev, DRIVER_MODESET))
825 return -ENODEV;
826
827 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
828 DRM_ERROR("called with no initialization\n");
829 return -EINVAL;
830 }
831
832 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
833
834 mutex_lock(&dev->struct_mutex);
835 result = i915_emit_irq(dev);
836 mutex_unlock(&dev->struct_mutex);
837
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100838 if (copy_to_user(emit->irq_seq, &result, sizeof(int))) {
Daniel Vetter94888672012-04-26 23:28:08 +0200839 DRM_ERROR("copy_to_user\n");
840 return -EFAULT;
841 }
842
843 return 0;
844}
845
846/* Doesn't need the hardware lock.
847 */
848static int i915_irq_wait(struct drm_device *dev, void *data,
849 struct drm_file *file_priv)
850{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300851 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200852 drm_i915_irq_wait_t *irqwait = data;
853
854 if (drm_core_check_feature(dev, DRIVER_MODESET))
855 return -ENODEV;
856
857 if (!dev_priv) {
858 DRM_ERROR("called with no initialization\n");
859 return -EINVAL;
860 }
861
862 return i915_wait_irq(dev, irqwait->irq_seq);
863}
864
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200865static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
866 struct drm_file *file_priv)
867{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300868 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200869 drm_i915_vblank_pipe_t *pipe = data;
870
871 if (drm_core_check_feature(dev, DRIVER_MODESET))
872 return -ENODEV;
873
874 if (!dev_priv) {
875 DRM_ERROR("called with no initialization\n");
876 return -EINVAL;
877 }
878
879 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
880
881 return 0;
882}
883
884/**
885 * Schedule buffer swap at given vertical blank.
886 */
887static int i915_vblank_swap(struct drm_device *dev, void *data,
888 struct drm_file *file_priv)
889{
890 /* The delayed swap mechanism was fundamentally racy, and has been
891 * removed. The model was that the client requested a delayed flip/swap
892 * from the kernel, then waited for vblank before continuing to perform
893 * rendering. The problem was that the kernel might wake the client
894 * up before it dispatched the vblank swap (since the lock has to be
895 * held while touching the ringbuffer), in which case the client would
896 * clear and start the next frame before the swap occurred, and
897 * flicker would occur in addition to likely missing the vblank.
898 *
899 * In the absence of this ioctl, userland falls back to a correct path
900 * of waiting for a vblank, then dispatching the swap on its own.
901 * Context switching to userland and back is plenty fast enough for
902 * meeting the requirements of vblank swapping.
903 */
904 return -EINVAL;
905}
906
Eric Anholtc153f452007-09-03 12:06:45 +1000907static int i915_flip_bufs(struct drm_device *dev, void *data,
908 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Eric Anholt546b0972008-09-01 16:45:29 -0700910 int ret;
911
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200912 if (drm_core_check_feature(dev, DRIVER_MODESET))
913 return -ENODEV;
914
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800915 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Eric Anholt546b0972008-09-01 16:45:29 -0700917 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Eric Anholt546b0972008-09-01 16:45:29 -0700919 mutex_lock(&dev->struct_mutex);
920 ret = i915_dispatch_flip(dev);
921 mutex_unlock(&dev->struct_mutex);
922
923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Eric Anholtc153f452007-09-03 12:06:45 +1000926static int i915_getparam(struct drm_device *dev, void *data,
927 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300929 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000930 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 int value;
932
933 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000934 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000935 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Eric Anholtc153f452007-09-03 12:06:45 +1000938 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700940 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 break;
942 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200943 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100945 case I915_PARAM_LAST_DISPATCH:
946 value = READ_BREADCRUMB(dev_priv);
947 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400948 case I915_PARAM_CHIPSET_ID:
Ville Syrjäläffbab09b2013-10-04 14:53:40 +0300949 value = dev->pdev->device;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400950 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700951 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200952 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700953 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800954 case I915_PARAM_NUM_FENCES_AVAIL:
955 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
956 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200957 case I915_PARAM_HAS_OVERLAY:
958 value = dev_priv->overlay ? 1 : 0;
959 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800960 case I915_PARAM_HAS_PAGEFLIPPING:
961 value = 1;
962 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500963 case I915_PARAM_HAS_EXECBUF2:
964 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200965 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500966 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800967 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100968 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800969 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100970 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100971 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100972 break;
Xiang, Haihaoa1f2cc72013-05-28 19:22:34 -0700973 case I915_PARAM_HAS_VEBOX:
974 value = intel_ring_initialized(&dev_priv->ring[VECS]);
975 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100976 case I915_PARAM_HAS_RELAXED_FENCING:
977 value = 1;
978 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100979 case I915_PARAM_HAS_COHERENT_RINGS:
980 value = 1;
981 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000982 case I915_PARAM_HAS_EXEC_CONSTANTS:
983 value = INTEL_INFO(dev)->gen >= 4;
984 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000985 case I915_PARAM_HAS_RELAXED_DELTA:
986 value = 1;
987 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800988 case I915_PARAM_HAS_GEN7_SOL_RESET:
989 value = 1;
990 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200991 case I915_PARAM_HAS_LLC:
992 value = HAS_LLC(dev);
993 break;
Chris Wilson651d7942013-08-08 14:41:10 +0100994 case I915_PARAM_HAS_WT:
995 value = HAS_WT(dev);
996 break;
Daniel Vetter777ee962012-02-15 23:50:25 +0100997 case I915_PARAM_HAS_ALIASING_PPGTT:
Daniel Vetter7d9c4772013-12-18 16:32:00 +0100998 value = dev_priv->mm.aliasing_ppgtt || USES_FULL_PPGTT(dev);
Daniel Vetter777ee962012-02-15 23:50:25 +0100999 break;
Ben Widawsky172cf152012-06-05 15:24:25 -07001000 case I915_PARAM_HAS_WAIT_TIMEOUT:
1001 value = 1;
1002 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +01001003 case I915_PARAM_HAS_SEMAPHORES:
1004 value = i915_semaphore_is_enabled(dev);
1005 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +10001006 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
1007 value = 1;
1008 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001009 case I915_PARAM_HAS_SECURE_BATCHES:
1010 value = capable(CAP_SYS_ADMIN);
1011 break;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001012 case I915_PARAM_HAS_PINNED_BATCHES:
1013 value = 1;
1014 break;
Daniel Vettered5982e2013-01-17 22:23:36 +01001015 case I915_PARAM_HAS_EXEC_NO_RELOC:
1016 value = 1;
1017 break;
Chris Wilsoneef90cc2013-01-08 10:53:17 +00001018 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
1019 value = 1;
1020 break;
Brad Volkind728c8e2014-02-18 10:15:56 -08001021 case I915_PARAM_CMD_PARSER_VERSION:
1022 value = i915_cmd_parser_get_version();
1023 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 default:
Ben Widawskye29c32d2013-05-31 11:28:45 -07001025 DRM_DEBUG("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001026 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001029 if (copy_to_user(param->value, &value, sizeof(int))) {
1030 DRM_ERROR("copy_to_user failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001031 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 return 0;
1035}
1036
Eric Anholtc153f452007-09-03 12:06:45 +10001037static int i915_setparam(struct drm_device *dev, void *data,
1038 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001040 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001041 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001044 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001045 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
Eric Anholtc153f452007-09-03 12:06:45 +10001048 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 break;
1053 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001054 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001056 case I915_SETPARAM_NUM_USED_FENCES:
1057 if (param->value > dev_priv->num_fence_regs ||
1058 param->value < 0)
1059 return -EINVAL;
1060 /* Userspace can use first N regs */
1061 dev_priv->fence_reg_start = param->value;
1062 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001064 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001065 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001066 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
1069 return 0;
1070}
1071
Eric Anholtc153f452007-09-03 12:06:45 +10001072static int i915_set_status_page(struct drm_device *dev, void *data,
1073 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001074{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001075 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001076 drm_i915_hws_addr_t *hws = data;
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001077 struct intel_ring_buffer *ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001078
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001079 if (drm_core_check_feature(dev, DRIVER_MODESET))
1080 return -ENODEV;
1081
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001082 if (!I915_NEED_GFX_HWS(dev))
1083 return -EINVAL;
1084
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001085 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001086 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001087 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001088 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001089
Jesse Barnes79e53942008-11-07 14:24:08 -08001090 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1091 WARN(1, "tried to set status page when mode setting active\n");
1092 return 0;
1093 }
1094
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001095 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001096
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001097 ring = LP_RING(dev_priv);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001098 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001099
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001100 dev_priv->dri1.gfx_hws_cpu_addr =
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001101 ioremap_wc(dev_priv->gtt.mappable_base + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001102 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001103 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001104 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001105 DRM_ERROR("can not ioremap virtual address for"
1106 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001107 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001108 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001109
1110 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001111 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001112
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001113 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001114 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001115 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001116 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001117 return 0;
1118}
1119
Dave Airlieec2a4c32009-08-04 11:43:41 +10001120static int i915_get_bridge_dev(struct drm_device *dev)
1121{
1122 struct drm_i915_private *dev_priv = dev->dev_private;
1123
Akshay Joshi0206e352011-08-16 15:34:10 -04001124 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001125 if (!dev_priv->bridge_dev) {
1126 DRM_ERROR("bridge device not found\n");
1127 return -1;
1128 }
1129 return 0;
1130}
1131
Zhenyu Wangc48044112009-12-17 14:48:43 +08001132#define MCHBAR_I915 0x44
1133#define MCHBAR_I965 0x48
1134#define MCHBAR_SIZE (4*4096)
1135
1136#define DEVEN_REG 0x54
1137#define DEVEN_MCHBAR_EN (1 << 28)
1138
1139/* Allocate space for the MCH regs if needed, return nonzero on error */
1140static int
1141intel_alloc_mchbar_resource(struct drm_device *dev)
1142{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001143 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001144 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001145 u32 temp_lo, temp_hi = 0;
1146 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001147 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001148
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001149 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001150 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1151 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1152 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1153
1154 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1155#ifdef CONFIG_PNP
1156 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001157 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1158 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001159#endif
1160
1161 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001162 dev_priv->mch_res.name = "i915 MCHBAR";
1163 dev_priv->mch_res.flags = IORESOURCE_MEM;
1164 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1165 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001166 MCHBAR_SIZE, MCHBAR_SIZE,
1167 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001168 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001169 dev_priv->bridge_dev);
1170 if (ret) {
1171 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1172 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001173 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001174 }
1175
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001176 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001177 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1178 upper_32_bits(dev_priv->mch_res.start));
1179
1180 pci_write_config_dword(dev_priv->bridge_dev, reg,
1181 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001182 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001183}
1184
1185/* Setup MCHBAR if possible, return true if we should disable it again */
1186static void
1187intel_setup_mchbar(struct drm_device *dev)
1188{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001189 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001190 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001191 u32 temp;
1192 bool enabled;
1193
Jesse Barnes11ea8b72014-03-03 14:27:57 -08001194 if (IS_VALLEYVIEW(dev))
1195 return;
1196
Zhenyu Wangc48044112009-12-17 14:48:43 +08001197 dev_priv->mchbar_need_disable = false;
1198
1199 if (IS_I915G(dev) || IS_I915GM(dev)) {
1200 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1201 enabled = !!(temp & DEVEN_MCHBAR_EN);
1202 } else {
1203 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1204 enabled = temp & 1;
1205 }
1206
1207 /* If it's already enabled, don't have to do anything */
1208 if (enabled)
1209 return;
1210
1211 if (intel_alloc_mchbar_resource(dev))
1212 return;
1213
1214 dev_priv->mchbar_need_disable = true;
1215
1216 /* Space is allocated or reserved, so enable it. */
1217 if (IS_I915G(dev) || IS_I915GM(dev)) {
1218 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1219 temp | DEVEN_MCHBAR_EN);
1220 } else {
1221 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1222 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1223 }
1224}
1225
1226static void
1227intel_teardown_mchbar(struct drm_device *dev)
1228{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001229 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001230 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001231 u32 temp;
1232
1233 if (dev_priv->mchbar_need_disable) {
1234 if (IS_I915G(dev) || IS_I915GM(dev)) {
1235 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1236 temp &= ~DEVEN_MCHBAR_EN;
1237 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1238 } else {
1239 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1240 temp &= ~1;
1241 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1242 }
1243 }
1244
1245 if (dev_priv->mch_res.start)
1246 release_resource(&dev_priv->mch_res);
1247}
1248
Dave Airlie28d52042009-09-21 14:33:58 +10001249/* true = enable decode, false = disable decoder */
1250static unsigned int i915_vga_set_decode(void *cookie, bool state)
1251{
1252 struct drm_device *dev = cookie;
1253
1254 intel_modeset_vga_set_state(dev, state);
1255 if (state)
1256 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1257 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1258 else
1259 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1260}
1261
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001262static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1263{
1264 struct drm_device *dev = pci_get_drvdata(pdev);
1265 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1266 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001267 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001268 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001269 /* i915 resume handler doesn't set to D0 */
1270 pci_set_power_state(dev->pdev, PCI_D0);
1271 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001272 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001273 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001274 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001275 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001276 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001277 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001278 }
1279}
1280
1281static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1282{
1283 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001284
Daniel Vetterfc8fd402013-11-03 20:46:34 +01001285 /*
1286 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1287 * locking inversion with the driver load path. And the access here is
1288 * completely racy anyway. So don't bother with locking for now.
1289 */
1290 return dev->open_count == 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001291}
1292
Takashi Iwai26ec6852012-05-11 07:51:17 +02001293static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1294 .set_gpu_state = i915_switcheroo_set_state,
1295 .reprobe = NULL,
1296 .can_switch = i915_switcheroo_can_switch,
1297};
1298
Chris Wilson2c7111d2011-03-29 10:40:27 +01001299static int i915_load_modeset_init(struct drm_device *dev)
1300{
1301 struct drm_i915_private *dev_priv = dev->dev_private;
1302 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001303
Bryan Freed6d139a82010-10-14 09:14:51 +01001304 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001305 if (ret)
1306 DRM_INFO("failed to find VBIOS tables\n");
1307
Chris Wilson934f992c2011-01-20 13:09:12 +00001308 /* If we have > 1 VGA cards, then we need to arbitrate access
1309 * to the common VGA resources.
1310 *
1311 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1312 * then we do not take part in VGA arbitration and the
1313 * vga_client_register() fails with -ENODEV.
1314 */
Dave Airlieebff5fa92013-10-11 15:12:04 +10001315 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1316 if (ret && ret != -ENODEV)
1317 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001318
Jesse Barnes723bfd72010-10-07 16:01:13 -07001319 intel_register_dsm_handler();
1320
Dave Airlie0d697042012-09-10 12:28:36 +10001321 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001322 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001323 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001324
Chris Wilson9797fbf2012-04-24 15:47:39 +01001325 /* Initialise stolen first so that we may reserve preallocated
1326 * objects for the BIOS to KMS transition.
1327 */
1328 ret = i915_gem_init_stolen(dev);
1329 if (ret)
1330 goto cleanup_vga_switcheroo;
1331
Imre Deake13192f2014-02-18 00:02:15 +02001332 intel_power_domains_init_hw(dev_priv);
1333
Daniel Vetterbb0f1b52013-11-03 21:09:27 +01001334 ret = drm_irq_install(dev, dev->pdev->irq);
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001335 if (ret)
1336 goto cleanup_gem_stolen;
1337
1338 /* Important: The output setup functions called by modeset_init need
1339 * working irqs for e.g. gmbus and dp aux transfers. */
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001340 intel_modeset_init(dev);
1341
Chris Wilson1070a422012-04-24 15:47:41 +01001342 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001343 if (ret)
Imre Deak713028b2014-04-25 17:28:00 +03001344 goto cleanup_irq;
Chris Wilson2c7111d2011-03-29 10:40:27 +01001345
Jesse Barnes073f34d2012-11-02 11:13:59 -07001346 INIT_WORK(&dev_priv->console_resume_work, intel_console_resume);
1347
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001348 intel_modeset_gem_init(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001349
Jesse Barnes79e53942008-11-07 14:24:08 -08001350 /* Always safe in the mode setting case. */
1351 /* FIXME: do pre/post-mode set stuff in core KMS code */
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001352 dev->vblank_disable_allowed = true;
Imre Deak713028b2014-04-25 17:28:00 +03001353 if (INTEL_INFO(dev)->num_pipes == 0)
Ben Widawskye3c74752013-04-05 13:12:39 -07001354 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001355
Chris Wilson5a793952010-06-06 10:50:03 +01001356 ret = intel_fbdev_init(dev);
1357 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001358 goto cleanup_gem;
1359
1360 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter20afbda2012-12-11 14:05:07 +01001361 intel_hpd_init(dev);
1362
1363 /*
1364 * Some ports require correctly set-up hpd registers for detection to
1365 * work properly (leading to ghost connected connector status), e.g. VGA
1366 * on gm45. Hence we can only set up the initial fbdev config after hpd
1367 * irqs are fully enabled. Now we should scan for the initial config
1368 * only once hotplug handling is enabled, but due to screwed-up locking
1369 * around kms/fbdev init we can't protect the fdbev initial config
1370 * scanning against hotplug events. Hence do this first and ignore the
1371 * tiny window where we will loose hotplug notifactions.
1372 */
1373 intel_fbdev_initial_config(dev);
1374
1375 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001376 dev_priv->enable_hotplug_processing = true;
Chris Wilson5a793952010-06-06 10:50:03 +01001377
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001378 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001379
Jesse Barnes79e53942008-11-07 14:24:08 -08001380 return 0;
1381
Chris Wilson2c7111d2011-03-29 10:40:27 +01001382cleanup_gem:
1383 mutex_lock(&dev->struct_mutex);
1384 i915_gem_cleanup_ringbuffer(dev);
Ben Widawsky55d23282013-05-25 12:26:39 -07001385 i915_gem_context_fini(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001386 mutex_unlock(&dev->struct_mutex);
Ben Widawskybdf4fd72013-12-06 14:11:18 -08001387 WARN_ON(dev_priv->mm.aliasing_ppgtt);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001388 drm_mm_takedown(&dev_priv->gtt.base.mm);
Imre Deak713028b2014-04-25 17:28:00 +03001389cleanup_irq:
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001390 drm_irq_uninstall(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001391cleanup_gem_stolen:
1392 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001393cleanup_vga_switcheroo:
1394 vga_switcheroo_unregister_client(dev->pdev);
1395cleanup_vga_client:
1396 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001397out:
1398 return ret;
1399}
1400
Dave Airlie7c1c2872008-11-28 14:22:24 +10001401int i915_master_create(struct drm_device *dev, struct drm_master *master)
1402{
1403 struct drm_i915_master_private *master_priv;
1404
Eric Anholt9a298b22009-03-24 12:23:04 -07001405 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001406 if (!master_priv)
1407 return -ENOMEM;
1408
1409 master->driver_priv = master_priv;
1410 return 0;
1411}
1412
1413void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1414{
1415 struct drm_i915_master_private *master_priv = master->driver_priv;
1416
1417 if (!master_priv)
1418 return;
1419
Eric Anholt9a298b22009-03-24 12:23:04 -07001420 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001421
1422 master->driver_priv = NULL;
1423}
1424
Daniel Vetter243eaf32013-12-17 10:00:54 +01001425#if IS_ENABLED(CONFIG_FB)
Daniel Vettere1887192012-06-12 11:28:17 +02001426static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1427{
1428 struct apertures_struct *ap;
1429 struct pci_dev *pdev = dev_priv->dev->pdev;
1430 bool primary;
1431
1432 ap = alloc_apertures(1);
1433 if (!ap)
1434 return;
1435
Ben Widawskydabb7a92013-01-17 12:45:16 -08001436 ap->ranges[0].base = dev_priv->gtt.mappable_base;
Ben Widawskyf64e2922013-05-25 12:26:36 -07001437 ap->ranges[0].size = dev_priv->gtt.mappable_end;
Ben Widawsky93d18792013-01-17 12:45:17 -08001438
Daniel Vettere1887192012-06-12 11:28:17 +02001439 primary =
1440 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1441
1442 remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
1443
1444 kfree(ap);
1445}
Daniel Vetter4520f532013-10-09 09:18:51 +02001446#else
1447static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1448{
1449}
1450#endif
Daniel Vettere1887192012-06-12 11:28:17 +02001451
Daniel Vetterc96ea642012-08-08 22:01:51 +02001452static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1453{
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001454 const struct intel_device_info *info = &dev_priv->info;
Daniel Vetterc96ea642012-08-08 22:01:51 +02001455
Damien Lespiaue2a58002013-04-23 16:38:34 +01001456#define PRINT_S(name) "%s"
1457#define SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001458#define PRINT_FLAG(name) info->name ? #name "," : ""
1459#define SEP_COMMA ,
Daniel Vetterc96ea642012-08-08 22:01:51 +02001460 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
Damien Lespiaue2a58002013-04-23 16:38:34 +01001461 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
Daniel Vetterc96ea642012-08-08 22:01:51 +02001462 info->gen,
1463 dev_priv->dev->pdev->device,
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001464 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
Damien Lespiaue2a58002013-04-23 16:38:34 +01001465#undef PRINT_S
1466#undef SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001467#undef PRINT_FLAG
1468#undef SEP_COMMA
Daniel Vetterc96ea642012-08-08 22:01:51 +02001469}
1470
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001471/*
1472 * Determine various intel_device_info fields at runtime.
1473 *
1474 * Use it when either:
1475 * - it's judged too laborious to fill n static structures with the limit
1476 * when a simple if statement does the job,
1477 * - run-time checks (eg read fuse/strap registers) are needed.
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001478 *
1479 * This function needs to be called:
1480 * - after the MMIO has been setup as we are reading registers,
1481 * - after the PCH has been detected,
1482 * - before the first usage of the fields it can tweak.
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001483 */
1484static void intel_device_info_runtime_init(struct drm_device *dev)
1485{
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001486 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001487 struct intel_device_info *info;
Damien Lespiaud615a162014-03-03 17:31:48 +00001488 enum pipe pipe;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001489
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001490 info = (struct intel_device_info *)&dev_priv->info;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001491
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001492 if (IS_VALLEYVIEW(dev))
Damien Lespiaud615a162014-03-03 17:31:48 +00001493 for_each_pipe(pipe)
1494 info->num_sprites[pipe] = 2;
1495 else
1496 for_each_pipe(pipe)
1497 info->num_sprites[pipe] = 1;
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001498
Damien Lespiaua0bae572014-02-10 17:20:55 +00001499 if (i915.disable_display) {
1500 DRM_INFO("Display disabled (module parameter)\n");
1501 info->num_pipes = 0;
1502 } else if (info->num_pipes > 0 &&
1503 (INTEL_INFO(dev)->gen == 7 || INTEL_INFO(dev)->gen == 8) &&
1504 !IS_VALLEYVIEW(dev)) {
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001505 u32 fuse_strap = I915_READ(FUSE_STRAP);
1506 u32 sfuse_strap = I915_READ(SFUSE_STRAP);
1507
1508 /*
1509 * SFUSE_STRAP is supposed to have a bit signalling the display
1510 * is fused off. Unfortunately it seems that, at least in
1511 * certain cases, fused off display means that PCH display
1512 * reads don't land anywhere. In that case, we read 0s.
1513 *
1514 * On CPT/PPT, we can detect this case as SFUSE_STRAP_FUSE_LOCK
1515 * should be set when taking over after the firmware.
1516 */
1517 if (fuse_strap & ILK_INTERNAL_DISPLAY_DISABLE ||
1518 sfuse_strap & SFUSE_STRAP_DISPLAY_DISABLED ||
1519 (dev_priv->pch_type == PCH_CPT &&
1520 !(sfuse_strap & SFUSE_STRAP_FUSE_LOCK))) {
1521 DRM_INFO("Display fused off, disabling\n");
1522 info->num_pipes = 0;
1523 }
1524 }
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001525}
1526
Eric Anholt63ee41d2010-12-20 18:40:06 -08001527/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001528 * i915_driver_load - setup chip and create an initial config
1529 * @dev: DRM device
1530 * @flags: startup flags
1531 *
1532 * The driver load routine has to do several things:
1533 * - drive output discovery via intel_modeset_init()
1534 * - initialize the memory manager
1535 * - allocate initial config memory
1536 * - setup the DRM framebuffer with the allocated memory
1537 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001538int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001539{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001540 struct drm_i915_private *dev_priv;
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001541 struct intel_device_info *info, *device_info;
Chris Wilson934d6082012-09-14 11:57:46 +01001542 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001543 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001544
Daniel Vetter26394d92012-03-26 21:33:18 +02001545 info = (struct intel_device_info *) flags;
1546
1547 /* Refuse to load on gen6+ without kms enabled. */
Jani Nikulae147acc2013-10-10 15:25:37 +03001548 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET)) {
1549 DRM_INFO("Your hardware requires kernel modesetting (KMS)\n");
1550 DRM_INFO("See CONFIG_DRM_I915_KMS, nomodeset, and i915.modeset parameters\n");
Daniel Vetter26394d92012-03-26 21:33:18 +02001551 return -ENODEV;
Jani Nikulae147acc2013-10-10 15:25:37 +03001552 }
Daniel Vetter26394d92012-03-26 21:33:18 +02001553
Daniel Vetter24986ee2013-12-11 11:34:33 +01001554 /* UMS needs agp support. */
1555 if (!drm_core_check_feature(dev, DRIVER_MODESET) && !dev->agp)
1556 return -EINVAL;
1557
Daniel Vetterb14c5672013-09-19 12:18:32 +02001558 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001559 if (dev_priv == NULL)
1560 return -ENOMEM;
1561
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001562 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001563 dev_priv->dev = dev;
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001564
1565 /* copy initial configuration to dev_priv->info */
1566 device_info = (struct intel_device_info *)&dev_priv->info;
1567 *device_info = *info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001568
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001569 spin_lock_init(&dev_priv->irq_lock);
1570 spin_lock_init(&dev_priv->gpu_error.lock);
Jani Nikula58c68772013-11-08 16:48:54 +02001571 spin_lock_init(&dev_priv->backlight_lock);
Chris Wilson907b28c2013-07-19 20:36:52 +01001572 spin_lock_init(&dev_priv->uncore.lock);
Daniel Vetterc20e8352013-07-24 22:40:23 +02001573 spin_lock_init(&dev_priv->mm.object_stat_lock);
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001574 dev_priv->ring_index = 0;
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001575 mutex_init(&dev_priv->dpio_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001576 mutex_init(&dev_priv->modeset_restore_lock);
1577
Daniel Vetterf742a552013-12-06 10:17:53 +01001578 intel_pm_setup(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03001579
Damien Lespiau07144422013-10-15 18:55:40 +01001580 intel_display_crc_init(dev);
1581
Daniel Vetterc96ea642012-08-08 22:01:51 +02001582 i915_dump_device_info(dev_priv);
1583
Paulo Zanonied1c9e22013-08-12 14:34:08 -03001584 /* Not all pre-production machines fall into this category, only the
1585 * very first ones. Almost everything should work, except for maybe
1586 * suspend/resume. And we don't implement workarounds that affect only
1587 * pre-production machines. */
1588 if (IS_HSW_EARLY_SDV(dev))
1589 DRM_INFO("This is an early pre-production Haswell machine. "
1590 "It may not be fully functional.\n");
1591
Dave Airlieec2a4c32009-08-04 11:43:41 +10001592 if (i915_get_bridge_dev(dev)) {
1593 ret = -EIO;
1594 goto free_priv;
1595 }
1596
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001597 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1598 /* Before gen4, the registers and the GTT are behind different BARs.
1599 * However, from gen4 onwards, the registers and the GTT are shared
1600 * in the same BAR, so we want to restrict this ioremap from
1601 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1602 * the register BAR remains the same size for all the earlier
1603 * generations up to Ironlake.
1604 */
1605 if (info->gen < 5)
1606 mmio_size = 512*1024;
1607 else
1608 mmio_size = 2*1024*1024;
1609
1610 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
1611 if (!dev_priv->regs) {
1612 DRM_ERROR("failed to map registers\n");
1613 ret = -EIO;
1614 goto put_bridge;
1615 }
1616
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001617 /* This must be called before any calls to HAS_PCH_* */
1618 intel_detect_pch(dev);
1619
1620 intel_uncore_init(dev);
1621
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001622 ret = i915_gem_gtt_init(dev);
1623 if (ret)
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001624 goto out_regs;
Daniel Vettere1887192012-06-12 11:28:17 +02001625
Chris Wilson16233922012-10-26 12:06:41 +01001626 if (drm_core_check_feature(dev, DRIVER_MODESET))
1627 i915_kick_out_firmware_fb(dev_priv);
Daniel Vettere1887192012-06-12 11:28:17 +02001628
Dave Airlie466e69b2011-12-19 11:15:29 +00001629 pci_set_master(dev->pdev);
1630
Daniel Vetter9f82d232010-08-30 21:25:23 +02001631 /* overlay on gen2 is broken and can't address above 1G */
1632 if (IS_GEN2(dev))
1633 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1634
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001635 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1636 * using 32bit addressing, overwriting memory if HWS is located
1637 * above 4GB.
1638 *
1639 * The documentation also mentions an issue with undefined
1640 * behaviour if any general state is accessed within a page above 4GB,
1641 * which also needs to be handled carefully.
1642 */
1643 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1644 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1645
Ben Widawsky93d18792013-01-17 12:45:17 -08001646 aperture_size = dev_priv->gtt.mappable_end;
Chris Wilson71e93392010-10-27 18:46:52 +01001647
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001648 dev_priv->gtt.mappable =
1649 io_mapping_create_wc(dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001650 aperture_size);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001651 if (dev_priv->gtt.mappable == NULL) {
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001652 ret = -EIO;
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001653 goto out_gtt;
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001654 }
1655
Ben Widawsky911bdf02013-06-27 16:30:23 -07001656 dev_priv->gtt.mtrr = arch_phys_wc_add(dev_priv->gtt.mappable_base,
1657 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001658
Chris Wilsone642abb2010-09-09 12:46:34 +01001659 /* The i915 workqueue is primarily used for batched retirement of
1660 * requests (and thus managing bo) once the task has been completed
1661 * by the GPU. i915_gem_retire_requests() is called directly when we
1662 * need high-priority retirement, such as waiting for an explicit
1663 * bo.
1664 *
1665 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001666 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001667 *
1668 * All tasks on the workqueue are expected to acquire the dev mutex
1669 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001670 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001671 */
Tejun Heo53621862012-08-22 16:40:57 -07001672 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001673 if (dev_priv->wq == NULL) {
1674 DRM_ERROR("Failed to create our workqueue.\n");
1675 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001676 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001677 }
1678
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001679 intel_irq_init(dev);
Ben Widawsky78511f22013-10-04 21:22:49 -07001680 intel_uncore_sanitize(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001681
Zhenyu Wangc48044112009-12-17 14:48:43 +08001682 /* Try to make sure MCHBAR is enabled before poking at it */
1683 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001684 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001685 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001686
Bryan Freed6d139a82010-10-14 09:14:51 +01001687 intel_setup_bios(dev);
1688
Eric Anholt673a3942008-07-30 12:06:12 -07001689 i915_gem_load(dev);
1690
Eric Anholted4cb412008-07-29 12:10:39 -07001691 /* On the 945G/GM, the chipset reports the MSI capability on the
1692 * integrated graphics even though the support isn't actually there
1693 * according to the published specs. It doesn't appear to function
1694 * correctly in testing on 945G.
1695 * This may be a side effect of MSI having been made available for PEG
1696 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001697 *
1698 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001699 * be lost or delayed, but we use them anyways to avoid
1700 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001701 */
Keith Packardb60678a2008-12-08 11:12:28 -08001702 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001703 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001704
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001705 intel_device_info_runtime_init(dev);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001706
Ben Widawskye3c74752013-04-05 13:12:39 -07001707 if (INTEL_INFO(dev)->num_pipes) {
1708 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1709 if (ret)
1710 goto out_gem_unload;
1711 }
Keith Packard52440212008-11-18 09:30:25 -08001712
Imre Deakda7e29b2014-02-18 00:02:02 +02001713 intel_power_domains_init(dev_priv);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001714
Jesse Barnes79e53942008-11-07 14:24:08 -08001715 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001716 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001717 if (ret < 0) {
1718 DRM_ERROR("failed to init modeset\n");
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001719 goto out_power_well;
Jesse Barnes79e53942008-11-07 14:24:08 -08001720 }
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001721 } else {
1722 /* Start out suspended in ums mode. */
1723 dev_priv->ums.mm_suspended = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001724 }
1725
Ben Widawsky0136db52012-04-10 21:17:01 -07001726 i915_setup_sysfs(dev);
1727
Ben Widawskye3c74752013-04-05 13:12:39 -07001728 if (INTEL_INFO(dev)->num_pipes) {
1729 /* Must be done after probing outputs */
1730 intel_opregion_init(dev);
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001731 acpi_video_register();
Ben Widawskye3c74752013-04-05 13:12:39 -07001732 }
Matthew Garrett74a365b2009-03-19 21:35:39 +00001733
Daniel Vettereb48eb02012-04-26 23:28:12 +02001734 if (IS_GEN5(dev))
1735 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001736
Paulo Zanoni8a187452013-12-06 20:32:13 -02001737 intel_init_runtime_pm(dev_priv);
1738
Jesse Barnes79e53942008-11-07 14:24:08 -08001739 return 0;
1740
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001741out_power_well:
Imre Deakda7e29b2014-02-18 00:02:02 +02001742 intel_power_domains_remove(dev_priv);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001743 drm_vblank_cleanup(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001744out_gem_unload:
Imre Deak4bdc7292014-05-20 19:47:20 +03001745 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1746 unregister_shrinker(&dev_priv->mm.shrinker);
Keith Packarda7b85d22011-07-10 13:12:17 -07001747
Chris Wilson56e2ea32010-11-08 17:10:29 +00001748 if (dev->pdev->msi_enabled)
1749 pci_disable_msi(dev->pdev);
1750
1751 intel_teardown_gmbus(dev);
1752 intel_teardown_mchbar(dev);
Stanislaw Gruszka22accca2014-01-25 10:13:37 +01001753 pm_qos_remove_request(&dev_priv->pm_qos);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001754 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001755out_mtrrfree:
Ben Widawsky911bdf02013-06-27 16:30:23 -07001756 arch_phys_wc_del(dev_priv->gtt.mtrr);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001757 io_mapping_free(dev_priv->gtt.mappable);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001758out_gtt:
1759 list_del(&dev_priv->gtt.base.global_link);
1760 drm_mm_takedown(&dev_priv->gtt.base.mm);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001761 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001762out_regs:
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001763 intel_uncore_fini(dev);
Chris Wilson6dda5692010-10-29 21:02:18 +01001764 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001765put_bridge:
1766 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001767free_priv:
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001768 if (dev_priv->slab)
1769 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001770 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001771 return ret;
1772}
1773
1774int i915_driver_unload(struct drm_device *dev)
1775{
1776 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001777 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001778
Chris Wilsonce58c322013-12-02 11:26:07 -02001779 ret = i915_gem_suspend(dev);
1780 if (ret) {
1781 DRM_ERROR("failed to idle hardware: %d\n", ret);
1782 return ret;
1783 }
1784
Paulo Zanoni8a187452013-12-06 20:32:13 -02001785 intel_fini_runtime_pm(dev_priv);
1786
Daniel Vettereb48eb02012-04-26 23:28:12 +02001787 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001788
Imre Deak1c2256d2013-11-25 17:15:34 +02001789 /* The i915.ko module is still not prepared to be loaded when
1790 * the power well is not enabled, so just enable it in case
1791 * we're going to unload/reload. */
Imre Deakda7e29b2014-02-18 00:02:02 +02001792 intel_display_set_init_power(dev_priv, true);
1793 intel_power_domains_remove(dev_priv);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001794
Ben Widawsky0136db52012-04-10 21:17:01 -07001795 i915_teardown_sysfs(dev);
1796
Imre Deak4bdc7292014-05-20 19:47:20 +03001797 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1798 unregister_shrinker(&dev_priv->mm.shrinker);
Chris Wilson17250b72010-10-28 12:51:39 +01001799
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001800 io_mapping_free(dev_priv->gtt.mappable);
Ben Widawsky911bdf02013-06-27 16:30:23 -07001801 arch_phys_wc_del(dev_priv->gtt.mtrr);
Eric Anholtab657db12009-01-23 12:57:47 -08001802
Chris Wilson44834a62010-08-19 16:09:23 +01001803 acpi_video_unregister();
1804
Jesse Barnes79e53942008-11-07 14:24:08 -08001805 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01001806 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001807 intel_modeset_cleanup(dev);
Jesse Barnes073f34d2012-11-02 11:13:59 -07001808 cancel_work_sync(&dev_priv->console_resume_work);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001809
Zhao Yakui6363ee62009-11-24 09:48:44 +08001810 /*
1811 * free the memory space allocated for the child device
1812 * config parsed from VBT
1813 */
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03001814 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1815 kfree(dev_priv->vbt.child_dev);
1816 dev_priv->vbt.child_dev = NULL;
1817 dev_priv->vbt.child_dev_num = 0;
Zhao Yakui6363ee62009-11-24 09:48:44 +08001818 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001819
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001820 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001821 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001822 }
1823
Daniel Vettera8b48992010-08-20 21:25:11 +02001824 /* Free error state after interrupts are fully disabled. */
Daniel Vetter99584db2012-11-14 17:14:04 +01001825 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
1826 cancel_work_sync(&dev_priv->gpu_error.work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001827 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001828
Eric Anholted4cb412008-07-29 12:10:39 -07001829 if (dev->pdev->msi_enabled)
1830 pci_disable_msi(dev->pdev);
1831
Chris Wilson44834a62010-08-19 16:09:23 +01001832 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001833
Jesse Barnes79e53942008-11-07 14:24:08 -08001834 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001835 /* Flush any outstanding unpin_work. */
1836 flush_workqueue(dev_priv->wq);
1837
Jesse Barnes79e53942008-11-07 14:24:08 -08001838 mutex_lock(&dev->struct_mutex);
Hugh Dickinsecbec532011-06-27 16:18:20 -07001839 i915_gem_free_all_phys_object(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001840 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001841 i915_gem_context_fini(dev);
Ben Widawskybdf4fd72013-12-06 14:11:18 -08001842 WARN_ON(dev_priv->mm.aliasing_ppgtt);
Jesse Barnes79e53942008-11-07 14:24:08 -08001843 mutex_unlock(&dev->struct_mutex);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001844 i915_gem_cleanup_stolen(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001845
1846 if (!I915_NEED_GFX_HWS(dev))
1847 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001848 }
1849
Ben Widawskya7bbbd62013-07-16 16:50:07 -07001850 list_del(&dev_priv->gtt.base.global_link);
1851 WARN_ON(!list_empty(&dev_priv->vm_list));
Daniel Vetter701394c2010-10-10 18:54:08 +01001852
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001853 drm_vblank_cleanup(dev);
1854
Chris Wilsonf899fc62010-07-20 15:44:45 -07001855 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001856 intel_teardown_mchbar(dev);
1857
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001858 destroy_workqueue(dev_priv->wq);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001859 pm_qos_remove_request(&dev_priv->pm_qos);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001860
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001861 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
Imre Deak6640aab2013-05-22 17:47:13 +03001862
Chris Wilsonaec347a2013-08-26 13:46:09 +01001863 intel_uncore_fini(dev);
1864 if (dev_priv->regs != NULL)
1865 pci_iounmap(dev->pdev, dev_priv->regs);
1866
Chris Wilson42dcedd2012-11-15 11:32:30 +00001867 if (dev_priv->slab)
1868 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001869
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001870 pci_dev_put(dev_priv->bridge_dev);
Daniel Vetter2206e6a2014-05-13 22:21:59 +02001871 kfree(dev_priv);
Dave Airlie22eae942005-11-10 22:16:34 +11001872
1873 return 0;
1874}
1875
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001876int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001877{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001878 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001879
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001880 ret = i915_gem_open(dev, file);
1881 if (ret)
1882 return ret;
Ben Widawsky254f9652012-06-04 14:42:42 -07001883
Eric Anholt673a3942008-07-30 12:06:12 -07001884 return 0;
1885}
1886
Jesse Barnes79e53942008-11-07 14:24:08 -08001887/**
1888 * i915_driver_lastclose - clean up after all DRM clients have exited
1889 * @dev: DRM device
1890 *
1891 * Take care of cleaning up after all DRM clients have exited. In the
1892 * mode setting case, we want to restore the kernel's initial mode (just
1893 * in case the last client left us in a bad state).
1894 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001895 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001896 * and DMA structures, since the kernel won't be using them, and clea
1897 * up any GEM state.
1898 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001899void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001901 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001902
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001903 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1904 * goes right around and calls lastclose. Check for this and don't clean
1905 * up anything. */
1906 if (!dev_priv)
1907 return;
1908
1909 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter0632fef2013-10-08 17:44:49 +02001910 intel_fbdev_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001911 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001912 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001913 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001914
Eric Anholt673a3942008-07-30 12:06:12 -07001915 i915_gem_lastclose(dev);
1916
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001917 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
Eric Anholt6c340ea2007-08-25 20:23:09 +10001920void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Chris Wilson0d1430a2013-12-04 14:52:06 +00001922 mutex_lock(&dev->struct_mutex);
Ben Widawsky254f9652012-06-04 14:42:42 -07001923 i915_gem_context_close(dev, file_priv);
Eric Anholtb9624422009-06-03 07:27:35 +00001924 i915_gem_release(dev, file_priv);
Chris Wilson0d1430a2013-12-04 14:52:06 +00001925 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001928void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001929{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001930 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001931
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001932 if (file_priv && file_priv->bsd_ring)
1933 file_priv->bsd_ring = NULL;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001934 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001935}
1936
Rob Clarkbaa70942013-08-02 13:27:49 -04001937const struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001938 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1939 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1940 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1941 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1942 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1943 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001944 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001945 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001946 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1947 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1948 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001949 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001950 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02001951 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001952 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
1953 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1954 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1955 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1956 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001957 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001958 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1959 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 +02001960 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1961 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1962 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1963 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 +10001964 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1965 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 +02001966 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1967 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1968 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1969 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1970 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1971 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1972 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1973 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1974 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1975 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 +10001976 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 +02001977 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001978 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1979 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08001980 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1981 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 +02001982 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1983 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1984 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1985 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Mika Kuoppalab6359912013-10-30 15:44:16 +02001986 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 +01001987 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airliec94f7022005-07-07 21:03:38 +10001988};
1989
1990int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001991
Daniel Vetter9021f282012-03-26 09:45:41 +02001992/*
1993 * This is really ugly: Because old userspace abused the linux agp interface to
1994 * manage the gtt, we need to claim that all intel devices are agp. For
1995 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10001996 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001997int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001998{
1999 return 1;
2000}