blob: 054576d5da996cc38ad994c13ac2769b284597cf [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
29#include "drmP.h"
30#include "drm.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080031#include "drm_crtc_helper.h"
32#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "i915_drm.h"
34#include "i915_drv.h"
35
yakui_zhaobe25ed92009-06-02 14:13:55 +080036#define I915_DRV "i915_drv"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* Really want an OS-independent resettable timer. Would like to have
39 * this loop run for (eg) 3 sec, but have the timer reset every time
40 * the head pointer changes, so that EBUSY only happens if the ring
41 * actually stalls for (eg) 3 seconds.
42 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100043int i915_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 drm_i915_private_t *dev_priv = dev->dev_private;
46 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
Keith Packardd3a6d442008-07-30 12:21:20 -070047 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
48 u32 last_acthd = I915_READ(acthd_reg);
49 u32 acthd;
Jesse Barnes585fb112008-07-29 11:54:06 -070050 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int i;
52
Keith Packardd3a6d442008-07-30 12:21:20 -070053 for (i = 0; i < 100000; i++) {
Jesse Barnes585fb112008-07-29 11:54:06 -070054 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Keith Packardd3a6d442008-07-30 12:21:20 -070055 acthd = I915_READ(acthd_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 ring->space = ring->head - (ring->tail + 8);
57 if (ring->space < 0)
58 ring->space += ring->Size;
59 if (ring->space >= n)
60 return 0;
61
Chris Wilson98787c02009-03-06 23:27:52 +000062 if (dev->primary->master) {
63 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
64 if (master_priv->sarea_priv)
65 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
66 }
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if (ring->head != last_head)
70 i = 0;
Keith Packardd3a6d442008-07-30 12:21:20 -070071 if (acthd != last_acthd)
72 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 last_head = ring->head;
Keith Packardd3a6d442008-07-30 12:21:20 -070075 last_acthd = acthd;
76 msleep_interruptible(10);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
Eric Anholt20caafa2007-08-25 19:22:43 +100080 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Keith Packard398c9cb2008-07-30 13:03:43 -070083/**
84 * Sets up the hardware status page for devices that need a physical address
85 * in the register.
86 */
Eric Anholt3043c602008-10-02 12:24:47 -070087static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070088{
89 drm_i915_private_t *dev_priv = dev->dev_private;
90 /* Program Hardware Status Page */
91 dev_priv->status_page_dmah =
92 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
93
94 if (!dev_priv->status_page_dmah) {
95 DRM_ERROR("Can not allocate hardware status page\n");
96 return -ENOMEM;
97 }
98 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
99 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
100
101 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
102
103 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
yakui_zhaobe25ed92009-06-02 14:13:55 +0800104 DRM_DEBUG_DRIVER(I915_DRV, "Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700105 return 0;
106}
107
108/**
109 * Frees the hardware status page, whether it's a physical address or a virtual
110 * address set up by the X Server.
111 */
Eric Anholt3043c602008-10-02 12:24:47 -0700112static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700113{
114 drm_i915_private_t *dev_priv = dev->dev_private;
115 if (dev_priv->status_page_dmah) {
116 drm_pci_free(dev, dev_priv->status_page_dmah);
117 dev_priv->status_page_dmah = NULL;
118 }
119
120 if (dev_priv->status_gfx_addr) {
121 dev_priv->status_gfx_addr = 0;
122 drm_core_ioremapfree(&dev_priv->hws_map, dev);
123 }
124
125 /* Need to rewrite hardware status page */
126 I915_WRITE(HWS_PGA, 0x1ffff000);
127}
128
Dave Airlie84b1fd12007-07-11 15:53:27 +1000129void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000132 struct drm_i915_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
134
Jesse Barnes79e53942008-11-07 14:24:08 -0800135 /*
136 * We should never lose context on the ring with modesetting
137 * as we don't expose it to userspace
138 */
139 if (drm_core_check_feature(dev, DRIVER_MODESET))
140 return;
141
Jesse Barnes585fb112008-07-29 11:54:06 -0700142 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
143 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ring->space = ring->head - (ring->tail + 8);
145 if (ring->space < 0)
146 ring->space += ring->Size;
147
Dave Airlie7c1c2872008-11-28 14:22:24 +1000148 if (!dev->primary->master)
149 return;
150
151 master_priv = dev->primary->master->driver_priv;
152 if (ring->head == ring->tail && master_priv->sarea_priv)
153 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Dave Airlie84b1fd12007-07-11 15:53:27 +1000156static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000158 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* Make sure interrupts are disabled here because the uninstall ioctl
160 * may not have been called from userspace and after dev_private
161 * is freed, it's too late.
162 */
Eric Anholted4cb412008-07-29 12:10:39 -0700163 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000166 if (dev_priv->ring.virtual_start) {
167 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Eric Anholt3043c602008-10-02 12:24:47 -0700168 dev_priv->ring.virtual_start = NULL;
169 dev_priv->ring.map.handle = NULL;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000170 dev_priv->ring.map.size = 0;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Keith Packard398c9cb2008-07-30 13:03:43 -0700173 /* Clear the HWS virtual address at teardown */
174 if (I915_NEED_GFX_HWS(dev))
175 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 return 0;
178}
179
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000180static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000182 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000183 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
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 {
yakui_zhaobe25ed92009-06-02 14:13:55 +0800190 DRM_DEBUG_DRIVER(I915_DRV,
191 "sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000192 }
193
Eric Anholt673a3942008-07-30 12:06:12 -0700194 if (init->ring_size != 0) {
195 if (dev_priv->ring.ring_obj != NULL) {
196 i915_dma_cleanup(dev);
197 DRM_ERROR("Client tried to initialize ringbuffer in "
198 "GEM mode\n");
199 return -EINVAL;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Eric Anholt673a3942008-07-30 12:06:12 -0700202 dev_priv->ring.Size = init->ring_size;
203 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Eric Anholt673a3942008-07-30 12:06:12 -0700205 dev_priv->ring.map.offset = init->ring_start;
206 dev_priv->ring.map.size = init->ring_size;
207 dev_priv->ring.map.type = 0;
208 dev_priv->ring.map.flags = 0;
209 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Jesse Barnes6fb88582009-02-23 10:08:21 +1000211 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700212
213 if (dev_priv->ring.map.handle == NULL) {
214 i915_dma_cleanup(dev);
215 DRM_ERROR("can not ioremap virtual address for"
216 " ring buffer\n");
217 return -ENOMEM;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
221 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
222
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000223 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dev_priv->back_offset = init->back_offset;
225 dev_priv->front_offset = init->front_offset;
226 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000227 if (master_priv->sarea_priv)
228 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Allow hardware batchbuffers unless told otherwise.
231 */
232 dev_priv->allow_batchbuffer = 1;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
235}
236
Dave Airlie84b1fd12007-07-11 15:53:27 +1000237static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
240
yakui_zhaobe25ed92009-06-02 14:13:55 +0800241 DRM_DEBUG_DRIVER(I915_DRV, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (dev_priv->ring.map.handle == NULL) {
244 DRM_ERROR("can not ioremap virtual address for"
245 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000246 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 /* Program Hardware Status Page */
250 if (!dev_priv->hw_status_page) {
251 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000252 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
yakui_zhaobe25ed92009-06-02 14:13:55 +0800254 DRM_DEBUG_DRIVER(I915_DRV, "hw status page @ %p\n",
255 dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000257 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700258 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000259 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700260 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
yakui_zhaobe25ed92009-06-02 14:13:55 +0800261 DRM_DEBUG_DRIVER(I915_DRV, "Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return 0;
264}
265
Eric Anholtc153f452007-09-03 12:06:45 +1000266static int i915_dma_init(struct drm_device *dev, void *data,
267 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Eric Anholtc153f452007-09-03 12:06:45 +1000269 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int retcode = 0;
271
Eric Anholtc153f452007-09-03 12:06:45 +1000272 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000274 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 case I915_CLEANUP_DMA:
277 retcode = i915_dma_cleanup(dev);
278 break;
279 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100280 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000283 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 }
286
287 return retcode;
288}
289
290/* Implement basically the same security restrictions as hardware does
291 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
292 *
293 * Most of the calculations below involve calculating the size of a
294 * particular instruction. It's important to get the size right as
295 * that tells us where the next instruction to check is. Any illegal
296 * instruction detected will be given a size of zero, which is a
297 * signal to abort the rest of the buffer.
298 */
299static int do_validate_cmd(int cmd)
300{
301 switch (((cmd >> 29) & 0x7)) {
302 case 0x0:
303 switch ((cmd >> 23) & 0x3f) {
304 case 0x0:
305 return 1; /* MI_NOOP */
306 case 0x4:
307 return 1; /* MI_FLUSH */
308 default:
309 return 0; /* disallow everything else */
310 }
311 break;
312 case 0x1:
313 return 0; /* reserved */
314 case 0x2:
315 return (cmd & 0xff) + 2; /* 2d commands */
316 case 0x3:
317 if (((cmd >> 24) & 0x1f) <= 0x18)
318 return 1;
319
320 switch ((cmd >> 24) & 0x1f) {
321 case 0x1c:
322 return 1;
323 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 case 0x3:
326 return (cmd & 0x1f) + 2;
327 case 0x4:
328 return (cmd & 0xf) + 2;
329 default:
330 return (cmd & 0xffff) + 2;
331 }
332 case 0x1e:
333 if (cmd & (1 << 23))
334 return (cmd & 0xffff) + 1;
335 else
336 return 1;
337 case 0x1f:
338 if ((cmd & (1 << 23)) == 0) /* inline vertices */
339 return (cmd & 0x1ffff) + 2;
340 else if (cmd & (1 << 17)) /* indirect random */
341 if ((cmd & 0xffff) == 0)
342 return 0; /* unknown length, too hard */
343 else
344 return (((cmd & 0xffff) + 1) / 2) + 1;
345 else
346 return 2; /* indirect sequential */
347 default:
348 return 0;
349 }
350 default:
351 return 0;
352 }
353
354 return 0;
355}
356
357static int validate_cmd(int cmd)
358{
359 int ret = do_validate_cmd(cmd);
360
Dave Airliebc5f4522007-11-05 12:50:58 +1000361/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 return ret;
364}
365
Eric Anholt201361a2009-03-11 12:30:04 -0700366static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 drm_i915_private_t *dev_priv = dev->dev_private;
369 int i;
370 RING_LOCALS;
371
Dave Airliede227f52006-01-25 15:31:43 +1100372 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000373 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100374
Alan Hourihanec29b6692006-08-12 16:29:24 +1000375 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 for (i = 0; i < dwords;) {
378 int cmd, sz;
379
Eric Anholt201361a2009-03-11 12:30:04 -0700380 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000383 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 OUT_RING(cmd);
386
387 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700388 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
Dave Airliede227f52006-01-25 15:31:43 +1100392 if (dwords & 1)
393 OUT_RING(0);
394
395 ADVANCE_LP_RING();
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 0;
398}
399
Eric Anholt673a3942008-07-30 12:06:12 -0700400int
401i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700402 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700403 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt201361a2009-03-11 12:30:04 -0700406 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 RING_LOCALS;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
410 DRM_ERROR("Bad box %d,%d..%d,%d\n",
411 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
Alan Hourihanec29b6692006-08-12 16:29:24 +1000415 if (IS_I965G(dev)) {
416 BEGIN_LP_RING(4);
417 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
418 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000419 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000420 OUT_RING(DR4);
421 ADVANCE_LP_RING();
422 } else {
423 BEGIN_LP_RING(6);
424 OUT_RING(GFX_OP_DRAWRECT_INFO);
425 OUT_RING(DR1);
426 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
427 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
428 OUT_RING(DR4);
429 OUT_RING(0);
430 ADVANCE_LP_RING();
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 return 0;
434}
435
Alan Hourihanec29b6692006-08-12 16:29:24 +1000436/* XXX: Emitting the counter should really be moved to part of the IRQ
437 * emit. For now, do it in both places:
438 */
439
Dave Airlie84b1fd12007-07-11 15:53:27 +1000440static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100441{
442 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000443 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100444 RING_LOCALS;
445
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400446 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000447 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400448 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000449 if (master_priv->sarea_priv)
450 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100451
452 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700453 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000454 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100455 OUT_RING(dev_priv->counter);
456 OUT_RING(0);
457 ADVANCE_LP_RING();
458}
459
Dave Airlie84b1fd12007-07-11 15:53:27 +1000460static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700461 drm_i915_cmdbuffer_t *cmd,
462 struct drm_clip_rect *cliprects,
463 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 int nbox = cmd->num_cliprects;
466 int i = 0, count, ret;
467
468 if (cmd->sz & 0x3) {
469 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000470 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 i915_kernel_lost_context(dev);
474
475 count = nbox ? nbox : 1;
476
477 for (i = 0; i < count; i++) {
478 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700479 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 cmd->DR1, cmd->DR4);
481 if (ret)
482 return ret;
483 }
484
Eric Anholt201361a2009-03-11 12:30:04 -0700485 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (ret)
487 return ret;
488 }
489
Dave Airliede227f52006-01-25 15:31:43 +1100490 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 0;
492}
493
Dave Airlie84b1fd12007-07-11 15:53:27 +1000494static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700495 drm_i915_batchbuffer_t * batch,
496 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int nbox = batch->num_cliprects;
500 int i = 0, count;
501 RING_LOCALS;
502
503 if ((batch->start | batch->used) & 0x7) {
504 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000505 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
508 i915_kernel_lost_context(dev);
509
510 count = nbox ? nbox : 1;
511
512 for (i = 0; i < count; i++) {
513 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700514 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 batch->DR1, batch->DR4);
516 if (ret)
517 return ret;
518 }
519
Keith Packard0790d5e2008-07-30 12:28:47 -0700520 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000522 if (IS_I965G(dev)) {
523 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
524 OUT_RING(batch->start);
525 } else {
526 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
527 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ADVANCE_LP_RING();
530 } else {
531 BEGIN_LP_RING(4);
532 OUT_RING(MI_BATCH_BUFFER);
533 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
534 OUT_RING(batch->start + batch->used - 4);
535 OUT_RING(0);
536 ADVANCE_LP_RING();
537 }
538 }
539
Dave Airliede227f52006-01-25 15:31:43 +1100540 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 return 0;
543}
544
Dave Airlieaf6061a2008-05-07 12:15:39 +1000545static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000548 struct drm_i915_master_private *master_priv =
549 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 RING_LOCALS;
551
Dave Airlie7c1c2872008-11-28 14:22:24 +1000552 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400553 return -EINVAL;
554
yakui_zhaobe25ed92009-06-02 14:13:55 +0800555 DRM_DEBUG_DRIVER(I915_DRV, "%s: page=%d pfCurrentPage=%d\n",
556 __func__,
557 dev_priv->current_page,
558 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Dave Airlieaf6061a2008-05-07 12:15:39 +1000560 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dave Airlieaf6061a2008-05-07 12:15:39 +1000562 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700563 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000564 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ADVANCE_LP_RING();
566
Dave Airlieaf6061a2008-05-07 12:15:39 +1000567 BEGIN_LP_RING(6);
568 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
569 OUT_RING(0);
570 if (dev_priv->current_page == 0) {
571 OUT_RING(dev_priv->back_offset);
572 dev_priv->current_page = 1;
573 } else {
574 OUT_RING(dev_priv->front_offset);
575 dev_priv->current_page = 0;
576 }
577 OUT_RING(0);
578 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000579
Dave Airlieaf6061a2008-05-07 12:15:39 +1000580 BEGIN_LP_RING(2);
581 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
582 OUT_RING(0);
583 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000584
Dave Airlie7c1c2872008-11-28 14:22:24 +1000585 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000586
Dave Airlieaf6061a2008-05-07 12:15:39 +1000587 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700588 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000589 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000590 OUT_RING(dev_priv->counter);
591 OUT_RING(0);
592 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000593
Dave Airlie7c1c2872008-11-28 14:22:24 +1000594 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Dave Airlie84b1fd12007-07-11 15:53:27 +1000598static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 drm_i915_private_t *dev_priv = dev->dev_private;
601
602 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700603 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Eric Anholtc153f452007-09-03 12:06:45 +1000606static int i915_flush_ioctl(struct drm_device *dev, void *data,
607 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Eric Anholt546b0972008-09-01 16:45:29 -0700609 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Eric Anholt546b0972008-09-01 16:45:29 -0700611 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
612
613 mutex_lock(&dev->struct_mutex);
614 ret = i915_quiescent(dev);
615 mutex_unlock(&dev->struct_mutex);
616
617 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Eric Anholtc153f452007-09-03 12:06:45 +1000620static int i915_batchbuffer(struct drm_device *dev, void *data,
621 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000624 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000626 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000627 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700629 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (!dev_priv->allow_batchbuffer) {
632 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000633 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
yakui_zhaobe25ed92009-06-02 14:13:55 +0800636 DRM_DEBUG_DRIVER(I915_DRV,
637 "i915 batchbuffer, start %x used %d cliprects %d\n",
638 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Eric Anholt546b0972008-09-01 16:45:29 -0700640 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric Anholt201361a2009-03-11 12:30:04 -0700642 if (batch->num_cliprects < 0)
643 return -EINVAL;
644
645 if (batch->num_cliprects) {
646 cliprects = drm_calloc(batch->num_cliprects,
647 sizeof(struct drm_clip_rect),
648 DRM_MEM_DRIVER);
649 if (cliprects == NULL)
650 return -ENOMEM;
651
652 ret = copy_from_user(cliprects, batch->cliprects,
653 batch->num_cliprects *
654 sizeof(struct drm_clip_rect));
655 if (ret != 0)
656 goto fail_free;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Eric Anholt546b0972008-09-01 16:45:29 -0700659 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700660 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700661 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400663 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000664 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700665
666fail_free:
667 drm_free(cliprects,
668 batch->num_cliprects * sizeof(struct drm_clip_rect),
669 DRM_MEM_DRIVER);
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return ret;
672}
673
Eric Anholtc153f452007-09-03 12:06:45 +1000674static int i915_cmdbuffer(struct drm_device *dev, void *data,
675 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000678 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000680 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000681 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700682 struct drm_clip_rect *cliprects = NULL;
683 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int ret;
685
yakui_zhaobe25ed92009-06-02 14:13:55 +0800686 DRM_DEBUG_DRIVER(I915_DRV,
687 "i915 cmdbuffer, buf %p sz %d cliprects %d\n",
688 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Eric Anholt546b0972008-09-01 16:45:29 -0700690 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Eric Anholt201361a2009-03-11 12:30:04 -0700692 if (cmdbuf->num_cliprects < 0)
693 return -EINVAL;
694
695 batch_data = drm_alloc(cmdbuf->sz, DRM_MEM_DRIVER);
696 if (batch_data == NULL)
697 return -ENOMEM;
698
699 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
700 if (ret != 0)
701 goto fail_batch_free;
702
703 if (cmdbuf->num_cliprects) {
704 cliprects = drm_calloc(cmdbuf->num_cliprects,
705 sizeof(struct drm_clip_rect),
706 DRM_MEM_DRIVER);
707 if (cliprects == NULL)
708 goto fail_batch_free;
709
710 ret = copy_from_user(cliprects, cmdbuf->cliprects,
711 cmdbuf->num_cliprects *
712 sizeof(struct drm_clip_rect));
713 if (ret != 0)
714 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
Eric Anholt546b0972008-09-01 16:45:29 -0700717 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700718 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700719 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (ret) {
721 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000722 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400725 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000726 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700727
Eric Anholt201361a2009-03-11 12:30:04 -0700728fail_clip_free:
729 drm_free(cliprects,
730 cmdbuf->num_cliprects * sizeof(struct drm_clip_rect),
731 DRM_MEM_DRIVER);
Chris Wright355d7f32009-04-17 01:18:55 +0000732fail_batch_free:
733 drm_free(batch_data, cmdbuf->sz, DRM_MEM_DRIVER);
Eric Anholt201361a2009-03-11 12:30:04 -0700734
735 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Eric Anholtc153f452007-09-03 12:06:45 +1000738static int i915_flip_bufs(struct drm_device *dev, void *data,
739 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Eric Anholt546b0972008-09-01 16:45:29 -0700741 int ret;
742
yakui_zhaobe25ed92009-06-02 14:13:55 +0800743 DRM_DEBUG_DRIVER(I915_DRV, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Eric Anholt546b0972008-09-01 16:45:29 -0700745 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Eric Anholt546b0972008-09-01 16:45:29 -0700747 mutex_lock(&dev->struct_mutex);
748 ret = i915_dispatch_flip(dev);
749 mutex_unlock(&dev->struct_mutex);
750
751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Eric Anholtc153f452007-09-03 12:06:45 +1000754static int i915_getparam(struct drm_device *dev, void *data,
755 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000758 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int value;
760
761 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000762 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000763 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
Eric Anholtc153f452007-09-03 12:06:45 +1000766 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700768 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
770 case I915_PARAM_ALLOW_BATCHBUFFER:
771 value = dev_priv->allow_batchbuffer ? 1 : 0;
772 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100773 case I915_PARAM_LAST_DISPATCH:
774 value = READ_BREADCRUMB(dev_priv);
775 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400776 case I915_PARAM_CHIPSET_ID:
777 value = dev->pci_device;
778 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700779 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000780 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700781 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800782 case I915_PARAM_NUM_FENCES_AVAIL:
783 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
784 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 default:
yakui_zhaobe25ed92009-06-02 14:13:55 +0800786 DRM_DEBUG_DRIVER(I915_DRV, "Unknown parameter %d\n",
787 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000788 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
Eric Anholtc153f452007-09-03 12:06:45 +1000791 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000793 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 return 0;
797}
798
Eric Anholtc153f452007-09-03 12:06:45 +1000799static int i915_setparam(struct drm_device *dev, void *data,
800 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000803 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000806 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000807 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
Eric Anholtc153f452007-09-03 12:06:45 +1000810 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000814 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 break;
816 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000817 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800819 case I915_SETPARAM_NUM_USED_FENCES:
820 if (param->value > dev_priv->num_fence_regs ||
821 param->value < 0)
822 return -EINVAL;
823 /* Userspace can use first N regs */
824 dev_priv->fence_reg_start = param->value;
825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 default:
yakui_zhaobe25ed92009-06-02 14:13:55 +0800827 DRM_DEBUG_DRIVER(I915_DRV, "unknown parameter %d\n",
828 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000829 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 return 0;
833}
834
Eric Anholtc153f452007-09-03 12:06:45 +1000835static int i915_set_status_page(struct drm_device *dev, void *data,
836 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000837{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000838 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000839 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000840
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000841 if (!I915_NEED_GFX_HWS(dev))
842 return -EINVAL;
843
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000844 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000845 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000846 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000847 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000848
Jesse Barnes79e53942008-11-07 14:24:08 -0800849 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
850 WARN(1, "tried to set status page when mode setting active\n");
851 return 0;
852 }
853
Eric Anholtc153f452007-09-03 12:06:45 +1000854 printk(KERN_DEBUG "set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000855
Eric Anholtc153f452007-09-03 12:06:45 +1000856 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
857
Eric Anholt8b409582007-11-22 16:40:37 +1000858 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000859 dev_priv->hws_map.size = 4*1024;
860 dev_priv->hws_map.type = 0;
861 dev_priv->hws_map.flags = 0;
862 dev_priv->hws_map.mtrr = 0;
863
Dave Airliedd0910b2009-02-25 14:49:21 +1000864 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000865 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000866 i915_dma_cleanup(dev);
867 dev_priv->status_gfx_addr = 0;
868 DRM_ERROR("can not ioremap virtual address for"
869 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000870 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000871 }
872 dev_priv->hw_status_page = dev_priv->hws_map.handle;
873
874 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700875 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
yakui_zhaobe25ed92009-06-02 14:13:55 +0800876 DRM_DEBUG_DRIVER(I915_DRV, "load hws HWS_PGA with gfx mem 0x%x\n",
877 dev_priv->status_gfx_addr);
878 DRM_DEBUG_DRIVER(I915_DRV, "load hws at %p\n",
879 dev_priv->hw_status_page);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000880 return 0;
881}
882
Jesse Barnes79e53942008-11-07 14:24:08 -0800883/**
884 * i915_probe_agp - get AGP bootup configuration
885 * @pdev: PCI device
886 * @aperture_size: returns AGP aperture configured size
887 * @preallocated_size: returns size of BIOS preallocated AGP space
888 *
889 * Since Intel integrated graphics are UMA, the BIOS has to set aside
890 * some RAM for the framebuffer at early boot. This code figures out
891 * how much was set aside so we can use it for our own purposes.
892 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100893static int i915_probe_agp(struct drm_device *dev, unsigned long *aperture_size,
894 unsigned long *preallocated_size)
Jesse Barnes79e53942008-11-07 14:24:08 -0800895{
896 struct pci_dev *bridge_dev;
897 u16 tmp = 0;
898 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -0800899 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -0800900
901 bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
902 if (!bridge_dev) {
903 DRM_ERROR("bridge device not found\n");
904 return -1;
905 }
906
907 /* Get the fb aperture size and "stolen" memory amount. */
908 pci_read_config_word(bridge_dev, INTEL_GMCH_CTRL, &tmp);
909 pci_dev_put(bridge_dev);
910
911 *aperture_size = 1024 * 1024;
912 *preallocated_size = 1024 * 1024;
913
Eric Anholt60fd99e2008-12-03 22:50:02 -0800914 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800915 case PCI_DEVICE_ID_INTEL_82830_CGC:
916 case PCI_DEVICE_ID_INTEL_82845G_IG:
917 case PCI_DEVICE_ID_INTEL_82855GM_IG:
918 case PCI_DEVICE_ID_INTEL_82865_IG:
919 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
920 *aperture_size *= 64;
921 else
922 *aperture_size *= 128;
923 break;
924 default:
925 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -0800926 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -0800927 break;
928 }
929
930 /*
931 * Some of the preallocated space is taken by the GTT
932 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
933 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800934 if (IS_G4X(dev) || IS_IGD(dev) || IS_IGDNG(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -0800935 overhead = 4096;
936 else
937 overhead = (*aperture_size / 1024) + 4096;
938
Eric Anholt241fa852009-01-02 18:05:51 -0800939 switch (tmp & INTEL_GMCH_GMS_MASK) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800940 case INTEL_855_GMCH_GMS_DISABLED:
941 DRM_ERROR("video memory is disabled\n");
942 return -1;
Eric Anholt241fa852009-01-02 18:05:51 -0800943 case INTEL_855_GMCH_GMS_STOLEN_1M:
944 stolen = 1 * 1024 * 1024;
945 break;
946 case INTEL_855_GMCH_GMS_STOLEN_4M:
947 stolen = 4 * 1024 * 1024;
948 break;
949 case INTEL_855_GMCH_GMS_STOLEN_8M:
950 stolen = 8 * 1024 * 1024;
951 break;
952 case INTEL_855_GMCH_GMS_STOLEN_16M:
953 stolen = 16 * 1024 * 1024;
954 break;
955 case INTEL_855_GMCH_GMS_STOLEN_32M:
956 stolen = 32 * 1024 * 1024;
957 break;
958 case INTEL_915G_GMCH_GMS_STOLEN_48M:
959 stolen = 48 * 1024 * 1024;
960 break;
961 case INTEL_915G_GMCH_GMS_STOLEN_64M:
962 stolen = 64 * 1024 * 1024;
963 break;
964 case INTEL_GMCH_GMS_STOLEN_128M:
965 stolen = 128 * 1024 * 1024;
966 break;
967 case INTEL_GMCH_GMS_STOLEN_256M:
968 stolen = 256 * 1024 * 1024;
969 break;
970 case INTEL_GMCH_GMS_STOLEN_96M:
971 stolen = 96 * 1024 * 1024;
972 break;
973 case INTEL_GMCH_GMS_STOLEN_160M:
974 stolen = 160 * 1024 * 1024;
975 break;
976 case INTEL_GMCH_GMS_STOLEN_224M:
977 stolen = 224 * 1024 * 1024;
978 break;
979 case INTEL_GMCH_GMS_STOLEN_352M:
980 stolen = 352 * 1024 * 1024;
981 break;
Jesse Barnes79e53942008-11-07 14:24:08 -0800982 default:
983 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
Eric Anholt241fa852009-01-02 18:05:51 -0800984 tmp & INTEL_GMCH_GMS_MASK);
Jesse Barnes79e53942008-11-07 14:24:08 -0800985 return -1;
986 }
Eric Anholt241fa852009-01-02 18:05:51 -0800987 *preallocated_size = stolen - overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -0800988
989 return 0;
990}
991
992static int i915_load_modeset_init(struct drm_device *dev)
993{
994 struct drm_i915_private *dev_priv = dev->dev_private;
995 unsigned long agp_size, prealloc_size;
996 int fb_bar = IS_I9XX(dev) ? 2 : 0;
997 int ret = 0;
998
Dave Airlieaa596622008-12-29 16:35:02 +1000999 dev->devname = kstrdup(DRIVER_NAME, GFP_KERNEL);
1000 if (!dev->devname) {
1001 ret = -ENOMEM;
1002 goto out;
1003 }
1004
Jesse Barnes79e53942008-11-07 14:24:08 -08001005 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
1006 0xff000000;
1007
Jesse Barnes2906f022009-01-20 19:10:54 -08001008 if (IS_MOBILE(dev) || IS_I9XX(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08001009 dev_priv->cursor_needs_physical = true;
1010 else
1011 dev_priv->cursor_needs_physical = false;
1012
Jesse Barnes2906f022009-01-20 19:10:54 -08001013 if (IS_I965G(dev) || IS_G33(dev))
1014 dev_priv->cursor_needs_physical = false;
1015
Dave Airlieaa596622008-12-29 16:35:02 +10001016 ret = i915_probe_agp(dev, &agp_size, &prealloc_size);
1017 if (ret)
1018 goto kfree_devname;
Jesse Barnes79e53942008-11-07 14:24:08 -08001019
1020 /* Basic memrange allocator for stolen space (aka vram) */
1021 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
1022
Eric Anholt13f4c432009-05-12 15:27:36 -07001023 /* Let GEM Manage from end of prealloc space to end of aperture.
1024 *
1025 * However, leave one page at the end still bound to the scratch page.
1026 * There are a number of places where the hardware apparently
1027 * prefetches past the end of the object, and we've seen multiple
1028 * hangs with the GPU head pointer stuck in a batchbuffer bound
1029 * at the last page of the aperture. One page should be enough to
1030 * keep any prefetching inside of the aperture.
1031 */
1032 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001033
1034 ret = i915_gem_init_ringbuffer(dev);
1035 if (ret)
Dave Airlieaa596622008-12-29 16:35:02 +10001036 goto kfree_devname;
Jesse Barnes79e53942008-11-07 14:24:08 -08001037
Jesse Barnes79e53942008-11-07 14:24:08 -08001038 /* Allow hardware batchbuffers unless told otherwise.
1039 */
1040 dev_priv->allow_batchbuffer = 1;
1041
1042 ret = intel_init_bios(dev);
1043 if (ret)
1044 DRM_INFO("failed to find VBIOS tables\n");
1045
1046 ret = drm_irq_install(dev);
1047 if (ret)
1048 goto destroy_ringbuffer;
1049
Jesse Barnes79e53942008-11-07 14:24:08 -08001050 /* Always safe in the mode setting case. */
1051 /* FIXME: do pre/post-mode set stuff in core KMS code */
1052 dev->vblank_disable_allowed = 1;
1053
1054 /*
1055 * Initialize the hardware status page IRQ location.
1056 */
1057
1058 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1059
1060 intel_modeset_init(dev);
1061
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001062 drm_helper_initial_config(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001063
Jesse Barnes79e53942008-11-07 14:24:08 -08001064 return 0;
1065
Jesse Barnes79e53942008-11-07 14:24:08 -08001066destroy_ringbuffer:
1067 i915_gem_cleanup_ringbuffer(dev);
Dave Airlieaa596622008-12-29 16:35:02 +10001068kfree_devname:
1069 kfree(dev->devname);
Jesse Barnes79e53942008-11-07 14:24:08 -08001070out:
1071 return ret;
1072}
1073
Dave Airlie7c1c2872008-11-28 14:22:24 +10001074int i915_master_create(struct drm_device *dev, struct drm_master *master)
1075{
1076 struct drm_i915_master_private *master_priv;
1077
1078 master_priv = drm_calloc(1, sizeof(*master_priv), DRM_MEM_DRIVER);
1079 if (!master_priv)
1080 return -ENOMEM;
1081
1082 master->driver_priv = master_priv;
1083 return 0;
1084}
1085
1086void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1087{
1088 struct drm_i915_master_private *master_priv = master->driver_priv;
1089
1090 if (!master_priv)
1091 return;
1092
1093 drm_free(master_priv, sizeof(*master_priv), DRM_MEM_DRIVER);
1094
1095 master->driver_priv = NULL;
1096}
1097
Jesse Barnes79e53942008-11-07 14:24:08 -08001098/**
1099 * i915_driver_load - setup chip and create an initial config
1100 * @dev: DRM device
1101 * @flags: startup flags
1102 *
1103 * The driver load routine has to do several things:
1104 * - drive output discovery via intel_modeset_init()
1105 * - initialize the memory manager
1106 * - allocate initial config memory
1107 * - setup the DRM framebuffer with the allocated memory
1108 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001109int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001110{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001111 struct drm_i915_private *dev_priv = dev->dev_private;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001112 resource_size_t base, size;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001113 int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1;
1114
Dave Airlie22eae942005-11-10 22:16:34 +11001115 /* i915 has 4 more counters */
1116 dev->counters += 4;
1117 dev->types[6] = _DRM_STAT_IRQ;
1118 dev->types[7] = _DRM_STAT_PRIMARY;
1119 dev->types[8] = _DRM_STAT_SECONDARY;
1120 dev->types[9] = _DRM_STAT_DMA;
1121
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001122 dev_priv = drm_alloc(sizeof(drm_i915_private_t), DRM_MEM_DRIVER);
1123 if (dev_priv == NULL)
1124 return -ENOMEM;
1125
1126 memset(dev_priv, 0, sizeof(drm_i915_private_t));
1127
1128 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001129 dev_priv->dev = dev;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001130
1131 /* Add register map (needed for suspend/resume) */
1132 base = drm_get_resource_start(dev, mmio_bar);
1133 size = drm_get_resource_len(dev, mmio_bar);
1134
Eric Anholt3043c602008-10-02 12:24:47 -07001135 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001136 if (!dev_priv->regs) {
1137 DRM_ERROR("failed to map registers\n");
1138 ret = -EIO;
1139 goto free_priv;
1140 }
Eric Anholted4cb412008-07-29 12:10:39 -07001141
Eric Anholtab657db12009-01-23 12:57:47 -08001142 dev_priv->mm.gtt_mapping =
1143 io_mapping_create_wc(dev->agp->base,
1144 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001145 if (dev_priv->mm.gtt_mapping == NULL) {
1146 ret = -EIO;
1147 goto out_rmmap;
1148 }
1149
Eric Anholtab657db12009-01-23 12:57:47 -08001150 /* Set up a WC MTRR for non-PAT systems. This is more common than
1151 * one would think, because the kernel disables PAT on first
1152 * generation Core chips because WC PAT gets overridden by a UC
1153 * MTRR if present. Even if a UC MTRR isn't present.
1154 */
1155 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1156 dev->agp->agp_info.aper_size *
1157 1024 * 1024,
1158 MTRR_TYPE_WRCOMB, 1);
1159 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001160 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001161 "performance may suffer.\n");
1162 }
1163
Dave Airlieac5c4e72008-12-19 15:38:34 +10001164#ifdef CONFIG_HIGHMEM64G
1165 /* don't enable GEM on PAE - needs agp + set_memory_* interface fixes */
1166 dev_priv->has_gem = 0;
1167#else
1168 /* enable GEM by default */
1169 dev_priv->has_gem = 1;
1170#endif
1171
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001172 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001173 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001174 if (IS_G4X(dev) || IS_IGDNG(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001175 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001176 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001177 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001178
Eric Anholt673a3942008-07-30 12:06:12 -07001179 i915_gem_load(dev);
1180
Keith Packard398c9cb2008-07-30 13:03:43 -07001181 /* Init HWS */
1182 if (!I915_NEED_GFX_HWS(dev)) {
1183 ret = i915_init_phys_hws(dev);
1184 if (ret != 0)
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001185 goto out_iomapfree;
Keith Packard398c9cb2008-07-30 13:03:43 -07001186 }
Eric Anholted4cb412008-07-29 12:10:39 -07001187
1188 /* On the 945G/GM, the chipset reports the MSI capability on the
1189 * integrated graphics even though the support isn't actually there
1190 * according to the published specs. It doesn't appear to function
1191 * correctly in testing on 945G.
1192 * This may be a side effect of MSI having been made available for PEG
1193 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001194 *
1195 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001196 * be lost or delayed, but we use them anyways to avoid
1197 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001198 */
Keith Packardb60678a2008-12-08 11:12:28 -08001199 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001200 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001201
1202 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes79e53942008-11-07 14:24:08 -08001203 dev_priv->user_irq_refcount = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001204
Keith Packard52440212008-11-18 09:30:25 -08001205 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1206
1207 if (ret) {
1208 (void) i915_driver_unload(dev);
1209 return ret;
1210 }
1211
Jesse Barnes79e53942008-11-07 14:24:08 -08001212 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1213 ret = i915_load_modeset_init(dev);
1214 if (ret < 0) {
1215 DRM_ERROR("failed to init modeset\n");
1216 goto out_rmmap;
1217 }
1218 }
1219
Matthew Garrett74a365b2009-03-19 21:35:39 +00001220 /* Must be done after probing outputs */
Zhenyu Wange170b032009-06-05 15:38:40 +08001221 /* FIXME: verify on IGDNG */
1222 if (!IS_IGDNG(dev))
1223 intel_opregion_init(dev, 0);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001224
Jesse Barnes79e53942008-11-07 14:24:08 -08001225 return 0;
1226
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001227out_iomapfree:
1228 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001229out_rmmap:
1230 iounmap(dev_priv->regs);
1231free_priv:
1232 drm_free(dev_priv, sizeof(struct drm_i915_private), DRM_MEM_DRIVER);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001233 return ret;
1234}
1235
1236int i915_driver_unload(struct drm_device *dev)
1237{
1238 struct drm_i915_private *dev_priv = dev->dev_private;
1239
Eric Anholtab657db12009-01-23 12:57:47 -08001240 io_mapping_free(dev_priv->mm.gtt_mapping);
1241 if (dev_priv->mm.gtt_mtrr >= 0) {
1242 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1243 dev->agp->agp_info.aper_size * 1024 * 1024);
1244 dev_priv->mm.gtt_mtrr = -1;
1245 }
1246
Jesse Barnes79e53942008-11-07 14:24:08 -08001247 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001248 drm_irq_uninstall(dev);
1249 }
1250
Eric Anholted4cb412008-07-29 12:10:39 -07001251 if (dev->pdev->msi_enabled)
1252 pci_disable_msi(dev->pdev);
1253
Eric Anholt3043c602008-10-02 12:24:47 -07001254 if (dev_priv->regs != NULL)
1255 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001256
Zhenyu Wange170b032009-06-05 15:38:40 +08001257 if (!IS_IGDNG(dev))
1258 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001259
Jesse Barnes79e53942008-11-07 14:24:08 -08001260 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1261 intel_modeset_cleanup(dev);
1262
Dave Airlie71acb5e2008-12-30 20:31:46 +10001263 i915_gem_free_all_phys_object(dev);
1264
Jesse Barnes79e53942008-11-07 14:24:08 -08001265 mutex_lock(&dev->struct_mutex);
1266 i915_gem_cleanup_ringbuffer(dev);
1267 mutex_unlock(&dev->struct_mutex);
1268 drm_mm_takedown(&dev_priv->vram);
1269 i915_gem_lastclose(dev);
1270 }
1271
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001272 drm_free(dev->dev_private, sizeof(drm_i915_private_t),
1273 DRM_MEM_DRIVER);
1274
Dave Airlie22eae942005-11-10 22:16:34 +11001275 return 0;
1276}
1277
Eric Anholt673a3942008-07-30 12:06:12 -07001278int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1279{
1280 struct drm_i915_file_private *i915_file_priv;
1281
yakui_zhaobe25ed92009-06-02 14:13:55 +08001282 DRM_DEBUG_DRIVER(I915_DRV, "\n");
Eric Anholt673a3942008-07-30 12:06:12 -07001283 i915_file_priv = (struct drm_i915_file_private *)
1284 drm_alloc(sizeof(*i915_file_priv), DRM_MEM_FILES);
1285
1286 if (!i915_file_priv)
1287 return -ENOMEM;
1288
1289 file_priv->driver_priv = i915_file_priv;
1290
Eric Anholtb9624422009-06-03 07:27:35 +00001291 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001292
1293 return 0;
1294}
1295
Jesse Barnes79e53942008-11-07 14:24:08 -08001296/**
1297 * i915_driver_lastclose - clean up after all DRM clients have exited
1298 * @dev: DRM device
1299 *
1300 * Take care of cleaning up after all DRM clients have exited. In the
1301 * mode setting case, we want to restore the kernel's initial mode (just
1302 * in case the last client left us in a bad state).
1303 *
1304 * Additionally, in the non-mode setting case, we'll tear down the AGP
1305 * and DMA structures, since the kernel won't be using them, and clea
1306 * up any GEM state.
1307 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001308void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001310 drm_i915_private_t *dev_priv = dev->dev_private;
1311
Jesse Barnes79e53942008-11-07 14:24:08 -08001312 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
1313 intelfb_restore();
Dave Airlie144a75f2008-03-30 07:53:58 +10001314 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001315 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001316
Eric Anholt673a3942008-07-30 12:06:12 -07001317 i915_gem_lastclose(dev);
1318
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001319 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001321
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001322 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Eric Anholt6c340ea2007-08-25 20:23:09 +10001325void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001327 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001328 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08001329 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1330 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Eric Anholt673a3942008-07-30 12:06:12 -07001333void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1334{
1335 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1336
1337 drm_free(i915_file_priv, sizeof(*i915_file_priv), DRM_MEM_FILES);
1338}
1339
Eric Anholtc153f452007-09-03 12:06:45 +10001340struct drm_ioctl_desc i915_ioctls[] = {
1341 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1342 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1343 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1344 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1345 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1346 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1347 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1348 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1349 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1350 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1351 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1352 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1353 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1354 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1355 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1356 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001357 DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie2bdf00b2008-10-07 13:40:10 +10001358 DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Eric Anholt673a3942008-07-30 12:06:12 -07001359 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
1360 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1361 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1362 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
1363 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
Dave Airlie2bdf00b2008-10-07 13:40:10 +10001364 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1365 DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Eric Anholt673a3942008-07-30 12:06:12 -07001366 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1367 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
1368 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
1369 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
Jesse Barnesde151cf2008-11-12 10:03:55 -08001370 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0),
Eric Anholt673a3942008-07-30 12:06:12 -07001371 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
1372 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
1373 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1374 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
Eric Anholt5a125c32008-10-22 21:40:13 -07001375 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0),
Carl Worth08d7b3d2009-04-29 14:43:54 -07001376 DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
Dave Airliec94f7022005-07-07 21:03:38 +10001377};
1378
1379int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001380
1381/**
1382 * Determine if the device really is AGP or not.
1383 *
1384 * All Intel graphics chipsets are treated as AGP, even if they are really
1385 * PCI-e.
1386 *
1387 * \param dev The device to be tested.
1388 *
1389 * \returns
1390 * A value of 1 is always retured to indictate every i9x5 is AGP.
1391 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001392int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001393{
1394 return 1;
1395}