blob: e177d021c44412e6a5ec497aed6fdc86dce3b847 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Daniel Vetter09422b22012-04-26 23:28:10 +020048#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
49
50#define BEGIN_LP_RING(n) \
51 intel_ring_begin(LP_RING(dev_priv), (n))
52
53#define OUT_RING(x) \
54 intel_ring_emit(LP_RING(dev_priv), x)
55
56#define ADVANCE_LP_RING() \
Chris Wilson09246732013-08-10 22:16:32 +010057 __intel_ring_advance(LP_RING(dev_priv))
Daniel Vetter09422b22012-04-26 23:28:10 +020058
59/**
60 * Lock test for when it's just for synchronization of ring access.
61 *
62 * In that case, we don't need to do it when GEM is initialized as nobody else
63 * has access to the ring.
64 */
65#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
66 if (LP_RING(dev->dev_private)->obj == NULL) \
67 LOCK_TEST_WITH_RETURN(dev, file); \
68} while (0)
69
Daniel Vetter316d3882012-04-26 23:28:15 +020070static inline u32
71intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
72{
73 if (I915_NEED_GFX_HWS(dev_priv->dev))
74 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
75 else
76 return intel_read_status_page(LP_RING(dev_priv), reg);
77}
78
79#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
Daniel Vetter09422b22012-04-26 23:28:10 +020080#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
81#define I915_BREADCRUMB_INDEX 0x21
82
Daniel Vetterd05c6172012-04-26 23:28:09 +020083void i915_update_dri1_breadcrumb(struct drm_device *dev)
84{
85 drm_i915_private_t *dev_priv = dev->dev_private;
86 struct drm_i915_master_private *master_priv;
87
Daniel Vetter6c719fa2013-12-10 13:20:59 +010088 /*
89 * The dri breadcrumb update races against the drm master disappearing.
90 * Instead of trying to fix this (this is by far not the only ums issue)
91 * just don't do the update in kms mode.
92 */
93 if (drm_core_check_feature(dev, DRIVER_MODESET))
94 return;
95
Daniel Vetterd05c6172012-04-26 23:28:09 +020096 if (dev->primary->master) {
97 master_priv = dev->primary->master->driver_priv;
98 if (master_priv->sarea_priv)
99 master_priv->sarea_priv->last_dispatch =
100 READ_BREADCRUMB(dev_priv);
101 }
102}
103
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000104static void i915_write_hws_pga(struct drm_device *dev)
105{
106 drm_i915_private_t *dev_priv = dev->dev_private;
107 u32 addr;
108
109 addr = dev_priv->status_page_dmah->busaddr;
110 if (INTEL_INFO(dev)->gen >= 4)
111 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
112 I915_WRITE(HWS_PGA, addr);
113}
114
Keith Packard398c9cb2008-07-30 13:03:43 -0700115/**
Keith Packard398c9cb2008-07-30 13:03:43 -0700116 * Frees the hardware status page, whether it's a physical address or a virtual
117 * address set up by the X Server.
118 */
Eric Anholt3043c602008-10-02 12:24:47 -0700119static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700120{
121 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000122 struct intel_ring_buffer *ring = LP_RING(dev_priv);
123
Keith Packard398c9cb2008-07-30 13:03:43 -0700124 if (dev_priv->status_page_dmah) {
125 drm_pci_free(dev, dev_priv->status_page_dmah);
126 dev_priv->status_page_dmah = NULL;
127 }
128
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000129 if (ring->status_page.gfx_addr) {
130 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200131 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700132 }
133
134 /* Need to rewrite hardware status page */
135 I915_WRITE(HWS_PGA, 0x1ffff000);
136}
137
Dave Airlie84b1fd12007-07-11 15:53:27 +1000138void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000141 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000142 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jesse Barnes79e53942008-11-07 14:24:08 -0800144 /*
145 * We should never lose context on the ring with modesetting
146 * as we don't expose it to userspace
147 */
148 if (drm_core_check_feature(dev, DRIVER_MODESET))
149 return;
150
Chris Wilson8168bd42010-11-11 17:54:52 +0000151 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
152 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Ville Syrjälä633cf8f2012-12-03 18:43:32 +0200153 ring->space = ring->head - (ring->tail + I915_RING_FREE_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800155 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Dave Airlie7c1c2872008-11-28 14:22:24 +1000157 if (!dev->primary->master)
158 return;
159
160 master_priv = dev->primary->master->driver_priv;
161 if (ring->head == ring->tail && master_priv->sarea_priv)
162 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Dave Airlie84b1fd12007-07-11 15:53:27 +1000165static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000167 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000168 int i;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* Make sure interrupts are disabled here because the uninstall ioctl
171 * may not have been called from userspace and after dev_private
172 * is freed, it's too late.
173 */
Eric Anholted4cb412008-07-29 12:10:39 -0700174 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000175 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200177 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000178 for (i = 0; i < I915_NUM_RINGS; i++)
179 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200180 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Keith Packard398c9cb2008-07-30 13:03:43 -0700182 /* Clear the HWS virtual address at teardown */
183 if (I915_NEED_GFX_HWS(dev))
184 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 return 0;
187}
188
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000189static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000191 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000192 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000193 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Dave Airlie3a03ac12009-01-11 09:03:49 +1000195 master_priv->sarea = drm_getsarea(dev);
196 if (master_priv->sarea) {
197 master_priv->sarea_priv = (drm_i915_sarea_t *)
198 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
199 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800200 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000201 }
202
Eric Anholt673a3942008-07-30 12:06:12 -0700203 if (init->ring_size != 0) {
Chris Wilsone8616b62011-01-20 09:57:11 +0000204 if (LP_RING(dev_priv)->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700205 i915_dma_cleanup(dev);
206 DRM_ERROR("Client tried to initialize ringbuffer in "
207 "GEM mode\n");
208 return -EINVAL;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Chris Wilsone8616b62011-01-20 09:57:11 +0000211 ret = intel_render_ring_init_dri(dev,
212 init->ring_start,
213 init->ring_size);
214 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700215 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000216 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200220 dev_priv->dri1.cpp = init->cpp;
221 dev_priv->dri1.back_offset = init->back_offset;
222 dev_priv->dri1.front_offset = init->front_offset;
223 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000224 if (master_priv->sarea_priv)
225 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* Allow hardware batchbuffers unless told otherwise.
228 */
Daniel Vetter87813422012-05-02 11:49:32 +0200229 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232}
233
Dave Airlie84b1fd12007-07-11 15:53:27 +1000234static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000237 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800239 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Daniel Vetter4225d0f2012-04-26 23:28:16 +0200241 if (ring->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 DRM_ERROR("can not ioremap virtual address for"
243 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000244 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800248 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000250 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800252 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800253 ring->status_page.page_addr);
254 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100255 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000256 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000257 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800258
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800259 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return 0;
262}
263
Eric Anholtc153f452007-09-03 12:06:45 +1000264static int i915_dma_init(struct drm_device *dev, void *data,
265 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Eric Anholtc153f452007-09-03 12:06:45 +1000267 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int retcode = 0;
269
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200270 if (drm_core_check_feature(dev, DRIVER_MODESET))
271 return -ENODEV;
272
Eric Anholtc153f452007-09-03 12:06:45 +1000273 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000275 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 case I915_CLEANUP_DMA:
278 retcode = i915_dma_cleanup(dev);
279 break;
280 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100281 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000284 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
286 }
287
288 return retcode;
289}
290
291/* Implement basically the same security restrictions as hardware does
292 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
293 *
294 * Most of the calculations below involve calculating the size of a
295 * particular instruction. It's important to get the size right as
296 * that tells us where the next instruction to check is. Any illegal
297 * instruction detected will be given a size of zero, which is a
298 * signal to abort the rest of the buffer.
299 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100300static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 switch (((cmd >> 29) & 0x7)) {
303 case 0x0:
304 switch ((cmd >> 23) & 0x3f) {
305 case 0x0:
306 return 1; /* MI_NOOP */
307 case 0x4:
308 return 1; /* MI_FLUSH */
309 default:
310 return 0; /* disallow everything else */
311 }
312 break;
313 case 0x1:
314 return 0; /* reserved */
315 case 0x2:
316 return (cmd & 0xff) + 2; /* 2d commands */
317 case 0x3:
318 if (((cmd >> 24) & 0x1f) <= 0x18)
319 return 1;
320
321 switch ((cmd >> 24) & 0x1f) {
322 case 0x1c:
323 return 1;
324 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 case 0x3:
327 return (cmd & 0x1f) + 2;
328 case 0x4:
329 return (cmd & 0xf) + 2;
330 default:
331 return (cmd & 0xffff) + 2;
332 }
333 case 0x1e:
334 if (cmd & (1 << 23))
335 return (cmd & 0xffff) + 1;
336 else
337 return 1;
338 case 0x1f:
339 if ((cmd & (1 << 23)) == 0) /* inline vertices */
340 return (cmd & 0x1ffff) + 2;
341 else if (cmd & (1 << 17)) /* indirect random */
342 if ((cmd & 0xffff) == 0)
343 return 0; /* unknown length, too hard */
344 else
345 return (((cmd & 0xffff) + 1) / 2) + 1;
346 else
347 return 2; /* indirect sequential */
348 default:
349 return 0;
350 }
351 default:
352 return 0;
353 }
354
355 return 0;
356}
357
Eric Anholt201361a2009-03-11 12:30:04 -0700358static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100361 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000363 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000364 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100367 int sz = validate_cmd(buffer[i]);
368 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000369 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100370 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100373 ret = BEGIN_LP_RING((dwords+1)&~1);
374 if (ret)
375 return ret;
376
377 for (i = 0; i < dwords; i++)
378 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100379 if (dwords & 1)
380 OUT_RING(0);
381
382 ADVANCE_LP_RING();
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
385}
386
Eric Anholt673a3942008-07-30 12:06:12 -0700387int
388i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000389 struct drm_clip_rect *box,
390 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100392 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100393 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000395 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
396 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000398 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100402 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100403 ret = BEGIN_LP_RING(4);
404 if (ret)
405 return ret;
406
Alan Hourihanec29b6692006-08-12 16:29:24 +1000407 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000408 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
409 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000410 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000411 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100412 ret = BEGIN_LP_RING(6);
413 if (ret)
414 return ret;
415
Alan Hourihanec29b6692006-08-12 16:29:24 +1000416 OUT_RING(GFX_OP_DRAWRECT_INFO);
417 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000418 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
419 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000420 OUT_RING(DR4);
421 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000422 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100423 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 return 0;
426}
427
Alan Hourihanec29b6692006-08-12 16:29:24 +1000428/* XXX: Emitting the counter should really be moved to part of the IRQ
429 * emit. For now, do it in both places:
430 */
431
Dave Airlie84b1fd12007-07-11 15:53:27 +1000432static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100433{
434 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000435 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100436
Daniel Vetter231f42a2012-11-02 19:55:05 +0100437 dev_priv->dri1.counter++;
438 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
439 dev_priv->dri1.counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000440 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100441 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Dave Airliede227f52006-01-25 15:31:43 +1100442
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100443 if (BEGIN_LP_RING(4) == 0) {
444 OUT_RING(MI_STORE_DWORD_INDEX);
445 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100446 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100447 OUT_RING(0);
448 ADVANCE_LP_RING();
449 }
Dave Airliede227f52006-01-25 15:31:43 +1100450}
451
Dave Airlie84b1fd12007-07-11 15:53:27 +1000452static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700453 drm_i915_cmdbuffer_t *cmd,
454 struct drm_clip_rect *cliprects,
455 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 int nbox = cmd->num_cliprects;
458 int i = 0, count, ret;
459
460 if (cmd->sz & 0x3) {
461 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000462 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
465 i915_kernel_lost_context(dev);
466
467 count = nbox ? nbox : 1;
468
469 for (i = 0; i < count; i++) {
470 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000471 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 cmd->DR1, cmd->DR4);
473 if (ret)
474 return ret;
475 }
476
Eric Anholt201361a2009-03-11 12:30:04 -0700477 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (ret)
479 return ret;
480 }
481
Dave Airliede227f52006-01-25 15:31:43 +1100482 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484}
485
Dave Airlie84b1fd12007-07-11 15:53:27 +1000486static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700487 drm_i915_batchbuffer_t * batch,
488 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100490 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100492 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if ((batch->start | batch->used) & 0x7) {
495 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
499 i915_kernel_lost_context(dev);
500
501 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 for (i = 0; i < count; i++) {
503 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000504 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100505 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (ret)
507 return ret;
508 }
509
Keith Packard0790d5e2008-07-30 12:28:47 -0700510 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100511 ret = BEGIN_LP_RING(2);
512 if (ret)
513 return ret;
514
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100515 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000516 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
517 OUT_RING(batch->start);
518 } else {
519 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
520 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100523 ret = BEGIN_LP_RING(4);
524 if (ret)
525 return ret;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 OUT_RING(MI_BATCH_BUFFER);
528 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
529 OUT_RING(batch->start + batch->used - 4);
530 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100532 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
Zou Nan hai1cafd342010-06-25 13:40:24 +0800535
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100536 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100537 if (BEGIN_LP_RING(2) == 0) {
538 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
539 OUT_RING(MI_NOOP);
540 ADVANCE_LP_RING();
541 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100544 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
546}
547
Dave Airlieaf6061a2008-05-07 12:15:39 +1000548static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000551 struct drm_i915_master_private *master_priv =
552 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100553 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Dave Airlie7c1c2872008-11-28 14:22:24 +1000555 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400556 return -EINVAL;
557
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800558 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800559 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200560 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800561 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Dave Airlieaf6061a2008-05-07 12:15:39 +1000563 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100565 ret = BEGIN_LP_RING(10);
566 if (ret)
567 return ret;
568
Jesse Barnes585fb112008-07-29 11:54:06 -0700569 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000570 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Dave Airlieaf6061a2008-05-07 12:15:39 +1000572 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
573 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200574 if (dev_priv->dri1.current_page == 0) {
575 OUT_RING(dev_priv->dri1.back_offset);
576 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000577 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200578 OUT_RING(dev_priv->dri1.front_offset);
579 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000580 }
581 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000582
Dave Airlieaf6061a2008-05-07 12:15:39 +1000583 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
584 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100585
Dave Airlieaf6061a2008-05-07 12:15:39 +1000586 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000587
Daniel Vetter231f42a2012-11-02 19:55:05 +0100588 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000589
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100590 if (BEGIN_LP_RING(4) == 0) {
591 OUT_RING(MI_STORE_DWORD_INDEX);
592 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100593 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100594 OUT_RING(0);
595 ADVANCE_LP_RING();
596 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000597
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200598 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000602static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 i915_kernel_lost_context(dev);
Chris Wilson3e960502012-11-27 16:22:54 +0000605 return intel_ring_idle(LP_RING(dev->dev_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Eric Anholtc153f452007-09-03 12:06:45 +1000608static int i915_flush_ioctl(struct drm_device *dev, void *data,
609 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Eric Anholt546b0972008-09-01 16:45:29 -0700611 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200613 if (drm_core_check_feature(dev, DRIVER_MODESET))
614 return -ENODEV;
615
Eric Anholt546b0972008-09-01 16:45:29 -0700616 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
617
618 mutex_lock(&dev->struct_mutex);
619 ret = i915_quiescent(dev);
620 mutex_unlock(&dev->struct_mutex);
621
622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Eric Anholtc153f452007-09-03 12:06:45 +1000625static int i915_batchbuffer(struct drm_device *dev, void *data,
626 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000629 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000631 master_priv->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 Vetter87813422012-05-02 11:49:32 +0200639 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000641 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800644 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800645 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Eric Anholt546b0972008-09-01 16:45:29 -0700647 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Eric Anholt201361a2009-03-11 12:30:04 -0700649 if (batch->num_cliprects < 0)
650 return -EINVAL;
651
652 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700653 cliprects = kcalloc(batch->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200654 sizeof(*cliprects),
Eric Anholt9a298b22009-03-24 12:23:04 -0700655 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700656 if (cliprects == NULL)
657 return -ENOMEM;
658
659 ret = copy_from_user(cliprects, batch->cliprects,
660 batch->num_cliprects *
661 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200662 if (ret != 0) {
663 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700664 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200665 }
Eric Anholt201361a2009-03-11 12:30:04 -0700666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Eric Anholt546b0972008-09-01 16:45:29 -0700668 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700669 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700670 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400672 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000673 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700674
675fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700676 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return ret;
679}
680
Eric Anholtc153f452007-09-03 12:06:45 +1000681static int i915_cmdbuffer(struct drm_device *dev, void *data,
682 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000685 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000687 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000688 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700689 struct drm_clip_rect *cliprects = NULL;
690 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int ret;
692
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800693 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800694 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200696 if (drm_core_check_feature(dev, DRIVER_MODESET))
697 return -ENODEV;
698
Eric Anholt546b0972008-09-01 16:45:29 -0700699 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Eric Anholt201361a2009-03-11 12:30:04 -0700701 if (cmdbuf->num_cliprects < 0)
702 return -EINVAL;
703
Eric Anholt9a298b22009-03-24 12:23:04 -0700704 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700705 if (batch_data == NULL)
706 return -ENOMEM;
707
708 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200709 if (ret != 0) {
710 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700711 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200712 }
Eric Anholt201361a2009-03-11 12:30:04 -0700713
714 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700715 cliprects = kcalloc(cmdbuf->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200716 sizeof(*cliprects), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000717 if (cliprects == NULL) {
718 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700719 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000720 }
Eric Anholt201361a2009-03-11 12:30:04 -0700721
722 ret = copy_from_user(cliprects, cmdbuf->cliprects,
723 cmdbuf->num_cliprects *
724 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200725 if (ret != 0) {
726 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700727 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Eric Anholt546b0972008-09-01 16:45:29 -0700731 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700732 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700733 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (ret) {
735 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000736 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400739 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000740 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700741
Eric Anholt201361a2009-03-11 12:30:04 -0700742fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700743 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000744fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700745 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700746
747 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Daniel Vetter94888672012-04-26 23:28:08 +0200750static int i915_emit_irq(struct drm_device * dev)
751{
752 drm_i915_private_t *dev_priv = dev->dev_private;
753 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
754
755 i915_kernel_lost_context(dev);
756
757 DRM_DEBUG_DRIVER("\n");
758
Daniel Vetter231f42a2012-11-02 19:55:05 +0100759 dev_priv->dri1.counter++;
760 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
761 dev_priv->dri1.counter = 1;
Daniel Vetter94888672012-04-26 23:28:08 +0200762 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100763 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200764
765 if (BEGIN_LP_RING(4) == 0) {
766 OUT_RING(MI_STORE_DWORD_INDEX);
767 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100768 OUT_RING(dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200769 OUT_RING(MI_USER_INTERRUPT);
770 ADVANCE_LP_RING();
771 }
772
Daniel Vetter231f42a2012-11-02 19:55:05 +0100773 return dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200774}
775
776static int i915_wait_irq(struct drm_device * dev, int irq_nr)
777{
778 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
779 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
780 int ret = 0;
781 struct intel_ring_buffer *ring = LP_RING(dev_priv);
782
783 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
784 READ_BREADCRUMB(dev_priv));
785
786 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
787 if (master_priv->sarea_priv)
788 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
789 return 0;
790 }
791
792 if (master_priv->sarea_priv)
793 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
794
795 if (ring->irq_get(ring)) {
Daniel Vetterbfd83032013-12-11 11:34:41 +0100796 DRM_WAIT_ON(ret, ring->irq_queue, 3 * HZ,
Daniel Vetter94888672012-04-26 23:28:08 +0200797 READ_BREADCRUMB(dev_priv) >= irq_nr);
798 ring->irq_put(ring);
799 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
800 ret = -EBUSY;
801
802 if (ret == -EBUSY) {
803 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Daniel Vetter231f42a2012-11-02 19:55:05 +0100804 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200805 }
806
807 return ret;
808}
809
810/* Needs the lock as it touches the ring.
811 */
812static int i915_irq_emit(struct drm_device *dev, void *data,
813 struct drm_file *file_priv)
814{
815 drm_i915_private_t *dev_priv = dev->dev_private;
816 drm_i915_irq_emit_t *emit = data;
817 int result;
818
819 if (drm_core_check_feature(dev, DRIVER_MODESET))
820 return -ENODEV;
821
822 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
823 DRM_ERROR("called with no initialization\n");
824 return -EINVAL;
825 }
826
827 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
828
829 mutex_lock(&dev->struct_mutex);
830 result = i915_emit_irq(dev);
831 mutex_unlock(&dev->struct_mutex);
832
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100833 if (copy_to_user(emit->irq_seq, &result, sizeof(int))) {
Daniel Vetter94888672012-04-26 23:28:08 +0200834 DRM_ERROR("copy_to_user\n");
835 return -EFAULT;
836 }
837
838 return 0;
839}
840
841/* Doesn't need the hardware lock.
842 */
843static int i915_irq_wait(struct drm_device *dev, void *data,
844 struct drm_file *file_priv)
845{
846 drm_i915_private_t *dev_priv = dev->dev_private;
847 drm_i915_irq_wait_t *irqwait = data;
848
849 if (drm_core_check_feature(dev, DRIVER_MODESET))
850 return -ENODEV;
851
852 if (!dev_priv) {
853 DRM_ERROR("called with no initialization\n");
854 return -EINVAL;
855 }
856
857 return i915_wait_irq(dev, irqwait->irq_seq);
858}
859
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200860static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
861 struct drm_file *file_priv)
862{
863 drm_i915_private_t *dev_priv = dev->dev_private;
864 drm_i915_vblank_pipe_t *pipe = data;
865
866 if (drm_core_check_feature(dev, DRIVER_MODESET))
867 return -ENODEV;
868
869 if (!dev_priv) {
870 DRM_ERROR("called with no initialization\n");
871 return -EINVAL;
872 }
873
874 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
875
876 return 0;
877}
878
879/**
880 * Schedule buffer swap at given vertical blank.
881 */
882static int i915_vblank_swap(struct drm_device *dev, void *data,
883 struct drm_file *file_priv)
884{
885 /* The delayed swap mechanism was fundamentally racy, and has been
886 * removed. The model was that the client requested a delayed flip/swap
887 * from the kernel, then waited for vblank before continuing to perform
888 * rendering. The problem was that the kernel might wake the client
889 * up before it dispatched the vblank swap (since the lock has to be
890 * held while touching the ringbuffer), in which case the client would
891 * clear and start the next frame before the swap occurred, and
892 * flicker would occur in addition to likely missing the vblank.
893 *
894 * In the absence of this ioctl, userland falls back to a correct path
895 * of waiting for a vblank, then dispatching the swap on its own.
896 * Context switching to userland and back is plenty fast enough for
897 * meeting the requirements of vblank swapping.
898 */
899 return -EINVAL;
900}
901
Eric Anholtc153f452007-09-03 12:06:45 +1000902static int i915_flip_bufs(struct drm_device *dev, void *data,
903 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Eric Anholt546b0972008-09-01 16:45:29 -0700905 int ret;
906
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200907 if (drm_core_check_feature(dev, DRIVER_MODESET))
908 return -ENODEV;
909
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800910 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Eric Anholt546b0972008-09-01 16:45:29 -0700912 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Eric Anholt546b0972008-09-01 16:45:29 -0700914 mutex_lock(&dev->struct_mutex);
915 ret = i915_dispatch_flip(dev);
916 mutex_unlock(&dev->struct_mutex);
917
918 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Eric Anholtc153f452007-09-03 12:06:45 +1000921static int i915_getparam(struct drm_device *dev, void *data,
922 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000925 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int value;
927
928 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000929 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000930 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Eric Anholtc153f452007-09-03 12:06:45 +1000933 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700935 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 break;
937 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200938 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100940 case I915_PARAM_LAST_DISPATCH:
941 value = READ_BREADCRUMB(dev_priv);
942 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400943 case I915_PARAM_CHIPSET_ID:
Ville Syrjäläffbab09b2013-10-04 14:53:40 +0300944 value = dev->pdev->device;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400945 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700946 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200947 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700948 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800949 case I915_PARAM_NUM_FENCES_AVAIL:
950 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
951 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200952 case I915_PARAM_HAS_OVERLAY:
953 value = dev_priv->overlay ? 1 : 0;
954 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800955 case I915_PARAM_HAS_PAGEFLIPPING:
956 value = 1;
957 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500958 case I915_PARAM_HAS_EXECBUF2:
959 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200960 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500961 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800962 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100963 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800964 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100965 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100966 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100967 break;
Xiang, Haihaoa1f2cc72013-05-28 19:22:34 -0700968 case I915_PARAM_HAS_VEBOX:
969 value = intel_ring_initialized(&dev_priv->ring[VECS]);
970 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100971 case I915_PARAM_HAS_RELAXED_FENCING:
972 value = 1;
973 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100974 case I915_PARAM_HAS_COHERENT_RINGS:
975 value = 1;
976 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000977 case I915_PARAM_HAS_EXEC_CONSTANTS:
978 value = INTEL_INFO(dev)->gen >= 4;
979 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000980 case I915_PARAM_HAS_RELAXED_DELTA:
981 value = 1;
982 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800983 case I915_PARAM_HAS_GEN7_SOL_RESET:
984 value = 1;
985 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200986 case I915_PARAM_HAS_LLC:
987 value = HAS_LLC(dev);
988 break;
Chris Wilson651d7942013-08-08 14:41:10 +0100989 case I915_PARAM_HAS_WT:
990 value = HAS_WT(dev);
991 break;
Daniel Vetter777ee962012-02-15 23:50:25 +0100992 case I915_PARAM_HAS_ALIASING_PPGTT:
993 value = dev_priv->mm.aliasing_ppgtt ? 1 : 0;
994 break;
Ben Widawsky172cf152012-06-05 15:24:25 -0700995 case I915_PARAM_HAS_WAIT_TIMEOUT:
996 value = 1;
997 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +0100998 case I915_PARAM_HAS_SEMAPHORES:
999 value = i915_semaphore_is_enabled(dev);
1000 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +10001001 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
1002 value = 1;
1003 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001004 case I915_PARAM_HAS_SECURE_BATCHES:
1005 value = capable(CAP_SYS_ADMIN);
1006 break;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001007 case I915_PARAM_HAS_PINNED_BATCHES:
1008 value = 1;
1009 break;
Daniel Vettered5982e2013-01-17 22:23:36 +01001010 case I915_PARAM_HAS_EXEC_NO_RELOC:
1011 value = 1;
1012 break;
Chris Wilsoneef90cc2013-01-08 10:53:17 +00001013 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
1014 value = 1;
1015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 default:
Ben Widawskye29c32d2013-05-31 11:28:45 -07001017 DRM_DEBUG("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001018 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001021 if (copy_to_user(param->value, &value, sizeof(int))) {
1022 DRM_ERROR("copy_to_user failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001023 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
1026 return 0;
1027}
1028
Eric Anholtc153f452007-09-03 12:06:45 +10001029static int i915_setparam(struct drm_device *dev, void *data,
1030 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001033 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001036 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001037 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Eric Anholtc153f452007-09-03 12:06:45 +10001040 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 break;
1043 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 break;
1045 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001046 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001048 case I915_SETPARAM_NUM_USED_FENCES:
1049 if (param->value > dev_priv->num_fence_regs ||
1050 param->value < 0)
1051 return -EINVAL;
1052 /* Userspace can use first N regs */
1053 dev_priv->fence_reg_start = param->value;
1054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001056 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001057 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001058 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
1061 return 0;
1062}
1063
Eric Anholtc153f452007-09-03 12:06:45 +10001064static int i915_set_status_page(struct drm_device *dev, void *data,
1065 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001066{
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001067 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001068 drm_i915_hws_addr_t *hws = data;
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001069 struct intel_ring_buffer *ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001070
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001071 if (drm_core_check_feature(dev, DRIVER_MODESET))
1072 return -ENODEV;
1073
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001074 if (!I915_NEED_GFX_HWS(dev))
1075 return -EINVAL;
1076
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001077 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001078 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001079 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001080 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001081
Jesse Barnes79e53942008-11-07 14:24:08 -08001082 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1083 WARN(1, "tried to set status page when mode setting active\n");
1084 return 0;
1085 }
1086
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001087 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001088
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001089 ring = LP_RING(dev_priv);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001090 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001091
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001092 dev_priv->dri1.gfx_hws_cpu_addr =
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001093 ioremap_wc(dev_priv->gtt.mappable_base + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001094 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001095 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001096 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001097 DRM_ERROR("can not ioremap virtual address for"
1098 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001099 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001100 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001101
1102 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001103 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001104
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001105 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001106 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001107 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001108 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001109 return 0;
1110}
1111
Dave Airlieec2a4c32009-08-04 11:43:41 +10001112static int i915_get_bridge_dev(struct drm_device *dev)
1113{
1114 struct drm_i915_private *dev_priv = dev->dev_private;
1115
Akshay Joshi0206e352011-08-16 15:34:10 -04001116 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001117 if (!dev_priv->bridge_dev) {
1118 DRM_ERROR("bridge device not found\n");
1119 return -1;
1120 }
1121 return 0;
1122}
1123
Zhenyu Wangc48044112009-12-17 14:48:43 +08001124#define MCHBAR_I915 0x44
1125#define MCHBAR_I965 0x48
1126#define MCHBAR_SIZE (4*4096)
1127
1128#define DEVEN_REG 0x54
1129#define DEVEN_MCHBAR_EN (1 << 28)
1130
1131/* Allocate space for the MCH regs if needed, return nonzero on error */
1132static int
1133intel_alloc_mchbar_resource(struct drm_device *dev)
1134{
1135 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001136 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001137 u32 temp_lo, temp_hi = 0;
1138 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001139 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001140
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001141 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001142 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1143 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1144 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1145
1146 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1147#ifdef CONFIG_PNP
1148 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001149 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1150 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001151#endif
1152
1153 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001154 dev_priv->mch_res.name = "i915 MCHBAR";
1155 dev_priv->mch_res.flags = IORESOURCE_MEM;
1156 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1157 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001158 MCHBAR_SIZE, MCHBAR_SIZE,
1159 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001160 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001161 dev_priv->bridge_dev);
1162 if (ret) {
1163 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1164 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001165 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001166 }
1167
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001168 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001169 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1170 upper_32_bits(dev_priv->mch_res.start));
1171
1172 pci_write_config_dword(dev_priv->bridge_dev, reg,
1173 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001174 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001175}
1176
1177/* Setup MCHBAR if possible, return true if we should disable it again */
1178static void
1179intel_setup_mchbar(struct drm_device *dev)
1180{
1181 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001182 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001183 u32 temp;
1184 bool enabled;
1185
1186 dev_priv->mchbar_need_disable = false;
1187
1188 if (IS_I915G(dev) || IS_I915GM(dev)) {
1189 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1190 enabled = !!(temp & DEVEN_MCHBAR_EN);
1191 } else {
1192 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1193 enabled = temp & 1;
1194 }
1195
1196 /* If it's already enabled, don't have to do anything */
1197 if (enabled)
1198 return;
1199
1200 if (intel_alloc_mchbar_resource(dev))
1201 return;
1202
1203 dev_priv->mchbar_need_disable = true;
1204
1205 /* Space is allocated or reserved, so enable it. */
1206 if (IS_I915G(dev) || IS_I915GM(dev)) {
1207 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1208 temp | DEVEN_MCHBAR_EN);
1209 } else {
1210 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1211 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1212 }
1213}
1214
1215static void
1216intel_teardown_mchbar(struct drm_device *dev)
1217{
1218 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001219 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001220 u32 temp;
1221
1222 if (dev_priv->mchbar_need_disable) {
1223 if (IS_I915G(dev) || IS_I915GM(dev)) {
1224 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1225 temp &= ~DEVEN_MCHBAR_EN;
1226 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1227 } else {
1228 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1229 temp &= ~1;
1230 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1231 }
1232 }
1233
1234 if (dev_priv->mch_res.start)
1235 release_resource(&dev_priv->mch_res);
1236}
1237
Dave Airlie28d52042009-09-21 14:33:58 +10001238/* true = enable decode, false = disable decoder */
1239static unsigned int i915_vga_set_decode(void *cookie, bool state)
1240{
1241 struct drm_device *dev = cookie;
1242
1243 intel_modeset_vga_set_state(dev, state);
1244 if (state)
1245 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1246 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1247 else
1248 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1249}
1250
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001251static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1252{
1253 struct drm_device *dev = pci_get_drvdata(pdev);
1254 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1255 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001256 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001257 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001258 /* i915 resume handler doesn't set to D0 */
1259 pci_set_power_state(dev->pdev, PCI_D0);
1260 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001261 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001262 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001263 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001264 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001265 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001266 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001267 }
1268}
1269
1270static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1271{
1272 struct drm_device *dev = pci_get_drvdata(pdev);
1273 bool can_switch;
1274
1275 spin_lock(&dev->count_lock);
1276 can_switch = (dev->open_count == 0);
1277 spin_unlock(&dev->count_lock);
1278 return can_switch;
1279}
1280
Takashi Iwai26ec6852012-05-11 07:51:17 +02001281static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1282 .set_gpu_state = i915_switcheroo_set_state,
1283 .reprobe = NULL,
1284 .can_switch = i915_switcheroo_can_switch,
1285};
1286
Chris Wilson2c7111d2011-03-29 10:40:27 +01001287static int i915_load_modeset_init(struct drm_device *dev)
1288{
1289 struct drm_i915_private *dev_priv = dev->dev_private;
1290 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001291
Bryan Freed6d139a82010-10-14 09:14:51 +01001292 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001293 if (ret)
1294 DRM_INFO("failed to find VBIOS tables\n");
1295
Chris Wilson934f9922011-01-20 13:09:12 +00001296 /* If we have > 1 VGA cards, then we need to arbitrate access
1297 * to the common VGA resources.
1298 *
1299 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1300 * then we do not take part in VGA arbitration and the
1301 * vga_client_register() fails with -ENODEV.
1302 */
Dave Airlieebff5fa92013-10-11 15:12:04 +10001303 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1304 if (ret && ret != -ENODEV)
1305 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001306
Jesse Barnes723bfd72010-10-07 16:01:13 -07001307 intel_register_dsm_handler();
1308
Dave Airlie0d697042012-09-10 12:28:36 +10001309 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001310 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001311 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001312
Chris Wilson9797fbf2012-04-24 15:47:39 +01001313 /* Initialise stolen first so that we may reserve preallocated
1314 * objects for the BIOS to KMS transition.
1315 */
1316 ret = i915_gem_init_stolen(dev);
1317 if (ret)
1318 goto cleanup_vga_switcheroo;
1319
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001320 ret = drm_irq_install(dev);
1321 if (ret)
1322 goto cleanup_gem_stolen;
1323
Imre Deakddb642f2013-10-28 17:20:35 +02001324 intel_power_domains_init_hw(dev);
Ville Syrjäläa1485322013-09-16 17:38:34 +03001325
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001326 /* Important: The output setup functions called by modeset_init need
1327 * working irqs for e.g. gmbus and dp aux transfers. */
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001328 intel_modeset_init(dev);
1329
Chris Wilson1070a422012-04-24 15:47:41 +01001330 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001331 if (ret)
Ville Syrjäläa1485322013-09-16 17:38:34 +03001332 goto cleanup_power;
Chris Wilson2c7111d2011-03-29 10:40:27 +01001333
Jesse Barnes073f34d2012-11-02 11:13:59 -07001334 INIT_WORK(&dev_priv->console_resume_work, intel_console_resume);
1335
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001336 intel_modeset_gem_init(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001337
Jesse Barnes79e53942008-11-07 14:24:08 -08001338 /* Always safe in the mode setting case. */
1339 /* FIXME: do pre/post-mode set stuff in core KMS code */
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001340 dev->vblank_disable_allowed = true;
Ville Syrjäläce352552013-09-20 10:14:23 +03001341 if (INTEL_INFO(dev)->num_pipes == 0) {
1342 intel_display_power_put(dev, POWER_DOMAIN_VGA);
Ben Widawskye3c74752013-04-05 13:12:39 -07001343 return 0;
Ville Syrjäläce352552013-09-20 10:14:23 +03001344 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001345
Chris Wilson5a793952010-06-06 10:50:03 +01001346 ret = intel_fbdev_init(dev);
1347 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001348 goto cleanup_gem;
1349
1350 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter20afbda2012-12-11 14:05:07 +01001351 intel_hpd_init(dev);
1352
1353 /*
1354 * Some ports require correctly set-up hpd registers for detection to
1355 * work properly (leading to ghost connected connector status), e.g. VGA
1356 * on gm45. Hence we can only set up the initial fbdev config after hpd
1357 * irqs are fully enabled. Now we should scan for the initial config
1358 * only once hotplug handling is enabled, but due to screwed-up locking
1359 * around kms/fbdev init we can't protect the fdbev initial config
1360 * scanning against hotplug events. Hence do this first and ignore the
1361 * tiny window where we will loose hotplug notifactions.
1362 */
1363 intel_fbdev_initial_config(dev);
1364
1365 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001366 dev_priv->enable_hotplug_processing = true;
Chris Wilson5a793952010-06-06 10:50:03 +01001367
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001368 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001369
Jesse Barnes79e53942008-11-07 14:24:08 -08001370 return 0;
1371
Chris Wilson2c7111d2011-03-29 10:40:27 +01001372cleanup_gem:
1373 mutex_lock(&dev->struct_mutex);
1374 i915_gem_cleanup_ringbuffer(dev);
Ben Widawsky55d23282013-05-25 12:26:39 -07001375 i915_gem_context_fini(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001376 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001377 i915_gem_cleanup_aliasing_ppgtt(dev);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001378 drm_mm_takedown(&dev_priv->gtt.base.mm);
Ville Syrjäläa1485322013-09-16 17:38:34 +03001379cleanup_power:
1380 intel_display_power_put(dev, POWER_DOMAIN_VGA);
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001381 drm_irq_uninstall(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001382cleanup_gem_stolen:
1383 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001384cleanup_vga_switcheroo:
1385 vga_switcheroo_unregister_client(dev->pdev);
1386cleanup_vga_client:
1387 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001388out:
1389 return ret;
1390}
1391
Dave Airlie7c1c2872008-11-28 14:22:24 +10001392int i915_master_create(struct drm_device *dev, struct drm_master *master)
1393{
1394 struct drm_i915_master_private *master_priv;
1395
Eric Anholt9a298b22009-03-24 12:23:04 -07001396 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001397 if (!master_priv)
1398 return -ENOMEM;
1399
1400 master->driver_priv = master_priv;
1401 return 0;
1402}
1403
1404void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1405{
1406 struct drm_i915_master_private *master_priv = master->driver_priv;
1407
1408 if (!master_priv)
1409 return;
1410
Eric Anholt9a298b22009-03-24 12:23:04 -07001411 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001412
1413 master->driver_priv = NULL;
1414}
1415
Daniel Vetter243eaf32013-12-17 10:00:54 +01001416#if IS_ENABLED(CONFIG_FB)
Daniel Vettere1887192012-06-12 11:28:17 +02001417static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1418{
1419 struct apertures_struct *ap;
1420 struct pci_dev *pdev = dev_priv->dev->pdev;
1421 bool primary;
1422
1423 ap = alloc_apertures(1);
1424 if (!ap)
1425 return;
1426
Ben Widawskydabb7a92013-01-17 12:45:16 -08001427 ap->ranges[0].base = dev_priv->gtt.mappable_base;
Ben Widawskyf64e2922013-05-25 12:26:36 -07001428 ap->ranges[0].size = dev_priv->gtt.mappable_end;
Ben Widawsky93d18792013-01-17 12:45:17 -08001429
Daniel Vettere1887192012-06-12 11:28:17 +02001430 primary =
1431 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1432
1433 remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
1434
1435 kfree(ap);
1436}
Daniel Vetter4520f532013-10-09 09:18:51 +02001437#else
1438static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1439{
1440}
1441#endif
Daniel Vettere1887192012-06-12 11:28:17 +02001442
Daniel Vetterc96ea642012-08-08 22:01:51 +02001443static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1444{
1445 const struct intel_device_info *info = dev_priv->info;
1446
Damien Lespiaue2a58002013-04-23 16:38:34 +01001447#define PRINT_S(name) "%s"
1448#define SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001449#define PRINT_FLAG(name) info->name ? #name "," : ""
1450#define SEP_COMMA ,
Daniel Vetterc96ea642012-08-08 22:01:51 +02001451 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
Damien Lespiaue2a58002013-04-23 16:38:34 +01001452 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
Daniel Vetterc96ea642012-08-08 22:01:51 +02001453 info->gen,
1454 dev_priv->dev->pdev->device,
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001455 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
Damien Lespiaue2a58002013-04-23 16:38:34 +01001456#undef PRINT_S
1457#undef SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001458#undef PRINT_FLAG
1459#undef SEP_COMMA
Daniel Vetterc96ea642012-08-08 22:01:51 +02001460}
1461
Eric Anholt63ee41d2010-12-20 18:40:06 -08001462/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001463 * i915_driver_load - setup chip and create an initial config
1464 * @dev: DRM device
1465 * @flags: startup flags
1466 *
1467 * The driver load routine has to do several things:
1468 * - drive output discovery via intel_modeset_init()
1469 * - initialize the memory manager
1470 * - allocate initial config memory
1471 * - setup the DRM framebuffer with the allocated memory
1472 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001473int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001474{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001475 struct drm_i915_private *dev_priv;
Daniel Vetter26394d92012-03-26 21:33:18 +02001476 struct intel_device_info *info;
Chris Wilson934d6082012-09-14 11:57:46 +01001477 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001478 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001479
Daniel Vetter26394d92012-03-26 21:33:18 +02001480 info = (struct intel_device_info *) flags;
1481
1482 /* Refuse to load on gen6+ without kms enabled. */
Jani Nikulae147acc2013-10-10 15:25:37 +03001483 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET)) {
1484 DRM_INFO("Your hardware requires kernel modesetting (KMS)\n");
1485 DRM_INFO("See CONFIG_DRM_I915_KMS, nomodeset, and i915.modeset parameters\n");
Daniel Vetter26394d92012-03-26 21:33:18 +02001486 return -ENODEV;
Jani Nikulae147acc2013-10-10 15:25:37 +03001487 }
Daniel Vetter26394d92012-03-26 21:33:18 +02001488
Daniel Vetter24986ee2013-12-11 11:34:33 +01001489 /* UMS needs agp support. */
1490 if (!drm_core_check_feature(dev, DRIVER_MODESET) && !dev->agp)
1491 return -EINVAL;
1492
Daniel Vetterb14c5672013-09-19 12:18:32 +02001493 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001494 if (dev_priv == NULL)
1495 return -ENOMEM;
1496
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001497 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001498 dev_priv->dev = dev;
Daniel Vetter26394d92012-03-26 21:33:18 +02001499 dev_priv->info = info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001500
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001501 spin_lock_init(&dev_priv->irq_lock);
1502 spin_lock_init(&dev_priv->gpu_error.lock);
Jani Nikula58c68772013-11-08 16:48:54 +02001503 spin_lock_init(&dev_priv->backlight_lock);
Chris Wilson907b28c2013-07-19 20:36:52 +01001504 spin_lock_init(&dev_priv->uncore.lock);
Daniel Vetterc20e8352013-07-24 22:40:23 +02001505 spin_lock_init(&dev_priv->mm.object_stat_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001506 mutex_init(&dev_priv->dpio_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001507 mutex_init(&dev_priv->modeset_restore_lock);
1508
Daniel Vetterf742a552013-12-06 10:17:53 +01001509 intel_pm_setup(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03001510
Damien Lespiau07144422013-10-15 18:55:40 +01001511 intel_display_crc_init(dev);
1512
Daniel Vetterc96ea642012-08-08 22:01:51 +02001513 i915_dump_device_info(dev_priv);
1514
Paulo Zanonied1c9e22013-08-12 14:34:08 -03001515 /* Not all pre-production machines fall into this category, only the
1516 * very first ones. Almost everything should work, except for maybe
1517 * suspend/resume. And we don't implement workarounds that affect only
1518 * pre-production machines. */
1519 if (IS_HSW_EARLY_SDV(dev))
1520 DRM_INFO("This is an early pre-production Haswell machine. "
1521 "It may not be fully functional.\n");
1522
Dave Airlieec2a4c32009-08-04 11:43:41 +10001523 if (i915_get_bridge_dev(dev)) {
1524 ret = -EIO;
1525 goto free_priv;
1526 }
1527
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001528 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1529 /* Before gen4, the registers and the GTT are behind different BARs.
1530 * However, from gen4 onwards, the registers and the GTT are shared
1531 * in the same BAR, so we want to restrict this ioremap from
1532 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1533 * the register BAR remains the same size for all the earlier
1534 * generations up to Ironlake.
1535 */
1536 if (info->gen < 5)
1537 mmio_size = 512*1024;
1538 else
1539 mmio_size = 2*1024*1024;
1540
1541 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
1542 if (!dev_priv->regs) {
1543 DRM_ERROR("failed to map registers\n");
1544 ret = -EIO;
1545 goto put_bridge;
1546 }
1547
Chris Wilson907b28c2013-07-19 20:36:52 +01001548 intel_uncore_early_sanitize(dev);
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001549
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001550 /* This must be called before any calls to HAS_PCH_* */
1551 intel_detect_pch(dev);
1552
1553 intel_uncore_init(dev);
1554
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001555 ret = i915_gem_gtt_init(dev);
1556 if (ret)
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001557 goto out_regs;
Daniel Vettere1887192012-06-12 11:28:17 +02001558
Chris Wilson16233922012-10-26 12:06:41 +01001559 if (drm_core_check_feature(dev, DRIVER_MODESET))
1560 i915_kick_out_firmware_fb(dev_priv);
Daniel Vettere1887192012-06-12 11:28:17 +02001561
Dave Airlie466e69b2011-12-19 11:15:29 +00001562 pci_set_master(dev->pdev);
1563
Daniel Vetter9f82d232010-08-30 21:25:23 +02001564 /* overlay on gen2 is broken and can't address above 1G */
1565 if (IS_GEN2(dev))
1566 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1567
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001568 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1569 * using 32bit addressing, overwriting memory if HWS is located
1570 * above 4GB.
1571 *
1572 * The documentation also mentions an issue with undefined
1573 * behaviour if any general state is accessed within a page above 4GB,
1574 * which also needs to be handled carefully.
1575 */
1576 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1577 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1578
Ben Widawsky93d18792013-01-17 12:45:17 -08001579 aperture_size = dev_priv->gtt.mappable_end;
Chris Wilson71e93392010-10-27 18:46:52 +01001580
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001581 dev_priv->gtt.mappable =
1582 io_mapping_create_wc(dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001583 aperture_size);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001584 if (dev_priv->gtt.mappable == NULL) {
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001585 ret = -EIO;
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001586 goto out_gtt;
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001587 }
1588
Ben Widawsky911bdf02013-06-27 16:30:23 -07001589 dev_priv->gtt.mtrr = arch_phys_wc_add(dev_priv->gtt.mappable_base,
1590 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001591
Chris Wilsone642abb2010-09-09 12:46:34 +01001592 /* The i915 workqueue is primarily used for batched retirement of
1593 * requests (and thus managing bo) once the task has been completed
1594 * by the GPU. i915_gem_retire_requests() is called directly when we
1595 * need high-priority retirement, such as waiting for an explicit
1596 * bo.
1597 *
1598 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001599 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001600 *
1601 * All tasks on the workqueue are expected to acquire the dev mutex
1602 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001603 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001604 */
Tejun Heo53621862012-08-22 16:40:57 -07001605 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001606 if (dev_priv->wq == NULL) {
1607 DRM_ERROR("Failed to create our workqueue.\n");
1608 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001609 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001610 }
1611
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001612 intel_irq_init(dev);
Ben Widawsky78511f22013-10-04 21:22:49 -07001613 intel_uncore_sanitize(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001614
Zhenyu Wangc48044112009-12-17 14:48:43 +08001615 /* Try to make sure MCHBAR is enabled before poking at it */
1616 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001617 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001618 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001619
Bryan Freed6d139a82010-10-14 09:14:51 +01001620 intel_setup_bios(dev);
1621
Eric Anholt673a3942008-07-30 12:06:12 -07001622 i915_gem_load(dev);
1623
Eric Anholted4cb412008-07-29 12:10:39 -07001624 /* On the 945G/GM, the chipset reports the MSI capability on the
1625 * integrated graphics even though the support isn't actually there
1626 * according to the published specs. It doesn't appear to function
1627 * correctly in testing on 945G.
1628 * This may be a side effect of MSI having been made available for PEG
1629 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001630 *
1631 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001632 * be lost or delayed, but we use them anyways to avoid
1633 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001634 */
Keith Packardb60678a2008-12-08 11:12:28 -08001635 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001636 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001637
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001638 dev_priv->num_plane = 1;
1639 if (IS_VALLEYVIEW(dev))
1640 dev_priv->num_plane = 2;
1641
Ben Widawskye3c74752013-04-05 13:12:39 -07001642 if (INTEL_INFO(dev)->num_pipes) {
1643 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1644 if (ret)
1645 goto out_gem_unload;
1646 }
Keith Packard52440212008-11-18 09:30:25 -08001647
Imre Deak1c2256d2013-11-25 17:15:34 +02001648 intel_power_domains_init(dev);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001649
Jesse Barnes79e53942008-11-07 14:24:08 -08001650 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001651 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001652 if (ret < 0) {
1653 DRM_ERROR("failed to init modeset\n");
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001654 goto out_power_well;
Jesse Barnes79e53942008-11-07 14:24:08 -08001655 }
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001656 } else {
1657 /* Start out suspended in ums mode. */
1658 dev_priv->ums.mm_suspended = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001659 }
1660
Ben Widawsky0136db582012-04-10 21:17:01 -07001661 i915_setup_sysfs(dev);
1662
Ben Widawskye3c74752013-04-05 13:12:39 -07001663 if (INTEL_INFO(dev)->num_pipes) {
1664 /* Must be done after probing outputs */
1665 intel_opregion_init(dev);
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001666 acpi_video_register();
Ben Widawskye3c74752013-04-05 13:12:39 -07001667 }
Matthew Garrett74a365b2009-03-19 21:35:39 +00001668
Daniel Vettereb48eb02012-04-26 23:28:12 +02001669 if (IS_GEN5(dev))
1670 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001671
Paulo Zanoni8a187452013-12-06 20:32:13 -02001672 intel_init_runtime_pm(dev_priv);
1673
Jesse Barnes79e53942008-11-07 14:24:08 -08001674 return 0;
1675
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001676out_power_well:
Imre Deak1c2256d2013-11-25 17:15:34 +02001677 intel_power_domains_remove(dev);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001678 drm_vblank_cleanup(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001679out_gem_unload:
Dave Chinner7dc19d52013-08-28 10:18:11 +10001680 if (dev_priv->mm.inactive_shrinker.scan_objects)
Keith Packarda7b85d22011-07-10 13:12:17 -07001681 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1682
Chris Wilson56e2ea32010-11-08 17:10:29 +00001683 if (dev->pdev->msi_enabled)
1684 pci_disable_msi(dev->pdev);
1685
1686 intel_teardown_gmbus(dev);
1687 intel_teardown_mchbar(dev);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001688 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001689out_mtrrfree:
Ben Widawsky911bdf02013-06-27 16:30:23 -07001690 arch_phys_wc_del(dev_priv->gtt.mtrr);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001691 io_mapping_free(dev_priv->gtt.mappable);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001692out_gtt:
1693 list_del(&dev_priv->gtt.base.global_link);
1694 drm_mm_takedown(&dev_priv->gtt.base.mm);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001695 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001696out_regs:
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001697 intel_uncore_fini(dev);
Chris Wilson6dda5692010-10-29 21:02:18 +01001698 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001699put_bridge:
1700 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001701free_priv:
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001702 if (dev_priv->slab)
1703 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001704 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001705 return ret;
1706}
1707
1708int i915_driver_unload(struct drm_device *dev)
1709{
1710 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001711 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001712
Chris Wilsonce58c322013-12-02 11:26:07 -02001713 ret = i915_gem_suspend(dev);
1714 if (ret) {
1715 DRM_ERROR("failed to idle hardware: %d\n", ret);
1716 return ret;
1717 }
1718
Paulo Zanoni8a187452013-12-06 20:32:13 -02001719 intel_fini_runtime_pm(dev_priv);
1720
Daniel Vettereb48eb02012-04-26 23:28:12 +02001721 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001722
Imre Deak1c2256d2013-11-25 17:15:34 +02001723 /* The i915.ko module is still not prepared to be loaded when
1724 * the power well is not enabled, so just enable it in case
1725 * we're going to unload/reload. */
1726 intel_display_set_init_power(dev, true);
1727 intel_power_domains_remove(dev);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001728
Ben Widawsky0136db582012-04-10 21:17:01 -07001729 i915_teardown_sysfs(dev);
1730
Dave Chinner7dc19d52013-08-28 10:18:11 +10001731 if (dev_priv->mm.inactive_shrinker.scan_objects)
Chris Wilson17250b72010-10-28 12:51:39 +01001732 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1733
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001734 io_mapping_free(dev_priv->gtt.mappable);
Ben Widawsky911bdf02013-06-27 16:30:23 -07001735 arch_phys_wc_del(dev_priv->gtt.mtrr);
Eric Anholtab657db12009-01-23 12:57:47 -08001736
Chris Wilson44834a62010-08-19 16:09:23 +01001737 acpi_video_unregister();
1738
Jesse Barnes79e53942008-11-07 14:24:08 -08001739 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01001740 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001741 intel_modeset_cleanup(dev);
Jesse Barnes073f34d2012-11-02 11:13:59 -07001742 cancel_work_sync(&dev_priv->console_resume_work);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001743
Zhao Yakui6363ee62009-11-24 09:48:44 +08001744 /*
1745 * free the memory space allocated for the child device
1746 * config parsed from VBT
1747 */
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03001748 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1749 kfree(dev_priv->vbt.child_dev);
1750 dev_priv->vbt.child_dev = NULL;
1751 dev_priv->vbt.child_dev_num = 0;
Zhao Yakui6363ee62009-11-24 09:48:44 +08001752 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001753
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001754 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001755 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001756 }
1757
Daniel Vettera8b48992010-08-20 21:25:11 +02001758 /* Free error state after interrupts are fully disabled. */
Daniel Vetter99584db2012-11-14 17:14:04 +01001759 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
1760 cancel_work_sync(&dev_priv->gpu_error.work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001761 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001762
Paulo Zanonic67a4702013-08-19 13:18:09 -03001763 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
1764
Eric Anholted4cb412008-07-29 12:10:39 -07001765 if (dev->pdev->msi_enabled)
1766 pci_disable_msi(dev->pdev);
1767
Chris Wilson44834a62010-08-19 16:09:23 +01001768 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001769
Jesse Barnes79e53942008-11-07 14:24:08 -08001770 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001771 /* Flush any outstanding unpin_work. */
1772 flush_workqueue(dev_priv->wq);
1773
Jesse Barnes79e53942008-11-07 14:24:08 -08001774 mutex_lock(&dev->struct_mutex);
Hugh Dickinsecbec532011-06-27 16:18:20 -07001775 i915_gem_free_all_phys_object(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001776 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001777 i915_gem_context_fini(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001778 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001779 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001780 i915_gem_cleanup_stolen(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001781
1782 if (!I915_NEED_GFX_HWS(dev))
1783 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001784 }
1785
Ben Widawskya7bbbd62013-07-16 16:50:07 -07001786 list_del(&dev_priv->gtt.base.global_link);
1787 WARN_ON(!list_empty(&dev_priv->vm_list));
Daniel Vetter701394c2010-10-10 18:54:08 +01001788
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001789 drm_vblank_cleanup(dev);
1790
Chris Wilsonf899fc62010-07-20 15:44:45 -07001791 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001792 intel_teardown_mchbar(dev);
1793
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001794 destroy_workqueue(dev_priv->wq);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001795 pm_qos_remove_request(&dev_priv->pm_qos);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001796
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001797 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
Imre Deak6640aab2013-05-22 17:47:13 +03001798
Chris Wilsonaec347a2013-08-26 13:46:09 +01001799 intel_uncore_fini(dev);
1800 if (dev_priv->regs != NULL)
1801 pci_iounmap(dev->pdev, dev_priv->regs);
1802
Chris Wilson42dcedd2012-11-15 11:32:30 +00001803 if (dev_priv->slab)
1804 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001805
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001806 pci_dev_put(dev_priv->bridge_dev);
Dave Airlie22eae942005-11-10 22:16:34 +11001807 kfree(dev->dev_private);
1808
1809 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001810}
1811
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001812int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001813{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001814 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001815
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001816 ret = i915_gem_open(dev, file);
1817 if (ret)
1818 return ret;
Ben Widawsky254f9652012-06-04 14:42:42 -07001819
Eric Anholt673a3942008-07-30 12:06:12 -07001820 return 0;
1821}
1822
Jesse Barnes79e53942008-11-07 14:24:08 -08001823/**
1824 * i915_driver_lastclose - clean up after all DRM clients have exited
1825 * @dev: DRM device
1826 *
1827 * Take care of cleaning up after all DRM clients have exited. In the
1828 * mode setting case, we want to restore the kernel's initial mode (just
1829 * in case the last client left us in a bad state).
1830 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001831 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001832 * and DMA structures, since the kernel won't be using them, and clea
1833 * up any GEM state.
1834 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001835void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001837 drm_i915_private_t *dev_priv = dev->dev_private;
1838
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001839 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1840 * goes right around and calls lastclose. Check for this and don't clean
1841 * up anything. */
1842 if (!dev_priv)
1843 return;
1844
1845 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter0632fef2013-10-08 17:44:49 +02001846 intel_fbdev_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001847 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001848 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001849 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001850
Eric Anholt673a3942008-07-30 12:06:12 -07001851 i915_gem_lastclose(dev);
1852
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001853 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Eric Anholt6c340ea2007-08-25 20:23:09 +10001856void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Chris Wilson0d1430a2013-12-04 14:52:06 +00001858 mutex_lock(&dev->struct_mutex);
Ben Widawsky254f9652012-06-04 14:42:42 -07001859 i915_gem_context_close(dev, file_priv);
Eric Anholtb9624422009-06-03 07:27:35 +00001860 i915_gem_release(dev, file_priv);
Chris Wilson0d1430a2013-12-04 14:52:06 +00001861 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001864void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001865{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001866 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001867
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001868 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001869}
1870
Rob Clarkbaa70942013-08-02 13:27:49 -04001871const struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001872 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1873 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1874 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1875 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1876 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1877 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001878 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001879 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001880 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1881 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1882 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001883 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001884 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02001885 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001886 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
1887 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1888 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1889 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1890 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001891 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001892 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1893 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 +02001894 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1895 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1896 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1897 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 +10001898 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1899 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 +02001900 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1901 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1902 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1903 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1904 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1905 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1906 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1907 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1908 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1909 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 +10001910 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 +02001911 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001912 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1913 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08001914 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1915 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 +02001916 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1917 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1918 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1919 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Mika Kuoppalab6359912013-10-30 15:44:16 +02001920 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_get_reset_stats_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airliec94f7022005-07-07 21:03:38 +10001921};
1922
1923int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001924
Daniel Vetter9021f282012-03-26 09:45:41 +02001925/*
1926 * This is really ugly: Because old userspace abused the linux agp interface to
1927 * manage the gtt, we need to claim that all intel devices are agp. For
1928 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10001929 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001930int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001931{
1932 return 1;
1933}