blob: c27a21034a5e56e6fca41d6ff710c9abef59bf72 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Daniel Vetter09422b22012-04-26 23:28:10 +020046#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
47
48#define BEGIN_LP_RING(n) \
49 intel_ring_begin(LP_RING(dev_priv), (n))
50
51#define OUT_RING(x) \
52 intel_ring_emit(LP_RING(dev_priv), x)
53
54#define ADVANCE_LP_RING() \
55 intel_ring_advance(LP_RING(dev_priv))
56
57/**
58 * Lock test for when it's just for synchronization of ring access.
59 *
60 * In that case, we don't need to do it when GEM is initialized as nobody else
61 * has access to the ring.
62 */
63#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
64 if (LP_RING(dev->dev_private)->obj == NULL) \
65 LOCK_TEST_WITH_RETURN(dev, file); \
66} while (0)
67
Daniel Vetter316d3882012-04-26 23:28:15 +020068static inline u32
69intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
70{
71 if (I915_NEED_GFX_HWS(dev_priv->dev))
72 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
73 else
74 return intel_read_status_page(LP_RING(dev_priv), reg);
75}
76
77#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
Daniel Vetter09422b22012-04-26 23:28:10 +020078#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
79#define I915_BREADCRUMB_INDEX 0x21
80
Daniel Vetterd05c6172012-04-26 23:28:09 +020081void i915_update_dri1_breadcrumb(struct drm_device *dev)
82{
83 drm_i915_private_t *dev_priv = dev->dev_private;
84 struct drm_i915_master_private *master_priv;
85
86 if (dev->primary->master) {
87 master_priv = dev->primary->master->driver_priv;
88 if (master_priv->sarea_priv)
89 master_priv->sarea_priv->last_dispatch =
90 READ_BREADCRUMB(dev_priv);
91 }
92}
93
Chris Wilson4cbf74c2011-02-25 22:26:23 +000094static void i915_write_hws_pga(struct drm_device *dev)
95{
96 drm_i915_private_t *dev_priv = dev->dev_private;
97 u32 addr;
98
99 addr = dev_priv->status_page_dmah->busaddr;
100 if (INTEL_INFO(dev)->gen >= 4)
101 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
102 I915_WRITE(HWS_PGA, addr);
103}
104
Keith Packard398c9cb2008-07-30 13:03:43 -0700105/**
Keith Packard398c9cb2008-07-30 13:03:43 -0700106 * Frees the hardware status page, whether it's a physical address or a virtual
107 * address set up by the X Server.
108 */
Eric Anholt3043c602008-10-02 12:24:47 -0700109static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700110{
111 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000112 struct intel_ring_buffer *ring = LP_RING(dev_priv);
113
Keith Packard398c9cb2008-07-30 13:03:43 -0700114 if (dev_priv->status_page_dmah) {
115 drm_pci_free(dev, dev_priv->status_page_dmah);
116 dev_priv->status_page_dmah = NULL;
117 }
118
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000119 if (ring->status_page.gfx_addr) {
120 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200121 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700122 }
123
124 /* Need to rewrite hardware status page */
125 I915_WRITE(HWS_PGA, 0x1ffff000);
126}
127
Dave Airlie84b1fd12007-07-11 15:53:27 +1000128void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000131 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000132 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jesse Barnes79e53942008-11-07 14:24:08 -0800134 /*
135 * We should never lose context on the ring with modesetting
136 * as we don't expose it to userspace
137 */
138 if (drm_core_check_feature(dev, DRIVER_MODESET))
139 return;
140
Chris Wilson8168bd42010-11-11 17:54:52 +0000141 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
142 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Ville Syrjälä633cf8f2012-12-03 18:43:32 +0200143 ring->space = ring->head - (ring->tail + I915_RING_FREE_SPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800145 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Dave Airlie7c1c2872008-11-28 14:22:24 +1000147 if (!dev->primary->master)
148 return;
149
150 master_priv = dev->primary->master->driver_priv;
151 if (ring->head == ring->tail && master_priv->sarea_priv)
152 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Dave Airlie84b1fd12007-07-11 15:53:27 +1000155static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000157 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000158 int i;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* Make sure interrupts are disabled here because the uninstall ioctl
161 * may not have been called from userspace and after dev_private
162 * is freed, it's too late.
163 */
Eric Anholted4cb412008-07-29 12:10:39 -0700164 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000165 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200167 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000168 for (i = 0; i < I915_NUM_RINGS; i++)
169 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200170 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Keith Packard398c9cb2008-07-30 13:03:43 -0700172 /* Clear the HWS virtual address at teardown */
173 if (I915_NEED_GFX_HWS(dev))
174 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 return 0;
177}
178
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000179static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000181 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000182 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000183 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Dave Airlie3a03ac12009-01-11 09:03:49 +1000185 master_priv->sarea = drm_getsarea(dev);
186 if (master_priv->sarea) {
187 master_priv->sarea_priv = (drm_i915_sarea_t *)
188 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
189 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800190 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000191 }
192
Eric Anholt673a3942008-07-30 12:06:12 -0700193 if (init->ring_size != 0) {
Chris Wilsone8616b62011-01-20 09:57:11 +0000194 if (LP_RING(dev_priv)->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700195 i915_dma_cleanup(dev);
196 DRM_ERROR("Client tried to initialize ringbuffer in "
197 "GEM mode\n");
198 return -EINVAL;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Chris Wilsone8616b62011-01-20 09:57:11 +0000201 ret = intel_render_ring_init_dri(dev,
202 init->ring_start,
203 init->ring_size);
204 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700205 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000206 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200210 dev_priv->dri1.cpp = init->cpp;
211 dev_priv->dri1.back_offset = init->back_offset;
212 dev_priv->dri1.front_offset = init->front_offset;
213 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000214 if (master_priv->sarea_priv)
215 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Allow hardware batchbuffers unless told otherwise.
218 */
Daniel Vetter87813422012-05-02 11:49:32 +0200219 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
222}
223
Dave Airlie84b1fd12007-07-11 15:53:27 +1000224static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000227 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800229 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Daniel Vetter4225d0f2012-04-26 23:28:16 +0200231 if (ring->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 DRM_ERROR("can not ioremap virtual address for"
233 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000234 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
237 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800238 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800242 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800243 ring->status_page.page_addr);
244 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100245 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000246 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000247 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800248
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800249 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 return 0;
252}
253
Eric Anholtc153f452007-09-03 12:06:45 +1000254static int i915_dma_init(struct drm_device *dev, void *data,
255 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Eric Anholtc153f452007-09-03 12:06:45 +1000257 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int retcode = 0;
259
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200260 if (drm_core_check_feature(dev, DRIVER_MODESET))
261 return -ENODEV;
262
Eric Anholtc153f452007-09-03 12:06:45 +1000263 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000265 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267 case I915_CLEANUP_DMA:
268 retcode = i915_dma_cleanup(dev);
269 break;
270 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100271 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 break;
273 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000274 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 }
277
278 return retcode;
279}
280
281/* Implement basically the same security restrictions as hardware does
282 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
283 *
284 * Most of the calculations below involve calculating the size of a
285 * particular instruction. It's important to get the size right as
286 * that tells us where the next instruction to check is. Any illegal
287 * instruction detected will be given a size of zero, which is a
288 * signal to abort the rest of the buffer.
289 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100290static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 switch (((cmd >> 29) & 0x7)) {
293 case 0x0:
294 switch ((cmd >> 23) & 0x3f) {
295 case 0x0:
296 return 1; /* MI_NOOP */
297 case 0x4:
298 return 1; /* MI_FLUSH */
299 default:
300 return 0; /* disallow everything else */
301 }
302 break;
303 case 0x1:
304 return 0; /* reserved */
305 case 0x2:
306 return (cmd & 0xff) + 2; /* 2d commands */
307 case 0x3:
308 if (((cmd >> 24) & 0x1f) <= 0x18)
309 return 1;
310
311 switch ((cmd >> 24) & 0x1f) {
312 case 0x1c:
313 return 1;
314 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 case 0x3:
317 return (cmd & 0x1f) + 2;
318 case 0x4:
319 return (cmd & 0xf) + 2;
320 default:
321 return (cmd & 0xffff) + 2;
322 }
323 case 0x1e:
324 if (cmd & (1 << 23))
325 return (cmd & 0xffff) + 1;
326 else
327 return 1;
328 case 0x1f:
329 if ((cmd & (1 << 23)) == 0) /* inline vertices */
330 return (cmd & 0x1ffff) + 2;
331 else if (cmd & (1 << 17)) /* indirect random */
332 if ((cmd & 0xffff) == 0)
333 return 0; /* unknown length, too hard */
334 else
335 return (((cmd & 0xffff) + 1) / 2) + 1;
336 else
337 return 2; /* indirect sequential */
338 default:
339 return 0;
340 }
341 default:
342 return 0;
343 }
344
345 return 0;
346}
347
Eric Anholt201361a2009-03-11 12:30:04 -0700348static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100351 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000353 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000354 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100357 int sz = validate_cmd(buffer[i]);
358 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000359 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100360 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100363 ret = BEGIN_LP_RING((dwords+1)&~1);
364 if (ret)
365 return ret;
366
367 for (i = 0; i < dwords; i++)
368 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100369 if (dwords & 1)
370 OUT_RING(0);
371
372 ADVANCE_LP_RING();
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375}
376
Eric Anholt673a3942008-07-30 12:06:12 -0700377int
378i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000379 struct drm_clip_rect *box,
380 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100382 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100383 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000385 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
386 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000388 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000389 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100392 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100393 ret = BEGIN_LP_RING(4);
394 if (ret)
395 return ret;
396
Alan Hourihanec29b6692006-08-12 16:29:24 +1000397 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000398 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
399 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000400 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000401 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100402 ret = BEGIN_LP_RING(6);
403 if (ret)
404 return ret;
405
Alan Hourihanec29b6692006-08-12 16:29:24 +1000406 OUT_RING(GFX_OP_DRAWRECT_INFO);
407 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000408 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
409 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000410 OUT_RING(DR4);
411 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000412 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100413 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 return 0;
416}
417
Alan Hourihanec29b6692006-08-12 16:29:24 +1000418/* XXX: Emitting the counter should really be moved to part of the IRQ
419 * emit. For now, do it in both places:
420 */
421
Dave Airlie84b1fd12007-07-11 15:53:27 +1000422static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100423{
424 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000425 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100426
Daniel Vetter231f42a2012-11-02 19:55:05 +0100427 dev_priv->dri1.counter++;
428 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
429 dev_priv->dri1.counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000430 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100431 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Dave Airliede227f52006-01-25 15:31:43 +1100432
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100433 if (BEGIN_LP_RING(4) == 0) {
434 OUT_RING(MI_STORE_DWORD_INDEX);
435 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100436 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100437 OUT_RING(0);
438 ADVANCE_LP_RING();
439 }
Dave Airliede227f52006-01-25 15:31:43 +1100440}
441
Dave Airlie84b1fd12007-07-11 15:53:27 +1000442static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700443 drm_i915_cmdbuffer_t *cmd,
444 struct drm_clip_rect *cliprects,
445 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 int nbox = cmd->num_cliprects;
448 int i = 0, count, ret;
449
450 if (cmd->sz & 0x3) {
451 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000452 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 i915_kernel_lost_context(dev);
456
457 count = nbox ? nbox : 1;
458
459 for (i = 0; i < count; i++) {
460 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000461 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 cmd->DR1, cmd->DR4);
463 if (ret)
464 return ret;
465 }
466
Eric Anholt201361a2009-03-11 12:30:04 -0700467 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (ret)
469 return ret;
470 }
471
Dave Airliede227f52006-01-25 15:31:43 +1100472 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 0;
474}
475
Dave Airlie84b1fd12007-07-11 15:53:27 +1000476static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700477 drm_i915_batchbuffer_t * batch,
478 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100480 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100482 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if ((batch->start | batch->used) & 0x7) {
485 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000486 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 i915_kernel_lost_context(dev);
490
491 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 for (i = 0; i < count; i++) {
493 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000494 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100495 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (ret)
497 return ret;
498 }
499
Keith Packard0790d5e2008-07-30 12:28:47 -0700500 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100501 ret = BEGIN_LP_RING(2);
502 if (ret)
503 return ret;
504
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100505 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000506 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
507 OUT_RING(batch->start);
508 } else {
509 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
510 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100513 ret = BEGIN_LP_RING(4);
514 if (ret)
515 return ret;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 OUT_RING(MI_BATCH_BUFFER);
518 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
519 OUT_RING(batch->start + batch->used - 4);
520 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100522 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Zou Nan hai1cafd342010-06-25 13:40:24 +0800525
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100526 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100527 if (BEGIN_LP_RING(2) == 0) {
528 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
529 OUT_RING(MI_NOOP);
530 ADVANCE_LP_RING();
531 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100534 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536}
537
Dave Airlieaf6061a2008-05-07 12:15:39 +1000538static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000541 struct drm_i915_master_private *master_priv =
542 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100543 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Airlie7c1c2872008-11-28 14:22:24 +1000545 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400546 return -EINVAL;
547
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800548 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800549 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200550 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800551 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Airlieaf6061a2008-05-07 12:15:39 +1000553 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100555 ret = BEGIN_LP_RING(10);
556 if (ret)
557 return ret;
558
Jesse Barnes585fb112008-07-29 11:54:06 -0700559 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000560 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dave Airlieaf6061a2008-05-07 12:15:39 +1000562 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
563 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200564 if (dev_priv->dri1.current_page == 0) {
565 OUT_RING(dev_priv->dri1.back_offset);
566 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000567 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200568 OUT_RING(dev_priv->dri1.front_offset);
569 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000570 }
571 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000572
Dave Airlieaf6061a2008-05-07 12:15:39 +1000573 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
574 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100575
Dave Airlieaf6061a2008-05-07 12:15:39 +1000576 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000577
Daniel Vetter231f42a2012-11-02 19:55:05 +0100578 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000579
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100580 if (BEGIN_LP_RING(4) == 0) {
581 OUT_RING(MI_STORE_DWORD_INDEX);
582 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100583 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100584 OUT_RING(0);
585 ADVANCE_LP_RING();
586 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000587
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200588 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000592static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 i915_kernel_lost_context(dev);
Chris Wilson3e960502012-11-27 16:22:54 +0000595 return intel_ring_idle(LP_RING(dev->dev_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Eric Anholtc153f452007-09-03 12:06:45 +1000598static int i915_flush_ioctl(struct drm_device *dev, void *data,
599 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Eric Anholt546b0972008-09-01 16:45:29 -0700601 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200603 if (drm_core_check_feature(dev, DRIVER_MODESET))
604 return -ENODEV;
605
Eric Anholt546b0972008-09-01 16:45:29 -0700606 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
607
608 mutex_lock(&dev->struct_mutex);
609 ret = i915_quiescent(dev);
610 mutex_unlock(&dev->struct_mutex);
611
612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Eric Anholtc153f452007-09-03 12:06:45 +1000615static int i915_batchbuffer(struct drm_device *dev, void *data,
616 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000619 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000621 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000622 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700624 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200626 if (drm_core_check_feature(dev, DRIVER_MODESET))
627 return -ENODEV;
628
Daniel Vetter87813422012-05-02 11:49:32 +0200629 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000631 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800634 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800635 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Anholt546b0972008-09-01 16:45:29 -0700637 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Eric Anholt201361a2009-03-11 12:30:04 -0700639 if (batch->num_cliprects < 0)
640 return -EINVAL;
641
642 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700643 cliprects = kcalloc(batch->num_cliprects,
644 sizeof(struct drm_clip_rect),
645 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700646 if (cliprects == NULL)
647 return -ENOMEM;
648
649 ret = copy_from_user(cliprects, batch->cliprects,
650 batch->num_cliprects *
651 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200652 if (ret != 0) {
653 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700654 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200655 }
Eric Anholt201361a2009-03-11 12:30:04 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Eric Anholt546b0972008-09-01 16:45:29 -0700658 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700659 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700660 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400662 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000663 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700664
665fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700666 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return ret;
669}
670
Eric Anholtc153f452007-09-03 12:06:45 +1000671static int i915_cmdbuffer(struct drm_device *dev, void *data,
672 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000675 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000677 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000678 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700679 struct drm_clip_rect *cliprects = NULL;
680 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int ret;
682
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800683 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800684 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200686 if (drm_core_check_feature(dev, DRIVER_MODESET))
687 return -ENODEV;
688
Eric Anholt546b0972008-09-01 16:45:29 -0700689 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Eric Anholt201361a2009-03-11 12:30:04 -0700691 if (cmdbuf->num_cliprects < 0)
692 return -EINVAL;
693
Eric Anholt9a298b22009-03-24 12:23:04 -0700694 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700695 if (batch_data == NULL)
696 return -ENOMEM;
697
698 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200699 if (ret != 0) {
700 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700701 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200702 }
Eric Anholt201361a2009-03-11 12:30:04 -0700703
704 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700705 cliprects = kcalloc(cmdbuf->num_cliprects,
706 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000707 if (cliprects == NULL) {
708 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700709 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000710 }
Eric Anholt201361a2009-03-11 12:30:04 -0700711
712 ret = copy_from_user(cliprects, cmdbuf->cliprects,
713 cmdbuf->num_cliprects *
714 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200715 if (ret != 0) {
716 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700717 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Eric Anholt546b0972008-09-01 16:45:29 -0700721 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700722 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700723 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (ret) {
725 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000726 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400729 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000730 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700731
Eric Anholt201361a2009-03-11 12:30:04 -0700732fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700733 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000734fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700735 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700736
737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Daniel Vetter94888672012-04-26 23:28:08 +0200740static int i915_emit_irq(struct drm_device * dev)
741{
742 drm_i915_private_t *dev_priv = dev->dev_private;
743 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
744
745 i915_kernel_lost_context(dev);
746
747 DRM_DEBUG_DRIVER("\n");
748
Daniel Vetter231f42a2012-11-02 19:55:05 +0100749 dev_priv->dri1.counter++;
750 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
751 dev_priv->dri1.counter = 1;
Daniel Vetter94888672012-04-26 23:28:08 +0200752 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100753 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200754
755 if (BEGIN_LP_RING(4) == 0) {
756 OUT_RING(MI_STORE_DWORD_INDEX);
757 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100758 OUT_RING(dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200759 OUT_RING(MI_USER_INTERRUPT);
760 ADVANCE_LP_RING();
761 }
762
Daniel Vetter231f42a2012-11-02 19:55:05 +0100763 return dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200764}
765
766static int i915_wait_irq(struct drm_device * dev, int irq_nr)
767{
768 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
769 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
770 int ret = 0;
771 struct intel_ring_buffer *ring = LP_RING(dev_priv);
772
773 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
774 READ_BREADCRUMB(dev_priv));
775
776 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
777 if (master_priv->sarea_priv)
778 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
779 return 0;
780 }
781
782 if (master_priv->sarea_priv)
783 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
784
785 if (ring->irq_get(ring)) {
786 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
787 READ_BREADCRUMB(dev_priv) >= irq_nr);
788 ring->irq_put(ring);
789 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
790 ret = -EBUSY;
791
792 if (ret == -EBUSY) {
793 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Daniel Vetter231f42a2012-11-02 19:55:05 +0100794 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200795 }
796
797 return ret;
798}
799
800/* Needs the lock as it touches the ring.
801 */
802static int i915_irq_emit(struct drm_device *dev, void *data,
803 struct drm_file *file_priv)
804{
805 drm_i915_private_t *dev_priv = dev->dev_private;
806 drm_i915_irq_emit_t *emit = data;
807 int result;
808
809 if (drm_core_check_feature(dev, DRIVER_MODESET))
810 return -ENODEV;
811
812 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
813 DRM_ERROR("called with no initialization\n");
814 return -EINVAL;
815 }
816
817 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
818
819 mutex_lock(&dev->struct_mutex);
820 result = i915_emit_irq(dev);
821 mutex_unlock(&dev->struct_mutex);
822
823 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
824 DRM_ERROR("copy_to_user\n");
825 return -EFAULT;
826 }
827
828 return 0;
829}
830
831/* Doesn't need the hardware lock.
832 */
833static int i915_irq_wait(struct drm_device *dev, void *data,
834 struct drm_file *file_priv)
835{
836 drm_i915_private_t *dev_priv = dev->dev_private;
837 drm_i915_irq_wait_t *irqwait = data;
838
839 if (drm_core_check_feature(dev, DRIVER_MODESET))
840 return -ENODEV;
841
842 if (!dev_priv) {
843 DRM_ERROR("called with no initialization\n");
844 return -EINVAL;
845 }
846
847 return i915_wait_irq(dev, irqwait->irq_seq);
848}
849
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200850static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
851 struct drm_file *file_priv)
852{
853 drm_i915_private_t *dev_priv = dev->dev_private;
854 drm_i915_vblank_pipe_t *pipe = data;
855
856 if (drm_core_check_feature(dev, DRIVER_MODESET))
857 return -ENODEV;
858
859 if (!dev_priv) {
860 DRM_ERROR("called with no initialization\n");
861 return -EINVAL;
862 }
863
864 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
865
866 return 0;
867}
868
869/**
870 * Schedule buffer swap at given vertical blank.
871 */
872static int i915_vblank_swap(struct drm_device *dev, void *data,
873 struct drm_file *file_priv)
874{
875 /* The delayed swap mechanism was fundamentally racy, and has been
876 * removed. The model was that the client requested a delayed flip/swap
877 * from the kernel, then waited for vblank before continuing to perform
878 * rendering. The problem was that the kernel might wake the client
879 * up before it dispatched the vblank swap (since the lock has to be
880 * held while touching the ringbuffer), in which case the client would
881 * clear and start the next frame before the swap occurred, and
882 * flicker would occur in addition to likely missing the vblank.
883 *
884 * In the absence of this ioctl, userland falls back to a correct path
885 * of waiting for a vblank, then dispatching the swap on its own.
886 * Context switching to userland and back is plenty fast enough for
887 * meeting the requirements of vblank swapping.
888 */
889 return -EINVAL;
890}
891
Eric Anholtc153f452007-09-03 12:06:45 +1000892static int i915_flip_bufs(struct drm_device *dev, void *data,
893 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Eric Anholt546b0972008-09-01 16:45:29 -0700895 int ret;
896
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200897 if (drm_core_check_feature(dev, DRIVER_MODESET))
898 return -ENODEV;
899
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800900 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Eric Anholt546b0972008-09-01 16:45:29 -0700902 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Eric Anholt546b0972008-09-01 16:45:29 -0700904 mutex_lock(&dev->struct_mutex);
905 ret = i915_dispatch_flip(dev);
906 mutex_unlock(&dev->struct_mutex);
907
908 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Eric Anholtc153f452007-09-03 12:06:45 +1000911static int i915_getparam(struct drm_device *dev, void *data,
912 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000915 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int value;
917
918 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000919 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000920 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
Eric Anholtc153f452007-09-03 12:06:45 +1000923 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700925 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
927 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200928 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100930 case I915_PARAM_LAST_DISPATCH:
931 value = READ_BREADCRUMB(dev_priv);
932 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400933 case I915_PARAM_CHIPSET_ID:
934 value = dev->pci_device;
935 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700936 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200937 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700938 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800939 case I915_PARAM_NUM_FENCES_AVAIL:
940 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
941 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200942 case I915_PARAM_HAS_OVERLAY:
943 value = dev_priv->overlay ? 1 : 0;
944 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800945 case I915_PARAM_HAS_PAGEFLIPPING:
946 value = 1;
947 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500948 case I915_PARAM_HAS_EXECBUF2:
949 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200950 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500951 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800952 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100953 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800954 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100955 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100956 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100957 break;
Xiang, Haihaoa1f2cc72013-05-28 19:22:34 -0700958 case I915_PARAM_HAS_VEBOX:
959 value = intel_ring_initialized(&dev_priv->ring[VECS]);
960 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100961 case I915_PARAM_HAS_RELAXED_FENCING:
962 value = 1;
963 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100964 case I915_PARAM_HAS_COHERENT_RINGS:
965 value = 1;
966 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000967 case I915_PARAM_HAS_EXEC_CONSTANTS:
968 value = INTEL_INFO(dev)->gen >= 4;
969 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000970 case I915_PARAM_HAS_RELAXED_DELTA:
971 value = 1;
972 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800973 case I915_PARAM_HAS_GEN7_SOL_RESET:
974 value = 1;
975 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200976 case I915_PARAM_HAS_LLC:
977 value = HAS_LLC(dev);
978 break;
Chris Wilson651d7942013-08-08 14:41:10 +0100979 case I915_PARAM_HAS_WT:
980 value = HAS_WT(dev);
981 break;
Daniel Vetter777ee962012-02-15 23:50:25 +0100982 case I915_PARAM_HAS_ALIASING_PPGTT:
983 value = dev_priv->mm.aliasing_ppgtt ? 1 : 0;
984 break;
Ben Widawsky172cf152012-06-05 15:24:25 -0700985 case I915_PARAM_HAS_WAIT_TIMEOUT:
986 value = 1;
987 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +0100988 case I915_PARAM_HAS_SEMAPHORES:
989 value = i915_semaphore_is_enabled(dev);
990 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +1000991 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
992 value = 1;
993 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +0100994 case I915_PARAM_HAS_SECURE_BATCHES:
995 value = capable(CAP_SYS_ADMIN);
996 break;
Daniel Vetterb45305f2012-12-17 16:21:27 +0100997 case I915_PARAM_HAS_PINNED_BATCHES:
998 value = 1;
999 break;
Daniel Vettered5982e2013-01-17 22:23:36 +01001000 case I915_PARAM_HAS_EXEC_NO_RELOC:
1001 value = 1;
1002 break;
Chris Wilsoneef90cc2013-01-08 10:53:17 +00001003 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
1004 value = 1;
1005 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 default:
Ben Widawskye29c32d2013-05-31 11:28:45 -07001007 DRM_DEBUG("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001008 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
Eric Anholtc153f452007-09-03 12:06:45 +10001011 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001013 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
1016 return 0;
1017}
1018
Eric Anholtc153f452007-09-03 12:06:45 +10001019static int i915_setparam(struct drm_device *dev, void *data,
1020 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001023 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001026 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001027 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
Eric Anholtc153f452007-09-03 12:06:45 +10001030 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 break;
1033 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
1035 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001036 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001038 case I915_SETPARAM_NUM_USED_FENCES:
1039 if (param->value > dev_priv->num_fence_regs ||
1040 param->value < 0)
1041 return -EINVAL;
1042 /* Userspace can use first N regs */
1043 dev_priv->fence_reg_start = param->value;
1044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001046 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001047 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001048 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
1051 return 0;
1052}
1053
Eric Anholtc153f452007-09-03 12:06:45 +10001054static int i915_set_status_page(struct drm_device *dev, void *data,
1055 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001056{
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001057 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001058 drm_i915_hws_addr_t *hws = data;
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001059 struct intel_ring_buffer *ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001060
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001061 if (drm_core_check_feature(dev, DRIVER_MODESET))
1062 return -ENODEV;
1063
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001064 if (!I915_NEED_GFX_HWS(dev))
1065 return -EINVAL;
1066
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001067 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001068 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001069 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001070 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001071
Jesse Barnes79e53942008-11-07 14:24:08 -08001072 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1073 WARN(1, "tried to set status page when mode setting active\n");
1074 return 0;
1075 }
1076
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001077 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001078
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001079 ring = LP_RING(dev_priv);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001080 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001081
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001082 dev_priv->dri1.gfx_hws_cpu_addr =
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001083 ioremap_wc(dev_priv->gtt.mappable_base + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001084 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001085 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001086 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001087 DRM_ERROR("can not ioremap virtual address for"
1088 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001089 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001090 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001091
1092 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001093 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001094
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001095 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001096 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001097 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001098 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001099 return 0;
1100}
1101
Dave Airlieec2a4c32009-08-04 11:43:41 +10001102static int i915_get_bridge_dev(struct drm_device *dev)
1103{
1104 struct drm_i915_private *dev_priv = dev->dev_private;
1105
Akshay Joshi0206e352011-08-16 15:34:10 -04001106 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001107 if (!dev_priv->bridge_dev) {
1108 DRM_ERROR("bridge device not found\n");
1109 return -1;
1110 }
1111 return 0;
1112}
1113
Zhenyu Wangc48044112009-12-17 14:48:43 +08001114#define MCHBAR_I915 0x44
1115#define MCHBAR_I965 0x48
1116#define MCHBAR_SIZE (4*4096)
1117
1118#define DEVEN_REG 0x54
1119#define DEVEN_MCHBAR_EN (1 << 28)
1120
1121/* Allocate space for the MCH regs if needed, return nonzero on error */
1122static int
1123intel_alloc_mchbar_resource(struct drm_device *dev)
1124{
1125 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001126 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001127 u32 temp_lo, temp_hi = 0;
1128 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001129 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001130
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001131 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001132 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1133 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1134 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1135
1136 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1137#ifdef CONFIG_PNP
1138 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001139 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1140 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001141#endif
1142
1143 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001144 dev_priv->mch_res.name = "i915 MCHBAR";
1145 dev_priv->mch_res.flags = IORESOURCE_MEM;
1146 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1147 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001148 MCHBAR_SIZE, MCHBAR_SIZE,
1149 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001150 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001151 dev_priv->bridge_dev);
1152 if (ret) {
1153 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1154 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001155 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001156 }
1157
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001158 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001159 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1160 upper_32_bits(dev_priv->mch_res.start));
1161
1162 pci_write_config_dword(dev_priv->bridge_dev, reg,
1163 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001164 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001165}
1166
1167/* Setup MCHBAR if possible, return true if we should disable it again */
1168static void
1169intel_setup_mchbar(struct drm_device *dev)
1170{
1171 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001172 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001173 u32 temp;
1174 bool enabled;
1175
1176 dev_priv->mchbar_need_disable = false;
1177
1178 if (IS_I915G(dev) || IS_I915GM(dev)) {
1179 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1180 enabled = !!(temp & DEVEN_MCHBAR_EN);
1181 } else {
1182 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1183 enabled = temp & 1;
1184 }
1185
1186 /* If it's already enabled, don't have to do anything */
1187 if (enabled)
1188 return;
1189
1190 if (intel_alloc_mchbar_resource(dev))
1191 return;
1192
1193 dev_priv->mchbar_need_disable = true;
1194
1195 /* Space is allocated or reserved, so enable it. */
1196 if (IS_I915G(dev) || IS_I915GM(dev)) {
1197 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1198 temp | DEVEN_MCHBAR_EN);
1199 } else {
1200 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1201 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1202 }
1203}
1204
1205static void
1206intel_teardown_mchbar(struct drm_device *dev)
1207{
1208 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001209 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001210 u32 temp;
1211
1212 if (dev_priv->mchbar_need_disable) {
1213 if (IS_I915G(dev) || IS_I915GM(dev)) {
1214 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1215 temp &= ~DEVEN_MCHBAR_EN;
1216 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1217 } else {
1218 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1219 temp &= ~1;
1220 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1221 }
1222 }
1223
1224 if (dev_priv->mch_res.start)
1225 release_resource(&dev_priv->mch_res);
1226}
1227
Dave Airlie28d52042009-09-21 14:33:58 +10001228/* true = enable decode, false = disable decoder */
1229static unsigned int i915_vga_set_decode(void *cookie, bool state)
1230{
1231 struct drm_device *dev = cookie;
1232
1233 intel_modeset_vga_set_state(dev, state);
1234 if (state)
1235 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1236 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1237 else
1238 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1239}
1240
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001241static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1242{
1243 struct drm_device *dev = pci_get_drvdata(pdev);
1244 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1245 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001246 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001247 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001248 /* i915 resume handler doesn't set to D0 */
1249 pci_set_power_state(dev->pdev, PCI_D0);
1250 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001251 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001252 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001253 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001254 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001255 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001256 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001257 }
1258}
1259
1260static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1261{
1262 struct drm_device *dev = pci_get_drvdata(pdev);
1263 bool can_switch;
1264
1265 spin_lock(&dev->count_lock);
1266 can_switch = (dev->open_count == 0);
1267 spin_unlock(&dev->count_lock);
1268 return can_switch;
1269}
1270
Takashi Iwai26ec6852012-05-11 07:51:17 +02001271static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1272 .set_gpu_state = i915_switcheroo_set_state,
1273 .reprobe = NULL,
1274 .can_switch = i915_switcheroo_can_switch,
1275};
1276
Chris Wilson2c7111d2011-03-29 10:40:27 +01001277static int i915_load_modeset_init(struct drm_device *dev)
1278{
1279 struct drm_i915_private *dev_priv = dev->dev_private;
1280 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001281
Bryan Freed6d139a82010-10-14 09:14:51 +01001282 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001283 if (ret)
1284 DRM_INFO("failed to find VBIOS tables\n");
1285
Chris Wilson934f9922011-01-20 13:09:12 +00001286 /* If we have > 1 VGA cards, then we need to arbitrate access
1287 * to the common VGA resources.
1288 *
1289 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1290 * then we do not take part in VGA arbitration and the
1291 * vga_client_register() fails with -ENODEV.
1292 */
Alex Williamson81b5c7b2013-08-28 09:39:08 -06001293 if (!HAS_PCH_SPLIT(dev)) {
1294 ret = vga_client_register(dev->pdev, dev, NULL,
1295 i915_vga_set_decode);
1296 if (ret && ret != -ENODEV)
1297 goto out;
1298 }
Dave Airlie28d52042009-09-21 14:33:58 +10001299
Jesse Barnes723bfd72010-10-07 16:01:13 -07001300 intel_register_dsm_handler();
1301
Dave Airlie0d697042012-09-10 12:28:36 +10001302 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001303 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001304 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001305
Chris Wilson9797fbf2012-04-24 15:47:39 +01001306 /* Initialise stolen first so that we may reserve preallocated
1307 * objects for the BIOS to KMS transition.
1308 */
1309 ret = i915_gem_init_stolen(dev);
1310 if (ret)
1311 goto cleanup_vga_switcheroo;
1312
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001313 ret = drm_irq_install(dev);
1314 if (ret)
1315 goto cleanup_gem_stolen;
1316
1317 /* Important: The output setup functions called by modeset_init need
1318 * working irqs for e.g. gmbus and dp aux transfers. */
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001319 intel_modeset_init(dev);
1320
Chris Wilson1070a422012-04-24 15:47:41 +01001321 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001322 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001323 goto cleanup_irq;
Chris Wilson2c7111d2011-03-29 10:40:27 +01001324
Jesse Barnes073f34d2012-11-02 11:13:59 -07001325 INIT_WORK(&dev_priv->console_resume_work, intel_console_resume);
1326
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001327 intel_modeset_gem_init(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001328
Jesse Barnes79e53942008-11-07 14:24:08 -08001329 /* Always safe in the mode setting case. */
1330 /* FIXME: do pre/post-mode set stuff in core KMS code */
1331 dev->vblank_disable_allowed = 1;
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001332 if (INTEL_INFO(dev)->num_pipes == 0)
Ben Widawskye3c74752013-04-05 13:12:39 -07001333 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001334
Chris Wilson5a793952010-06-06 10:50:03 +01001335 ret = intel_fbdev_init(dev);
1336 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001337 goto cleanup_gem;
1338
1339 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter20afbda2012-12-11 14:05:07 +01001340 intel_hpd_init(dev);
1341
1342 /*
1343 * Some ports require correctly set-up hpd registers for detection to
1344 * work properly (leading to ghost connected connector status), e.g. VGA
1345 * on gm45. Hence we can only set up the initial fbdev config after hpd
1346 * irqs are fully enabled. Now we should scan for the initial config
1347 * only once hotplug handling is enabled, but due to screwed-up locking
1348 * around kms/fbdev init we can't protect the fdbev initial config
1349 * scanning against hotplug events. Hence do this first and ignore the
1350 * tiny window where we will loose hotplug notifactions.
1351 */
1352 intel_fbdev_initial_config(dev);
1353
Ville Syrjälä6e1b4fd2013-09-05 20:40:52 +03001354 /*
1355 * Must do this after fbcon init so that
1356 * vgacon_save_screen() works during the handover.
1357 */
1358 i915_disable_vga_mem(dev);
1359
Daniel Vetter20afbda2012-12-11 14:05:07 +01001360 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001361 dev_priv->enable_hotplug_processing = true;
Chris Wilson5a793952010-06-06 10:50:03 +01001362
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001363 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001364
Jesse Barnes79e53942008-11-07 14:24:08 -08001365 return 0;
1366
Chris Wilson2c7111d2011-03-29 10:40:27 +01001367cleanup_gem:
1368 mutex_lock(&dev->struct_mutex);
1369 i915_gem_cleanup_ringbuffer(dev);
Ben Widawsky55d23282013-05-25 12:26:39 -07001370 i915_gem_context_fini(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001371 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001372 i915_gem_cleanup_aliasing_ppgtt(dev);
Ben Widawsky93bd8642013-07-16 16:50:06 -07001373 drm_mm_takedown(&dev_priv->gtt.base.mm);
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001374cleanup_irq:
1375 drm_irq_uninstall(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001376cleanup_gem_stolen:
1377 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001378cleanup_vga_switcheroo:
1379 vga_switcheroo_unregister_client(dev->pdev);
1380cleanup_vga_client:
1381 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001382out:
1383 return ret;
1384}
1385
Dave Airlie7c1c2872008-11-28 14:22:24 +10001386int i915_master_create(struct drm_device *dev, struct drm_master *master)
1387{
1388 struct drm_i915_master_private *master_priv;
1389
Eric Anholt9a298b22009-03-24 12:23:04 -07001390 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001391 if (!master_priv)
1392 return -ENOMEM;
1393
1394 master->driver_priv = master_priv;
1395 return 0;
1396}
1397
1398void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1399{
1400 struct drm_i915_master_private *master_priv = master->driver_priv;
1401
1402 if (!master_priv)
1403 return;
1404
Eric Anholt9a298b22009-03-24 12:23:04 -07001405 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001406
1407 master->driver_priv = NULL;
1408}
1409
Daniel Vettere1887192012-06-12 11:28:17 +02001410static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1411{
1412 struct apertures_struct *ap;
1413 struct pci_dev *pdev = dev_priv->dev->pdev;
1414 bool primary;
1415
1416 ap = alloc_apertures(1);
1417 if (!ap)
1418 return;
1419
Ben Widawskydabb7a92013-01-17 12:45:16 -08001420 ap->ranges[0].base = dev_priv->gtt.mappable_base;
Ben Widawskyf64e2922013-05-25 12:26:36 -07001421 ap->ranges[0].size = dev_priv->gtt.mappable_end;
Ben Widawsky93d18792013-01-17 12:45:17 -08001422
Daniel Vettere1887192012-06-12 11:28:17 +02001423 primary =
1424 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1425
1426 remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
1427
1428 kfree(ap);
1429}
1430
Daniel Vetterc96ea642012-08-08 22:01:51 +02001431static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1432{
1433 const struct intel_device_info *info = dev_priv->info;
1434
Damien Lespiaue2a58002013-04-23 16:38:34 +01001435#define PRINT_S(name) "%s"
1436#define SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001437#define PRINT_FLAG(name) info->name ? #name "," : ""
1438#define SEP_COMMA ,
Daniel Vetterc96ea642012-08-08 22:01:51 +02001439 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
Damien Lespiaue2a58002013-04-23 16:38:34 +01001440 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
Daniel Vetterc96ea642012-08-08 22:01:51 +02001441 info->gen,
1442 dev_priv->dev->pdev->device,
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001443 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
Damien Lespiaue2a58002013-04-23 16:38:34 +01001444#undef PRINT_S
1445#undef SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001446#undef PRINT_FLAG
1447#undef SEP_COMMA
Daniel Vetterc96ea642012-08-08 22:01:51 +02001448}
1449
Eric Anholt63ee41d2010-12-20 18:40:06 -08001450/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001451 * i915_driver_load - setup chip and create an initial config
1452 * @dev: DRM device
1453 * @flags: startup flags
1454 *
1455 * The driver load routine has to do several things:
1456 * - drive output discovery via intel_modeset_init()
1457 * - initialize the memory manager
1458 * - allocate initial config memory
1459 * - setup the DRM framebuffer with the allocated memory
1460 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001461int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001462{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001463 struct drm_i915_private *dev_priv;
Daniel Vetter26394d92012-03-26 21:33:18 +02001464 struct intel_device_info *info;
Chris Wilson934d6082012-09-14 11:57:46 +01001465 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001466 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001467
Daniel Vetter26394d92012-03-26 21:33:18 +02001468 info = (struct intel_device_info *) flags;
1469
1470 /* Refuse to load on gen6+ without kms enabled. */
1471 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET))
1472 return -ENODEV;
1473
Dave Airlie22eae942005-11-10 22:16:34 +11001474 /* i915 has 4 more counters */
1475 dev->counters += 4;
1476 dev->types[6] = _DRM_STAT_IRQ;
1477 dev->types[7] = _DRM_STAT_PRIMARY;
1478 dev->types[8] = _DRM_STAT_SECONDARY;
1479 dev->types[9] = _DRM_STAT_DMA;
1480
Eric Anholt9a298b22009-03-24 12:23:04 -07001481 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001482 if (dev_priv == NULL)
1483 return -ENOMEM;
1484
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001485 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001486 dev_priv->dev = dev;
Daniel Vetter26394d92012-03-26 21:33:18 +02001487 dev_priv->info = info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001488
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001489 spin_lock_init(&dev_priv->irq_lock);
1490 spin_lock_init(&dev_priv->gpu_error.lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001491 spin_lock_init(&dev_priv->backlight.lock);
Chris Wilson907b28c2013-07-19 20:36:52 +01001492 spin_lock_init(&dev_priv->uncore.lock);
Daniel Vetterc20e8352013-07-24 22:40:23 +02001493 spin_lock_init(&dev_priv->mm.object_stat_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001494 mutex_init(&dev_priv->dpio_lock);
1495 mutex_init(&dev_priv->rps.hw_lock);
1496 mutex_init(&dev_priv->modeset_restore_lock);
1497
Paulo Zanonic67a4702013-08-19 13:18:09 -03001498 mutex_init(&dev_priv->pc8.lock);
1499 dev_priv->pc8.requirements_met = false;
1500 dev_priv->pc8.gpu_idle = false;
1501 dev_priv->pc8.irqs_disabled = false;
1502 dev_priv->pc8.enabled = false;
1503 dev_priv->pc8.disable_count = 2; /* requirements_met + gpu_idle */
1504 INIT_DELAYED_WORK(&dev_priv->pc8.enable_work, hsw_enable_pc8_work);
1505
Daniel Vetterc96ea642012-08-08 22:01:51 +02001506 i915_dump_device_info(dev_priv);
1507
Paulo Zanonied1c9e22013-08-12 14:34:08 -03001508 /* Not all pre-production machines fall into this category, only the
1509 * very first ones. Almost everything should work, except for maybe
1510 * suspend/resume. And we don't implement workarounds that affect only
1511 * pre-production machines. */
1512 if (IS_HSW_EARLY_SDV(dev))
1513 DRM_INFO("This is an early pre-production Haswell machine. "
1514 "It may not be fully functional.\n");
1515
Dave Airlieec2a4c32009-08-04 11:43:41 +10001516 if (i915_get_bridge_dev(dev)) {
1517 ret = -EIO;
1518 goto free_priv;
1519 }
1520
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001521 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1522 /* Before gen4, the registers and the GTT are behind different BARs.
1523 * However, from gen4 onwards, the registers and the GTT are shared
1524 * in the same BAR, so we want to restrict this ioremap from
1525 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1526 * the register BAR remains the same size for all the earlier
1527 * generations up to Ironlake.
1528 */
1529 if (info->gen < 5)
1530 mmio_size = 512*1024;
1531 else
1532 mmio_size = 2*1024*1024;
1533
1534 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
1535 if (!dev_priv->regs) {
1536 DRM_ERROR("failed to map registers\n");
1537 ret = -EIO;
1538 goto put_bridge;
1539 }
1540
Chris Wilson907b28c2013-07-19 20:36:52 +01001541 intel_uncore_early_sanitize(dev);
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001542
Ben Widawsky59124502013-07-04 11:02:05 -07001543 if (IS_HASWELL(dev) && (I915_READ(HSW_EDRAM_PRESENT) == 1)) {
1544 /* The docs do not explain exactly how the calculation can be
1545 * made. It is somewhat guessable, but for now, it's always
1546 * 128MB.
1547 * NB: We can't write IDICR yet because we do not have gt funcs
1548 * set up */
1549 dev_priv->ellc_size = 128;
1550 DRM_INFO("Found %zuMB of eLLC\n", dev_priv->ellc_size);
1551 }
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001552
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001553 ret = i915_gem_gtt_init(dev);
1554 if (ret)
Daniel Vettere1887192012-06-12 11:28:17 +02001555 goto put_bridge;
Daniel Vettere1887192012-06-12 11:28:17 +02001556
Chris Wilson16233922012-10-26 12:06:41 +01001557 if (drm_core_check_feature(dev, DRIVER_MODESET))
1558 i915_kick_out_firmware_fb(dev_priv);
Daniel Vettere1887192012-06-12 11:28:17 +02001559
Dave Airlie466e69b2011-12-19 11:15:29 +00001560 pci_set_master(dev->pdev);
1561
Daniel Vetter9f82d232010-08-30 21:25:23 +02001562 /* overlay on gen2 is broken and can't address above 1G */
1563 if (IS_GEN2(dev))
1564 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1565
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001566 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1567 * using 32bit addressing, overwriting memory if HWS is located
1568 * above 4GB.
1569 *
1570 * The documentation also mentions an issue with undefined
1571 * behaviour if any general state is accessed within a page above 4GB,
1572 * which also needs to be handled carefully.
1573 */
1574 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1575 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1576
Ben Widawsky93d18792013-01-17 12:45:17 -08001577 aperture_size = dev_priv->gtt.mappable_end;
Chris Wilson71e93392010-10-27 18:46:52 +01001578
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001579 dev_priv->gtt.mappable =
1580 io_mapping_create_wc(dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001581 aperture_size);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001582 if (dev_priv->gtt.mappable == NULL) {
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001583 ret = -EIO;
Daniel Vettere1887192012-06-12 11:28:17 +02001584 goto out_rmmap;
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001585 }
1586
Ben Widawsky911bdf02013-06-27 16:30:23 -07001587 dev_priv->gtt.mtrr = arch_phys_wc_add(dev_priv->gtt.mappable_base,
1588 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001589
Chris Wilsone642abb2010-09-09 12:46:34 +01001590 /* The i915 workqueue is primarily used for batched retirement of
1591 * requests (and thus managing bo) once the task has been completed
1592 * by the GPU. i915_gem_retire_requests() is called directly when we
1593 * need high-priority retirement, such as waiting for an explicit
1594 * bo.
1595 *
1596 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001597 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001598 *
1599 * All tasks on the workqueue are expected to acquire the dev mutex
1600 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001601 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001602 */
Tejun Heo53621862012-08-22 16:40:57 -07001603 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001604 if (dev_priv->wq == NULL) {
1605 DRM_ERROR("Failed to create our workqueue.\n");
1606 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001607 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001608 }
1609
Paulo Zanoni45e6e3a2012-07-03 15:57:32 -03001610 /* This must be called before any calls to HAS_PCH_* */
1611 intel_detect_pch(dev);
1612
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001613 intel_irq_init(dev);
Chris Wilson907b28c2013-07-19 20:36:52 +01001614 intel_pm_init(dev);
1615 intel_uncore_sanitize(dev);
1616 intel_uncore_init(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001617
Zhenyu Wangc48044112009-12-17 14:48:43 +08001618 /* Try to make sure MCHBAR is enabled before poking at it */
1619 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001620 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001621 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001622
Bryan Freed6d139a82010-10-14 09:14:51 +01001623 intel_setup_bios(dev);
1624
Eric Anholt673a3942008-07-30 12:06:12 -07001625 i915_gem_load(dev);
1626
Eric Anholted4cb412008-07-29 12:10:39 -07001627 /* On the 945G/GM, the chipset reports the MSI capability on the
1628 * integrated graphics even though the support isn't actually there
1629 * according to the published specs. It doesn't appear to function
1630 * correctly in testing on 945G.
1631 * This may be a side effect of MSI having been made available for PEG
1632 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001633 *
1634 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001635 * be lost or delayed, but we use them anyways to avoid
1636 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001637 */
Keith Packardb60678a2008-12-08 11:12:28 -08001638 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001639 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001640
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001641 dev_priv->num_plane = 1;
1642 if (IS_VALLEYVIEW(dev))
1643 dev_priv->num_plane = 2;
1644
Ben Widawskye3c74752013-04-05 13:12:39 -07001645 if (INTEL_INFO(dev)->num_pipes) {
1646 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1647 if (ret)
1648 goto out_gem_unload;
1649 }
Keith Packard52440212008-11-18 09:30:25 -08001650
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001651 if (HAS_POWER_WELL(dev))
1652 i915_init_power_well(dev);
1653
Jesse Barnes79e53942008-11-07 14:24:08 -08001654 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001655 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001656 if (ret < 0) {
1657 DRM_ERROR("failed to init modeset\n");
Chris Wilson56e2ea32010-11-08 17:10:29 +00001658 goto out_gem_unload;
Jesse Barnes79e53942008-11-07 14:24:08 -08001659 }
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001660 } else {
1661 /* Start out suspended in ums mode. */
1662 dev_priv->ums.mm_suspended = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001663 }
1664
Ben Widawsky0136db582012-04-10 21:17:01 -07001665 i915_setup_sysfs(dev);
1666
Ben Widawskye3c74752013-04-05 13:12:39 -07001667 if (INTEL_INFO(dev)->num_pipes) {
1668 /* Must be done after probing outputs */
1669 intel_opregion_init(dev);
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001670 acpi_video_register();
Ben Widawskye3c74752013-04-05 13:12:39 -07001671 }
Matthew Garrett74a365b2009-03-19 21:35:39 +00001672
Daniel Vettereb48eb02012-04-26 23:28:12 +02001673 if (IS_GEN5(dev))
1674 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001675
Jesse Barnes79e53942008-11-07 14:24:08 -08001676 return 0;
1677
Chris Wilson56e2ea32010-11-08 17:10:29 +00001678out_gem_unload:
Dave Chinner7dc19d52013-08-28 10:18:11 +10001679 if (dev_priv->mm.inactive_shrinker.scan_objects)
Keith Packarda7b85d22011-07-10 13:12:17 -07001680 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1681
Chris Wilson56e2ea32010-11-08 17:10:29 +00001682 if (dev->pdev->msi_enabled)
1683 pci_disable_msi(dev->pdev);
1684
1685 intel_teardown_gmbus(dev);
1686 intel_teardown_mchbar(dev);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001687 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001688out_mtrrfree:
Ben Widawsky911bdf02013-06-27 16:30:23 -07001689 arch_phys_wc_del(dev_priv->gtt.mtrr);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001690 io_mapping_free(dev_priv->gtt.mappable);
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001691 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
Jesse Barnes79e53942008-11-07 14:24:08 -08001692out_rmmap:
Chris Wilson6dda5692010-10-29 21:02:18 +01001693 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001694put_bridge:
1695 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001696free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001697 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001698 return ret;
1699}
1700
1701int i915_driver_unload(struct drm_device *dev)
1702{
1703 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001704 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001705
Daniel Vettereb48eb02012-04-26 23:28:12 +02001706 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001707
Paulo Zanoni79f8dea2013-08-14 14:40:37 -03001708 if (HAS_POWER_WELL(dev)) {
1709 /* The i915.ko module is still not prepared to be loaded when
1710 * the power well is not enabled, so just enable it in case
1711 * we're going to unload/reload. */
1712 intel_set_power_well(dev, true);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001713 i915_remove_power_well(dev);
Paulo Zanoni79f8dea2013-08-14 14:40:37 -03001714 }
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001715
Ben Widawsky0136db582012-04-10 21:17:01 -07001716 i915_teardown_sysfs(dev);
1717
Dave Chinner7dc19d52013-08-28 10:18:11 +10001718 if (dev_priv->mm.inactive_shrinker.scan_objects)
Chris Wilson17250b72010-10-28 12:51:39 +01001719 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);
Ben Widawsky911bdf02013-06-27 16:30:23 -07001732 arch_phys_wc_del(dev_priv->gtt.mtrr);
Eric Anholtab657db12009-01-23 12:57:47 -08001733
Chris Wilson44834a62010-08-19 16:09:23 +01001734 acpi_video_unregister();
1735
Jesse Barnes79e53942008-11-07 14:24:08 -08001736 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01001737 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001738 intel_modeset_cleanup(dev);
Jesse Barnes073f34d2012-11-02 11:13:59 -07001739 cancel_work_sync(&dev_priv->console_resume_work);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001740
Zhao Yakui6363ee62009-11-24 09:48:44 +08001741 /*
1742 * free the memory space allocated for the child device
1743 * config parsed from VBT
1744 */
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03001745 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1746 kfree(dev_priv->vbt.child_dev);
1747 dev_priv->vbt.child_dev = NULL;
1748 dev_priv->vbt.child_dev_num = 0;
Zhao Yakui6363ee62009-11-24 09:48:44 +08001749 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001750
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001751 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001752 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001753 }
1754
Daniel Vettera8b48992010-08-20 21:25:11 +02001755 /* Free error state after interrupts are fully disabled. */
Daniel Vetter99584db2012-11-14 17:14:04 +01001756 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
1757 cancel_work_sync(&dev_priv->gpu_error.work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001758 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001759
Paulo Zanonic67a4702013-08-19 13:18:09 -03001760 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
1761
Eric Anholted4cb412008-07-29 12:10:39 -07001762 if (dev->pdev->msi_enabled)
1763 pci_disable_msi(dev->pdev);
1764
Chris Wilson44834a62010-08-19 16:09:23 +01001765 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001766
Jesse Barnes79e53942008-11-07 14:24:08 -08001767 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001768 /* Flush any outstanding unpin_work. */
1769 flush_workqueue(dev_priv->wq);
1770
Jesse Barnes79e53942008-11-07 14:24:08 -08001771 mutex_lock(&dev->struct_mutex);
Hugh Dickinsecbec532011-06-27 16:18:20 -07001772 i915_gem_free_all_phys_object(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001773 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001774 i915_gem_context_fini(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001775 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001776 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001777 i915_gem_cleanup_stolen(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001778
1779 if (!I915_NEED_GFX_HWS(dev))
1780 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001781 }
1782
Ben Widawskya7bbbd62013-07-16 16:50:07 -07001783 list_del(&dev_priv->gtt.base.global_link);
1784 WARN_ON(!list_empty(&dev_priv->vm_list));
Ben Widawsky93bd8642013-07-16 16:50:06 -07001785 drm_mm_takedown(&dev_priv->gtt.base.mm);
Daniel Vetter701394c2010-10-10 18:54:08 +01001786 if (dev_priv->regs != NULL)
Chris Wilson6dda5692010-10-29 21:02:18 +01001787 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vetter701394c2010-10-10 18:54:08 +01001788
Chris Wilsonf899fc62010-07-20 15:44:45 -07001789 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001790 intel_teardown_mchbar(dev);
1791
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001792 destroy_workqueue(dev_priv->wq);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001793 pm_qos_remove_request(&dev_priv->pm_qos);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001794
Ben Widawsky853ba5d2013-07-16 16:50:05 -07001795 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
Imre Deak6640aab2013-05-22 17:47:13 +03001796
Chris Wilson42dcedd2012-11-15 11:32:30 +00001797 if (dev_priv->slab)
1798 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001799
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001800 pci_dev_put(dev_priv->bridge_dev);
Dave Airlie22eae942005-11-10 22:16:34 +11001801 kfree(dev->dev_private);
1802
1803 return 0;
Eric Anholt673a3942008-07-30 12:06:12 -07001804}
1805
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001806int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001807{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001808 struct drm_i915_file_private *file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001809
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001810 DRM_DEBUG_DRIVER("\n");
Mika Kuoppalae59ec132013-06-12 12:35:28 +03001811 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001812 if (!file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07001813 return -ENOMEM;
1814
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001815 file->driver_priv = file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001816
Chris Wilson1c255952010-09-26 11:03:27 +01001817 spin_lock_init(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001818 INIT_LIST_HEAD(&file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001819
Daniel Vetterdf12c6d2012-06-19 16:52:30 +02001820 idr_init(&file_priv->context_idr);
Ben Widawsky254f9652012-06-04 14:42:42 -07001821
Eric Anholt673a3942008-07-30 12:06:12 -07001822 return 0;
1823}
1824
Jesse Barnes79e53942008-11-07 14:24:08 -08001825/**
1826 * i915_driver_lastclose - clean up after all DRM clients have exited
1827 * @dev: DRM device
1828 *
1829 * Take care of cleaning up after all DRM clients have exited. In the
1830 * mode setting case, we want to restore the kernel's initial mode (just
1831 * in case the last client left us in a bad state).
1832 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001833 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001834 * and DMA structures, since the kernel won't be using them, and clea
1835 * up any GEM state.
1836 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001837void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001839 drm_i915_private_t *dev_priv = dev->dev_private;
1840
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001841 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1842 * goes right around and calls lastclose. Check for this and don't clean
1843 * up anything. */
1844 if (!dev_priv)
1845 return;
1846
1847 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +01001848 intel_fb_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001849 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001850 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001851 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001852
Eric Anholt673a3942008-07-30 12:06:12 -07001853 i915_gem_lastclose(dev);
1854
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001855 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Eric Anholt6c340ea2007-08-25 20:23:09 +10001858void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Ben Widawsky254f9652012-06-04 14:42:42 -07001860 i915_gem_context_close(dev, file_priv);
Eric Anholtb9624422009-06-03 07:27:35 +00001861 i915_gem_release(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001864void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001865{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001866 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001867
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001868 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001869}
1870
Rob Clarkbaa70942013-08-02 13:27:49 -04001871const struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001872 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1873 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1874 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1875 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1876 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1877 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001878 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001879 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001880 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1881 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1882 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001883 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01001884 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02001885 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001886 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
1887 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1888 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1889 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1890 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001891 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001892 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1893 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001894 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1895 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1896 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1897 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001898 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1899 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001900 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1901 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1902 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1903 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1904 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1905 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1906 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1907 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1908 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1909 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001910 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001911 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001912 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1913 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08001914 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1915 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, intel_sprite_get_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001916 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1917 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1918 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1919 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airliec94f7022005-07-07 21:03:38 +10001920};
1921
1922int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001923
Daniel Vetter9021f282012-03-26 09:45:41 +02001924/*
1925 * This is really ugly: Because old userspace abused the linux agp interface to
1926 * manage the gtt, we need to claim that all intel devices are agp. For
1927 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10001928 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001929int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001930{
1931 return 1;
1932}