blob: e67080729746ef81ea52e9ffcd5a014fbe266516 [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>
Adam Jackson9e984bc12012-03-14 11:22:11 -040045#include <asm/pat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Daniel Vetter09422b22012-04-26 23:28:10 +020047#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
48
49#define BEGIN_LP_RING(n) \
50 intel_ring_begin(LP_RING(dev_priv), (n))
51
52#define OUT_RING(x) \
53 intel_ring_emit(LP_RING(dev_priv), x)
54
55#define ADVANCE_LP_RING() \
56 intel_ring_advance(LP_RING(dev_priv))
57
58/**
59 * Lock test for when it's just for synchronization of ring access.
60 *
61 * In that case, we don't need to do it when GEM is initialized as nobody else
62 * has access to the ring.
63 */
64#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
65 if (LP_RING(dev->dev_private)->obj == NULL) \
66 LOCK_TEST_WITH_RETURN(dev, file); \
67} while (0)
68
Daniel Vetter316d3882012-04-26 23:28:15 +020069static inline u32
70intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
71{
72 if (I915_NEED_GFX_HWS(dev_priv->dev))
73 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
74 else
75 return intel_read_status_page(LP_RING(dev_priv), reg);
76}
77
78#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
Daniel Vetter09422b22012-04-26 23:28:10 +020079#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
80#define I915_BREADCRUMB_INDEX 0x21
81
Daniel Vetterd05c6172012-04-26 23:28:09 +020082void i915_update_dri1_breadcrumb(struct drm_device *dev)
83{
84 drm_i915_private_t *dev_priv = dev->dev_private;
85 struct drm_i915_master_private *master_priv;
86
87 if (dev->primary->master) {
88 master_priv = dev->primary->master->driver_priv;
89 if (master_priv->sarea_priv)
90 master_priv->sarea_priv->last_dispatch =
91 READ_BREADCRUMB(dev_priv);
92 }
93}
94
Chris Wilson4cbf74c2011-02-25 22:26:23 +000095static void i915_write_hws_pga(struct drm_device *dev)
96{
97 drm_i915_private_t *dev_priv = dev->dev_private;
98 u32 addr;
99
100 addr = dev_priv->status_page_dmah->busaddr;
101 if (INTEL_INFO(dev)->gen >= 4)
102 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
103 I915_WRITE(HWS_PGA, addr);
104}
105
Keith Packard398c9cb2008-07-30 13:03:43 -0700106/**
107 * Sets up the hardware status page for devices that need a physical address
108 * in the register.
109 */
Eric Anholt3043c602008-10-02 12:24:47 -0700110static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700111{
112 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000113
Keith Packard398c9cb2008-07-30 13:03:43 -0700114 /* Program Hardware Status Page */
115 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800116 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -0700117
118 if (!dev_priv->status_page_dmah) {
119 DRM_ERROR("Can not allocate hardware status page\n");
120 return -ENOMEM;
121 }
Keith Packard398c9cb2008-07-30 13:03:43 -0700122
Keith Packardf3234702011-07-22 10:44:39 -0700123 memset_io((void __force __iomem *)dev_priv->status_page_dmah->vaddr,
124 0, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -0700125
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000126 i915_write_hws_pga(dev);
Zhenyu Wang9b974cc2010-01-05 11:25:06 +0800127
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800128 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700129 return 0;
130}
131
132/**
133 * Frees the hardware status page, whether it's a physical address or a virtual
134 * address set up by the X Server.
135 */
Eric Anholt3043c602008-10-02 12:24:47 -0700136static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700137{
138 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000139 struct intel_ring_buffer *ring = LP_RING(dev_priv);
140
Keith Packard398c9cb2008-07-30 13:03:43 -0700141 if (dev_priv->status_page_dmah) {
142 drm_pci_free(dev, dev_priv->status_page_dmah);
143 dev_priv->status_page_dmah = NULL;
144 }
145
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000146 if (ring->status_page.gfx_addr) {
147 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200148 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700149 }
150
151 /* Need to rewrite hardware status page */
152 I915_WRITE(HWS_PGA, 0x1ffff000);
153}
154
Dave Airlie84b1fd12007-07-11 15:53:27 +1000155void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000158 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000159 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jesse Barnes79e53942008-11-07 14:24:08 -0800161 /*
162 * We should never lose context on the ring with modesetting
163 * as we don't expose it to userspace
164 */
165 if (drm_core_check_feature(dev, DRIVER_MODESET))
166 return;
167
Chris Wilson8168bd42010-11-11 17:54:52 +0000168 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
169 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 ring->space = ring->head - (ring->tail + 8);
171 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800172 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Dave Airlie7c1c2872008-11-28 14:22:24 +1000174 if (!dev->primary->master)
175 return;
176
177 master_priv = dev->primary->master->driver_priv;
178 if (ring->head == ring->tail && master_priv->sarea_priv)
179 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Dave Airlie84b1fd12007-07-11 15:53:27 +1000182static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000184 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000185 int i;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* Make sure interrupts are disabled here because the uninstall ioctl
188 * may not have been called from userspace and after dev_private
189 * is freed, it's too late.
190 */
Eric Anholted4cb412008-07-29 12:10:39 -0700191 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200194 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000195 for (i = 0; i < I915_NUM_RINGS; i++)
196 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200197 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Keith Packard398c9cb2008-07-30 13:03:43 -0700199 /* Clear the HWS virtual address at teardown */
200 if (I915_NEED_GFX_HWS(dev))
201 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 return 0;
204}
205
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000206static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000208 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000209 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000210 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Dave Airlie3a03ac12009-01-11 09:03:49 +1000212 master_priv->sarea = drm_getsarea(dev);
213 if (master_priv->sarea) {
214 master_priv->sarea_priv = (drm_i915_sarea_t *)
215 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
216 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800217 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000218 }
219
Eric Anholt673a3942008-07-30 12:06:12 -0700220 if (init->ring_size != 0) {
Chris Wilsone8616b62011-01-20 09:57:11 +0000221 if (LP_RING(dev_priv)->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700222 i915_dma_cleanup(dev);
223 DRM_ERROR("Client tried to initialize ringbuffer in "
224 "GEM mode\n");
225 return -EINVAL;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Chris Wilsone8616b62011-01-20 09:57:11 +0000228 ret = intel_render_ring_init_dri(dev,
229 init->ring_start,
230 init->ring_size);
231 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700232 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000233 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200237 dev_priv->dri1.cpp = init->cpp;
238 dev_priv->dri1.back_offset = init->back_offset;
239 dev_priv->dri1.front_offset = init->front_offset;
240 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000241 if (master_priv->sarea_priv)
242 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Allow hardware batchbuffers unless told otherwise.
245 */
Daniel Vetter87813422012-05-02 11:49:32 +0200246 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
249}
250
Dave Airlie84b1fd12007-07-11 15:53:27 +1000251static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000254 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800256 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Daniel Vetter4225d0f2012-04-26 23:28:16 +0200258 if (ring->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 DRM_ERROR("can not ioremap virtual address for"
260 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000261 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
264 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800265 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000267 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800269 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800270 ring->status_page.page_addr);
271 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100272 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000273 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000274 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800275
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800276 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return 0;
279}
280
Eric Anholtc153f452007-09-03 12:06:45 +1000281static int i915_dma_init(struct drm_device *dev, void *data,
282 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Eric Anholtc153f452007-09-03 12:06:45 +1000284 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int retcode = 0;
286
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200287 if (drm_core_check_feature(dev, DRIVER_MODESET))
288 return -ENODEV;
289
Eric Anholtc153f452007-09-03 12:06:45 +1000290 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000292 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294 case I915_CLEANUP_DMA:
295 retcode = i915_dma_cleanup(dev);
296 break;
297 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100298 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 break;
300 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000301 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303 }
304
305 return retcode;
306}
307
308/* Implement basically the same security restrictions as hardware does
309 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
310 *
311 * Most of the calculations below involve calculating the size of a
312 * particular instruction. It's important to get the size right as
313 * that tells us where the next instruction to check is. Any illegal
314 * instruction detected will be given a size of zero, which is a
315 * signal to abort the rest of the buffer.
316 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100317static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 switch (((cmd >> 29) & 0x7)) {
320 case 0x0:
321 switch ((cmd >> 23) & 0x3f) {
322 case 0x0:
323 return 1; /* MI_NOOP */
324 case 0x4:
325 return 1; /* MI_FLUSH */
326 default:
327 return 0; /* disallow everything else */
328 }
329 break;
330 case 0x1:
331 return 0; /* reserved */
332 case 0x2:
333 return (cmd & 0xff) + 2; /* 2d commands */
334 case 0x3:
335 if (((cmd >> 24) & 0x1f) <= 0x18)
336 return 1;
337
338 switch ((cmd >> 24) & 0x1f) {
339 case 0x1c:
340 return 1;
341 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 case 0x3:
344 return (cmd & 0x1f) + 2;
345 case 0x4:
346 return (cmd & 0xf) + 2;
347 default:
348 return (cmd & 0xffff) + 2;
349 }
350 case 0x1e:
351 if (cmd & (1 << 23))
352 return (cmd & 0xffff) + 1;
353 else
354 return 1;
355 case 0x1f:
356 if ((cmd & (1 << 23)) == 0) /* inline vertices */
357 return (cmd & 0x1ffff) + 2;
358 else if (cmd & (1 << 17)) /* indirect random */
359 if ((cmd & 0xffff) == 0)
360 return 0; /* unknown length, too hard */
361 else
362 return (((cmd & 0xffff) + 1) / 2) + 1;
363 else
364 return 2; /* indirect sequential */
365 default:
366 return 0;
367 }
368 default:
369 return 0;
370 }
371
372 return 0;
373}
374
Eric Anholt201361a2009-03-11 12:30:04 -0700375static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100378 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000380 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000381 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100384 int sz = validate_cmd(buffer[i]);
385 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000386 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100387 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100390 ret = BEGIN_LP_RING((dwords+1)&~1);
391 if (ret)
392 return ret;
393
394 for (i = 0; i < dwords; i++)
395 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100396 if (dwords & 1)
397 OUT_RING(0);
398
399 ADVANCE_LP_RING();
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
402}
403
Eric Anholt673a3942008-07-30 12:06:12 -0700404int
405i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000406 struct drm_clip_rect *box,
407 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100409 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100410 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000412 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
413 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000415 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000416 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100419 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100420 ret = BEGIN_LP_RING(4);
421 if (ret)
422 return ret;
423
Alan Hourihanec29b6692006-08-12 16:29:24 +1000424 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000425 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
426 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000427 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000428 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100429 ret = BEGIN_LP_RING(6);
430 if (ret)
431 return ret;
432
Alan Hourihanec29b6692006-08-12 16:29:24 +1000433 OUT_RING(GFX_OP_DRAWRECT_INFO);
434 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000435 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
436 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000437 OUT_RING(DR4);
438 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000439 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100440 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 return 0;
443}
444
Alan Hourihanec29b6692006-08-12 16:29:24 +1000445/* XXX: Emitting the counter should really be moved to part of the IRQ
446 * emit. For now, do it in both places:
447 */
448
Dave Airlie84b1fd12007-07-11 15:53:27 +1000449static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100450{
451 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000452 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100453
Daniel Vetter231f42a2012-11-02 19:55:05 +0100454 dev_priv->dri1.counter++;
455 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
456 dev_priv->dri1.counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000457 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100458 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Dave Airliede227f52006-01-25 15:31:43 +1100459
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100460 if (BEGIN_LP_RING(4) == 0) {
461 OUT_RING(MI_STORE_DWORD_INDEX);
462 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100463 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100464 OUT_RING(0);
465 ADVANCE_LP_RING();
466 }
Dave Airliede227f52006-01-25 15:31:43 +1100467}
468
Dave Airlie84b1fd12007-07-11 15:53:27 +1000469static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700470 drm_i915_cmdbuffer_t *cmd,
471 struct drm_clip_rect *cliprects,
472 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 int nbox = cmd->num_cliprects;
475 int i = 0, count, ret;
476
477 if (cmd->sz & 0x3) {
478 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 i915_kernel_lost_context(dev);
483
484 count = nbox ? nbox : 1;
485
486 for (i = 0; i < count; i++) {
487 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000488 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 cmd->DR1, cmd->DR4);
490 if (ret)
491 return ret;
492 }
493
Eric Anholt201361a2009-03-11 12:30:04 -0700494 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (ret)
496 return ret;
497 }
498
Dave Airliede227f52006-01-25 15:31:43 +1100499 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
Dave Airlie84b1fd12007-07-11 15:53:27 +1000503static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700504 drm_i915_batchbuffer_t * batch,
505 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100507 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100509 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if ((batch->start | batch->used) & 0x7) {
512 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000513 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 i915_kernel_lost_context(dev);
517
518 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 for (i = 0; i < count; i++) {
520 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000521 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100522 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (ret)
524 return ret;
525 }
526
Keith Packard0790d5e2008-07-30 12:28:47 -0700527 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100528 ret = BEGIN_LP_RING(2);
529 if (ret)
530 return ret;
531
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100532 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000533 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
534 OUT_RING(batch->start);
535 } else {
536 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
537 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100540 ret = BEGIN_LP_RING(4);
541 if (ret)
542 return ret;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 OUT_RING(MI_BATCH_BUFFER);
545 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
546 OUT_RING(batch->start + batch->used - 4);
547 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100549 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
Zou Nan hai1cafd342010-06-25 13:40:24 +0800552
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100553 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100554 if (BEGIN_LP_RING(2) == 0) {
555 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
556 OUT_RING(MI_NOOP);
557 ADVANCE_LP_RING();
558 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100561 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 0;
563}
564
Dave Airlieaf6061a2008-05-07 12:15:39 +1000565static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000568 struct drm_i915_master_private *master_priv =
569 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100570 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Dave Airlie7c1c2872008-11-28 14:22:24 +1000572 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400573 return -EINVAL;
574
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800575 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800576 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200577 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800578 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Dave Airlieaf6061a2008-05-07 12:15:39 +1000580 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100582 ret = BEGIN_LP_RING(10);
583 if (ret)
584 return ret;
585
Jesse Barnes585fb112008-07-29 11:54:06 -0700586 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000587 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Dave Airlieaf6061a2008-05-07 12:15:39 +1000589 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
590 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200591 if (dev_priv->dri1.current_page == 0) {
592 OUT_RING(dev_priv->dri1.back_offset);
593 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000594 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200595 OUT_RING(dev_priv->dri1.front_offset);
596 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000597 }
598 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000599
Dave Airlieaf6061a2008-05-07 12:15:39 +1000600 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
601 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100602
Dave Airlieaf6061a2008-05-07 12:15:39 +1000603 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000604
Daniel Vetter231f42a2012-11-02 19:55:05 +0100605 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000606
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100607 if (BEGIN_LP_RING(4) == 0) {
608 OUT_RING(MI_STORE_DWORD_INDEX);
609 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100610 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100611 OUT_RING(0);
612 ADVANCE_LP_RING();
613 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000614
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200615 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000619static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000621 struct intel_ring_buffer *ring = LP_RING(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 i915_kernel_lost_context(dev);
Ben Widawsky96f298a2011-03-19 18:14:27 -0700624 return intel_wait_ring_idle(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Eric Anholtc153f452007-09-03 12:06:45 +1000627static int i915_flush_ioctl(struct drm_device *dev, void *data,
628 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Eric Anholt546b0972008-09-01 16:45:29 -0700630 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200632 if (drm_core_check_feature(dev, DRIVER_MODESET))
633 return -ENODEV;
634
Eric Anholt546b0972008-09-01 16:45:29 -0700635 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
636
637 mutex_lock(&dev->struct_mutex);
638 ret = i915_quiescent(dev);
639 mutex_unlock(&dev->struct_mutex);
640
641 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Eric Anholtc153f452007-09-03 12:06:45 +1000644static int i915_batchbuffer(struct drm_device *dev, void *data,
645 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000648 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000650 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000651 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700653 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200655 if (drm_core_check_feature(dev, DRIVER_MODESET))
656 return -ENODEV;
657
Daniel Vetter87813422012-05-02 11:49:32 +0200658 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800663 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800664 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Eric Anholt546b0972008-09-01 16:45:29 -0700666 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Eric Anholt201361a2009-03-11 12:30:04 -0700668 if (batch->num_cliprects < 0)
669 return -EINVAL;
670
671 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700672 cliprects = kcalloc(batch->num_cliprects,
673 sizeof(struct drm_clip_rect),
674 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700675 if (cliprects == NULL)
676 return -ENOMEM;
677
678 ret = copy_from_user(cliprects, batch->cliprects,
679 batch->num_cliprects *
680 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200681 if (ret != 0) {
682 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700683 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200684 }
Eric Anholt201361a2009-03-11 12:30:04 -0700685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Eric Anholt546b0972008-09-01 16:45:29 -0700687 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700688 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700689 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400691 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000692 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700693
694fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700695 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return ret;
698}
699
Eric Anholtc153f452007-09-03 12:06:45 +1000700static int i915_cmdbuffer(struct drm_device *dev, void *data,
701 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000704 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000706 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000707 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700708 struct drm_clip_rect *cliprects = NULL;
709 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int ret;
711
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800712 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800713 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200715 if (drm_core_check_feature(dev, DRIVER_MODESET))
716 return -ENODEV;
717
Eric Anholt546b0972008-09-01 16:45:29 -0700718 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Eric Anholt201361a2009-03-11 12:30:04 -0700720 if (cmdbuf->num_cliprects < 0)
721 return -EINVAL;
722
Eric Anholt9a298b22009-03-24 12:23:04 -0700723 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700724 if (batch_data == NULL)
725 return -ENOMEM;
726
727 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200728 if (ret != 0) {
729 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700730 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200731 }
Eric Anholt201361a2009-03-11 12:30:04 -0700732
733 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700734 cliprects = kcalloc(cmdbuf->num_cliprects,
735 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000736 if (cliprects == NULL) {
737 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700738 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000739 }
Eric Anholt201361a2009-03-11 12:30:04 -0700740
741 ret = copy_from_user(cliprects, cmdbuf->cliprects,
742 cmdbuf->num_cliprects *
743 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200744 if (ret != 0) {
745 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700746 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
Eric Anholt546b0972008-09-01 16:45:29 -0700750 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700751 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700752 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (ret) {
754 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000755 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400758 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000759 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700760
Eric Anholt201361a2009-03-11 12:30:04 -0700761fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700762 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000763fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700764 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700765
766 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Daniel Vetter94888672012-04-26 23:28:08 +0200769static int i915_emit_irq(struct drm_device * dev)
770{
771 drm_i915_private_t *dev_priv = dev->dev_private;
772 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
773
774 i915_kernel_lost_context(dev);
775
776 DRM_DEBUG_DRIVER("\n");
777
Daniel Vetter231f42a2012-11-02 19:55:05 +0100778 dev_priv->dri1.counter++;
779 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
780 dev_priv->dri1.counter = 1;
Daniel Vetter94888672012-04-26 23:28:08 +0200781 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100782 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200783
784 if (BEGIN_LP_RING(4) == 0) {
785 OUT_RING(MI_STORE_DWORD_INDEX);
786 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100787 OUT_RING(dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200788 OUT_RING(MI_USER_INTERRUPT);
789 ADVANCE_LP_RING();
790 }
791
Daniel Vetter231f42a2012-11-02 19:55:05 +0100792 return dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200793}
794
795static int i915_wait_irq(struct drm_device * dev, int irq_nr)
796{
797 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
798 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
799 int ret = 0;
800 struct intel_ring_buffer *ring = LP_RING(dev_priv);
801
802 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
803 READ_BREADCRUMB(dev_priv));
804
805 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
806 if (master_priv->sarea_priv)
807 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
808 return 0;
809 }
810
811 if (master_priv->sarea_priv)
812 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
813
814 if (ring->irq_get(ring)) {
815 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
816 READ_BREADCRUMB(dev_priv) >= irq_nr);
817 ring->irq_put(ring);
818 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
819 ret = -EBUSY;
820
821 if (ret == -EBUSY) {
822 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Daniel Vetter231f42a2012-11-02 19:55:05 +0100823 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200824 }
825
826 return ret;
827}
828
829/* Needs the lock as it touches the ring.
830 */
831static int i915_irq_emit(struct drm_device *dev, void *data,
832 struct drm_file *file_priv)
833{
834 drm_i915_private_t *dev_priv = dev->dev_private;
835 drm_i915_irq_emit_t *emit = data;
836 int result;
837
838 if (drm_core_check_feature(dev, DRIVER_MODESET))
839 return -ENODEV;
840
841 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
842 DRM_ERROR("called with no initialization\n");
843 return -EINVAL;
844 }
845
846 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
847
848 mutex_lock(&dev->struct_mutex);
849 result = i915_emit_irq(dev);
850 mutex_unlock(&dev->struct_mutex);
851
852 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
853 DRM_ERROR("copy_to_user\n");
854 return -EFAULT;
855 }
856
857 return 0;
858}
859
860/* Doesn't need the hardware lock.
861 */
862static int i915_irq_wait(struct drm_device *dev, void *data,
863 struct drm_file *file_priv)
864{
865 drm_i915_private_t *dev_priv = dev->dev_private;
866 drm_i915_irq_wait_t *irqwait = data;
867
868 if (drm_core_check_feature(dev, DRIVER_MODESET))
869 return -ENODEV;
870
871 if (!dev_priv) {
872 DRM_ERROR("called with no initialization\n");
873 return -EINVAL;
874 }
875
876 return i915_wait_irq(dev, irqwait->irq_seq);
877}
878
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200879static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
880 struct drm_file *file_priv)
881{
882 drm_i915_private_t *dev_priv = dev->dev_private;
883 drm_i915_vblank_pipe_t *pipe = data;
884
885 if (drm_core_check_feature(dev, DRIVER_MODESET))
886 return -ENODEV;
887
888 if (!dev_priv) {
889 DRM_ERROR("called with no initialization\n");
890 return -EINVAL;
891 }
892
893 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
894
895 return 0;
896}
897
898/**
899 * Schedule buffer swap at given vertical blank.
900 */
901static int i915_vblank_swap(struct drm_device *dev, void *data,
902 struct drm_file *file_priv)
903{
904 /* The delayed swap mechanism was fundamentally racy, and has been
905 * removed. The model was that the client requested a delayed flip/swap
906 * from the kernel, then waited for vblank before continuing to perform
907 * rendering. The problem was that the kernel might wake the client
908 * up before it dispatched the vblank swap (since the lock has to be
909 * held while touching the ringbuffer), in which case the client would
910 * clear and start the next frame before the swap occurred, and
911 * flicker would occur in addition to likely missing the vblank.
912 *
913 * In the absence of this ioctl, userland falls back to a correct path
914 * of waiting for a vblank, then dispatching the swap on its own.
915 * Context switching to userland and back is plenty fast enough for
916 * meeting the requirements of vblank swapping.
917 */
918 return -EINVAL;
919}
920
Eric Anholtc153f452007-09-03 12:06:45 +1000921static int i915_flip_bufs(struct drm_device *dev, void *data,
922 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Eric Anholt546b0972008-09-01 16:45:29 -0700924 int ret;
925
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200926 if (drm_core_check_feature(dev, DRIVER_MODESET))
927 return -ENODEV;
928
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800929 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Eric Anholt546b0972008-09-01 16:45:29 -0700931 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Eric Anholt546b0972008-09-01 16:45:29 -0700933 mutex_lock(&dev->struct_mutex);
934 ret = i915_dispatch_flip(dev);
935 mutex_unlock(&dev->struct_mutex);
936
937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Eric Anholtc153f452007-09-03 12:06:45 +1000940static int i915_getparam(struct drm_device *dev, void *data,
941 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000944 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int value;
946
947 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000948 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000949 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Eric Anholtc153f452007-09-03 12:06:45 +1000952 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700954 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200957 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100959 case I915_PARAM_LAST_DISPATCH:
960 value = READ_BREADCRUMB(dev_priv);
961 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400962 case I915_PARAM_CHIPSET_ID:
963 value = dev->pci_device;
964 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700965 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200966 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700967 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800968 case I915_PARAM_NUM_FENCES_AVAIL:
969 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
970 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200971 case I915_PARAM_HAS_OVERLAY:
972 value = dev_priv->overlay ? 1 : 0;
973 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800974 case I915_PARAM_HAS_PAGEFLIPPING:
975 value = 1;
976 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500977 case I915_PARAM_HAS_EXECBUF2:
978 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200979 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500980 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800981 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100982 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800983 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100984 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100985 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100986 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100987 case I915_PARAM_HAS_RELAXED_FENCING:
988 value = 1;
989 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100990 case I915_PARAM_HAS_COHERENT_RINGS:
991 value = 1;
992 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000993 case I915_PARAM_HAS_EXEC_CONSTANTS:
994 value = INTEL_INFO(dev)->gen >= 4;
995 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000996 case I915_PARAM_HAS_RELAXED_DELTA:
997 value = 1;
998 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800999 case I915_PARAM_HAS_GEN7_SOL_RESET:
1000 value = 1;
1001 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02001002 case I915_PARAM_HAS_LLC:
1003 value = HAS_LLC(dev);
1004 break;
Daniel Vetter777ee962012-02-15 23:50:25 +01001005 case I915_PARAM_HAS_ALIASING_PPGTT:
1006 value = dev_priv->mm.aliasing_ppgtt ? 1 : 0;
1007 break;
Ben Widawsky172cf152012-06-05 15:24:25 -07001008 case I915_PARAM_HAS_WAIT_TIMEOUT:
1009 value = 1;
1010 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +01001011 case I915_PARAM_HAS_SEMAPHORES:
1012 value = i915_semaphore_is_enabled(dev);
1013 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +10001014 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
1015 value = 1;
1016 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001017 case I915_PARAM_HAS_SECURE_BATCHES:
1018 value = capable(CAP_SYS_ADMIN);
1019 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001021 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -05001022 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001023 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
Eric Anholtc153f452007-09-03 12:06:45 +10001026 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001028 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030
1031 return 0;
1032}
1033
Eric Anholtc153f452007-09-03 12:06:45 +10001034static int i915_setparam(struct drm_device *dev, void *data,
1035 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001038 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001041 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001042 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Eric Anholtc153f452007-09-03 12:06:45 +10001045 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 break;
1048 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001051 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001053 case I915_SETPARAM_NUM_USED_FENCES:
1054 if (param->value > dev_priv->num_fence_regs ||
1055 param->value < 0)
1056 return -EINVAL;
1057 /* Userspace can use first N regs */
1058 dev_priv->fence_reg_start = param->value;
1059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001061 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001062 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001063 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 return 0;
1067}
1068
Eric Anholtc153f452007-09-03 12:06:45 +10001069static int i915_set_status_page(struct drm_device *dev, void *data,
1070 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001071{
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001072 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001073 drm_i915_hws_addr_t *hws = data;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001074 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001075
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001076 if (drm_core_check_feature(dev, DRIVER_MODESET))
1077 return -ENODEV;
1078
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001079 if (!I915_NEED_GFX_HWS(dev))
1080 return -EINVAL;
1081
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001082 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001083 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001084 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001085 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001086
Jesse Barnes79e53942008-11-07 14:24:08 -08001087 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1088 WARN(1, "tried to set status page when mode setting active\n");
1089 return 0;
1090 }
1091
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001092 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001093
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001094 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001095
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001096 dev_priv->dri1.gfx_hws_cpu_addr =
1097 ioremap_wc(dev_priv->mm.gtt_base_addr + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001098 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001099 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001100 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001101 DRM_ERROR("can not ioremap virtual address for"
1102 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001103 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001104 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001105
1106 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001107 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001108
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001109 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001110 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001111 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001112 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001113 return 0;
1114}
1115
Dave Airlieec2a4c32009-08-04 11:43:41 +10001116static int i915_get_bridge_dev(struct drm_device *dev)
1117{
1118 struct drm_i915_private *dev_priv = dev->dev_private;
1119
Akshay Joshi0206e352011-08-16 15:34:10 -04001120 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001121 if (!dev_priv->bridge_dev) {
1122 DRM_ERROR("bridge device not found\n");
1123 return -1;
1124 }
1125 return 0;
1126}
1127
Zhenyu Wangc48044112009-12-17 14:48:43 +08001128#define MCHBAR_I915 0x44
1129#define MCHBAR_I965 0x48
1130#define MCHBAR_SIZE (4*4096)
1131
1132#define DEVEN_REG 0x54
1133#define DEVEN_MCHBAR_EN (1 << 28)
1134
1135/* Allocate space for the MCH regs if needed, return nonzero on error */
1136static int
1137intel_alloc_mchbar_resource(struct drm_device *dev)
1138{
1139 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001140 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001141 u32 temp_lo, temp_hi = 0;
1142 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001143 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001144
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001145 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001146 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1147 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1148 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1149
1150 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1151#ifdef CONFIG_PNP
1152 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001153 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1154 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001155#endif
1156
1157 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001158 dev_priv->mch_res.name = "i915 MCHBAR";
1159 dev_priv->mch_res.flags = IORESOURCE_MEM;
1160 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1161 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001162 MCHBAR_SIZE, MCHBAR_SIZE,
1163 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001164 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001165 dev_priv->bridge_dev);
1166 if (ret) {
1167 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1168 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001169 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001170 }
1171
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001172 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001173 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1174 upper_32_bits(dev_priv->mch_res.start));
1175
1176 pci_write_config_dword(dev_priv->bridge_dev, reg,
1177 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001178 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001179}
1180
1181/* Setup MCHBAR if possible, return true if we should disable it again */
1182static void
1183intel_setup_mchbar(struct drm_device *dev)
1184{
1185 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001186 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001187 u32 temp;
1188 bool enabled;
1189
1190 dev_priv->mchbar_need_disable = false;
1191
1192 if (IS_I915G(dev) || IS_I915GM(dev)) {
1193 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1194 enabled = !!(temp & DEVEN_MCHBAR_EN);
1195 } else {
1196 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1197 enabled = temp & 1;
1198 }
1199
1200 /* If it's already enabled, don't have to do anything */
1201 if (enabled)
1202 return;
1203
1204 if (intel_alloc_mchbar_resource(dev))
1205 return;
1206
1207 dev_priv->mchbar_need_disable = true;
1208
1209 /* Space is allocated or reserved, so enable it. */
1210 if (IS_I915G(dev) || IS_I915GM(dev)) {
1211 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1212 temp | DEVEN_MCHBAR_EN);
1213 } else {
1214 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1215 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1216 }
1217}
1218
1219static void
1220intel_teardown_mchbar(struct drm_device *dev)
1221{
1222 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001223 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001224 u32 temp;
1225
1226 if (dev_priv->mchbar_need_disable) {
1227 if (IS_I915G(dev) || IS_I915GM(dev)) {
1228 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1229 temp &= ~DEVEN_MCHBAR_EN;
1230 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1231 } else {
1232 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1233 temp &= ~1;
1234 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1235 }
1236 }
1237
1238 if (dev_priv->mch_res.start)
1239 release_resource(&dev_priv->mch_res);
1240}
1241
Dave Airlie28d52042009-09-21 14:33:58 +10001242/* true = enable decode, false = disable decoder */
1243static unsigned int i915_vga_set_decode(void *cookie, bool state)
1244{
1245 struct drm_device *dev = cookie;
1246
1247 intel_modeset_vga_set_state(dev, state);
1248 if (state)
1249 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1250 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1251 else
1252 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1253}
1254
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001255static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1256{
1257 struct drm_device *dev = pci_get_drvdata(pdev);
1258 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1259 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001260 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001261 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001262 /* i915 resume handler doesn't set to D0 */
1263 pci_set_power_state(dev->pdev, PCI_D0);
1264 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001265 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001266 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001267 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001268 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001269 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001270 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001271 }
1272}
1273
1274static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1275{
1276 struct drm_device *dev = pci_get_drvdata(pdev);
1277 bool can_switch;
1278
1279 spin_lock(&dev->count_lock);
1280 can_switch = (dev->open_count == 0);
1281 spin_unlock(&dev->count_lock);
1282 return can_switch;
1283}
1284
Takashi Iwai26ec6852012-05-11 07:51:17 +02001285static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1286 .set_gpu_state = i915_switcheroo_set_state,
1287 .reprobe = NULL,
1288 .can_switch = i915_switcheroo_can_switch,
1289};
1290
Chris Wilson2c7111d2011-03-29 10:40:27 +01001291static int i915_load_modeset_init(struct drm_device *dev)
1292{
1293 struct drm_i915_private *dev_priv = dev->dev_private;
1294 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001295
Bryan Freed6d139a82010-10-14 09:14:51 +01001296 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001297 if (ret)
1298 DRM_INFO("failed to find VBIOS tables\n");
1299
Chris Wilson934f9922011-01-20 13:09:12 +00001300 /* If we have > 1 VGA cards, then we need to arbitrate access
1301 * to the common VGA resources.
1302 *
1303 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1304 * then we do not take part in VGA arbitration and the
1305 * vga_client_register() fails with -ENODEV.
1306 */
Dave Airlie28d52042009-09-21 14:33:58 +10001307 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
Chris Wilson934f9922011-01-20 13:09:12 +00001308 if (ret && ret != -ENODEV)
Chris Wilson2c7111d2011-03-29 10:40:27 +01001309 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001310
Jesse Barnes723bfd72010-10-07 16:01:13 -07001311 intel_register_dsm_handler();
1312
Takashi Iwai26ec6852012-05-11 07:51:17 +02001313 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001314 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001315 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001316
Chris Wilson9797fbf2012-04-24 15:47:39 +01001317 /* Initialise stolen first so that we may reserve preallocated
1318 * objects for the BIOS to KMS transition.
1319 */
1320 ret = i915_gem_init_stolen(dev);
1321 if (ret)
1322 goto cleanup_vga_switcheroo;
1323
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001324 intel_modeset_init(dev);
1325
Chris Wilson1070a422012-04-24 15:47:41 +01001326 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001327 if (ret)
Chris Wilson9797fbf2012-04-24 15:47:39 +01001328 goto cleanup_gem_stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -08001329
Chris Wilson2c7111d2011-03-29 10:40:27 +01001330 intel_modeset_gem_init(dev);
1331
1332 ret = drm_irq_install(dev);
1333 if (ret)
1334 goto cleanup_gem;
1335
Jesse Barnes79e53942008-11-07 14:24:08 -08001336 /* Always safe in the mode setting case. */
1337 /* FIXME: do pre/post-mode set stuff in core KMS code */
1338 dev->vblank_disable_allowed = 1;
1339
Chris Wilson5a793952010-06-06 10:50:03 +01001340 ret = intel_fbdev_init(dev);
1341 if (ret)
1342 goto cleanup_irq;
1343
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001344 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001345
1346 /* We're off and running w/KMS */
1347 dev_priv->mm.suspended = 0;
1348
Jesse Barnes79e53942008-11-07 14:24:08 -08001349 return 0;
1350
Chris Wilson5a793952010-06-06 10:50:03 +01001351cleanup_irq:
1352 drm_irq_uninstall(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001353cleanup_gem:
1354 mutex_lock(&dev->struct_mutex);
1355 i915_gem_cleanup_ringbuffer(dev);
1356 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001357 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001358cleanup_gem_stolen:
1359 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001360cleanup_vga_switcheroo:
1361 vga_switcheroo_unregister_client(dev->pdev);
1362cleanup_vga_client:
1363 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001364out:
1365 return ret;
1366}
1367
Dave Airlie7c1c2872008-11-28 14:22:24 +10001368int i915_master_create(struct drm_device *dev, struct drm_master *master)
1369{
1370 struct drm_i915_master_private *master_priv;
1371
Eric Anholt9a298b22009-03-24 12:23:04 -07001372 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001373 if (!master_priv)
1374 return -ENOMEM;
1375
1376 master->driver_priv = master_priv;
1377 return 0;
1378}
1379
1380void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1381{
1382 struct drm_i915_master_private *master_priv = master->driver_priv;
1383
1384 if (!master_priv)
1385 return;
1386
Eric Anholt9a298b22009-03-24 12:23:04 -07001387 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001388
1389 master->driver_priv = NULL;
1390}
1391
Adam Jacksone2b665c2012-03-14 11:22:10 -04001392static void
1393i915_mtrr_setup(struct drm_i915_private *dev_priv, unsigned long base,
1394 unsigned long size)
1395{
Chris Wilson23f54be2012-03-23 17:38:49 +00001396 dev_priv->mm.gtt_mtrr = -1;
1397
Adam Jackson9e984bc12012-03-14 11:22:11 -04001398#if defined(CONFIG_X86_PAT)
1399 if (cpu_has_pat)
1400 return;
1401#endif
1402
Adam Jacksone2b665c2012-03-14 11:22:10 -04001403 /* Set up a WC MTRR for non-PAT systems. This is more common than
1404 * one would think, because the kernel disables PAT on first
1405 * generation Core chips because WC PAT gets overridden by a UC
1406 * MTRR if present. Even if a UC MTRR isn't present.
1407 */
1408 dev_priv->mm.gtt_mtrr = mtrr_add(base, size, MTRR_TYPE_WRCOMB, 1);
1409 if (dev_priv->mm.gtt_mtrr < 0) {
1410 DRM_INFO("MTRR allocation failed. Graphics "
1411 "performance may suffer.\n");
1412 }
1413}
1414
Daniel Vettere1887192012-06-12 11:28:17 +02001415static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1416{
1417 struct apertures_struct *ap;
1418 struct pci_dev *pdev = dev_priv->dev->pdev;
1419 bool primary;
1420
1421 ap = alloc_apertures(1);
1422 if (!ap)
1423 return;
1424
Daniel Vetter87207ca2012-06-24 20:51:36 +02001425 ap->ranges[0].base = dev_priv->mm.gtt->gma_bus_addr;
Daniel Vettere1887192012-06-12 11:28:17 +02001426 ap->ranges[0].size =
1427 dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
1428 primary =
1429 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1430
1431 remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
1432
1433 kfree(ap);
1434}
1435
Daniel Vetterc96ea642012-08-08 22:01:51 +02001436static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1437{
1438 const struct intel_device_info *info = dev_priv->info;
1439
1440#define DEV_INFO_FLAG(name) info->name ? #name "," : ""
1441#define DEV_INFO_SEP ,
1442 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
1443 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1444 info->gen,
1445 dev_priv->dev->pdev->device,
1446 DEV_INFO_FLAGS);
1447#undef DEV_INFO_FLAG
1448#undef DEV_INFO_SEP
1449}
1450
Eric Anholt63ee41d2010-12-20 18:40:06 -08001451/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001452 * i915_driver_load - setup chip and create an initial config
1453 * @dev: DRM device
1454 * @flags: startup flags
1455 *
1456 * The driver load routine has to do several things:
1457 * - drive output discovery via intel_modeset_init()
1458 * - initialize the memory manager
1459 * - allocate initial config memory
1460 * - setup the DRM framebuffer with the allocated memory
1461 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001462int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001463{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001464 struct drm_i915_private *dev_priv;
Daniel Vetter26394d92012-03-26 21:33:18 +02001465 struct intel_device_info *info;
Chris Wilson934d6082012-09-14 11:57:46 +01001466 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001467 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001468
Daniel Vetter26394d92012-03-26 21:33:18 +02001469 info = (struct intel_device_info *) flags;
1470
1471 /* Refuse to load on gen6+ without kms enabled. */
1472 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET))
1473 return -ENODEV;
1474
Dave Airlie22eae942005-11-10 22:16:34 +11001475 /* i915 has 4 more counters */
1476 dev->counters += 4;
1477 dev->types[6] = _DRM_STAT_IRQ;
1478 dev->types[7] = _DRM_STAT_PRIMARY;
1479 dev->types[8] = _DRM_STAT_SECONDARY;
1480 dev->types[9] = _DRM_STAT_DMA;
1481
Eric Anholt9a298b22009-03-24 12:23:04 -07001482 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001483 if (dev_priv == NULL)
1484 return -ENOMEM;
1485
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001486 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001487 dev_priv->dev = dev;
Daniel Vetter26394d92012-03-26 21:33:18 +02001488 dev_priv->info = info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001489
Daniel Vetterc96ea642012-08-08 22:01:51 +02001490 i915_dump_device_info(dev_priv);
1491
Dave Airlieec2a4c32009-08-04 11:43:41 +10001492 if (i915_get_bridge_dev(dev)) {
1493 ret = -EIO;
1494 goto free_priv;
1495 }
1496
Daniel Vettere1887192012-06-12 11:28:17 +02001497 ret = intel_gmch_probe(dev_priv->bridge_dev, dev->pdev, NULL);
1498 if (!ret) {
1499 DRM_ERROR("failed to set up gmch\n");
1500 ret = -EIO;
1501 goto put_bridge;
1502 }
1503
1504 dev_priv->mm.gtt = intel_gtt_get();
1505 if (!dev_priv->mm.gtt) {
1506 DRM_ERROR("Failed to initialize GTT\n");
1507 ret = -ENODEV;
1508 goto put_gmch;
1509 }
1510
1511 i915_kick_out_firmware_fb(dev_priv);
1512
Dave Airlie466e69b2011-12-19 11:15:29 +00001513 pci_set_master(dev->pdev);
1514
Daniel Vetter9f82d232010-08-30 21:25:23 +02001515 /* overlay on gen2 is broken and can't address above 1G */
1516 if (IS_GEN2(dev))
1517 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1518
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001519 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1520 * using 32bit addressing, overwriting memory if HWS is located
1521 * above 4GB.
1522 *
1523 * The documentation also mentions an issue with undefined
1524 * behaviour if any general state is accessed within a page above 4GB,
1525 * which also needs to be handled carefully.
1526 */
1527 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1528 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1529
Chris Wilsonb4ce0f82010-10-28 11:26:06 +01001530 mmio_bar = IS_GEN2(dev) ? 1 : 0;
Chris Wilson934d6082012-09-14 11:57:46 +01001531 /* Before gen4, the registers and the GTT are behind different BARs.
1532 * However, from gen4 onwards, the registers and the GTT are shared
1533 * in the same BAR, so we want to restrict this ioremap from
1534 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1535 * the register BAR remains the same size for all the earlier
1536 * generations up to Ironlake.
1537 */
1538 if (info->gen < 5)
1539 mmio_size = 512*1024;
1540 else
1541 mmio_size = 2*1024*1024;
1542
1543 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
Chris Wilsonb4ce0f82010-10-28 11:26:06 +01001544 if (!dev_priv->regs) {
1545 DRM_ERROR("failed to map registers\n");
1546 ret = -EIO;
Daniel Vetter14be93d2012-06-08 15:55:40 +02001547 goto put_gmch;
Chris Wilson71e93392010-10-27 18:46:52 +01001548 }
1549
Daniel Vetter9021f282012-03-26 09:45:41 +02001550 aperture_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001551 dev_priv->mm.gtt_base_addr = dev_priv->mm.gtt->gma_bus_addr;
Chris Wilson71e93392010-10-27 18:46:52 +01001552
Akshay Joshi0206e352011-08-16 15:34:10 -04001553 dev_priv->mm.gtt_mapping =
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001554 io_mapping_create_wc(dev_priv->mm.gtt_base_addr,
1555 aperture_size);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001556 if (dev_priv->mm.gtt_mapping == NULL) {
1557 ret = -EIO;
Daniel Vettere1887192012-06-12 11:28:17 +02001558 goto out_rmmap;
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001559 }
1560
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001561 i915_mtrr_setup(dev_priv, dev_priv->mm.gtt_base_addr,
1562 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001563
Chris Wilsone642abb2010-09-09 12:46:34 +01001564 /* The i915 workqueue is primarily used for batched retirement of
1565 * requests (and thus managing bo) once the task has been completed
1566 * by the GPU. i915_gem_retire_requests() is called directly when we
1567 * need high-priority retirement, such as waiting for an explicit
1568 * bo.
1569 *
1570 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001571 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001572 *
1573 * All tasks on the workqueue are expected to acquire the dev mutex
1574 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001575 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001576 */
Tejun Heo53621862012-08-22 16:40:57 -07001577 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001578 if (dev_priv->wq == NULL) {
1579 DRM_ERROR("Failed to create our workqueue.\n");
1580 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001581 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001582 }
1583
Paulo Zanoni45e6e3a2012-07-03 15:57:32 -03001584 /* This must be called before any calls to HAS_PCH_* */
1585 intel_detect_pch(dev);
1586
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001587 intel_irq_init(dev);
Chris Wilson990bbda2012-07-02 11:51:02 -03001588 intel_gt_init(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001589
Zhenyu Wangc48044112009-12-17 14:48:43 +08001590 /* Try to make sure MCHBAR is enabled before poking at it */
1591 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001592 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001593 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001594
Bryan Freed6d139a82010-10-14 09:14:51 +01001595 /* Make sure the bios did its job and set up vital registers */
1596 intel_setup_bios(dev);
1597
Eric Anholt673a3942008-07-30 12:06:12 -07001598 i915_gem_load(dev);
1599
Keith Packard398c9cb2008-07-30 13:03:43 -07001600 /* Init HWS */
1601 if (!I915_NEED_GFX_HWS(dev)) {
1602 ret = i915_init_phys_hws(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001603 if (ret)
1604 goto out_gem_unload;
Keith Packard398c9cb2008-07-30 13:03:43 -07001605 }
Eric Anholted4cb412008-07-29 12:10:39 -07001606
1607 /* On the 945G/GM, the chipset reports the MSI capability on the
1608 * integrated graphics even though the support isn't actually there
1609 * according to the published specs. It doesn't appear to function
1610 * correctly in testing on 945G.
1611 * This may be a side effect of MSI having been made available for PEG
1612 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001613 *
1614 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001615 * be lost or delayed, but we use them anyways to avoid
1616 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001617 */
Keith Packardb60678a2008-12-08 11:12:28 -08001618 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001619 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001620
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001621 spin_lock_init(&dev_priv->irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001622 spin_lock_init(&dev_priv->error_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02001623 spin_lock_init(&dev_priv->rps.lock);
Alexander Shishkin99d0b1d2012-08-31 15:50:55 +03001624 spin_lock_init(&dev_priv->dpio_lock);
Eric Anholted4cb412008-07-29 12:10:39 -07001625
Eugeni Dodonovc51ed782012-04-13 17:08:45 -03001626 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
Jesse Barnes27f82272011-09-02 12:54:37 -07001627 dev_priv->num_pipe = 3;
1628 else if (IS_MOBILE(dev) || !IS_GEN2(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001629 dev_priv->num_pipe = 2;
1630 else
1631 dev_priv->num_pipe = 1;
1632
1633 ret = drm_vblank_init(dev, dev_priv->num_pipe);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001634 if (ret)
1635 goto out_gem_unload;
Keith Packard52440212008-11-18 09:30:25 -08001636
Ben Gamari11ed50e2009-09-14 17:48:45 -04001637 /* Start out suspended */
1638 dev_priv->mm.suspended = 1;
1639
Jesse Barnes79e53942008-11-07 14:24:08 -08001640 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001641 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001642 if (ret < 0) {
1643 DRM_ERROR("failed to init modeset\n");
Chris Wilson56e2ea32010-11-08 17:10:29 +00001644 goto out_gem_unload;
Jesse Barnes79e53942008-11-07 14:24:08 -08001645 }
1646 }
1647
Ben Widawsky0136db582012-04-10 21:17:01 -07001648 i915_setup_sysfs(dev);
1649
Matthew Garrett74a365b2009-03-19 21:35:39 +00001650 /* Must be done after probing outputs */
Chris Wilson44834a62010-08-19 16:09:23 +01001651 intel_opregion_init(dev);
1652 acpi_video_register();
Matthew Garrett74a365b2009-03-19 21:35:39 +00001653
Ben Gamarif65d9422009-09-14 17:48:44 -04001654 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
1655 (unsigned long) dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001656
Daniel Vettereb48eb02012-04-26 23:28:12 +02001657 if (IS_GEN5(dev))
1658 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001659
Jesse Barnes79e53942008-11-07 14:24:08 -08001660 return 0;
1661
Chris Wilson56e2ea32010-11-08 17:10:29 +00001662out_gem_unload:
Keith Packarda7b85d22011-07-10 13:12:17 -07001663 if (dev_priv->mm.inactive_shrinker.shrink)
1664 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1665
Chris Wilson56e2ea32010-11-08 17:10:29 +00001666 if (dev->pdev->msi_enabled)
1667 pci_disable_msi(dev->pdev);
1668
1669 intel_teardown_gmbus(dev);
1670 intel_teardown_mchbar(dev);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001671 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001672out_mtrrfree:
1673 if (dev_priv->mm.gtt_mtrr >= 0) {
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001674 mtrr_del(dev_priv->mm.gtt_mtrr,
1675 dev_priv->mm.gtt_base_addr,
1676 aperture_size);
Keith Packarda7b85d22011-07-10 13:12:17 -07001677 dev_priv->mm.gtt_mtrr = -1;
1678 }
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001679 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001680out_rmmap:
Chris Wilson6dda5692010-10-29 21:02:18 +01001681 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vettere1887192012-06-12 11:28:17 +02001682put_gmch:
1683 intel_gmch_remove();
Dave Airlieec2a4c32009-08-04 11:43:41 +10001684put_bridge:
1685 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001686free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001687 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001688 return ret;
1689}
1690
1691int i915_driver_unload(struct drm_device *dev)
1692{
1693 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001694 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001695
Daniel Vettereb48eb02012-04-26 23:28:12 +02001696 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001697
Ben Widawsky0136db582012-04-10 21:17:01 -07001698 i915_teardown_sysfs(dev);
1699
Chris Wilson17250b72010-10-28 12:51:39 +01001700 if (dev_priv->mm.inactive_shrinker.shrink)
1701 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1702
Daniel Vetterc911fc12010-08-20 21:23:20 +02001703 mutex_lock(&dev->struct_mutex);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001704 ret = i915_gpu_idle(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02001705 if (ret)
1706 DRM_ERROR("failed to idle hardware: %d\n", ret);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001707 i915_gem_retire_requests(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02001708 mutex_unlock(&dev->struct_mutex);
1709
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001710 /* Cancel the retire work handler, which should be idle now. */
1711 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
1712
Eric Anholtab657db12009-01-23 12:57:47 -08001713 io_mapping_free(dev_priv->mm.gtt_mapping);
1714 if (dev_priv->mm.gtt_mtrr >= 0) {
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001715 mtrr_del(dev_priv->mm.gtt_mtrr,
1716 dev_priv->mm.gtt_base_addr,
1717 dev_priv->mm.gtt->gtt_mappable_entries * PAGE_SIZE);
Eric Anholtab657db12009-01-23 12:57:47 -08001718 dev_priv->mm.gtt_mtrr = -1;
1719 }
1720
Chris Wilson44834a62010-08-19 16:09:23 +01001721 acpi_video_unregister();
1722
Jesse Barnes79e53942008-11-07 14:24:08 -08001723 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01001724 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001725 intel_modeset_cleanup(dev);
1726
Zhao Yakui6363ee62009-11-24 09:48:44 +08001727 /*
1728 * free the memory space allocated for the child device
1729 * config parsed from VBT
1730 */
1731 if (dev_priv->child_dev && dev_priv->child_dev_num) {
1732 kfree(dev_priv->child_dev);
1733 dev_priv->child_dev = NULL;
1734 dev_priv->child_dev_num = 0;
1735 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001736
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001737 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001738 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001739 }
1740
Daniel Vettera8b48992010-08-20 21:25:11 +02001741 /* Free error state after interrupts are fully disabled. */
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001742 del_timer_sync(&dev_priv->hangcheck_timer);
1743 cancel_work_sync(&dev_priv->error_work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001744 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001745
Eric Anholted4cb412008-07-29 12:10:39 -07001746 if (dev->pdev->msi_enabled)
1747 pci_disable_msi(dev->pdev);
1748
Chris Wilson44834a62010-08-19 16:09:23 +01001749 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001750
Jesse Barnes79e53942008-11-07 14:24:08 -08001751 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001752 /* Flush any outstanding unpin_work. */
1753 flush_workqueue(dev_priv->wq);
1754
Jesse Barnes79e53942008-11-07 14:24:08 -08001755 mutex_lock(&dev->struct_mutex);
Hugh Dickinsecbec532011-06-27 16:18:20 -07001756 i915_gem_free_all_phys_object(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001757 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001758 i915_gem_context_fini(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001759 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001760 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001761 i915_gem_cleanup_stolen(dev);
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001762 drm_mm_takedown(&dev_priv->mm.stolen);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001763
1764 intel_cleanup_overlay(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001765
1766 if (!I915_NEED_GFX_HWS(dev))
1767 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001768 }
1769
Daniel Vetter701394c2010-10-10 18:54:08 +01001770 if (dev_priv->regs != NULL)
Chris Wilson6dda5692010-10-29 21:02:18 +01001771 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vetter701394c2010-10-10 18:54:08 +01001772
Chris Wilsonf899fc62010-07-20 15:44:45 -07001773 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001774 intel_teardown_mchbar(dev);
1775
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001776 destroy_workqueue(dev_priv->wq);
1777
Dave Airlieec2a4c32009-08-04 11:43:41 +10001778 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07001779 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001780
Dave Airlie22eae942005-11-10 22:16:34 +11001781 return 0;
1782}
1783
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001784int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001785{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001786 struct drm_i915_file_private *file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001787
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001788 DRM_DEBUG_DRIVER("\n");
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001789 file_priv = kmalloc(sizeof(*file_priv), GFP_KERNEL);
1790 if (!file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07001791 return -ENOMEM;
1792
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001793 file->driver_priv = file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001794
Chris Wilson1c255952010-09-26 11:03:27 +01001795 spin_lock_init(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001796 INIT_LIST_HEAD(&file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001797
Daniel Vetterdf12c6d2012-06-19 16:52:30 +02001798 idr_init(&file_priv->context_idr);
Ben Widawsky254f9652012-06-04 14:42:42 -07001799
Eric Anholt673a3942008-07-30 12:06:12 -07001800 return 0;
1801}
1802
Jesse Barnes79e53942008-11-07 14:24:08 -08001803/**
1804 * i915_driver_lastclose - clean up after all DRM clients have exited
1805 * @dev: DRM device
1806 *
1807 * Take care of cleaning up after all DRM clients have exited. In the
1808 * mode setting case, we want to restore the kernel's initial mode (just
1809 * in case the last client left us in a bad state).
1810 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001811 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001812 * and DMA structures, since the kernel won't be using them, and clea
1813 * up any GEM state.
1814 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001815void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001817 drm_i915_private_t *dev_priv = dev->dev_private;
1818
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001819 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1820 * goes right around and calls lastclose. Check for this and don't clean
1821 * up anything. */
1822 if (!dev_priv)
1823 return;
1824
1825 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +01001826 intel_fb_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001827 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001828 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001829 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001830
Eric Anholt673a3942008-07-30 12:06:12 -07001831 i915_gem_lastclose(dev);
1832
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001833 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Eric Anholt6c340ea2007-08-25 20:23:09 +10001836void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Ben Widawsky254f9652012-06-04 14:42:42 -07001838 i915_gem_context_close(dev, file_priv);
Eric Anholtb9624422009-06-03 07:27:35 +00001839 i915_gem_release(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001842void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001843{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001844 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001845
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001846 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001847}
1848
Eric Anholtc153f452007-09-03 12:06:45 +10001849struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001850 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1851 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1852 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1853 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1854 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1855 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1856 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH),
1857 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001858 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1859 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1860 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001861 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001862 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02001863 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001864 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
1865 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1866 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1867 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1868 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
1869 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
1870 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1871 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1872 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
Ben Widawsky199adf42012-09-21 17:01:20 -07001873 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED),
1874 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001875 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
1876 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1877 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1878 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
1879 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
1880 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
1881 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
1882 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
1883 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
1884 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
1885 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
1886 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
1887 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
1888 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
1889 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
1890 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1891 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08001892 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1893 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, intel_sprite_get_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Ben Widawsky23ba4fd2012-05-24 15:03:10 -07001894 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED),
Ben Widawsky84624812012-06-04 14:42:54 -07001895 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED),
1896 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED),
Ben Widawskyc0c7bab2012-07-12 11:01:05 -07001897 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10001898};
1899
1900int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001901
Daniel Vetter9021f282012-03-26 09:45:41 +02001902/*
1903 * This is really ugly: Because old userspace abused the linux agp interface to
1904 * manage the gtt, we need to claim that all intel devices are agp. For
1905 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10001906 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001907int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001908{
1909 return 1;
1910}