blob: 14271aab72bbc19303395ae711caae5b01cb4539 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "drmP.h"
32#include "drm.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include "drm_crtc_helper.h"
Dave Airlie785b93e2009-08-28 15:46:53 +100034#include "drm_fb_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "i915_drm.h"
37#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010038#include "i915_trace.h"
Jordan Crousedcdb1672010-05-27 13:40:25 -060039#include <linux/pci.h>
Dave Airlie28d52042009-09-21 14:33:58 +100040#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080041#include <linux/acpi.h>
42#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100043#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Chris Wilson44834a62010-08-19 16:09:23 +010045#include <acpi/video.h>
Adam Jackson9e984bc12012-03-14 11:22:11 -040046#include <asm/pat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Daniel Vetter09422b22012-04-26 23:28:10 +020048#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
49
50#define BEGIN_LP_RING(n) \
51 intel_ring_begin(LP_RING(dev_priv), (n))
52
53#define OUT_RING(x) \
54 intel_ring_emit(LP_RING(dev_priv), x)
55
56#define ADVANCE_LP_RING() \
57 intel_ring_advance(LP_RING(dev_priv))
58
59/**
60 * Lock test for when it's just for synchronization of ring access.
61 *
62 * In that case, we don't need to do it when GEM is initialized as nobody else
63 * has access to the ring.
64 */
65#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
66 if (LP_RING(dev->dev_private)->obj == NULL) \
67 LOCK_TEST_WITH_RETURN(dev, file); \
68} while (0)
69
Daniel Vetter316d3882012-04-26 23:28:15 +020070static inline u32
71intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
72{
73 if (I915_NEED_GFX_HWS(dev_priv->dev))
74 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
75 else
76 return intel_read_status_page(LP_RING(dev_priv), reg);
77}
78
79#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
Daniel Vetter09422b22012-04-26 23:28:10 +020080#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
81#define I915_BREADCRUMB_INDEX 0x21
82
Daniel Vetterd05c6172012-04-26 23:28:09 +020083void i915_update_dri1_breadcrumb(struct drm_device *dev)
84{
85 drm_i915_private_t *dev_priv = dev->dev_private;
86 struct drm_i915_master_private *master_priv;
87
88 if (dev->primary->master) {
89 master_priv = dev->primary->master->driver_priv;
90 if (master_priv->sarea_priv)
91 master_priv->sarea_priv->last_dispatch =
92 READ_BREADCRUMB(dev_priv);
93 }
94}
95
Chris Wilson4cbf74c2011-02-25 22:26:23 +000096static void i915_write_hws_pga(struct drm_device *dev)
97{
98 drm_i915_private_t *dev_priv = dev->dev_private;
99 u32 addr;
100
101 addr = dev_priv->status_page_dmah->busaddr;
102 if (INTEL_INFO(dev)->gen >= 4)
103 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
104 I915_WRITE(HWS_PGA, addr);
105}
106
Keith Packard398c9cb2008-07-30 13:03:43 -0700107/**
108 * Sets up the hardware status page for devices that need a physical address
109 * in the register.
110 */
Eric Anholt3043c602008-10-02 12:24:47 -0700111static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700112{
113 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000114
Keith Packard398c9cb2008-07-30 13:03:43 -0700115 /* Program Hardware Status Page */
116 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800117 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -0700118
119 if (!dev_priv->status_page_dmah) {
120 DRM_ERROR("Can not allocate hardware status page\n");
121 return -ENOMEM;
122 }
Keith Packard398c9cb2008-07-30 13:03:43 -0700123
Keith Packardf3234702011-07-22 10:44:39 -0700124 memset_io((void __force __iomem *)dev_priv->status_page_dmah->vaddr,
125 0, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -0700126
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000127 i915_write_hws_pga(dev);
Zhenyu Wang9b974cc2010-01-05 11:25:06 +0800128
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800129 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700130 return 0;
131}
132
133/**
134 * Frees the hardware status page, whether it's a physical address or a virtual
135 * address set up by the X Server.
136 */
Eric Anholt3043c602008-10-02 12:24:47 -0700137static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700138{
139 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000140 struct intel_ring_buffer *ring = LP_RING(dev_priv);
141
Keith Packard398c9cb2008-07-30 13:03:43 -0700142 if (dev_priv->status_page_dmah) {
143 drm_pci_free(dev, dev_priv->status_page_dmah);
144 dev_priv->status_page_dmah = NULL;
145 }
146
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000147 if (ring->status_page.gfx_addr) {
148 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200149 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700150 }
151
152 /* Need to rewrite hardware status page */
153 I915_WRITE(HWS_PGA, 0x1ffff000);
154}
155
Dave Airlie84b1fd12007-07-11 15:53:27 +1000156void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000159 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000160 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jesse Barnes79e53942008-11-07 14:24:08 -0800162 /*
163 * We should never lose context on the ring with modesetting
164 * as we don't expose it to userspace
165 */
166 if (drm_core_check_feature(dev, DRIVER_MODESET))
167 return;
168
Chris Wilson8168bd42010-11-11 17:54:52 +0000169 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
170 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 ring->space = ring->head - (ring->tail + 8);
172 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800173 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Dave Airlie7c1c2872008-11-28 14:22:24 +1000175 if (!dev->primary->master)
176 return;
177
178 master_priv = dev->primary->master->driver_priv;
179 if (ring->head == ring->tail && master_priv->sarea_priv)
180 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Dave Airlie84b1fd12007-07-11 15:53:27 +1000183static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000185 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000186 int i;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* Make sure interrupts are disabled here because the uninstall ioctl
189 * may not have been called from userspace and after dev_private
190 * is freed, it's too late.
191 */
Eric Anholted4cb412008-07-29 12:10:39 -0700192 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200195 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000196 for (i = 0; i < I915_NUM_RINGS; i++)
197 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200198 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Keith Packard398c9cb2008-07-30 13:03:43 -0700200 /* Clear the HWS virtual address at teardown */
201 if (I915_NEED_GFX_HWS(dev))
202 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 return 0;
205}
206
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000207static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000209 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000210 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000211 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Dave Airlie3a03ac12009-01-11 09:03:49 +1000213 master_priv->sarea = drm_getsarea(dev);
214 if (master_priv->sarea) {
215 master_priv->sarea_priv = (drm_i915_sarea_t *)
216 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
217 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800218 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000219 }
220
Eric Anholt673a3942008-07-30 12:06:12 -0700221 if (init->ring_size != 0) {
Chris Wilsone8616b62011-01-20 09:57:11 +0000222 if (LP_RING(dev_priv)->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700223 i915_dma_cleanup(dev);
224 DRM_ERROR("Client tried to initialize ringbuffer in "
225 "GEM mode\n");
226 return -EINVAL;
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Chris Wilsone8616b62011-01-20 09:57:11 +0000229 ret = intel_render_ring_init_dri(dev,
230 init->ring_start,
231 init->ring_size);
232 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700233 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000234 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200238 dev_priv->dri1.cpp = init->cpp;
239 dev_priv->dri1.back_offset = init->back_offset;
240 dev_priv->dri1.front_offset = init->front_offset;
241 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000242 if (master_priv->sarea_priv)
243 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Allow hardware batchbuffers unless told otherwise.
246 */
Daniel Vetter87813422012-05-02 11:49:32 +0200247 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250}
251
Dave Airlie84b1fd12007-07-11 15:53:27 +1000252static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000255 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800257 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Daniel Vetter4225d0f2012-04-26 23:28:16 +0200259 if (ring->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 DRM_ERROR("can not ioremap virtual address for"
261 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000262 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
265 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800266 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800270 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800271 ring->status_page.page_addr);
272 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100273 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000274 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000275 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800276
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800277 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 return 0;
280}
281
Eric Anholtc153f452007-09-03 12:06:45 +1000282static int i915_dma_init(struct drm_device *dev, void *data,
283 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Eric Anholtc153f452007-09-03 12:06:45 +1000285 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 int retcode = 0;
287
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200288 if (drm_core_check_feature(dev, DRIVER_MODESET))
289 return -ENODEV;
290
Eric Anholtc153f452007-09-03 12:06:45 +1000291 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000293 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
295 case I915_CLEANUP_DMA:
296 retcode = i915_dma_cleanup(dev);
297 break;
298 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100299 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000302 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 }
305
306 return retcode;
307}
308
309/* Implement basically the same security restrictions as hardware does
310 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
311 *
312 * Most of the calculations below involve calculating the size of a
313 * particular instruction. It's important to get the size right as
314 * that tells us where the next instruction to check is. Any illegal
315 * instruction detected will be given a size of zero, which is a
316 * signal to abort the rest of the buffer.
317 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100318static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 switch (((cmd >> 29) & 0x7)) {
321 case 0x0:
322 switch ((cmd >> 23) & 0x3f) {
323 case 0x0:
324 return 1; /* MI_NOOP */
325 case 0x4:
326 return 1; /* MI_FLUSH */
327 default:
328 return 0; /* disallow everything else */
329 }
330 break;
331 case 0x1:
332 return 0; /* reserved */
333 case 0x2:
334 return (cmd & 0xff) + 2; /* 2d commands */
335 case 0x3:
336 if (((cmd >> 24) & 0x1f) <= 0x18)
337 return 1;
338
339 switch ((cmd >> 24) & 0x1f) {
340 case 0x1c:
341 return 1;
342 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 case 0x3:
345 return (cmd & 0x1f) + 2;
346 case 0x4:
347 return (cmd & 0xf) + 2;
348 default:
349 return (cmd & 0xffff) + 2;
350 }
351 case 0x1e:
352 if (cmd & (1 << 23))
353 return (cmd & 0xffff) + 1;
354 else
355 return 1;
356 case 0x1f:
357 if ((cmd & (1 << 23)) == 0) /* inline vertices */
358 return (cmd & 0x1ffff) + 2;
359 else if (cmd & (1 << 17)) /* indirect random */
360 if ((cmd & 0xffff) == 0)
361 return 0; /* unknown length, too hard */
362 else
363 return (((cmd & 0xffff) + 1) / 2) + 1;
364 else
365 return 2; /* indirect sequential */
366 default:
367 return 0;
368 }
369 default:
370 return 0;
371 }
372
373 return 0;
374}
375
Eric Anholt201361a2009-03-11 12:30:04 -0700376static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100379 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000381 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000382 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100385 int sz = validate_cmd(buffer[i]);
386 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000387 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100388 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100391 ret = BEGIN_LP_RING((dwords+1)&~1);
392 if (ret)
393 return ret;
394
395 for (i = 0; i < dwords; i++)
396 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100397 if (dwords & 1)
398 OUT_RING(0);
399
400 ADVANCE_LP_RING();
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403}
404
Eric Anholt673a3942008-07-30 12:06:12 -0700405int
406i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000407 struct drm_clip_rect *box,
408 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100410 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100411 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000413 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
414 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000416 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000417 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100420 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100421 ret = BEGIN_LP_RING(4);
422 if (ret)
423 return ret;
424
Alan Hourihanec29b6692006-08-12 16:29:24 +1000425 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000426 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
427 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000428 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000429 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100430 ret = BEGIN_LP_RING(6);
431 if (ret)
432 return ret;
433
Alan Hourihanec29b6692006-08-12 16:29:24 +1000434 OUT_RING(GFX_OP_DRAWRECT_INFO);
435 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000436 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
437 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000438 OUT_RING(DR4);
439 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000440 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100441 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 return 0;
444}
445
Alan Hourihanec29b6692006-08-12 16:29:24 +1000446/* XXX: Emitting the counter should really be moved to part of the IRQ
447 * emit. For now, do it in both places:
448 */
449
Dave Airlie84b1fd12007-07-11 15:53:27 +1000450static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100451{
452 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000453 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100454
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400455 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000456 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400457 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000458 if (master_priv->sarea_priv)
459 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100460
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100461 if (BEGIN_LP_RING(4) == 0) {
462 OUT_RING(MI_STORE_DWORD_INDEX);
463 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
464 OUT_RING(dev_priv->counter);
465 OUT_RING(0);
466 ADVANCE_LP_RING();
467 }
Dave Airliede227f52006-01-25 15:31:43 +1100468}
469
Dave Airlie84b1fd12007-07-11 15:53:27 +1000470static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700471 drm_i915_cmdbuffer_t *cmd,
472 struct drm_clip_rect *cliprects,
473 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 int nbox = cmd->num_cliprects;
476 int i = 0, count, ret;
477
478 if (cmd->sz & 0x3) {
479 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000480 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
483 i915_kernel_lost_context(dev);
484
485 count = nbox ? nbox : 1;
486
487 for (i = 0; i < count; i++) {
488 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000489 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 cmd->DR1, cmd->DR4);
491 if (ret)
492 return ret;
493 }
494
Eric Anholt201361a2009-03-11 12:30:04 -0700495 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (ret)
497 return ret;
498 }
499
Dave Airliede227f52006-01-25 15:31:43 +1100500 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502}
503
Dave Airlie84b1fd12007-07-11 15:53:27 +1000504static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700505 drm_i915_batchbuffer_t * batch,
506 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100508 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100510 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 if ((batch->start | batch->used) & 0x7) {
513 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000514 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
517 i915_kernel_lost_context(dev);
518
519 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 for (i = 0; i < count; i++) {
521 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000522 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100523 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (ret)
525 return ret;
526 }
527
Keith Packard0790d5e2008-07-30 12:28:47 -0700528 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100529 ret = BEGIN_LP_RING(2);
530 if (ret)
531 return ret;
532
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100533 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000534 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
535 OUT_RING(batch->start);
536 } else {
537 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
538 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100541 ret = BEGIN_LP_RING(4);
542 if (ret)
543 return ret;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 OUT_RING(MI_BATCH_BUFFER);
546 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
547 OUT_RING(batch->start + batch->used - 4);
548 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100550 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
Zou Nan hai1cafd342010-06-25 13:40:24 +0800553
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100554 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100555 if (BEGIN_LP_RING(2) == 0) {
556 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
557 OUT_RING(MI_NOOP);
558 ADVANCE_LP_RING();
559 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100562 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564}
565
Dave Airlieaf6061a2008-05-07 12:15:39 +1000566static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000569 struct drm_i915_master_private *master_priv =
570 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100571 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Dave Airlie7c1c2872008-11-28 14:22:24 +1000573 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400574 return -EINVAL;
575
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800576 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800577 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200578 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800579 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Dave Airlieaf6061a2008-05-07 12:15:39 +1000581 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100583 ret = BEGIN_LP_RING(10);
584 if (ret)
585 return ret;
586
Jesse Barnes585fb112008-07-29 11:54:06 -0700587 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000588 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Dave Airlieaf6061a2008-05-07 12:15:39 +1000590 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
591 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200592 if (dev_priv->dri1.current_page == 0) {
593 OUT_RING(dev_priv->dri1.back_offset);
594 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000595 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200596 OUT_RING(dev_priv->dri1.front_offset);
597 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000598 }
599 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000600
Dave Airlieaf6061a2008-05-07 12:15:39 +1000601 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
602 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100603
Dave Airlieaf6061a2008-05-07 12:15:39 +1000604 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000605
Dave Airlie7c1c2872008-11-28 14:22:24 +1000606 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000607
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100608 if (BEGIN_LP_RING(4) == 0) {
609 OUT_RING(MI_STORE_DWORD_INDEX);
610 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
611 OUT_RING(dev_priv->counter);
612 OUT_RING(0);
613 ADVANCE_LP_RING();
614 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000615
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200616 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000620static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000622 struct intel_ring_buffer *ring = LP_RING(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 i915_kernel_lost_context(dev);
Ben Widawsky96f298a2011-03-19 18:14:27 -0700625 return intel_wait_ring_idle(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Eric Anholtc153f452007-09-03 12:06:45 +1000628static int i915_flush_ioctl(struct drm_device *dev, void *data,
629 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Eric Anholt546b0972008-09-01 16:45:29 -0700631 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200633 if (drm_core_check_feature(dev, DRIVER_MODESET))
634 return -ENODEV;
635
Eric Anholt546b0972008-09-01 16:45:29 -0700636 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
637
638 mutex_lock(&dev->struct_mutex);
639 ret = i915_quiescent(dev);
640 mutex_unlock(&dev->struct_mutex);
641
642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Eric Anholtc153f452007-09-03 12:06:45 +1000645static int i915_batchbuffer(struct drm_device *dev, void *data,
646 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000649 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000651 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000652 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700654 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200656 if (drm_core_check_feature(dev, DRIVER_MODESET))
657 return -ENODEV;
658
Daniel Vetter87813422012-05-02 11:49:32 +0200659 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000661 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800664 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800665 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Eric Anholt546b0972008-09-01 16:45:29 -0700667 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Eric Anholt201361a2009-03-11 12:30:04 -0700669 if (batch->num_cliprects < 0)
670 return -EINVAL;
671
672 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700673 cliprects = kcalloc(batch->num_cliprects,
674 sizeof(struct drm_clip_rect),
675 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700676 if (cliprects == NULL)
677 return -ENOMEM;
678
679 ret = copy_from_user(cliprects, batch->cliprects,
680 batch->num_cliprects *
681 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200682 if (ret != 0) {
683 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700684 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200685 }
Eric Anholt201361a2009-03-11 12:30:04 -0700686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Eric Anholt546b0972008-09-01 16:45:29 -0700688 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700689 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700690 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400692 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000693 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700694
695fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700696 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return ret;
699}
700
Eric Anholtc153f452007-09-03 12:06:45 +1000701static int i915_cmdbuffer(struct drm_device *dev, void *data,
702 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000705 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000707 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000708 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700709 struct drm_clip_rect *cliprects = NULL;
710 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int ret;
712
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800713 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800714 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200716 if (drm_core_check_feature(dev, DRIVER_MODESET))
717 return -ENODEV;
718
Eric Anholt546b0972008-09-01 16:45:29 -0700719 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Eric Anholt201361a2009-03-11 12:30:04 -0700721 if (cmdbuf->num_cliprects < 0)
722 return -EINVAL;
723
Eric Anholt9a298b22009-03-24 12:23:04 -0700724 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700725 if (batch_data == NULL)
726 return -ENOMEM;
727
728 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200729 if (ret != 0) {
730 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700731 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200732 }
Eric Anholt201361a2009-03-11 12:30:04 -0700733
734 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700735 cliprects = kcalloc(cmdbuf->num_cliprects,
736 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000737 if (cliprects == NULL) {
738 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700739 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000740 }
Eric Anholt201361a2009-03-11 12:30:04 -0700741
742 ret = copy_from_user(cliprects, cmdbuf->cliprects,
743 cmdbuf->num_cliprects *
744 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200745 if (ret != 0) {
746 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700747 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
Eric Anholt546b0972008-09-01 16:45:29 -0700751 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700752 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700753 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (ret) {
755 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000756 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400759 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000760 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700761
Eric Anholt201361a2009-03-11 12:30:04 -0700762fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700763 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000764fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700765 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700766
767 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Daniel Vetter94888672012-04-26 23:28:08 +0200770static int i915_emit_irq(struct drm_device * dev)
771{
772 drm_i915_private_t *dev_priv = dev->dev_private;
773 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
774
775 i915_kernel_lost_context(dev);
776
777 DRM_DEBUG_DRIVER("\n");
778
779 dev_priv->counter++;
780 if (dev_priv->counter > 0x7FFFFFFFUL)
781 dev_priv->counter = 1;
782 if (master_priv->sarea_priv)
783 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
784
785 if (BEGIN_LP_RING(4) == 0) {
786 OUT_RING(MI_STORE_DWORD_INDEX);
787 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
788 OUT_RING(dev_priv->counter);
789 OUT_RING(MI_USER_INTERRUPT);
790 ADVANCE_LP_RING();
791 }
792
793 return dev_priv->counter;
794}
795
796static int i915_wait_irq(struct drm_device * dev, int irq_nr)
797{
798 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
799 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
800 int ret = 0;
801 struct intel_ring_buffer *ring = LP_RING(dev_priv);
802
803 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
804 READ_BREADCRUMB(dev_priv));
805
806 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
807 if (master_priv->sarea_priv)
808 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
809 return 0;
810 }
811
812 if (master_priv->sarea_priv)
813 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
814
815 if (ring->irq_get(ring)) {
816 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
817 READ_BREADCRUMB(dev_priv) >= irq_nr);
818 ring->irq_put(ring);
819 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
820 ret = -EBUSY;
821
822 if (ret == -EBUSY) {
823 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
824 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
825 }
826
827 return ret;
828}
829
830/* Needs the lock as it touches the ring.
831 */
832static int i915_irq_emit(struct drm_device *dev, void *data,
833 struct drm_file *file_priv)
834{
835 drm_i915_private_t *dev_priv = dev->dev_private;
836 drm_i915_irq_emit_t *emit = data;
837 int result;
838
839 if (drm_core_check_feature(dev, DRIVER_MODESET))
840 return -ENODEV;
841
842 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
843 DRM_ERROR("called with no initialization\n");
844 return -EINVAL;
845 }
846
847 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
848
849 mutex_lock(&dev->struct_mutex);
850 result = i915_emit_irq(dev);
851 mutex_unlock(&dev->struct_mutex);
852
853 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
854 DRM_ERROR("copy_to_user\n");
855 return -EFAULT;
856 }
857
858 return 0;
859}
860
861/* Doesn't need the hardware lock.
862 */
863static int i915_irq_wait(struct drm_device *dev, void *data,
864 struct drm_file *file_priv)
865{
866 drm_i915_private_t *dev_priv = dev->dev_private;
867 drm_i915_irq_wait_t *irqwait = data;
868
869 if (drm_core_check_feature(dev, DRIVER_MODESET))
870 return -ENODEV;
871
872 if (!dev_priv) {
873 DRM_ERROR("called with no initialization\n");
874 return -EINVAL;
875 }
876
877 return i915_wait_irq(dev, irqwait->irq_seq);
878}
879
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200880static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
881 struct drm_file *file_priv)
882{
883 drm_i915_private_t *dev_priv = dev->dev_private;
884 drm_i915_vblank_pipe_t *pipe = data;
885
886 if (drm_core_check_feature(dev, DRIVER_MODESET))
887 return -ENODEV;
888
889 if (!dev_priv) {
890 DRM_ERROR("called with no initialization\n");
891 return -EINVAL;
892 }
893
894 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
895
896 return 0;
897}
898
899/**
900 * Schedule buffer swap at given vertical blank.
901 */
902static int i915_vblank_swap(struct drm_device *dev, void *data,
903 struct drm_file *file_priv)
904{
905 /* The delayed swap mechanism was fundamentally racy, and has been
906 * removed. The model was that the client requested a delayed flip/swap
907 * from the kernel, then waited for vblank before continuing to perform
908 * rendering. The problem was that the kernel might wake the client
909 * up before it dispatched the vblank swap (since the lock has to be
910 * held while touching the ringbuffer), in which case the client would
911 * clear and start the next frame before the swap occurred, and
912 * flicker would occur in addition to likely missing the vblank.
913 *
914 * In the absence of this ioctl, userland falls back to a correct path
915 * of waiting for a vblank, then dispatching the swap on its own.
916 * Context switching to userland and back is plenty fast enough for
917 * meeting the requirements of vblank swapping.
918 */
919 return -EINVAL;
920}
921
Eric Anholtc153f452007-09-03 12:06:45 +1000922static int i915_flip_bufs(struct drm_device *dev, void *data,
923 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Eric Anholt546b0972008-09-01 16:45:29 -0700925 int ret;
926
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200927 if (drm_core_check_feature(dev, DRIVER_MODESET))
928 return -ENODEV;
929
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800930 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Eric Anholt546b0972008-09-01 16:45:29 -0700932 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Eric Anholt546b0972008-09-01 16:45:29 -0700934 mutex_lock(&dev->struct_mutex);
935 ret = i915_dispatch_flip(dev);
936 mutex_unlock(&dev->struct_mutex);
937
938 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Eric Anholtc153f452007-09-03 12:06:45 +1000941static int i915_getparam(struct drm_device *dev, void *data,
942 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000945 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int value;
947
948 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000949 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000950 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
Eric Anholtc153f452007-09-03 12:06:45 +1000953 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700955 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200958 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100960 case I915_PARAM_LAST_DISPATCH:
961 value = READ_BREADCRUMB(dev_priv);
962 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400963 case I915_PARAM_CHIPSET_ID:
964 value = dev->pci_device;
965 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700966 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200967 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700968 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800969 case I915_PARAM_NUM_FENCES_AVAIL:
970 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
971 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200972 case I915_PARAM_HAS_OVERLAY:
973 value = dev_priv->overlay ? 1 : 0;
974 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800975 case I915_PARAM_HAS_PAGEFLIPPING:
976 value = 1;
977 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500978 case I915_PARAM_HAS_EXECBUF2:
979 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200980 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500981 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800982 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100983 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800984 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100985 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100986 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100987 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100988 case I915_PARAM_HAS_RELAXED_FENCING:
989 value = 1;
990 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100991 case I915_PARAM_HAS_COHERENT_RINGS:
992 value = 1;
993 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000994 case I915_PARAM_HAS_EXEC_CONSTANTS:
995 value = INTEL_INFO(dev)->gen >= 4;
996 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000997 case I915_PARAM_HAS_RELAXED_DELTA:
998 value = 1;
999 break;
Eric Anholtae662d32012-01-03 09:23:29 -08001000 case I915_PARAM_HAS_GEN7_SOL_RESET:
1001 value = 1;
1002 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -02001003 case I915_PARAM_HAS_LLC:
1004 value = HAS_LLC(dev);
1005 break;
Daniel Vetter777ee962012-02-15 23:50:25 +01001006 case I915_PARAM_HAS_ALIASING_PPGTT:
1007 value = dev_priv->mm.aliasing_ppgtt ? 1 : 0;
1008 break;
Ben Widawsky172cf152012-06-05 15:24:25 -07001009 case I915_PARAM_HAS_WAIT_TIMEOUT:
1010 value = 1;
1011 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +01001012 case I915_PARAM_HAS_SEMAPHORES:
1013 value = i915_semaphore_is_enabled(dev);
1014 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +10001015 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
1016 value = 1;
1017 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001018 case I915_PARAM_HAS_SECURE_BATCHES:
1019 value = capable(CAP_SYS_ADMIN);
1020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001022 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -05001023 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001024 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Eric Anholtc153f452007-09-03 12:06:45 +10001027 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001029 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032 return 0;
1033}
1034
Eric Anholtc153f452007-09-03 12:06:45 +10001035static int i915_setparam(struct drm_device *dev, void *data,
1036 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001039 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001042 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001043 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
Eric Anholtc153f452007-09-03 12:06:45 +10001046 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 break;
1049 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001052 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001054 case I915_SETPARAM_NUM_USED_FENCES:
1055 if (param->value > dev_priv->num_fence_regs ||
1056 param->value < 0)
1057 return -EINVAL;
1058 /* Userspace can use first N regs */
1059 dev_priv->fence_reg_start = param->value;
1060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001062 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001063 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001064 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
1067 return 0;
1068}
1069
Eric Anholtc153f452007-09-03 12:06:45 +10001070static int i915_set_status_page(struct drm_device *dev, void *data,
1071 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001072{
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001073 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001074 drm_i915_hws_addr_t *hws = data;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001075 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001076
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001077 if (drm_core_check_feature(dev, DRIVER_MODESET))
1078 return -ENODEV;
1079
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001080 if (!I915_NEED_GFX_HWS(dev))
1081 return -EINVAL;
1082
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001083 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001084 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001085 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001086 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001087
Jesse Barnes79e53942008-11-07 14:24:08 -08001088 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1089 WARN(1, "tried to set status page when mode setting active\n");
1090 return 0;
1091 }
1092
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001093 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001094
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001095 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001096
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001097 dev_priv->dri1.gfx_hws_cpu_addr =
1098 ioremap_wc(dev_priv->mm.gtt_base_addr + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001099 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001100 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001101 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001102 DRM_ERROR("can not ioremap virtual address for"
1103 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001104 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001105 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001106
1107 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001108 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001109
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001110 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001111 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001112 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001113 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001114 return 0;
1115}
1116
Dave Airlieec2a4c32009-08-04 11:43:41 +10001117static int i915_get_bridge_dev(struct drm_device *dev)
1118{
1119 struct drm_i915_private *dev_priv = dev->dev_private;
1120
Akshay Joshi0206e352011-08-16 15:34:10 -04001121 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001122 if (!dev_priv->bridge_dev) {
1123 DRM_ERROR("bridge device not found\n");
1124 return -1;
1125 }
1126 return 0;
1127}
1128
Zhenyu Wangc48044112009-12-17 14:48:43 +08001129#define MCHBAR_I915 0x44
1130#define MCHBAR_I965 0x48
1131#define MCHBAR_SIZE (4*4096)
1132
1133#define DEVEN_REG 0x54
1134#define DEVEN_MCHBAR_EN (1 << 28)
1135
1136/* Allocate space for the MCH regs if needed, return nonzero on error */
1137static int
1138intel_alloc_mchbar_resource(struct drm_device *dev)
1139{
1140 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001141 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001142 u32 temp_lo, temp_hi = 0;
1143 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001144 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001145
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001146 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001147 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1148 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1149 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1150
1151 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1152#ifdef CONFIG_PNP
1153 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001154 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1155 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001156#endif
1157
1158 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001159 dev_priv->mch_res.name = "i915 MCHBAR";
1160 dev_priv->mch_res.flags = IORESOURCE_MEM;
1161 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1162 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001163 MCHBAR_SIZE, MCHBAR_SIZE,
1164 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001165 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001166 dev_priv->bridge_dev);
1167 if (ret) {
1168 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1169 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001170 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001171 }
1172
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001173 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001174 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1175 upper_32_bits(dev_priv->mch_res.start));
1176
1177 pci_write_config_dword(dev_priv->bridge_dev, reg,
1178 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001179 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001180}
1181
1182/* Setup MCHBAR if possible, return true if we should disable it again */
1183static void
1184intel_setup_mchbar(struct drm_device *dev)
1185{
1186 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001187 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001188 u32 temp;
1189 bool enabled;
1190
1191 dev_priv->mchbar_need_disable = false;
1192
1193 if (IS_I915G(dev) || IS_I915GM(dev)) {
1194 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1195 enabled = !!(temp & DEVEN_MCHBAR_EN);
1196 } else {
1197 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1198 enabled = temp & 1;
1199 }
1200
1201 /* If it's already enabled, don't have to do anything */
1202 if (enabled)
1203 return;
1204
1205 if (intel_alloc_mchbar_resource(dev))
1206 return;
1207
1208 dev_priv->mchbar_need_disable = true;
1209
1210 /* Space is allocated or reserved, so enable it. */
1211 if (IS_I915G(dev) || IS_I915GM(dev)) {
1212 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1213 temp | DEVEN_MCHBAR_EN);
1214 } else {
1215 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1216 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1217 }
1218}
1219
1220static void
1221intel_teardown_mchbar(struct drm_device *dev)
1222{
1223 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001224 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001225 u32 temp;
1226
1227 if (dev_priv->mchbar_need_disable) {
1228 if (IS_I915G(dev) || IS_I915GM(dev)) {
1229 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1230 temp &= ~DEVEN_MCHBAR_EN;
1231 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1232 } else {
1233 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1234 temp &= ~1;
1235 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1236 }
1237 }
1238
1239 if (dev_priv->mch_res.start)
1240 release_resource(&dev_priv->mch_res);
1241}
1242
Dave Airlie28d52042009-09-21 14:33:58 +10001243/* true = enable decode, false = disable decoder */
1244static unsigned int i915_vga_set_decode(void *cookie, bool state)
1245{
1246 struct drm_device *dev = cookie;
1247
1248 intel_modeset_vga_set_state(dev, state);
1249 if (state)
1250 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1251 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1252 else
1253 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1254}
1255
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001256static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1257{
1258 struct drm_device *dev = pci_get_drvdata(pdev);
1259 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1260 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001261 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001262 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001263 /* i915 resume handler doesn't set to D0 */
1264 pci_set_power_state(dev->pdev, PCI_D0);
1265 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001266 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001267 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001268 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001269 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001270 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001271 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001272 }
1273}
1274
1275static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1276{
1277 struct drm_device *dev = pci_get_drvdata(pdev);
1278 bool can_switch;
1279
1280 spin_lock(&dev->count_lock);
1281 can_switch = (dev->open_count == 0);
1282 spin_unlock(&dev->count_lock);
1283 return can_switch;
1284}
1285
Takashi Iwai26ec6852012-05-11 07:51:17 +02001286static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1287 .set_gpu_state = i915_switcheroo_set_state,
1288 .reprobe = NULL,
1289 .can_switch = i915_switcheroo_can_switch,
1290};
1291
Chris Wilson2c7111d2011-03-29 10:40:27 +01001292static int i915_load_modeset_init(struct drm_device *dev)
1293{
1294 struct drm_i915_private *dev_priv = dev->dev_private;
1295 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001296
Bryan Freed6d139a82010-10-14 09:14:51 +01001297 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001298 if (ret)
1299 DRM_INFO("failed to find VBIOS tables\n");
1300
Chris Wilson934f9922011-01-20 13:09:12 +00001301 /* If we have > 1 VGA cards, then we need to arbitrate access
1302 * to the common VGA resources.
1303 *
1304 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1305 * then we do not take part in VGA arbitration and the
1306 * vga_client_register() fails with -ENODEV.
1307 */
Dave Airlie28d52042009-09-21 14:33:58 +10001308 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
Chris Wilson934f9922011-01-20 13:09:12 +00001309 if (ret && ret != -ENODEV)
Chris Wilson2c7111d2011-03-29 10:40:27 +01001310 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001311
Jesse Barnes723bfd72010-10-07 16:01:13 -07001312 intel_register_dsm_handler();
1313
Takashi Iwai26ec6852012-05-11 07:51:17 +02001314 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001315 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001316 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001317
Chris Wilson9797fbf2012-04-24 15:47:39 +01001318 /* Initialise stolen first so that we may reserve preallocated
1319 * objects for the BIOS to KMS transition.
1320 */
1321 ret = i915_gem_init_stolen(dev);
1322 if (ret)
1323 goto cleanup_vga_switcheroo;
1324
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001325 intel_modeset_init(dev);
1326
Chris Wilson1070a422012-04-24 15:47:41 +01001327 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001328 if (ret)
Chris Wilson9797fbf2012-04-24 15:47:39 +01001329 goto cleanup_gem_stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -08001330
Chris Wilson2c7111d2011-03-29 10:40:27 +01001331 intel_modeset_gem_init(dev);
1332
1333 ret = drm_irq_install(dev);
1334 if (ret)
1335 goto cleanup_gem;
1336
Jesse Barnes79e53942008-11-07 14:24:08 -08001337 /* Always safe in the mode setting case. */
1338 /* FIXME: do pre/post-mode set stuff in core KMS code */
1339 dev->vblank_disable_allowed = 1;
1340
Chris Wilson5a793952010-06-06 10:50:03 +01001341 ret = intel_fbdev_init(dev);
1342 if (ret)
1343 goto cleanup_irq;
1344
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001345 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001346
1347 /* We're off and running w/KMS */
1348 dev_priv->mm.suspended = 0;
1349
Jesse Barnes79e53942008-11-07 14:24:08 -08001350 return 0;
1351
Chris Wilson5a793952010-06-06 10:50:03 +01001352cleanup_irq:
1353 drm_irq_uninstall(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001354cleanup_gem:
1355 mutex_lock(&dev->struct_mutex);
1356 i915_gem_cleanup_ringbuffer(dev);
1357 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001358 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001359cleanup_gem_stolen:
1360 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001361cleanup_vga_switcheroo:
1362 vga_switcheroo_unregister_client(dev->pdev);
1363cleanup_vga_client:
1364 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001365out:
1366 return ret;
1367}
1368
Dave Airlie7c1c2872008-11-28 14:22:24 +10001369int i915_master_create(struct drm_device *dev, struct drm_master *master)
1370{
1371 struct drm_i915_master_private *master_priv;
1372
Eric Anholt9a298b22009-03-24 12:23:04 -07001373 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001374 if (!master_priv)
1375 return -ENOMEM;
1376
1377 master->driver_priv = master_priv;
1378 return 0;
1379}
1380
1381void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1382{
1383 struct drm_i915_master_private *master_priv = master->driver_priv;
1384
1385 if (!master_priv)
1386 return;
1387
Eric Anholt9a298b22009-03-24 12:23:04 -07001388 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001389
1390 master->driver_priv = NULL;
1391}
1392
Adam Jacksone2b665c2012-03-14 11:22:10 -04001393static void
1394i915_mtrr_setup(struct drm_i915_private *dev_priv, unsigned long base,
1395 unsigned long size)
1396{
Chris Wilson23f54be2012-03-23 17:38:49 +00001397 dev_priv->mm.gtt_mtrr = -1;
1398
Adam Jackson9e984bc12012-03-14 11:22:11 -04001399#if defined(CONFIG_X86_PAT)
1400 if (cpu_has_pat)
1401 return;
1402#endif
1403
Adam Jacksone2b665c2012-03-14 11:22:10 -04001404 /* Set up a WC MTRR for non-PAT systems. This is more common than
1405 * one would think, because the kernel disables PAT on first
1406 * generation Core chips because WC PAT gets overridden by a UC
1407 * MTRR if present. Even if a UC MTRR isn't present.
1408 */
1409 dev_priv->mm.gtt_mtrr = mtrr_add(base, size, MTRR_TYPE_WRCOMB, 1);
1410 if (dev_priv->mm.gtt_mtrr < 0) {
1411 DRM_INFO("MTRR allocation failed. Graphics "
1412 "performance may suffer.\n");
1413 }
1414}
1415
Daniel Vettere1887192012-06-12 11:28:17 +02001416static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1417{
1418 struct apertures_struct *ap;
1419 struct pci_dev *pdev = dev_priv->dev->pdev;
1420 bool primary;
1421
1422 ap = alloc_apertures(1);
1423 if (!ap)
1424 return;
1425
Daniel Vetter87207ca2012-06-24 20:51:36 +02001426 ap->ranges[0].base = dev_priv->mm.gtt->gma_bus_addr;
Daniel Vettere1887192012-06-12 11:28:17 +02001427 ap->ranges[0].size =
1428 dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
1429 primary =
1430 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1431
1432 remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
1433
1434 kfree(ap);
1435}
1436
Daniel Vetterc96ea642012-08-08 22:01:51 +02001437static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1438{
1439 const struct intel_device_info *info = dev_priv->info;
1440
1441#define DEV_INFO_FLAG(name) info->name ? #name "," : ""
1442#define DEV_INFO_SEP ,
1443 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
1444 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1445 info->gen,
1446 dev_priv->dev->pdev->device,
1447 DEV_INFO_FLAGS);
1448#undef DEV_INFO_FLAG
1449#undef DEV_INFO_SEP
1450}
1451
Eric Anholt63ee41d2010-12-20 18:40:06 -08001452/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001453 * i915_driver_load - setup chip and create an initial config
1454 * @dev: DRM device
1455 * @flags: startup flags
1456 *
1457 * The driver load routine has to do several things:
1458 * - drive output discovery via intel_modeset_init()
1459 * - initialize the memory manager
1460 * - allocate initial config memory
1461 * - setup the DRM framebuffer with the allocated memory
1462 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001463int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001464{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001465 struct drm_i915_private *dev_priv;
Daniel Vetter26394d92012-03-26 21:33:18 +02001466 struct intel_device_info *info;
Chris Wilson934d6082012-09-14 11:57:46 +01001467 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001468 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001469
Daniel Vetter26394d92012-03-26 21:33:18 +02001470 info = (struct intel_device_info *) flags;
1471
1472 /* Refuse to load on gen6+ without kms enabled. */
1473 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET))
1474 return -ENODEV;
1475
Dave Airlie22eae942005-11-10 22:16:34 +11001476 /* i915 has 4 more counters */
1477 dev->counters += 4;
1478 dev->types[6] = _DRM_STAT_IRQ;
1479 dev->types[7] = _DRM_STAT_PRIMARY;
1480 dev->types[8] = _DRM_STAT_SECONDARY;
1481 dev->types[9] = _DRM_STAT_DMA;
1482
Eric Anholt9a298b22009-03-24 12:23:04 -07001483 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001484 if (dev_priv == NULL)
1485 return -ENOMEM;
1486
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001487 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001488 dev_priv->dev = dev;
Daniel Vetter26394d92012-03-26 21:33:18 +02001489 dev_priv->info = info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001490
Daniel Vetterc96ea642012-08-08 22:01:51 +02001491 i915_dump_device_info(dev_priv);
1492
Dave Airlieec2a4c32009-08-04 11:43:41 +10001493 if (i915_get_bridge_dev(dev)) {
1494 ret = -EIO;
1495 goto free_priv;
1496 }
1497
Daniel Vettere1887192012-06-12 11:28:17 +02001498 ret = intel_gmch_probe(dev_priv->bridge_dev, dev->pdev, NULL);
1499 if (!ret) {
1500 DRM_ERROR("failed to set up gmch\n");
1501 ret = -EIO;
1502 goto put_bridge;
1503 }
1504
1505 dev_priv->mm.gtt = intel_gtt_get();
1506 if (!dev_priv->mm.gtt) {
1507 DRM_ERROR("Failed to initialize GTT\n");
1508 ret = -ENODEV;
1509 goto put_gmch;
1510 }
1511
1512 i915_kick_out_firmware_fb(dev_priv);
1513
Dave Airlie466e69b2011-12-19 11:15:29 +00001514 pci_set_master(dev->pdev);
1515
Daniel Vetter9f82d232010-08-30 21:25:23 +02001516 /* overlay on gen2 is broken and can't address above 1G */
1517 if (IS_GEN2(dev))
1518 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1519
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001520 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1521 * using 32bit addressing, overwriting memory if HWS is located
1522 * above 4GB.
1523 *
1524 * The documentation also mentions an issue with undefined
1525 * behaviour if any general state is accessed within a page above 4GB,
1526 * which also needs to be handled carefully.
1527 */
1528 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1529 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1530
Chris Wilsonb4ce0f82010-10-28 11:26:06 +01001531 mmio_bar = IS_GEN2(dev) ? 1 : 0;
Chris Wilson934d6082012-09-14 11:57:46 +01001532 /* Before gen4, the registers and the GTT are behind different BARs.
1533 * However, from gen4 onwards, the registers and the GTT are shared
1534 * in the same BAR, so we want to restrict this ioremap from
1535 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1536 * the register BAR remains the same size for all the earlier
1537 * generations up to Ironlake.
1538 */
1539 if (info->gen < 5)
1540 mmio_size = 512*1024;
1541 else
1542 mmio_size = 2*1024*1024;
1543
1544 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
Chris Wilsonb4ce0f82010-10-28 11:26:06 +01001545 if (!dev_priv->regs) {
1546 DRM_ERROR("failed to map registers\n");
1547 ret = -EIO;
Daniel Vetter14be93d2012-06-08 15:55:40 +02001548 goto put_gmch;
Chris Wilson71e93392010-10-27 18:46:52 +01001549 }
1550
Daniel Vetter9021f282012-03-26 09:45:41 +02001551 aperture_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001552 dev_priv->mm.gtt_base_addr = dev_priv->mm.gtt->gma_bus_addr;
Chris Wilson71e93392010-10-27 18:46:52 +01001553
Akshay Joshi0206e352011-08-16 15:34:10 -04001554 dev_priv->mm.gtt_mapping =
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001555 io_mapping_create_wc(dev_priv->mm.gtt_base_addr,
1556 aperture_size);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001557 if (dev_priv->mm.gtt_mapping == NULL) {
1558 ret = -EIO;
Daniel Vettere1887192012-06-12 11:28:17 +02001559 goto out_rmmap;
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001560 }
1561
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001562 i915_mtrr_setup(dev_priv, dev_priv->mm.gtt_base_addr,
1563 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001564
Chris Wilsone642abb2010-09-09 12:46:34 +01001565 /* The i915 workqueue is primarily used for batched retirement of
1566 * requests (and thus managing bo) once the task has been completed
1567 * by the GPU. i915_gem_retire_requests() is called directly when we
1568 * need high-priority retirement, such as waiting for an explicit
1569 * bo.
1570 *
1571 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001572 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001573 *
1574 * All tasks on the workqueue are expected to acquire the dev mutex
1575 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001576 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001577 */
Tejun Heo53621862012-08-22 16:40:57 -07001578 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001579 if (dev_priv->wq == NULL) {
1580 DRM_ERROR("Failed to create our workqueue.\n");
1581 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001582 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001583 }
1584
Paulo Zanoni45e6e3a2012-07-03 15:57:32 -03001585 /* This must be called before any calls to HAS_PCH_* */
1586 intel_detect_pch(dev);
1587
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001588 intel_irq_init(dev);
Chris Wilson990bbda2012-07-02 11:51:02 -03001589 intel_gt_init(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001590
Zhenyu Wangc48044112009-12-17 14:48:43 +08001591 /* Try to make sure MCHBAR is enabled before poking at it */
1592 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001593 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001594 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001595
Bryan Freed6d139a82010-10-14 09:14:51 +01001596 /* Make sure the bios did its job and set up vital registers */
1597 intel_setup_bios(dev);
1598
Eric Anholt673a3942008-07-30 12:06:12 -07001599 i915_gem_load(dev);
1600
Keith Packard398c9cb2008-07-30 13:03:43 -07001601 /* Init HWS */
1602 if (!I915_NEED_GFX_HWS(dev)) {
1603 ret = i915_init_phys_hws(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001604 if (ret)
1605 goto out_gem_unload;
Keith Packard398c9cb2008-07-30 13:03:43 -07001606 }
Eric Anholted4cb412008-07-29 12:10:39 -07001607
1608 /* On the 945G/GM, the chipset reports the MSI capability on the
1609 * integrated graphics even though the support isn't actually there
1610 * according to the published specs. It doesn't appear to function
1611 * correctly in testing on 945G.
1612 * This may be a side effect of MSI having been made available for PEG
1613 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001614 *
1615 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001616 * be lost or delayed, but we use them anyways to avoid
1617 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001618 */
Keith Packardb60678a2008-12-08 11:12:28 -08001619 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001620 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001621
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001622 spin_lock_init(&dev_priv->irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001623 spin_lock_init(&dev_priv->error_lock);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02001624 spin_lock_init(&dev_priv->rps.lock);
Alexander Shishkin99d0b1d2012-08-31 15:50:55 +03001625 spin_lock_init(&dev_priv->dpio_lock);
Eric Anholted4cb412008-07-29 12:10:39 -07001626
Eugeni Dodonovc51ed782012-04-13 17:08:45 -03001627 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
Jesse Barnes27f82272011-09-02 12:54:37 -07001628 dev_priv->num_pipe = 3;
1629 else if (IS_MOBILE(dev) || !IS_GEN2(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001630 dev_priv->num_pipe = 2;
1631 else
1632 dev_priv->num_pipe = 1;
1633
1634 ret = drm_vblank_init(dev, dev_priv->num_pipe);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001635 if (ret)
1636 goto out_gem_unload;
Keith Packard52440212008-11-18 09:30:25 -08001637
Ben Gamari11ed50e2009-09-14 17:48:45 -04001638 /* Start out suspended */
1639 dev_priv->mm.suspended = 1;
1640
Jesse Barnes79e53942008-11-07 14:24:08 -08001641 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001642 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001643 if (ret < 0) {
1644 DRM_ERROR("failed to init modeset\n");
Chris Wilson56e2ea32010-11-08 17:10:29 +00001645 goto out_gem_unload;
Jesse Barnes79e53942008-11-07 14:24:08 -08001646 }
1647 }
1648
Ben Widawsky0136db582012-04-10 21:17:01 -07001649 i915_setup_sysfs(dev);
1650
Matthew Garrett74a365b2009-03-19 21:35:39 +00001651 /* Must be done after probing outputs */
Chris Wilson44834a62010-08-19 16:09:23 +01001652 intel_opregion_init(dev);
1653 acpi_video_register();
Matthew Garrett74a365b2009-03-19 21:35:39 +00001654
Ben Gamarif65d9422009-09-14 17:48:44 -04001655 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
1656 (unsigned long) dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001657
Daniel Vettereb48eb02012-04-26 23:28:12 +02001658 if (IS_GEN5(dev))
1659 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001660
Jesse Barnes79e53942008-11-07 14:24:08 -08001661 return 0;
1662
Chris Wilson56e2ea32010-11-08 17:10:29 +00001663out_gem_unload:
Keith Packarda7b85d22011-07-10 13:12:17 -07001664 if (dev_priv->mm.inactive_shrinker.shrink)
1665 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1666
Chris Wilson56e2ea32010-11-08 17:10:29 +00001667 if (dev->pdev->msi_enabled)
1668 pci_disable_msi(dev->pdev);
1669
1670 intel_teardown_gmbus(dev);
1671 intel_teardown_mchbar(dev);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001672 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001673out_mtrrfree:
1674 if (dev_priv->mm.gtt_mtrr >= 0) {
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001675 mtrr_del(dev_priv->mm.gtt_mtrr,
1676 dev_priv->mm.gtt_base_addr,
1677 aperture_size);
Keith Packarda7b85d22011-07-10 13:12:17 -07001678 dev_priv->mm.gtt_mtrr = -1;
1679 }
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001680 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001681out_rmmap:
Chris Wilson6dda5692010-10-29 21:02:18 +01001682 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vettere1887192012-06-12 11:28:17 +02001683put_gmch:
1684 intel_gmch_remove();
Dave Airlieec2a4c32009-08-04 11:43:41 +10001685put_bridge:
1686 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001687free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001688 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001689 return ret;
1690}
1691
1692int i915_driver_unload(struct drm_device *dev)
1693{
1694 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001695 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001696
Daniel Vettereb48eb02012-04-26 23:28:12 +02001697 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001698
Ben Widawsky0136db582012-04-10 21:17:01 -07001699 i915_teardown_sysfs(dev);
1700
Chris Wilson17250b72010-10-28 12:51:39 +01001701 if (dev_priv->mm.inactive_shrinker.shrink)
1702 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1703
Daniel Vetterc911fc12010-08-20 21:23:20 +02001704 mutex_lock(&dev->struct_mutex);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001705 ret = i915_gpu_idle(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02001706 if (ret)
1707 DRM_ERROR("failed to idle hardware: %d\n", ret);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07001708 i915_gem_retire_requests(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02001709 mutex_unlock(&dev->struct_mutex);
1710
Daniel Vetter75ef9da2010-08-21 00:25:16 +02001711 /* Cancel the retire work handler, which should be idle now. */
1712 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
1713
Eric Anholtab657db12009-01-23 12:57:47 -08001714 io_mapping_free(dev_priv->mm.gtt_mapping);
1715 if (dev_priv->mm.gtt_mtrr >= 0) {
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001716 mtrr_del(dev_priv->mm.gtt_mtrr,
1717 dev_priv->mm.gtt_base_addr,
1718 dev_priv->mm.gtt->gtt_mappable_entries * PAGE_SIZE);
Eric Anholtab657db12009-01-23 12:57:47 -08001719 dev_priv->mm.gtt_mtrr = -1;
1720 }
1721
Chris Wilson44834a62010-08-19 16:09:23 +01001722 acpi_video_unregister();
1723
Jesse Barnes79e53942008-11-07 14:24:08 -08001724 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01001725 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001726 intel_modeset_cleanup(dev);
1727
Zhao Yakui6363ee62009-11-24 09:48:44 +08001728 /*
1729 * free the memory space allocated for the child device
1730 * config parsed from VBT
1731 */
1732 if (dev_priv->child_dev && dev_priv->child_dev_num) {
1733 kfree(dev_priv->child_dev);
1734 dev_priv->child_dev = NULL;
1735 dev_priv->child_dev_num = 0;
1736 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001737
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001738 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001739 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001740 }
1741
Daniel Vettera8b48992010-08-20 21:25:11 +02001742 /* Free error state after interrupts are fully disabled. */
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001743 del_timer_sync(&dev_priv->hangcheck_timer);
1744 cancel_work_sync(&dev_priv->error_work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001745 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001746
Eric Anholted4cb412008-07-29 12:10:39 -07001747 if (dev->pdev->msi_enabled)
1748 pci_disable_msi(dev->pdev);
1749
Chris Wilson44834a62010-08-19 16:09:23 +01001750 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001751
Jesse Barnes79e53942008-11-07 14:24:08 -08001752 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001753 /* Flush any outstanding unpin_work. */
1754 flush_workqueue(dev_priv->wq);
1755
Jesse Barnes79e53942008-11-07 14:24:08 -08001756 mutex_lock(&dev->struct_mutex);
Hugh Dickinsecbec532011-06-27 16:18:20 -07001757 i915_gem_free_all_phys_object(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001758 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001759 i915_gem_context_fini(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001760 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001761 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001762 i915_gem_cleanup_stolen(dev);
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001763 drm_mm_takedown(&dev_priv->mm.stolen);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001764
1765 intel_cleanup_overlay(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001766
1767 if (!I915_NEED_GFX_HWS(dev))
1768 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001769 }
1770
Daniel Vetter701394c2010-10-10 18:54:08 +01001771 if (dev_priv->regs != NULL)
Chris Wilson6dda5692010-10-29 21:02:18 +01001772 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vetter701394c2010-10-10 18:54:08 +01001773
Chris Wilsonf899fc62010-07-20 15:44:45 -07001774 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001775 intel_teardown_mchbar(dev);
1776
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001777 destroy_workqueue(dev_priv->wq);
1778
Dave Airlieec2a4c32009-08-04 11:43:41 +10001779 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07001780 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001781
Dave Airlie22eae942005-11-10 22:16:34 +11001782 return 0;
1783}
1784
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001785int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001786{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001787 struct drm_i915_file_private *file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001788
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001789 DRM_DEBUG_DRIVER("\n");
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001790 file_priv = kmalloc(sizeof(*file_priv), GFP_KERNEL);
1791 if (!file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07001792 return -ENOMEM;
1793
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001794 file->driver_priv = file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001795
Chris Wilson1c255952010-09-26 11:03:27 +01001796 spin_lock_init(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001797 INIT_LIST_HEAD(&file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001798
Daniel Vetterdf12c6d2012-06-19 16:52:30 +02001799 idr_init(&file_priv->context_idr);
Ben Widawsky254f9652012-06-04 14:42:42 -07001800
Eric Anholt673a3942008-07-30 12:06:12 -07001801 return 0;
1802}
1803
Jesse Barnes79e53942008-11-07 14:24:08 -08001804/**
1805 * i915_driver_lastclose - clean up after all DRM clients have exited
1806 * @dev: DRM device
1807 *
1808 * Take care of cleaning up after all DRM clients have exited. In the
1809 * mode setting case, we want to restore the kernel's initial mode (just
1810 * in case the last client left us in a bad state).
1811 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001812 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001813 * and DMA structures, since the kernel won't be using them, and clea
1814 * up any GEM state.
1815 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001816void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001818 drm_i915_private_t *dev_priv = dev->dev_private;
1819
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001820 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1821 * goes right around and calls lastclose. Check for this and don't clean
1822 * up anything. */
1823 if (!dev_priv)
1824 return;
1825
1826 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +01001827 intel_fb_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001828 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001829 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001830 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001831
Eric Anholt673a3942008-07-30 12:06:12 -07001832 i915_gem_lastclose(dev);
1833
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001834 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Eric Anholt6c340ea2007-08-25 20:23:09 +10001837void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Ben Widawsky254f9652012-06-04 14:42:42 -07001839 i915_gem_context_close(dev, file_priv);
Eric Anholtb9624422009-06-03 07:27:35 +00001840 i915_gem_release(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001843void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001844{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001845 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001846
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001847 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001848}
1849
Eric Anholtc153f452007-09-03 12:06:45 +10001850struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001851 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1852 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1853 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1854 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1855 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1856 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1857 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH),
1858 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001859 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1860 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1861 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001862 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001863 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02001864 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001865 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
1866 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1867 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1868 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1869 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
1870 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
1871 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1872 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1873 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
Chris Wilsone6994ae2012-07-10 10:27:08 +01001874 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHEING, i915_gem_set_cacheing_ioctl, DRM_UNLOCKED),
1875 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHEING, i915_gem_get_cacheing_ioctl, DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001876 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
1877 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1878 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1879 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
1880 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
1881 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
1882 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
1883 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
1884 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
1885 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
1886 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
1887 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
1888 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
1889 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
1890 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
1891 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1892 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08001893 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1894 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 -07001895 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED),
Ben Widawsky84624812012-06-04 14:42:54 -07001896 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED),
1897 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED),
Ben Widawskyc0c7bab2012-07-12 11:01:05 -07001898 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10001899};
1900
1901int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001902
Daniel Vetter9021f282012-03-26 09:45:41 +02001903/*
1904 * This is really ugly: Because old userspace abused the linux agp interface to
1905 * manage the gtt, we need to claim that all intel devices are agp. For
1906 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10001907 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001908int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001909{
1910 return 1;
1911}