blob: 03ee1936e73741b3b6a321d1009e74bdf75ee4e5 [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/**
Keith Packard398c9cb2008-07-30 13:03:43 -0700107 * Frees the hardware status page, whether it's a physical address or a virtual
108 * address set up by the X Server.
109 */
Eric Anholt3043c602008-10-02 12:24:47 -0700110static void i915_free_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 struct intel_ring_buffer *ring = LP_RING(dev_priv);
114
Keith Packard398c9cb2008-07-30 13:03:43 -0700115 if (dev_priv->status_page_dmah) {
116 drm_pci_free(dev, dev_priv->status_page_dmah);
117 dev_priv->status_page_dmah = NULL;
118 }
119
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000120 if (ring->status_page.gfx_addr) {
121 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200122 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700123 }
124
125 /* Need to rewrite hardware status page */
126 I915_WRITE(HWS_PGA, 0x1ffff000);
127}
128
Dave Airlie84b1fd12007-07-11 15:53:27 +1000129void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000132 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000133 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Jesse Barnes79e53942008-11-07 14:24:08 -0800135 /*
136 * We should never lose context on the ring with modesetting
137 * as we don't expose it to userspace
138 */
139 if (drm_core_check_feature(dev, DRIVER_MODESET))
140 return;
141
Chris Wilson8168bd42010-11-11 17:54:52 +0000142 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
143 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Ville Syrjälä633cf8f2012-12-03 18:43:32 +0200144 ring->space = ring->head - (ring->tail + I915_RING_FREE_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800146 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Dave Airlie7c1c2872008-11-28 14:22:24 +1000148 if (!dev->primary->master)
149 return;
150
151 master_priv = dev->primary->master->driver_priv;
152 if (ring->head == ring->tail && master_priv->sarea_priv)
153 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Dave Airlie84b1fd12007-07-11 15:53:27 +1000156static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000158 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000159 int i;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* Make sure interrupts are disabled here because the uninstall ioctl
162 * may not have been called from userspace and after dev_private
163 * is freed, it's too late.
164 */
Eric Anholted4cb412008-07-29 12:10:39 -0700165 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000166 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200168 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000169 for (i = 0; i < I915_NUM_RINGS; i++)
170 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200171 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Keith Packard398c9cb2008-07-30 13:03:43 -0700173 /* Clear the HWS virtual address at teardown */
174 if (I915_NEED_GFX_HWS(dev))
175 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 return 0;
178}
179
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000180static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000182 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000183 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000184 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dave Airlie3a03ac12009-01-11 09:03:49 +1000186 master_priv->sarea = drm_getsarea(dev);
187 if (master_priv->sarea) {
188 master_priv->sarea_priv = (drm_i915_sarea_t *)
189 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
190 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800191 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000192 }
193
Eric Anholt673a3942008-07-30 12:06:12 -0700194 if (init->ring_size != 0) {
Chris Wilsone8616b62011-01-20 09:57:11 +0000195 if (LP_RING(dev_priv)->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700196 i915_dma_cleanup(dev);
197 DRM_ERROR("Client tried to initialize ringbuffer in "
198 "GEM mode\n");
199 return -EINVAL;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Chris Wilsone8616b62011-01-20 09:57:11 +0000202 ret = intel_render_ring_init_dri(dev,
203 init->ring_start,
204 init->ring_size);
205 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700206 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000207 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200211 dev_priv->dri1.cpp = init->cpp;
212 dev_priv->dri1.back_offset = init->back_offset;
213 dev_priv->dri1.front_offset = init->front_offset;
214 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000215 if (master_priv->sarea_priv)
216 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 /* Allow hardware batchbuffers unless told otherwise.
219 */
Daniel Vetter87813422012-05-02 11:49:32 +0200220 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223}
224
Dave Airlie84b1fd12007-07-11 15:53:27 +1000225static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000228 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800230 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Daniel Vetter4225d0f2012-04-26 23:28:16 +0200232 if (ring->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 DRM_ERROR("can not ioremap virtual address for"
234 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000235 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
238 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800239 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000241 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800243 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800244 ring->status_page.page_addr);
245 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100246 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000247 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000248 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800249
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800250 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 return 0;
253}
254
Eric Anholtc153f452007-09-03 12:06:45 +1000255static int i915_dma_init(struct drm_device *dev, void *data,
256 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Eric Anholtc153f452007-09-03 12:06:45 +1000258 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 int retcode = 0;
260
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200261 if (drm_core_check_feature(dev, DRIVER_MODESET))
262 return -ENODEV;
263
Eric Anholtc153f452007-09-03 12:06:45 +1000264 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000266 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
268 case I915_CLEANUP_DMA:
269 retcode = i915_dma_cleanup(dev);
270 break;
271 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100272 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 break;
274 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000275 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 }
278
279 return retcode;
280}
281
282/* Implement basically the same security restrictions as hardware does
283 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
284 *
285 * Most of the calculations below involve calculating the size of a
286 * particular instruction. It's important to get the size right as
287 * that tells us where the next instruction to check is. Any illegal
288 * instruction detected will be given a size of zero, which is a
289 * signal to abort the rest of the buffer.
290 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100291static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 switch (((cmd >> 29) & 0x7)) {
294 case 0x0:
295 switch ((cmd >> 23) & 0x3f) {
296 case 0x0:
297 return 1; /* MI_NOOP */
298 case 0x4:
299 return 1; /* MI_FLUSH */
300 default:
301 return 0; /* disallow everything else */
302 }
303 break;
304 case 0x1:
305 return 0; /* reserved */
306 case 0x2:
307 return (cmd & 0xff) + 2; /* 2d commands */
308 case 0x3:
309 if (((cmd >> 24) & 0x1f) <= 0x18)
310 return 1;
311
312 switch ((cmd >> 24) & 0x1f) {
313 case 0x1c:
314 return 1;
315 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 case 0x3:
318 return (cmd & 0x1f) + 2;
319 case 0x4:
320 return (cmd & 0xf) + 2;
321 default:
322 return (cmd & 0xffff) + 2;
323 }
324 case 0x1e:
325 if (cmd & (1 << 23))
326 return (cmd & 0xffff) + 1;
327 else
328 return 1;
329 case 0x1f:
330 if ((cmd & (1 << 23)) == 0) /* inline vertices */
331 return (cmd & 0x1ffff) + 2;
332 else if (cmd & (1 << 17)) /* indirect random */
333 if ((cmd & 0xffff) == 0)
334 return 0; /* unknown length, too hard */
335 else
336 return (((cmd & 0xffff) + 1) / 2) + 1;
337 else
338 return 2; /* indirect sequential */
339 default:
340 return 0;
341 }
342 default:
343 return 0;
344 }
345
346 return 0;
347}
348
Eric Anholt201361a2009-03-11 12:30:04 -0700349static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100352 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000354 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000355 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100358 int sz = validate_cmd(buffer[i]);
359 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000360 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100361 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100364 ret = BEGIN_LP_RING((dwords+1)&~1);
365 if (ret)
366 return ret;
367
368 for (i = 0; i < dwords; i++)
369 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100370 if (dwords & 1)
371 OUT_RING(0);
372
373 ADVANCE_LP_RING();
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
376}
377
Eric Anholt673a3942008-07-30 12:06:12 -0700378int
379i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000380 struct drm_clip_rect *box,
381 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100383 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100384 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000386 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
387 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000389 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000390 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100393 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100394 ret = BEGIN_LP_RING(4);
395 if (ret)
396 return ret;
397
Alan Hourihanec29b6692006-08-12 16:29:24 +1000398 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000399 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
400 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000401 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000402 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100403 ret = BEGIN_LP_RING(6);
404 if (ret)
405 return ret;
406
Alan Hourihanec29b6692006-08-12 16:29:24 +1000407 OUT_RING(GFX_OP_DRAWRECT_INFO);
408 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000409 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
410 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000411 OUT_RING(DR4);
412 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000413 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100414 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 return 0;
417}
418
Alan Hourihanec29b6692006-08-12 16:29:24 +1000419/* XXX: Emitting the counter should really be moved to part of the IRQ
420 * emit. For now, do it in both places:
421 */
422
Dave Airlie84b1fd12007-07-11 15:53:27 +1000423static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100424{
425 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000426 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100427
Daniel Vetter231f42a2012-11-02 19:55:05 +0100428 dev_priv->dri1.counter++;
429 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
430 dev_priv->dri1.counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000431 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100432 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Dave Airliede227f52006-01-25 15:31:43 +1100433
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100434 if (BEGIN_LP_RING(4) == 0) {
435 OUT_RING(MI_STORE_DWORD_INDEX);
436 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100437 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100438 OUT_RING(0);
439 ADVANCE_LP_RING();
440 }
Dave Airliede227f52006-01-25 15:31:43 +1100441}
442
Dave Airlie84b1fd12007-07-11 15:53:27 +1000443static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700444 drm_i915_cmdbuffer_t *cmd,
445 struct drm_clip_rect *cliprects,
446 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 int nbox = cmd->num_cliprects;
449 int i = 0, count, ret;
450
451 if (cmd->sz & 0x3) {
452 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 i915_kernel_lost_context(dev);
457
458 count = nbox ? nbox : 1;
459
460 for (i = 0; i < count; i++) {
461 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000462 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 cmd->DR1, cmd->DR4);
464 if (ret)
465 return ret;
466 }
467
Eric Anholt201361a2009-03-11 12:30:04 -0700468 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (ret)
470 return ret;
471 }
472
Dave Airliede227f52006-01-25 15:31:43 +1100473 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475}
476
Dave Airlie84b1fd12007-07-11 15:53:27 +1000477static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700478 drm_i915_batchbuffer_t * batch,
479 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100481 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100483 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if ((batch->start | batch->used) & 0x7) {
486 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 i915_kernel_lost_context(dev);
491
492 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 for (i = 0; i < count; i++) {
494 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000495 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100496 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (ret)
498 return ret;
499 }
500
Keith Packard0790d5e2008-07-30 12:28:47 -0700501 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100502 ret = BEGIN_LP_RING(2);
503 if (ret)
504 return ret;
505
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100506 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000507 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
508 OUT_RING(batch->start);
509 } else {
510 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
511 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100514 ret = BEGIN_LP_RING(4);
515 if (ret)
516 return ret;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 OUT_RING(MI_BATCH_BUFFER);
519 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
520 OUT_RING(batch->start + batch->used - 4);
521 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100523 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525
Zou Nan hai1cafd342010-06-25 13:40:24 +0800526
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100527 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100528 if (BEGIN_LP_RING(2) == 0) {
529 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
530 OUT_RING(MI_NOOP);
531 ADVANCE_LP_RING();
532 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100535 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537}
538
Dave Airlieaf6061a2008-05-07 12:15:39 +1000539static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000542 struct drm_i915_master_private *master_priv =
543 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100544 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dave Airlie7c1c2872008-11-28 14:22:24 +1000546 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400547 return -EINVAL;
548
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800549 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800550 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200551 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800552 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Dave Airlieaf6061a2008-05-07 12:15:39 +1000554 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100556 ret = BEGIN_LP_RING(10);
557 if (ret)
558 return ret;
559
Jesse Barnes585fb112008-07-29 11:54:06 -0700560 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000561 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Dave Airlieaf6061a2008-05-07 12:15:39 +1000563 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
564 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200565 if (dev_priv->dri1.current_page == 0) {
566 OUT_RING(dev_priv->dri1.back_offset);
567 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000568 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200569 OUT_RING(dev_priv->dri1.front_offset);
570 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000571 }
572 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000573
Dave Airlieaf6061a2008-05-07 12:15:39 +1000574 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
575 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100576
Dave Airlieaf6061a2008-05-07 12:15:39 +1000577 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000578
Daniel Vetter231f42a2012-11-02 19:55:05 +0100579 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000580
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100581 if (BEGIN_LP_RING(4) == 0) {
582 OUT_RING(MI_STORE_DWORD_INDEX);
583 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100584 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100585 OUT_RING(0);
586 ADVANCE_LP_RING();
587 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000588
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200589 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000593static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 i915_kernel_lost_context(dev);
Chris Wilson3e960502012-11-27 16:22:54 +0000596 return intel_ring_idle(LP_RING(dev->dev_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Eric Anholtc153f452007-09-03 12:06:45 +1000599static int i915_flush_ioctl(struct drm_device *dev, void *data,
600 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Eric Anholt546b0972008-09-01 16:45:29 -0700602 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200604 if (drm_core_check_feature(dev, DRIVER_MODESET))
605 return -ENODEV;
606
Eric Anholt546b0972008-09-01 16:45:29 -0700607 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
608
609 mutex_lock(&dev->struct_mutex);
610 ret = i915_quiescent(dev);
611 mutex_unlock(&dev->struct_mutex);
612
613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Eric Anholtc153f452007-09-03 12:06:45 +1000616static int i915_batchbuffer(struct drm_device *dev, void *data,
617 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000620 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000622 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000623 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700625 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200627 if (drm_core_check_feature(dev, DRIVER_MODESET))
628 return -ENODEV;
629
Daniel Vetter87813422012-05-02 11:49:32 +0200630 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000632 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800635 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800636 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Eric Anholt546b0972008-09-01 16:45:29 -0700638 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Eric Anholt201361a2009-03-11 12:30:04 -0700640 if (batch->num_cliprects < 0)
641 return -EINVAL;
642
643 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700644 cliprects = kcalloc(batch->num_cliprects,
645 sizeof(struct drm_clip_rect),
646 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700647 if (cliprects == NULL)
648 return -ENOMEM;
649
650 ret = copy_from_user(cliprects, batch->cliprects,
651 batch->num_cliprects *
652 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200653 if (ret != 0) {
654 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700655 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200656 }
Eric Anholt201361a2009-03-11 12:30:04 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Eric Anholt546b0972008-09-01 16:45:29 -0700659 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700660 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700661 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400663 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000664 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700665
666fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700667 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return ret;
670}
671
Eric Anholtc153f452007-09-03 12:06:45 +1000672static int i915_cmdbuffer(struct drm_device *dev, void *data,
673 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000676 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000678 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000679 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700680 struct drm_clip_rect *cliprects = NULL;
681 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int ret;
683
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800684 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800685 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200687 if (drm_core_check_feature(dev, DRIVER_MODESET))
688 return -ENODEV;
689
Eric Anholt546b0972008-09-01 16:45:29 -0700690 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Eric Anholt201361a2009-03-11 12:30:04 -0700692 if (cmdbuf->num_cliprects < 0)
693 return -EINVAL;
694
Eric Anholt9a298b22009-03-24 12:23:04 -0700695 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700696 if (batch_data == NULL)
697 return -ENOMEM;
698
699 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200700 if (ret != 0) {
701 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700702 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200703 }
Eric Anholt201361a2009-03-11 12:30:04 -0700704
705 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700706 cliprects = kcalloc(cmdbuf->num_cliprects,
707 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000708 if (cliprects == NULL) {
709 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700710 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000711 }
Eric Anholt201361a2009-03-11 12:30:04 -0700712
713 ret = copy_from_user(cliprects, cmdbuf->cliprects,
714 cmdbuf->num_cliprects *
715 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200716 if (ret != 0) {
717 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700718 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
Eric Anholt546b0972008-09-01 16:45:29 -0700722 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700723 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700724 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (ret) {
726 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000727 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400730 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000731 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700732
Eric Anholt201361a2009-03-11 12:30:04 -0700733fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700734 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000735fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700736 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700737
738 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Daniel Vetter94888672012-04-26 23:28:08 +0200741static int i915_emit_irq(struct drm_device * dev)
742{
743 drm_i915_private_t *dev_priv = dev->dev_private;
744 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
745
746 i915_kernel_lost_context(dev);
747
748 DRM_DEBUG_DRIVER("\n");
749
Daniel Vetter231f42a2012-11-02 19:55:05 +0100750 dev_priv->dri1.counter++;
751 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
752 dev_priv->dri1.counter = 1;
Daniel Vetter94888672012-04-26 23:28:08 +0200753 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100754 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200755
756 if (BEGIN_LP_RING(4) == 0) {
757 OUT_RING(MI_STORE_DWORD_INDEX);
758 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100759 OUT_RING(dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200760 OUT_RING(MI_USER_INTERRUPT);
761 ADVANCE_LP_RING();
762 }
763
Daniel Vetter231f42a2012-11-02 19:55:05 +0100764 return dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200765}
766
767static int i915_wait_irq(struct drm_device * dev, int irq_nr)
768{
769 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
770 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
771 int ret = 0;
772 struct intel_ring_buffer *ring = LP_RING(dev_priv);
773
774 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
775 READ_BREADCRUMB(dev_priv));
776
777 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
778 if (master_priv->sarea_priv)
779 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
780 return 0;
781 }
782
783 if (master_priv->sarea_priv)
784 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
785
786 if (ring->irq_get(ring)) {
787 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
788 READ_BREADCRUMB(dev_priv) >= irq_nr);
789 ring->irq_put(ring);
790 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
791 ret = -EBUSY;
792
793 if (ret == -EBUSY) {
794 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Daniel Vetter231f42a2012-11-02 19:55:05 +0100795 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200796 }
797
798 return ret;
799}
800
801/* Needs the lock as it touches the ring.
802 */
803static int i915_irq_emit(struct drm_device *dev, void *data,
804 struct drm_file *file_priv)
805{
806 drm_i915_private_t *dev_priv = dev->dev_private;
807 drm_i915_irq_emit_t *emit = data;
808 int result;
809
810 if (drm_core_check_feature(dev, DRIVER_MODESET))
811 return -ENODEV;
812
813 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
814 DRM_ERROR("called with no initialization\n");
815 return -EINVAL;
816 }
817
818 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
819
820 mutex_lock(&dev->struct_mutex);
821 result = i915_emit_irq(dev);
822 mutex_unlock(&dev->struct_mutex);
823
824 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
825 DRM_ERROR("copy_to_user\n");
826 return -EFAULT;
827 }
828
829 return 0;
830}
831
832/* Doesn't need the hardware lock.
833 */
834static int i915_irq_wait(struct drm_device *dev, void *data,
835 struct drm_file *file_priv)
836{
837 drm_i915_private_t *dev_priv = dev->dev_private;
838 drm_i915_irq_wait_t *irqwait = data;
839
840 if (drm_core_check_feature(dev, DRIVER_MODESET))
841 return -ENODEV;
842
843 if (!dev_priv) {
844 DRM_ERROR("called with no initialization\n");
845 return -EINVAL;
846 }
847
848 return i915_wait_irq(dev, irqwait->irq_seq);
849}
850
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200851static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
852 struct drm_file *file_priv)
853{
854 drm_i915_private_t *dev_priv = dev->dev_private;
855 drm_i915_vblank_pipe_t *pipe = data;
856
857 if (drm_core_check_feature(dev, DRIVER_MODESET))
858 return -ENODEV;
859
860 if (!dev_priv) {
861 DRM_ERROR("called with no initialization\n");
862 return -EINVAL;
863 }
864
865 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
866
867 return 0;
868}
869
870/**
871 * Schedule buffer swap at given vertical blank.
872 */
873static int i915_vblank_swap(struct drm_device *dev, void *data,
874 struct drm_file *file_priv)
875{
876 /* The delayed swap mechanism was fundamentally racy, and has been
877 * removed. The model was that the client requested a delayed flip/swap
878 * from the kernel, then waited for vblank before continuing to perform
879 * rendering. The problem was that the kernel might wake the client
880 * up before it dispatched the vblank swap (since the lock has to be
881 * held while touching the ringbuffer), in which case the client would
882 * clear and start the next frame before the swap occurred, and
883 * flicker would occur in addition to likely missing the vblank.
884 *
885 * In the absence of this ioctl, userland falls back to a correct path
886 * of waiting for a vblank, then dispatching the swap on its own.
887 * Context switching to userland and back is plenty fast enough for
888 * meeting the requirements of vblank swapping.
889 */
890 return -EINVAL;
891}
892
Eric Anholtc153f452007-09-03 12:06:45 +1000893static int i915_flip_bufs(struct drm_device *dev, void *data,
894 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Eric Anholt546b0972008-09-01 16:45:29 -0700896 int ret;
897
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200898 if (drm_core_check_feature(dev, DRIVER_MODESET))
899 return -ENODEV;
900
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800901 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Eric Anholt546b0972008-09-01 16:45:29 -0700903 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Eric Anholt546b0972008-09-01 16:45:29 -0700905 mutex_lock(&dev->struct_mutex);
906 ret = i915_dispatch_flip(dev);
907 mutex_unlock(&dev->struct_mutex);
908
909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Eric Anholtc153f452007-09-03 12:06:45 +1000912static int i915_getparam(struct drm_device *dev, void *data,
913 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000916 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int value;
918
919 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000920 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000921 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Eric Anholtc153f452007-09-03 12:06:45 +1000924 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700926 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 break;
928 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200929 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100931 case I915_PARAM_LAST_DISPATCH:
932 value = READ_BREADCRUMB(dev_priv);
933 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400934 case I915_PARAM_CHIPSET_ID:
935 value = dev->pci_device;
936 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700937 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200938 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700939 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800940 case I915_PARAM_NUM_FENCES_AVAIL:
941 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
942 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200943 case I915_PARAM_HAS_OVERLAY:
944 value = dev_priv->overlay ? 1 : 0;
945 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800946 case I915_PARAM_HAS_PAGEFLIPPING:
947 value = 1;
948 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500949 case I915_PARAM_HAS_EXECBUF2:
950 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200951 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500952 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800953 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100954 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800955 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100956 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100957 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100958 break;
Xiang, Haihaoa1f2cc72013-05-28 19:22:34 -0700959 case I915_PARAM_HAS_VEBOX:
960 value = intel_ring_initialized(&dev_priv->ring[VECS]);
961 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100962 case I915_PARAM_HAS_RELAXED_FENCING:
963 value = 1;
964 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100965 case I915_PARAM_HAS_COHERENT_RINGS:
966 value = 1;
967 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000968 case I915_PARAM_HAS_EXEC_CONSTANTS:
969 value = INTEL_INFO(dev)->gen >= 4;
970 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000971 case I915_PARAM_HAS_RELAXED_DELTA:
972 value = 1;
973 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800974 case I915_PARAM_HAS_GEN7_SOL_RESET:
975 value = 1;
976 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200977 case I915_PARAM_HAS_LLC:
978 value = HAS_LLC(dev);
979 break;
Daniel Vetter777ee962012-02-15 23:50:25 +0100980 case I915_PARAM_HAS_ALIASING_PPGTT:
981 value = dev_priv->mm.aliasing_ppgtt ? 1 : 0;
982 break;
Ben Widawsky172cf152012-06-05 15:24:25 -0700983 case I915_PARAM_HAS_WAIT_TIMEOUT:
984 value = 1;
985 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +0100986 case I915_PARAM_HAS_SEMAPHORES:
987 value = i915_semaphore_is_enabled(dev);
988 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000989 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
990 value = 1;
991 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100992 case I915_PARAM_HAS_SECURE_BATCHES:
993 value = capable(CAP_SYS_ADMIN);
994 break;
Daniel Vetterb45305f2012-12-17 16:21:27 +0100995 case I915_PARAM_HAS_PINNED_BATCHES:
996 value = 1;
997 break;
Daniel Vettered5982e2013-01-17 22:23:36 +0100998 case I915_PARAM_HAS_EXEC_NO_RELOC:
999 value = 1;
1000 break;
Chris Wilsoneef90cc2013-01-08 10:53:17 +00001001 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
1002 value = 1;
1003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001005 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -05001006 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001007 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
Eric Anholtc153f452007-09-03 12:06:45 +10001010 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001012 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
1015 return 0;
1016}
1017
Eric Anholtc153f452007-09-03 12:06:45 +10001018static int i915_setparam(struct drm_device *dev, void *data,
1019 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001022 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001025 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001026 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
Eric Anholtc153f452007-09-03 12:06:45 +10001029 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
1034 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001035 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001037 case I915_SETPARAM_NUM_USED_FENCES:
1038 if (param->value > dev_priv->num_fence_regs ||
1039 param->value < 0)
1040 return -EINVAL;
1041 /* Userspace can use first N regs */
1042 dev_priv->fence_reg_start = param->value;
1043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001045 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001046 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001047 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049
1050 return 0;
1051}
1052
Eric Anholtc153f452007-09-03 12:06:45 +10001053static int i915_set_status_page(struct drm_device *dev, void *data,
1054 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001055{
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001056 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001057 drm_i915_hws_addr_t *hws = data;
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001058 struct intel_ring_buffer *ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001059
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001060 if (drm_core_check_feature(dev, DRIVER_MODESET))
1061 return -ENODEV;
1062
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001063 if (!I915_NEED_GFX_HWS(dev))
1064 return -EINVAL;
1065
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001066 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001067 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001068 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001069 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001070
Jesse Barnes79e53942008-11-07 14:24:08 -08001071 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1072 WARN(1, "tried to set status page when mode setting active\n");
1073 return 0;
1074 }
1075
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001076 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001077
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001078 ring = LP_RING(dev_priv);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001079 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001080
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001081 dev_priv->dri1.gfx_hws_cpu_addr =
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001082 ioremap_wc(dev_priv->gtt.mappable_base + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001083 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001084 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001085 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001086 DRM_ERROR("can not ioremap virtual address for"
1087 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001088 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001089 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001090
1091 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001092 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001093
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001094 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001095 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001096 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001097 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001098 return 0;
1099}
1100
Dave Airlieec2a4c32009-08-04 11:43:41 +10001101static int i915_get_bridge_dev(struct drm_device *dev)
1102{
1103 struct drm_i915_private *dev_priv = dev->dev_private;
1104
Akshay Joshi0206e352011-08-16 15:34:10 -04001105 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001106 if (!dev_priv->bridge_dev) {
1107 DRM_ERROR("bridge device not found\n");
1108 return -1;
1109 }
1110 return 0;
1111}
1112
Zhenyu Wangc48044112009-12-17 14:48:43 +08001113#define MCHBAR_I915 0x44
1114#define MCHBAR_I965 0x48
1115#define MCHBAR_SIZE (4*4096)
1116
1117#define DEVEN_REG 0x54
1118#define DEVEN_MCHBAR_EN (1 << 28)
1119
1120/* Allocate space for the MCH regs if needed, return nonzero on error */
1121static int
1122intel_alloc_mchbar_resource(struct drm_device *dev)
1123{
1124 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001125 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001126 u32 temp_lo, temp_hi = 0;
1127 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001128 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001129
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001130 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001131 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1132 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1133 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1134
1135 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1136#ifdef CONFIG_PNP
1137 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001138 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1139 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001140#endif
1141
1142 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001143 dev_priv->mch_res.name = "i915 MCHBAR";
1144 dev_priv->mch_res.flags = IORESOURCE_MEM;
1145 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1146 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001147 MCHBAR_SIZE, MCHBAR_SIZE,
1148 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001149 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001150 dev_priv->bridge_dev);
1151 if (ret) {
1152 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1153 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001154 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001155 }
1156
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001157 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001158 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1159 upper_32_bits(dev_priv->mch_res.start));
1160
1161 pci_write_config_dword(dev_priv->bridge_dev, reg,
1162 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001163 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001164}
1165
1166/* Setup MCHBAR if possible, return true if we should disable it again */
1167static void
1168intel_setup_mchbar(struct drm_device *dev)
1169{
1170 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001171 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001172 u32 temp;
1173 bool enabled;
1174
1175 dev_priv->mchbar_need_disable = false;
1176
1177 if (IS_I915G(dev) || IS_I915GM(dev)) {
1178 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1179 enabled = !!(temp & DEVEN_MCHBAR_EN);
1180 } else {
1181 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1182 enabled = temp & 1;
1183 }
1184
1185 /* If it's already enabled, don't have to do anything */
1186 if (enabled)
1187 return;
1188
1189 if (intel_alloc_mchbar_resource(dev))
1190 return;
1191
1192 dev_priv->mchbar_need_disable = true;
1193
1194 /* Space is allocated or reserved, so enable it. */
1195 if (IS_I915G(dev) || IS_I915GM(dev)) {
1196 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1197 temp | DEVEN_MCHBAR_EN);
1198 } else {
1199 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1200 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1201 }
1202}
1203
1204static void
1205intel_teardown_mchbar(struct drm_device *dev)
1206{
1207 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001208 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001209 u32 temp;
1210
1211 if (dev_priv->mchbar_need_disable) {
1212 if (IS_I915G(dev) || IS_I915GM(dev)) {
1213 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1214 temp &= ~DEVEN_MCHBAR_EN;
1215 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1216 } else {
1217 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1218 temp &= ~1;
1219 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1220 }
1221 }
1222
1223 if (dev_priv->mch_res.start)
1224 release_resource(&dev_priv->mch_res);
1225}
1226
Dave Airlie28d52042009-09-21 14:33:58 +10001227/* true = enable decode, false = disable decoder */
1228static unsigned int i915_vga_set_decode(void *cookie, bool state)
1229{
1230 struct drm_device *dev = cookie;
1231
1232 intel_modeset_vga_set_state(dev, state);
1233 if (state)
1234 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1235 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1236 else
1237 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1238}
1239
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001240static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1241{
1242 struct drm_device *dev = pci_get_drvdata(pdev);
1243 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1244 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001245 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001246 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001247 /* i915 resume handler doesn't set to D0 */
1248 pci_set_power_state(dev->pdev, PCI_D0);
1249 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001250 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001251 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001252 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001253 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001254 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001255 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001256 }
1257}
1258
1259static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1260{
1261 struct drm_device *dev = pci_get_drvdata(pdev);
1262 bool can_switch;
1263
1264 spin_lock(&dev->count_lock);
1265 can_switch = (dev->open_count == 0);
1266 spin_unlock(&dev->count_lock);
1267 return can_switch;
1268}
1269
Takashi Iwai26ec6852012-05-11 07:51:17 +02001270static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1271 .set_gpu_state = i915_switcheroo_set_state,
1272 .reprobe = NULL,
1273 .can_switch = i915_switcheroo_can_switch,
1274};
1275
Chris Wilson2c7111d2011-03-29 10:40:27 +01001276static int i915_load_modeset_init(struct drm_device *dev)
1277{
1278 struct drm_i915_private *dev_priv = dev->dev_private;
1279 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001280
Bryan Freed6d139a82010-10-14 09:14:51 +01001281 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001282 if (ret)
1283 DRM_INFO("failed to find VBIOS tables\n");
1284
Chris Wilson934f992c2011-01-20 13:09:12 +00001285 /* If we have > 1 VGA cards, then we need to arbitrate access
1286 * to the common VGA resources.
1287 *
1288 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1289 * then we do not take part in VGA arbitration and the
1290 * vga_client_register() fails with -ENODEV.
1291 */
Dave Airlie28d52042009-09-21 14:33:58 +10001292 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
Chris Wilson934f992c2011-01-20 13:09:12 +00001293 if (ret && ret != -ENODEV)
Chris Wilson2c7111d2011-03-29 10:40:27 +01001294 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001295
Jesse Barnes723bfd72010-10-07 16:01:13 -07001296 intel_register_dsm_handler();
1297
Takashi Iwai26ec6852012-05-11 07:51:17 +02001298 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001299 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001300 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001301
Chris Wilson9797fbf2012-04-24 15:47:39 +01001302 /* Initialise stolen first so that we may reserve preallocated
1303 * objects for the BIOS to KMS transition.
1304 */
1305 ret = i915_gem_init_stolen(dev);
1306 if (ret)
1307 goto cleanup_vga_switcheroo;
1308
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001309 ret = drm_irq_install(dev);
1310 if (ret)
1311 goto cleanup_gem_stolen;
1312
1313 /* Important: The output setup functions called by modeset_init need
1314 * working irqs for e.g. gmbus and dp aux transfers. */
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001315 intel_modeset_init(dev);
1316
Chris Wilson1070a422012-04-24 15:47:41 +01001317 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001318 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001319 goto cleanup_irq;
Chris Wilson2c7111d2011-03-29 10:40:27 +01001320
Jesse Barnes073f34d2012-11-02 11:13:59 -07001321 INIT_WORK(&dev_priv->console_resume_work, intel_console_resume);
1322
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001323 intel_modeset_gem_init(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001324
Jesse Barnes79e53942008-11-07 14:24:08 -08001325 /* Always safe in the mode setting case. */
1326 /* FIXME: do pre/post-mode set stuff in core KMS code */
1327 dev->vblank_disable_allowed = 1;
Ben Widawskye3c74752013-04-05 13:12:39 -07001328 if (INTEL_INFO(dev)->num_pipes == 0) {
1329 dev_priv->mm.suspended = 0;
1330 return 0;
1331 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001332
Chris Wilson5a793952010-06-06 10:50:03 +01001333 ret = intel_fbdev_init(dev);
1334 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001335 goto cleanup_gem;
1336
1337 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter20afbda2012-12-11 14:05:07 +01001338 intel_hpd_init(dev);
1339
1340 /*
1341 * Some ports require correctly set-up hpd registers for detection to
1342 * work properly (leading to ghost connected connector status), e.g. VGA
1343 * on gm45. Hence we can only set up the initial fbdev config after hpd
1344 * irqs are fully enabled. Now we should scan for the initial config
1345 * only once hotplug handling is enabled, but due to screwed-up locking
1346 * around kms/fbdev init we can't protect the fdbev initial config
1347 * scanning against hotplug events. Hence do this first and ignore the
1348 * tiny window where we will loose hotplug notifactions.
1349 */
1350 intel_fbdev_initial_config(dev);
1351
1352 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001353 dev_priv->enable_hotplug_processing = true;
Chris Wilson5a793952010-06-06 10:50:03 +01001354
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001355 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001356
1357 /* We're off and running w/KMS */
1358 dev_priv->mm.suspended = 0;
1359
Jesse Barnes79e53942008-11-07 14:24:08 -08001360 return 0;
1361
Chris Wilson2c7111d2011-03-29 10:40:27 +01001362cleanup_gem:
1363 mutex_lock(&dev->struct_mutex);
1364 i915_gem_cleanup_ringbuffer(dev);
Ben Widawsky55d23282013-05-25 12:26:39 -07001365 i915_gem_context_fini(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001366 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001367 i915_gem_cleanup_aliasing_ppgtt(dev);
Ben Widawskydd62eab2013-05-25 12:26:37 -07001368 drm_mm_takedown(&dev_priv->mm.gtt_space);
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001369cleanup_irq:
1370 drm_irq_uninstall(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001371cleanup_gem_stolen:
1372 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001373cleanup_vga_switcheroo:
1374 vga_switcheroo_unregister_client(dev->pdev);
1375cleanup_vga_client:
1376 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001377out:
1378 return ret;
1379}
1380
Dave Airlie7c1c2872008-11-28 14:22:24 +10001381int i915_master_create(struct drm_device *dev, struct drm_master *master)
1382{
1383 struct drm_i915_master_private *master_priv;
1384
Eric Anholt9a298b22009-03-24 12:23:04 -07001385 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001386 if (!master_priv)
1387 return -ENOMEM;
1388
1389 master->driver_priv = master_priv;
1390 return 0;
1391}
1392
1393void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1394{
1395 struct drm_i915_master_private *master_priv = master->driver_priv;
1396
1397 if (!master_priv)
1398 return;
1399
Eric Anholt9a298b22009-03-24 12:23:04 -07001400 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001401
1402 master->driver_priv = NULL;
1403}
1404
Adam Jacksone2b665c2012-03-14 11:22:10 -04001405static void
1406i915_mtrr_setup(struct drm_i915_private *dev_priv, unsigned long base,
1407 unsigned long size)
1408{
Chris Wilson23f54be2012-03-23 17:38:49 +00001409 dev_priv->mm.gtt_mtrr = -1;
1410
Adam Jackson9e984bc12012-03-14 11:22:11 -04001411#if defined(CONFIG_X86_PAT)
1412 if (cpu_has_pat)
1413 return;
1414#endif
1415
Adam Jacksone2b665c2012-03-14 11:22:10 -04001416 /* Set up a WC MTRR for non-PAT systems. This is more common than
1417 * one would think, because the kernel disables PAT on first
1418 * generation Core chips because WC PAT gets overridden by a UC
1419 * MTRR if present. Even if a UC MTRR isn't present.
1420 */
1421 dev_priv->mm.gtt_mtrr = mtrr_add(base, size, MTRR_TYPE_WRCOMB, 1);
1422 if (dev_priv->mm.gtt_mtrr < 0) {
1423 DRM_INFO("MTRR allocation failed. Graphics "
1424 "performance may suffer.\n");
1425 }
1426}
1427
Daniel Vettere1887192012-06-12 11:28:17 +02001428static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1429{
1430 struct apertures_struct *ap;
1431 struct pci_dev *pdev = dev_priv->dev->pdev;
1432 bool primary;
1433
1434 ap = alloc_apertures(1);
1435 if (!ap)
1436 return;
1437
Ben Widawskydabb7a92013-01-17 12:45:16 -08001438 ap->ranges[0].base = dev_priv->gtt.mappable_base;
Ben Widawskyf64e2922013-05-25 12:26:36 -07001439 ap->ranges[0].size = dev_priv->gtt.mappable_end;
Ben Widawsky93d18792013-01-17 12:45:17 -08001440
Daniel Vettere1887192012-06-12 11:28:17 +02001441 primary =
1442 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1443
1444 remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
1445
1446 kfree(ap);
1447}
1448
Daniel Vetterc96ea642012-08-08 22:01:51 +02001449static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1450{
1451 const struct intel_device_info *info = dev_priv->info;
1452
Damien Lespiaue2a58002013-04-23 16:38:34 +01001453#define PRINT_S(name) "%s"
1454#define SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001455#define PRINT_FLAG(name) info->name ? #name "," : ""
1456#define SEP_COMMA ,
Daniel Vetterc96ea642012-08-08 22:01:51 +02001457 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
Damien Lespiaue2a58002013-04-23 16:38:34 +01001458 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
Daniel Vetterc96ea642012-08-08 22:01:51 +02001459 info->gen,
1460 dev_priv->dev->pdev->device,
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001461 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
Damien Lespiaue2a58002013-04-23 16:38:34 +01001462#undef PRINT_S
1463#undef SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001464#undef PRINT_FLAG
1465#undef SEP_COMMA
Daniel Vetterc96ea642012-08-08 22:01:51 +02001466}
1467
Eric Anholt63ee41d2010-12-20 18:40:06 -08001468/**
Paulo Zanoni02bcca02013-02-19 16:13:35 -03001469 * intel_early_sanitize_regs - clean up BIOS state
1470 * @dev: DRM device
1471 *
1472 * This function must be called before we do any I915_READ or I915_WRITE. Its
1473 * purpose is to clean up any state left by the BIOS that may affect us when
1474 * reading and/or writing registers.
1475 */
1476static void intel_early_sanitize_regs(struct drm_device *dev)
1477{
1478 struct drm_i915_private *dev_priv = dev->dev_private;
1479
Damien Lespiaue76ebff2013-04-22 18:40:40 +01001480 if (HAS_FPGA_DBG_UNCLAIMED(dev))
Paulo Zanoni02bcca02013-02-19 16:13:35 -03001481 I915_WRITE_NOTRACE(FPGA_DBG, FPGA_DBG_RM_NOCLAIM);
1482}
1483
1484/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001485 * i915_driver_load - setup chip and create an initial config
1486 * @dev: DRM device
1487 * @flags: startup flags
1488 *
1489 * The driver load routine has to do several things:
1490 * - drive output discovery via intel_modeset_init()
1491 * - initialize the memory manager
1492 * - allocate initial config memory
1493 * - setup the DRM framebuffer with the allocated memory
1494 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001495int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001496{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001497 struct drm_i915_private *dev_priv;
Daniel Vetter26394d92012-03-26 21:33:18 +02001498 struct intel_device_info *info;
Chris Wilson934d6082012-09-14 11:57:46 +01001499 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001500 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001501
Daniel Vetter26394d92012-03-26 21:33:18 +02001502 info = (struct intel_device_info *) flags;
1503
1504 /* Refuse to load on gen6+ without kms enabled. */
1505 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET))
1506 return -ENODEV;
1507
Dave Airlie22eae942005-11-10 22:16:34 +11001508 /* i915 has 4 more counters */
1509 dev->counters += 4;
1510 dev->types[6] = _DRM_STAT_IRQ;
1511 dev->types[7] = _DRM_STAT_PRIMARY;
1512 dev->types[8] = _DRM_STAT_SECONDARY;
1513 dev->types[9] = _DRM_STAT_DMA;
1514
Eric Anholt9a298b22009-03-24 12:23:04 -07001515 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001516 if (dev_priv == NULL)
1517 return -ENOMEM;
1518
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001519 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001520 dev_priv->dev = dev;
Daniel Vetter26394d92012-03-26 21:33:18 +02001521 dev_priv->info = info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001522
Daniel Vetterc96ea642012-08-08 22:01:51 +02001523 i915_dump_device_info(dev_priv);
1524
Dave Airlieec2a4c32009-08-04 11:43:41 +10001525 if (i915_get_bridge_dev(dev)) {
1526 ret = -EIO;
1527 goto free_priv;
1528 }
1529
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001530 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1531 /* 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);
1544 if (!dev_priv->regs) {
1545 DRM_ERROR("failed to map registers\n");
1546 ret = -EIO;
1547 goto put_bridge;
1548 }
1549
1550 intel_early_sanitize_regs(dev);
1551
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001552 ret = i915_gem_gtt_init(dev);
1553 if (ret)
Daniel Vettere1887192012-06-12 11:28:17 +02001554 goto put_bridge;
Daniel Vettere1887192012-06-12 11:28:17 +02001555
Chris Wilson16233922012-10-26 12:06:41 +01001556 if (drm_core_check_feature(dev, DRIVER_MODESET))
1557 i915_kick_out_firmware_fb(dev_priv);
Daniel Vettere1887192012-06-12 11:28:17 +02001558
Dave Airlie466e69b2011-12-19 11:15:29 +00001559 pci_set_master(dev->pdev);
1560
Daniel Vetter9f82d232010-08-30 21:25:23 +02001561 /* overlay on gen2 is broken and can't address above 1G */
1562 if (IS_GEN2(dev))
1563 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1564
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001565 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1566 * using 32bit addressing, overwriting memory if HWS is located
1567 * above 4GB.
1568 *
1569 * The documentation also mentions an issue with undefined
1570 * behaviour if any general state is accessed within a page above 4GB,
1571 * which also needs to be handled carefully.
1572 */
1573 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1574 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1575
Ben Widawsky93d18792013-01-17 12:45:17 -08001576 aperture_size = dev_priv->gtt.mappable_end;
Chris Wilson71e93392010-10-27 18:46:52 +01001577
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001578 dev_priv->gtt.mappable =
1579 io_mapping_create_wc(dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001580 aperture_size);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001581 if (dev_priv->gtt.mappable == NULL) {
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001582 ret = -EIO;
Daniel Vettere1887192012-06-12 11:28:17 +02001583 goto out_rmmap;
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001584 }
1585
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001586 i915_mtrr_setup(dev_priv, dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001587 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001588
Chris Wilsone642abb2010-09-09 12:46:34 +01001589 /* The i915 workqueue is primarily used for batched retirement of
1590 * requests (and thus managing bo) once the task has been completed
1591 * by the GPU. i915_gem_retire_requests() is called directly when we
1592 * need high-priority retirement, such as waiting for an explicit
1593 * bo.
1594 *
1595 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001596 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001597 *
1598 * All tasks on the workqueue are expected to acquire the dev mutex
1599 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001600 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001601 */
Tejun Heo53621862012-08-22 16:40:57 -07001602 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001603 if (dev_priv->wq == NULL) {
1604 DRM_ERROR("Failed to create our workqueue.\n");
1605 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001606 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001607 }
1608
Paulo Zanoni45e6e3a2012-07-03 15:57:32 -03001609 /* This must be called before any calls to HAS_PCH_* */
1610 intel_detect_pch(dev);
1611
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001612 intel_irq_init(dev);
Chris Wilson990bbda2012-07-02 11:51:02 -03001613 intel_gt_init(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001614
Zhenyu Wangc48044112009-12-17 14:48:43 +08001615 /* Try to make sure MCHBAR is enabled before poking at it */
1616 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001617 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001618 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001619
Bryan Freed6d139a82010-10-14 09:14:51 +01001620 intel_setup_bios(dev);
1621
Eric Anholt673a3942008-07-30 12:06:12 -07001622 i915_gem_load(dev);
1623
Eric Anholted4cb412008-07-29 12:10:39 -07001624 /* On the 945G/GM, the chipset reports the MSI capability on the
1625 * integrated graphics even though the support isn't actually there
1626 * according to the published specs. It doesn't appear to function
1627 * correctly in testing on 945G.
1628 * This may be a side effect of MSI having been made available for PEG
1629 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001630 *
1631 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001632 * be lost or delayed, but we use them anyways to avoid
1633 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001634 */
Keith Packardb60678a2008-12-08 11:12:28 -08001635 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001636 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001637
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001638 spin_lock_init(&dev_priv->irq_lock);
Daniel Vetter99584db2012-11-14 17:14:04 +01001639 spin_lock_init(&dev_priv->gpu_error.lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02001640 spin_lock_init(&dev_priv->rps.lock);
Jani Nikula8ba2d182013-04-12 15:18:37 +03001641 spin_lock_init(&dev_priv->backlight.lock);
Daniel Vetter09153002012-12-12 14:06:44 +01001642 mutex_init(&dev_priv->dpio_lock);
Eric Anholted4cb412008-07-29 12:10:39 -07001643
Jesse Barnes4fc688c2012-11-02 11:14:01 -07001644 mutex_init(&dev_priv->rps.hw_lock);
Zhang Ruib8efb172013-02-05 15:41:53 +08001645 mutex_init(&dev_priv->modeset_restore_lock);
Jesse Barnes4fc688c2012-11-02 11:14:01 -07001646
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001647 dev_priv->num_plane = 1;
1648 if (IS_VALLEYVIEW(dev))
1649 dev_priv->num_plane = 2;
1650
Ben Widawskye3c74752013-04-05 13:12:39 -07001651 if (INTEL_INFO(dev)->num_pipes) {
1652 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1653 if (ret)
1654 goto out_gem_unload;
1655 }
Keith Packard52440212008-11-18 09:30:25 -08001656
Ben Gamari11ed50e2009-09-14 17:48:45 -04001657 /* Start out suspended */
1658 dev_priv->mm.suspended = 1;
1659
Jesse Barnes79e53942008-11-07 14:24:08 -08001660 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001661 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001662 if (ret < 0) {
1663 DRM_ERROR("failed to init modeset\n");
Chris Wilson56e2ea32010-11-08 17:10:29 +00001664 goto out_gem_unload;
Jesse Barnes79e53942008-11-07 14:24:08 -08001665 }
1666 }
1667
Ben Widawsky0136db52012-04-10 21:17:01 -07001668 i915_setup_sysfs(dev);
1669
Ben Widawskye3c74752013-04-05 13:12:39 -07001670 if (INTEL_INFO(dev)->num_pipes) {
1671 /* Must be done after probing outputs */
1672 intel_opregion_init(dev);
1673 acpi_video_register();
1674 }
Matthew Garrett74a365b2009-03-19 21:35:39 +00001675
Daniel Vettereb48eb02012-04-26 23:28:12 +02001676 if (IS_GEN5(dev))
1677 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001678
Jesse Barnes79e53942008-11-07 14:24:08 -08001679 return 0;
1680
Chris Wilson56e2ea32010-11-08 17:10:29 +00001681out_gem_unload:
Keith Packarda7b85d22011-07-10 13:12:17 -07001682 if (dev_priv->mm.inactive_shrinker.shrink)
1683 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1684
Chris Wilson56e2ea32010-11-08 17:10:29 +00001685 if (dev->pdev->msi_enabled)
1686 pci_disable_msi(dev->pdev);
1687
1688 intel_teardown_gmbus(dev);
1689 intel_teardown_mchbar(dev);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001690 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001691out_mtrrfree:
1692 if (dev_priv->mm.gtt_mtrr >= 0) {
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001693 mtrr_del(dev_priv->mm.gtt_mtrr,
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001694 dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001695 aperture_size);
Keith Packarda7b85d22011-07-10 13:12:17 -07001696 dev_priv->mm.gtt_mtrr = -1;
1697 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001698 io_mapping_free(dev_priv->gtt.mappable);
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001699 dev_priv->gtt.gtt_remove(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001700out_rmmap:
Chris Wilson6dda5692010-10-29 21:02:18 +01001701 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001702put_bridge:
1703 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001704free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001705 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001706 return ret;
1707}
1708
1709int i915_driver_unload(struct drm_device *dev)
1710{
1711 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001712 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001713
Daniel Vettereb48eb02012-04-26 23:28:12 +02001714 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001715
Ben Widawsky0136db52012-04-10 21:17:01 -07001716 i915_teardown_sysfs(dev);
1717
Chris Wilson17250b72010-10-28 12:51:39 +01001718 if (dev_priv->mm.inactive_shrinker.shrink)
1719 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1720
Daniel Vetterc911fc12010-08-20 21:23:20 +02001721 mutex_lock(&dev->struct_mutex);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001722 ret = i915_gpu_idle(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02001723 if (ret)
1724 DRM_ERROR("failed to idle hardware: %d\n", ret);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001725 i915_gem_retire_requests(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02001726 mutex_unlock(&dev->struct_mutex);
1727
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001728 /* Cancel the retire work handler, which should be idle now. */
1729 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
1730
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001731 io_mapping_free(dev_priv->gtt.mappable);
Eric Anholtab657db12009-01-23 12:57:47 -08001732 if (dev_priv->mm.gtt_mtrr >= 0) {
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001733 mtrr_del(dev_priv->mm.gtt_mtrr,
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001734 dev_priv->gtt.mappable_base,
Ben Widawsky93d18792013-01-17 12:45:17 -08001735 dev_priv->gtt.mappable_end);
Eric Anholtab657db12009-01-23 12:57:47 -08001736 dev_priv->mm.gtt_mtrr = -1;
1737 }
1738
Chris Wilson44834a62010-08-19 16:09:23 +01001739 acpi_video_unregister();
1740
Jesse Barnes79e53942008-11-07 14:24:08 -08001741 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01001742 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001743 intel_modeset_cleanup(dev);
Jesse Barnes073f34d2012-11-02 11:13:59 -07001744 cancel_work_sync(&dev_priv->console_resume_work);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001745
Zhao Yakui6363ee62009-11-24 09:48:44 +08001746 /*
1747 * free the memory space allocated for the child device
1748 * config parsed from VBT
1749 */
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03001750 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1751 kfree(dev_priv->vbt.child_dev);
1752 dev_priv->vbt.child_dev = NULL;
1753 dev_priv->vbt.child_dev_num = 0;
Zhao Yakui6363ee62009-11-24 09:48:44 +08001754 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001755
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001756 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001757 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001758 }
1759
Daniel Vettera8b48992010-08-20 21:25:11 +02001760 /* Free error state after interrupts are fully disabled. */
Daniel Vetter99584db2012-11-14 17:14:04 +01001761 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
1762 cancel_work_sync(&dev_priv->gpu_error.work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001763 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001764
Eric Anholted4cb412008-07-29 12:10:39 -07001765 if (dev->pdev->msi_enabled)
1766 pci_disable_msi(dev->pdev);
1767
Chris Wilson44834a62010-08-19 16:09:23 +01001768 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001769
Jesse Barnes79e53942008-11-07 14:24:08 -08001770 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001771 /* Flush any outstanding unpin_work. */
1772 flush_workqueue(dev_priv->wq);
1773
Jesse Barnes79e53942008-11-07 14:24:08 -08001774 mutex_lock(&dev->struct_mutex);
Hugh Dickinsecbec532011-06-27 16:18:20 -07001775 i915_gem_free_all_phys_object(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001776 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001777 i915_gem_context_fini(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001778 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001779 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001780 i915_gem_cleanup_stolen(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001781
1782 if (!I915_NEED_GFX_HWS(dev))
1783 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001784 }
1785
Ben Widawskydd62eab2013-05-25 12:26:37 -07001786 drm_mm_takedown(&dev_priv->mm.gtt_space);
Daniel Vetter701394c2010-10-10 18:54:08 +01001787 if (dev_priv->regs != NULL)
Chris Wilson6dda5692010-10-29 21:02:18 +01001788 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vetter701394c2010-10-10 18:54:08 +01001789
Chris Wilsonf899fc62010-07-20 15:44:45 -07001790 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001791 intel_teardown_mchbar(dev);
1792
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001793 destroy_workqueue(dev_priv->wq);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001794 pm_qos_remove_request(&dev_priv->pm_qos);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001795
Imre Deak6640aab2013-05-22 17:47:13 +03001796 dev_priv->gtt.gtt_remove(dev);
1797
Chris Wilson42dcedd2012-11-15 11:32:30 +00001798 if (dev_priv->slab)
1799 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001800
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001801 pci_dev_put(dev_priv->bridge_dev);
Dave Airlie22eae942005-11-10 22:16:34 +11001802 kfree(dev->dev_private);
1803
1804 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001805}
1806
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001807int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001808{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001809 struct drm_i915_file_private *file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001810
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001811 DRM_DEBUG_DRIVER("\n");
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001812 file_priv = kmalloc(sizeof(*file_priv), GFP_KERNEL);
1813 if (!file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07001814 return -ENOMEM;
1815
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001816 file->driver_priv = file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001817
Chris Wilson1c255952010-09-26 11:03:27 +01001818 spin_lock_init(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001819 INIT_LIST_HEAD(&file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001820
Daniel Vetterdf12c6d2012-06-19 16:52:30 +02001821 idr_init(&file_priv->context_idr);
Ben Widawsky254f9652012-06-04 14:42:42 -07001822
Eric Anholt673a3942008-07-30 12:06:12 -07001823 return 0;
1824}
1825
Jesse Barnes79e53942008-11-07 14:24:08 -08001826/**
1827 * i915_driver_lastclose - clean up after all DRM clients have exited
1828 * @dev: DRM device
1829 *
1830 * Take care of cleaning up after all DRM clients have exited. In the
1831 * mode setting case, we want to restore the kernel's initial mode (just
1832 * in case the last client left us in a bad state).
1833 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001834 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001835 * and DMA structures, since the kernel won't be using them, and clea
1836 * up any GEM state.
1837 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001838void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001840 drm_i915_private_t *dev_priv = dev->dev_private;
1841
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001842 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1843 * goes right around and calls lastclose. Check for this and don't clean
1844 * up anything. */
1845 if (!dev_priv)
1846 return;
1847
1848 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +01001849 intel_fb_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001850 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001851 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001852 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001853
Eric Anholt673a3942008-07-30 12:06:12 -07001854 i915_gem_lastclose(dev);
1855
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001856 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Eric Anholt6c340ea2007-08-25 20:23:09 +10001859void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Ben Widawsky254f9652012-06-04 14:42:42 -07001861 i915_gem_context_close(dev, file_priv);
Eric Anholtb9624422009-06-03 07:27:35 +00001862 i915_gem_release(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001865void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001866{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001867 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001868
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001869 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001870}
1871
Eric Anholtc153f452007-09-03 12:06:45 +10001872struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001873 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1874 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1875 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1876 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1877 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1878 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1879 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH),
1880 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001881 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1882 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1883 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001884 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001885 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02001886 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001887 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
1888 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1889 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1890 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1891 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
1892 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
1893 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1894 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1895 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
Ben Widawsky199adf42012-09-21 17:01:20 -07001896 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED),
1897 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001898 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
1899 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1900 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1901 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
1902 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
1903 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
1904 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
1905 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
1906 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
1907 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
1908 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
1909 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
1910 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
1911 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
1912 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
1913 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1914 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08001915 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1916 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 -07001917 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED),
Ben Widawsky84624812012-06-04 14:42:54 -07001918 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED),
1919 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED),
Ben Widawskyc0c7bab2012-07-12 11:01:05 -07001920 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10001921};
1922
1923int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001924
Daniel Vetter9021f282012-03-26 09:45:41 +02001925/*
1926 * This is really ugly: Because old userspace abused the linux agp interface to
1927 * manage the gtt, we need to claim that all intel devices are agp. For
1928 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10001929 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001930int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001931{
1932 return 1;
1933}