blob: 051134c56aef5d52b4aa8813ffd850b1d3b33a1f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Really want an OS-independent resettable timer. Would like to have
37 * this loop run for (eg) 3 sec, but have the timer reset every time
38 * the head pointer changes, so that EBUSY only happens if the ring
39 * actually stalls for (eg) 3 seconds.
40 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100041int i915_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 drm_i915_private_t *dev_priv = dev->dev_private;
44 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
Keith Packardd3a6d442008-07-30 12:21:20 -070045 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
46 u32 last_acthd = I915_READ(acthd_reg);
47 u32 acthd;
Jesse Barnes585fb112008-07-29 11:54:06 -070048 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int i;
50
Keith Packardd3a6d442008-07-30 12:21:20 -070051 for (i = 0; i < 100000; i++) {
Jesse Barnes585fb112008-07-29 11:54:06 -070052 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Keith Packardd3a6d442008-07-30 12:21:20 -070053 acthd = I915_READ(acthd_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 ring->space = ring->head - (ring->tail + 8);
55 if (ring->space < 0)
56 ring->space += ring->Size;
57 if (ring->space >= n)
58 return 0;
59
Chris Wilson98787c02009-03-06 23:27:52 +000060 if (dev->primary->master) {
61 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
62 if (master_priv->sarea_priv)
63 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
64 }
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (ring->head != last_head)
68 i = 0;
Keith Packardd3a6d442008-07-30 12:21:20 -070069 if (acthd != last_acthd)
70 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 last_head = ring->head;
Keith Packardd3a6d442008-07-30 12:21:20 -070073 last_acthd = acthd;
74 msleep_interruptible(10);
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
Eric Anholt20caafa2007-08-25 19:22:43 +100078 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Keith Packard398c9cb2008-07-30 13:03:43 -070081/**
82 * Sets up the hardware status page for devices that need a physical address
83 * in the register.
84 */
Eric Anholt3043c602008-10-02 12:24:47 -070085static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070086{
87 drm_i915_private_t *dev_priv = dev->dev_private;
88 /* Program Hardware Status Page */
89 dev_priv->status_page_dmah =
90 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
91
92 if (!dev_priv->status_page_dmah) {
93 DRM_ERROR("Can not allocate hardware status page\n");
94 return -ENOMEM;
95 }
96 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
97 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
98
99 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
100
101 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
102 DRM_DEBUG("Enabled hardware status page\n");
103 return 0;
104}
105
106/**
107 * Frees the hardware status page, whether it's a physical address or a virtual
108 * address set up by the X Server.
109 */
Eric Anholt3043c602008-10-02 12:24:47 -0700110static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700111{
112 drm_i915_private_t *dev_priv = dev->dev_private;
113 if (dev_priv->status_page_dmah) {
114 drm_pci_free(dev, dev_priv->status_page_dmah);
115 dev_priv->status_page_dmah = NULL;
116 }
117
118 if (dev_priv->status_gfx_addr) {
119 dev_priv->status_gfx_addr = 0;
120 drm_core_ioremapfree(&dev_priv->hws_map, dev);
121 }
122
123 /* Need to rewrite hardware status page */
124 I915_WRITE(HWS_PGA, 0x1ffff000);
125}
126
Dave Airlie84b1fd12007-07-11 15:53:27 +1000127void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000130 struct drm_i915_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
132
Jesse Barnes79e53942008-11-07 14:24:08 -0800133 /*
134 * We should never lose context on the ring with modesetting
135 * as we don't expose it to userspace
136 */
137 if (drm_core_check_feature(dev, DRIVER_MODESET))
138 return;
139
Jesse Barnes585fb112008-07-29 11:54:06 -0700140 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
141 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ring->space = ring->head - (ring->tail + 8);
143 if (ring->space < 0)
144 ring->space += ring->Size;
145
Dave Airlie7c1c2872008-11-28 14:22:24 +1000146 if (!dev->primary->master)
147 return;
148
149 master_priv = dev->primary->master->driver_priv;
150 if (ring->head == ring->tail && master_priv->sarea_priv)
151 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Dave Airlie84b1fd12007-07-11 15:53:27 +1000154static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000156 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Make sure interrupts are disabled here because the uninstall ioctl
158 * may not have been called from userspace and after dev_private
159 * is freed, it's too late.
160 */
Eric Anholted4cb412008-07-29 12:10:39 -0700161 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000164 if (dev_priv->ring.virtual_start) {
165 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Eric Anholt3043c602008-10-02 12:24:47 -0700166 dev_priv->ring.virtual_start = NULL;
167 dev_priv->ring.map.handle = NULL;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000168 dev_priv->ring.map.size = 0;
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Keith Packard398c9cb2008-07-30 13:03:43 -0700171 /* Clear the HWS virtual address at teardown */
172 if (I915_NEED_GFX_HWS(dev))
173 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return 0;
176}
177
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000178static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000180 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000181 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Dave Airlie3a03ac12009-01-11 09:03:49 +1000183 master_priv->sarea = drm_getsarea(dev);
184 if (master_priv->sarea) {
185 master_priv->sarea_priv = (drm_i915_sarea_t *)
186 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
187 } else {
188 DRM_DEBUG("sarea not found assuming DRI2 userspace\n");
189 }
190
Eric Anholt673a3942008-07-30 12:06:12 -0700191 if (init->ring_size != 0) {
192 if (dev_priv->ring.ring_obj != NULL) {
193 i915_dma_cleanup(dev);
194 DRM_ERROR("Client tried to initialize ringbuffer in "
195 "GEM mode\n");
196 return -EINVAL;
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Eric Anholt673a3942008-07-30 12:06:12 -0700199 dev_priv->ring.Size = init->ring_size;
200 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Eric Anholt673a3942008-07-30 12:06:12 -0700202 dev_priv->ring.map.offset = init->ring_start;
203 dev_priv->ring.map.size = init->ring_size;
204 dev_priv->ring.map.type = 0;
205 dev_priv->ring.map.flags = 0;
206 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jesse Barnes6fb88582009-02-23 10:08:21 +1000208 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700209
210 if (dev_priv->ring.map.handle == NULL) {
211 i915_dma_cleanup(dev);
212 DRM_ERROR("can not ioremap virtual address for"
213 " ring buffer\n");
214 return -ENOMEM;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
219
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000220 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 dev_priv->back_offset = init->back_offset;
222 dev_priv->front_offset = init->front_offset;
223 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000224 if (master_priv->sarea_priv)
225 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* Allow hardware batchbuffers unless told otherwise.
228 */
229 dev_priv->allow_batchbuffer = 1;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return 0;
232}
233
Dave Airlie84b1fd12007-07-11 15:53:27 +1000234static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
237
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700238 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (dev_priv->ring.map.handle == NULL) {
241 DRM_ERROR("can not ioremap virtual address for"
242 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000243 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 /* Program Hardware Status Page */
247 if (!dev_priv->hw_status_page) {
248 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000249 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
252
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000253 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700254 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000255 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700256 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 DRM_DEBUG("Enabled hardware status page\n");
258
259 return 0;
260}
261
Eric Anholtc153f452007-09-03 12:06:45 +1000262static int i915_dma_init(struct drm_device *dev, void *data,
263 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Eric Anholtc153f452007-09-03 12:06:45 +1000265 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int retcode = 0;
267
Eric Anholtc153f452007-09-03 12:06:45 +1000268 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000270 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 break;
272 case I915_CLEANUP_DMA:
273 retcode = i915_dma_cleanup(dev);
274 break;
275 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100276 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000279 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281 }
282
283 return retcode;
284}
285
286/* Implement basically the same security restrictions as hardware does
287 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
288 *
289 * Most of the calculations below involve calculating the size of a
290 * particular instruction. It's important to get the size right as
291 * that tells us where the next instruction to check is. Any illegal
292 * instruction detected will be given a size of zero, which is a
293 * signal to abort the rest of the buffer.
294 */
295static int do_validate_cmd(int cmd)
296{
297 switch (((cmd >> 29) & 0x7)) {
298 case 0x0:
299 switch ((cmd >> 23) & 0x3f) {
300 case 0x0:
301 return 1; /* MI_NOOP */
302 case 0x4:
303 return 1; /* MI_FLUSH */
304 default:
305 return 0; /* disallow everything else */
306 }
307 break;
308 case 0x1:
309 return 0; /* reserved */
310 case 0x2:
311 return (cmd & 0xff) + 2; /* 2d commands */
312 case 0x3:
313 if (((cmd >> 24) & 0x1f) <= 0x18)
314 return 1;
315
316 switch ((cmd >> 24) & 0x1f) {
317 case 0x1c:
318 return 1;
319 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 case 0x3:
322 return (cmd & 0x1f) + 2;
323 case 0x4:
324 return (cmd & 0xf) + 2;
325 default:
326 return (cmd & 0xffff) + 2;
327 }
328 case 0x1e:
329 if (cmd & (1 << 23))
330 return (cmd & 0xffff) + 1;
331 else
332 return 1;
333 case 0x1f:
334 if ((cmd & (1 << 23)) == 0) /* inline vertices */
335 return (cmd & 0x1ffff) + 2;
336 else if (cmd & (1 << 17)) /* indirect random */
337 if ((cmd & 0xffff) == 0)
338 return 0; /* unknown length, too hard */
339 else
340 return (((cmd & 0xffff) + 1) / 2) + 1;
341 else
342 return 2; /* indirect sequential */
343 default:
344 return 0;
345 }
346 default:
347 return 0;
348 }
349
350 return 0;
351}
352
353static int validate_cmd(int cmd)
354{
355 int ret = do_validate_cmd(cmd);
356
Dave Airliebc5f4522007-11-05 12:50:58 +1000357/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 return ret;
360}
361
Eric Anholt201361a2009-03-11 12:30:04 -0700362static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 drm_i915_private_t *dev_priv = dev->dev_private;
365 int i;
366 RING_LOCALS;
367
Dave Airliede227f52006-01-25 15:31:43 +1100368 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000369 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100370
Alan Hourihanec29b6692006-08-12 16:29:24 +1000371 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 for (i = 0; i < dwords;) {
374 int cmd, sz;
375
Eric Anholt201361a2009-03-11 12:30:04 -0700376 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 OUT_RING(cmd);
382
383 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700384 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Dave Airliede227f52006-01-25 15:31:43 +1100388 if (dwords & 1)
389 OUT_RING(0);
390
391 ADVANCE_LP_RING();
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return 0;
394}
395
Eric Anholt673a3942008-07-30 12:06:12 -0700396int
397i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700398 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700399 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt201361a2009-03-11 12:30:04 -0700402 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 RING_LOCALS;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
406 DRM_ERROR("Bad box %d,%d..%d,%d\n",
407 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000408 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Alan Hourihanec29b6692006-08-12 16:29:24 +1000411 if (IS_I965G(dev)) {
412 BEGIN_LP_RING(4);
413 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
414 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000415 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000416 OUT_RING(DR4);
417 ADVANCE_LP_RING();
418 } else {
419 BEGIN_LP_RING(6);
420 OUT_RING(GFX_OP_DRAWRECT_INFO);
421 OUT_RING(DR1);
422 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
423 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
424 OUT_RING(DR4);
425 OUT_RING(0);
426 ADVANCE_LP_RING();
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 return 0;
430}
431
Alan Hourihanec29b6692006-08-12 16:29:24 +1000432/* XXX: Emitting the counter should really be moved to part of the IRQ
433 * emit. For now, do it in both places:
434 */
435
Dave Airlie84b1fd12007-07-11 15:53:27 +1000436static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100437{
438 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000439 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100440 RING_LOCALS;
441
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400442 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000443 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400444 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000445 if (master_priv->sarea_priv)
446 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100447
448 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700449 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000450 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100451 OUT_RING(dev_priv->counter);
452 OUT_RING(0);
453 ADVANCE_LP_RING();
454}
455
Dave Airlie84b1fd12007-07-11 15:53:27 +1000456static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700457 drm_i915_cmdbuffer_t *cmd,
458 struct drm_clip_rect *cliprects,
459 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 int nbox = cmd->num_cliprects;
462 int i = 0, count, ret;
463
464 if (cmd->sz & 0x3) {
465 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000466 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 i915_kernel_lost_context(dev);
470
471 count = nbox ? nbox : 1;
472
473 for (i = 0; i < count; i++) {
474 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700475 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 cmd->DR1, cmd->DR4);
477 if (ret)
478 return ret;
479 }
480
Eric Anholt201361a2009-03-11 12:30:04 -0700481 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (ret)
483 return ret;
484 }
485
Dave Airliede227f52006-01-25 15:31:43 +1100486 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488}
489
Dave Airlie84b1fd12007-07-11 15:53:27 +1000490static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700491 drm_i915_batchbuffer_t * batch,
492 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int nbox = batch->num_cliprects;
496 int i = 0, count;
497 RING_LOCALS;
498
499 if ((batch->start | batch->used) & 0x7) {
500 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000501 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
504 i915_kernel_lost_context(dev);
505
506 count = nbox ? nbox : 1;
507
508 for (i = 0; i < count; i++) {
509 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700510 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 batch->DR1, batch->DR4);
512 if (ret)
513 return ret;
514 }
515
Keith Packard0790d5e2008-07-30 12:28:47 -0700516 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000518 if (IS_I965G(dev)) {
519 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
520 OUT_RING(batch->start);
521 } else {
522 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
523 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 ADVANCE_LP_RING();
526 } else {
527 BEGIN_LP_RING(4);
528 OUT_RING(MI_BATCH_BUFFER);
529 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
530 OUT_RING(batch->start + batch->used - 4);
531 OUT_RING(0);
532 ADVANCE_LP_RING();
533 }
534 }
535
Dave Airliede227f52006-01-25 15:31:43 +1100536 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 return 0;
539}
540
Dave Airlieaf6061a2008-05-07 12:15:39 +1000541static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000544 struct drm_i915_master_private *master_priv =
545 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 RING_LOCALS;
547
Dave Airlie7c1c2872008-11-28 14:22:24 +1000548 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400549 return -EINVAL;
550
Dave Airlieaf6061a2008-05-07 12:15:39 +1000551 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700552 __func__,
Dave Airlieaf6061a2008-05-07 12:15:39 +1000553 dev_priv->current_page,
Dave Airlie7c1c2872008-11-28 14:22:24 +1000554 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dave Airlieaf6061a2008-05-07 12:15:39 +1000556 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Dave Airlieaf6061a2008-05-07 12:15:39 +1000558 BEGIN_LP_RING(2);
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 ADVANCE_LP_RING();
562
Dave Airlieaf6061a2008-05-07 12:15:39 +1000563 BEGIN_LP_RING(6);
564 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
565 OUT_RING(0);
566 if (dev_priv->current_page == 0) {
567 OUT_RING(dev_priv->back_offset);
568 dev_priv->current_page = 1;
569 } else {
570 OUT_RING(dev_priv->front_offset);
571 dev_priv->current_page = 0;
572 }
573 OUT_RING(0);
574 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000575
Dave Airlieaf6061a2008-05-07 12:15:39 +1000576 BEGIN_LP_RING(2);
577 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
578 OUT_RING(0);
579 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000580
Dave Airlie7c1c2872008-11-28 14:22:24 +1000581 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000582
Dave Airlieaf6061a2008-05-07 12:15:39 +1000583 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700584 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000585 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000586 OUT_RING(dev_priv->counter);
587 OUT_RING(0);
588 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000589
Dave Airlie7c1c2872008-11-28 14:22:24 +1000590 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Dave Airlie84b1fd12007-07-11 15:53:27 +1000594static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 drm_i915_private_t *dev_priv = dev->dev_private;
597
598 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700599 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Eric Anholtc153f452007-09-03 12:06:45 +1000602static int i915_flush_ioctl(struct drm_device *dev, void *data,
603 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Eric Anholt546b0972008-09-01 16:45:29 -0700605 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Eric Anholt546b0972008-09-01 16:45:29 -0700607 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
608
609 mutex_lock(&dev->struct_mutex);
610 ret = i915_quiescent(dev);
611 mutex_unlock(&dev->struct_mutex);
612
613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Eric Anholtc153f452007-09-03 12:06:45 +1000616static int i915_batchbuffer(struct drm_device *dev, void *data,
617 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000620 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000622 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000623 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700625 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (!dev_priv->allow_batchbuffer) {
628 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000629 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 DRM_DEBUG("i915 batchbuffer, start %x used %d cliprects %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000633 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Eric Anholt546b0972008-09-01 16:45:29 -0700635 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Anholt201361a2009-03-11 12:30:04 -0700637 if (batch->num_cliprects < 0)
638 return -EINVAL;
639
640 if (batch->num_cliprects) {
641 cliprects = drm_calloc(batch->num_cliprects,
642 sizeof(struct drm_clip_rect),
643 DRM_MEM_DRIVER);
644 if (cliprects == NULL)
645 return -ENOMEM;
646
647 ret = copy_from_user(cliprects, batch->cliprects,
648 batch->num_cliprects *
649 sizeof(struct drm_clip_rect));
650 if (ret != 0)
651 goto fail_free;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Eric Anholt546b0972008-09-01 16:45:29 -0700654 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700655 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700656 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400658 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000659 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700660
661fail_free:
662 drm_free(cliprects,
663 batch->num_cliprects * sizeof(struct drm_clip_rect),
664 DRM_MEM_DRIVER);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return ret;
667}
668
Eric Anholtc153f452007-09-03 12:06:45 +1000669static int i915_cmdbuffer(struct drm_device *dev, void *data,
670 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000673 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000675 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000676 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700677 struct drm_clip_rect *cliprects = NULL;
678 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int ret;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 DRM_DEBUG("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000682 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Eric Anholt546b0972008-09-01 16:45:29 -0700684 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Eric Anholt201361a2009-03-11 12:30:04 -0700686 if (cmdbuf->num_cliprects < 0)
687 return -EINVAL;
688
689 batch_data = drm_alloc(cmdbuf->sz, DRM_MEM_DRIVER);
690 if (batch_data == NULL)
691 return -ENOMEM;
692
693 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
694 if (ret != 0)
695 goto fail_batch_free;
696
697 if (cmdbuf->num_cliprects) {
698 cliprects = drm_calloc(cmdbuf->num_cliprects,
699 sizeof(struct drm_clip_rect),
700 DRM_MEM_DRIVER);
701 if (cliprects == NULL)
702 goto fail_batch_free;
703
704 ret = copy_from_user(cliprects, cmdbuf->cliprects,
705 cmdbuf->num_cliprects *
706 sizeof(struct drm_clip_rect));
707 if (ret != 0)
708 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Eric Anholt546b0972008-09-01 16:45:29 -0700711 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700712 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700713 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (ret) {
715 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000716 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400719 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000720 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700721
Eric Anholt201361a2009-03-11 12:30:04 -0700722fail_clip_free:
723 drm_free(cliprects,
724 cmdbuf->num_cliprects * sizeof(struct drm_clip_rect),
725 DRM_MEM_DRIVER);
Chris Wright355d7f32009-04-17 01:18:55 +0000726fail_batch_free:
727 drm_free(batch_data, cmdbuf->sz, DRM_MEM_DRIVER);
Eric Anholt201361a2009-03-11 12:30:04 -0700728
729 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Eric Anholtc153f452007-09-03 12:06:45 +1000732static int i915_flip_bufs(struct drm_device *dev, void *data,
733 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Eric Anholt546b0972008-09-01 16:45:29 -0700735 int ret;
736
Harvey Harrison80a914d2008-10-15 22:01:25 -0700737 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Eric Anholt546b0972008-09-01 16:45:29 -0700739 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Eric Anholt546b0972008-09-01 16:45:29 -0700741 mutex_lock(&dev->struct_mutex);
742 ret = i915_dispatch_flip(dev);
743 mutex_unlock(&dev->struct_mutex);
744
745 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Eric Anholtc153f452007-09-03 12:06:45 +1000748static int i915_getparam(struct drm_device *dev, void *data,
749 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000752 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int value;
754
755 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000756 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
Eric Anholtc153f452007-09-03 12:06:45 +1000760 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700762 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
764 case I915_PARAM_ALLOW_BATCHBUFFER:
765 value = dev_priv->allow_batchbuffer ? 1 : 0;
766 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100767 case I915_PARAM_LAST_DISPATCH:
768 value = READ_BREADCRUMB(dev_priv);
769 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400770 case I915_PARAM_CHIPSET_ID:
771 value = dev->pci_device;
772 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700773 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000774 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700775 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800776 case I915_PARAM_NUM_FENCES_AVAIL:
777 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 default:
Eric Anholt122ee2a2009-02-03 12:10:21 -0800780 DRM_DEBUG("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000781 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Eric Anholtc153f452007-09-03 12:06:45 +1000784 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000786 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
789 return 0;
790}
791
Eric Anholtc153f452007-09-03 12:06:45 +1000792static int i915_setparam(struct drm_device *dev, void *data,
793 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000796 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000799 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000800 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Eric Anholtc153f452007-09-03 12:06:45 +1000803 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
806 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000807 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 break;
809 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000810 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800812 case I915_SETPARAM_NUM_USED_FENCES:
813 if (param->value > dev_priv->num_fence_regs ||
814 param->value < 0)
815 return -EINVAL;
816 /* Userspace can use first N regs */
817 dev_priv->fence_reg_start = param->value;
818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 default:
Eric Anholt122ee2a2009-02-03 12:10:21 -0800820 DRM_DEBUG("unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000821 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
824 return 0;
825}
826
Eric Anholtc153f452007-09-03 12:06:45 +1000827static int i915_set_status_page(struct drm_device *dev, void *data,
828 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000829{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000830 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000831 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000832
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000833 if (!I915_NEED_GFX_HWS(dev))
834 return -EINVAL;
835
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000836 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000837 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000838 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000839 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000840
Jesse Barnes79e53942008-11-07 14:24:08 -0800841 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
842 WARN(1, "tried to set status page when mode setting active\n");
843 return 0;
844 }
845
Eric Anholtc153f452007-09-03 12:06:45 +1000846 printk(KERN_DEBUG "set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000847
Eric Anholtc153f452007-09-03 12:06:45 +1000848 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
849
Eric Anholt8b409582007-11-22 16:40:37 +1000850 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000851 dev_priv->hws_map.size = 4*1024;
852 dev_priv->hws_map.type = 0;
853 dev_priv->hws_map.flags = 0;
854 dev_priv->hws_map.mtrr = 0;
855
Dave Airliedd0910b2009-02-25 14:49:21 +1000856 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000857 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000858 i915_dma_cleanup(dev);
859 dev_priv->status_gfx_addr = 0;
860 DRM_ERROR("can not ioremap virtual address for"
861 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000862 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000863 }
864 dev_priv->hw_status_page = dev_priv->hws_map.handle;
865
866 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700867 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
868 DRM_DEBUG("load hws HWS_PGA with gfx mem 0x%x\n",
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000869 dev_priv->status_gfx_addr);
870 DRM_DEBUG("load hws at %p\n", dev_priv->hw_status_page);
871 return 0;
872}
873
Jesse Barnes79e53942008-11-07 14:24:08 -0800874/**
875 * i915_probe_agp - get AGP bootup configuration
876 * @pdev: PCI device
877 * @aperture_size: returns AGP aperture configured size
878 * @preallocated_size: returns size of BIOS preallocated AGP space
879 *
880 * Since Intel integrated graphics are UMA, the BIOS has to set aside
881 * some RAM for the framebuffer at early boot. This code figures out
882 * how much was set aside so we can use it for our own purposes.
883 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100884static int i915_probe_agp(struct drm_device *dev, unsigned long *aperture_size,
885 unsigned long *preallocated_size)
Jesse Barnes79e53942008-11-07 14:24:08 -0800886{
887 struct pci_dev *bridge_dev;
888 u16 tmp = 0;
889 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -0800890 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -0800891
892 bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
893 if (!bridge_dev) {
894 DRM_ERROR("bridge device not found\n");
895 return -1;
896 }
897
898 /* Get the fb aperture size and "stolen" memory amount. */
899 pci_read_config_word(bridge_dev, INTEL_GMCH_CTRL, &tmp);
900 pci_dev_put(bridge_dev);
901
902 *aperture_size = 1024 * 1024;
903 *preallocated_size = 1024 * 1024;
904
Eric Anholt60fd99e2008-12-03 22:50:02 -0800905 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800906 case PCI_DEVICE_ID_INTEL_82830_CGC:
907 case PCI_DEVICE_ID_INTEL_82845G_IG:
908 case PCI_DEVICE_ID_INTEL_82855GM_IG:
909 case PCI_DEVICE_ID_INTEL_82865_IG:
910 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
911 *aperture_size *= 64;
912 else
913 *aperture_size *= 128;
914 break;
915 default:
916 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -0800917 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -0800918 break;
919 }
920
921 /*
922 * Some of the preallocated space is taken by the GTT
923 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
924 */
Shaohua Li4a8df452009-03-13 16:22:33 +0800925 if (IS_G4X(dev) || IS_IGD(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -0800926 overhead = 4096;
927 else
928 overhead = (*aperture_size / 1024) + 4096;
929
Eric Anholt241fa852009-01-02 18:05:51 -0800930 switch (tmp & INTEL_GMCH_GMS_MASK) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800931 case INTEL_855_GMCH_GMS_DISABLED:
932 DRM_ERROR("video memory is disabled\n");
933 return -1;
Eric Anholt241fa852009-01-02 18:05:51 -0800934 case INTEL_855_GMCH_GMS_STOLEN_1M:
935 stolen = 1 * 1024 * 1024;
936 break;
937 case INTEL_855_GMCH_GMS_STOLEN_4M:
938 stolen = 4 * 1024 * 1024;
939 break;
940 case INTEL_855_GMCH_GMS_STOLEN_8M:
941 stolen = 8 * 1024 * 1024;
942 break;
943 case INTEL_855_GMCH_GMS_STOLEN_16M:
944 stolen = 16 * 1024 * 1024;
945 break;
946 case INTEL_855_GMCH_GMS_STOLEN_32M:
947 stolen = 32 * 1024 * 1024;
948 break;
949 case INTEL_915G_GMCH_GMS_STOLEN_48M:
950 stolen = 48 * 1024 * 1024;
951 break;
952 case INTEL_915G_GMCH_GMS_STOLEN_64M:
953 stolen = 64 * 1024 * 1024;
954 break;
955 case INTEL_GMCH_GMS_STOLEN_128M:
956 stolen = 128 * 1024 * 1024;
957 break;
958 case INTEL_GMCH_GMS_STOLEN_256M:
959 stolen = 256 * 1024 * 1024;
960 break;
961 case INTEL_GMCH_GMS_STOLEN_96M:
962 stolen = 96 * 1024 * 1024;
963 break;
964 case INTEL_GMCH_GMS_STOLEN_160M:
965 stolen = 160 * 1024 * 1024;
966 break;
967 case INTEL_GMCH_GMS_STOLEN_224M:
968 stolen = 224 * 1024 * 1024;
969 break;
970 case INTEL_GMCH_GMS_STOLEN_352M:
971 stolen = 352 * 1024 * 1024;
972 break;
Jesse Barnes79e53942008-11-07 14:24:08 -0800973 default:
974 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
Eric Anholt241fa852009-01-02 18:05:51 -0800975 tmp & INTEL_GMCH_GMS_MASK);
Jesse Barnes79e53942008-11-07 14:24:08 -0800976 return -1;
977 }
Eric Anholt241fa852009-01-02 18:05:51 -0800978 *preallocated_size = stolen - overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -0800979
980 return 0;
981}
982
983static int i915_load_modeset_init(struct drm_device *dev)
984{
985 struct drm_i915_private *dev_priv = dev->dev_private;
986 unsigned long agp_size, prealloc_size;
987 int fb_bar = IS_I9XX(dev) ? 2 : 0;
988 int ret = 0;
989
Dave Airlieaa596622008-12-29 16:35:02 +1000990 dev->devname = kstrdup(DRIVER_NAME, GFP_KERNEL);
991 if (!dev->devname) {
992 ret = -ENOMEM;
993 goto out;
994 }
995
Jesse Barnes79e53942008-11-07 14:24:08 -0800996 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
997 0xff000000;
998
Jesse Barnes2906f022009-01-20 19:10:54 -0800999 if (IS_MOBILE(dev) || IS_I9XX(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08001000 dev_priv->cursor_needs_physical = true;
1001 else
1002 dev_priv->cursor_needs_physical = false;
1003
Jesse Barnes2906f022009-01-20 19:10:54 -08001004 if (IS_I965G(dev) || IS_G33(dev))
1005 dev_priv->cursor_needs_physical = false;
1006
Dave Airlieaa596622008-12-29 16:35:02 +10001007 ret = i915_probe_agp(dev, &agp_size, &prealloc_size);
1008 if (ret)
1009 goto kfree_devname;
Jesse Barnes79e53942008-11-07 14:24:08 -08001010
1011 /* Basic memrange allocator for stolen space (aka vram) */
1012 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
1013
1014 /* Let GEM Manage from end of prealloc space to end of aperture */
1015 i915_gem_do_init(dev, prealloc_size, agp_size);
1016
1017 ret = i915_gem_init_ringbuffer(dev);
1018 if (ret)
Dave Airlieaa596622008-12-29 16:35:02 +10001019 goto kfree_devname;
Jesse Barnes79e53942008-11-07 14:24:08 -08001020
Jesse Barnes79e53942008-11-07 14:24:08 -08001021 /* Allow hardware batchbuffers unless told otherwise.
1022 */
1023 dev_priv->allow_batchbuffer = 1;
1024
1025 ret = intel_init_bios(dev);
1026 if (ret)
1027 DRM_INFO("failed to find VBIOS tables\n");
1028
1029 ret = drm_irq_install(dev);
1030 if (ret)
1031 goto destroy_ringbuffer;
1032
Jesse Barnes79e53942008-11-07 14:24:08 -08001033 /* Always safe in the mode setting case. */
1034 /* FIXME: do pre/post-mode set stuff in core KMS code */
1035 dev->vblank_disable_allowed = 1;
1036
1037 /*
1038 * Initialize the hardware status page IRQ location.
1039 */
1040
1041 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1042
1043 intel_modeset_init(dev);
1044
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001045 drm_helper_initial_config(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001046
Jesse Barnes79e53942008-11-07 14:24:08 -08001047 return 0;
1048
Jesse Barnes79e53942008-11-07 14:24:08 -08001049destroy_ringbuffer:
1050 i915_gem_cleanup_ringbuffer(dev);
Dave Airlieaa596622008-12-29 16:35:02 +10001051kfree_devname:
1052 kfree(dev->devname);
Jesse Barnes79e53942008-11-07 14:24:08 -08001053out:
1054 return ret;
1055}
1056
Dave Airlie7c1c2872008-11-28 14:22:24 +10001057int i915_master_create(struct drm_device *dev, struct drm_master *master)
1058{
1059 struct drm_i915_master_private *master_priv;
1060
1061 master_priv = drm_calloc(1, sizeof(*master_priv), DRM_MEM_DRIVER);
1062 if (!master_priv)
1063 return -ENOMEM;
1064
1065 master->driver_priv = master_priv;
1066 return 0;
1067}
1068
1069void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1070{
1071 struct drm_i915_master_private *master_priv = master->driver_priv;
1072
1073 if (!master_priv)
1074 return;
1075
1076 drm_free(master_priv, sizeof(*master_priv), DRM_MEM_DRIVER);
1077
1078 master->driver_priv = NULL;
1079}
1080
Jesse Barnes79e53942008-11-07 14:24:08 -08001081/**
1082 * i915_driver_load - setup chip and create an initial config
1083 * @dev: DRM device
1084 * @flags: startup flags
1085 *
1086 * The driver load routine has to do several things:
1087 * - drive output discovery via intel_modeset_init()
1088 * - initialize the memory manager
1089 * - allocate initial config memory
1090 * - setup the DRM framebuffer with the allocated memory
1091 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001092int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001093{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001094 struct drm_i915_private *dev_priv = dev->dev_private;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001095 resource_size_t base, size;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001096 int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1;
1097
Dave Airlie22eae942005-11-10 22:16:34 +11001098 /* i915 has 4 more counters */
1099 dev->counters += 4;
1100 dev->types[6] = _DRM_STAT_IRQ;
1101 dev->types[7] = _DRM_STAT_PRIMARY;
1102 dev->types[8] = _DRM_STAT_SECONDARY;
1103 dev->types[9] = _DRM_STAT_DMA;
1104
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001105 dev_priv = drm_alloc(sizeof(drm_i915_private_t), DRM_MEM_DRIVER);
1106 if (dev_priv == NULL)
1107 return -ENOMEM;
1108
1109 memset(dev_priv, 0, sizeof(drm_i915_private_t));
1110
1111 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001112 dev_priv->dev = dev;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001113
1114 /* Add register map (needed for suspend/resume) */
1115 base = drm_get_resource_start(dev, mmio_bar);
1116 size = drm_get_resource_len(dev, mmio_bar);
1117
Eric Anholt3043c602008-10-02 12:24:47 -07001118 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001119 if (!dev_priv->regs) {
1120 DRM_ERROR("failed to map registers\n");
1121 ret = -EIO;
1122 goto free_priv;
1123 }
Eric Anholted4cb412008-07-29 12:10:39 -07001124
Eric Anholtab657db12009-01-23 12:57:47 -08001125 dev_priv->mm.gtt_mapping =
1126 io_mapping_create_wc(dev->agp->base,
1127 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001128 if (dev_priv->mm.gtt_mapping == NULL) {
1129 ret = -EIO;
1130 goto out_rmmap;
1131 }
1132
Eric Anholtab657db12009-01-23 12:57:47 -08001133 /* Set up a WC MTRR for non-PAT systems. This is more common than
1134 * one would think, because the kernel disables PAT on first
1135 * generation Core chips because WC PAT gets overridden by a UC
1136 * MTRR if present. Even if a UC MTRR isn't present.
1137 */
1138 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1139 dev->agp->agp_info.aper_size *
1140 1024 * 1024,
1141 MTRR_TYPE_WRCOMB, 1);
1142 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001143 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001144 "performance may suffer.\n");
1145 }
1146
Dave Airlieac5c4e72008-12-19 15:38:34 +10001147#ifdef CONFIG_HIGHMEM64G
1148 /* don't enable GEM on PAE - needs agp + set_memory_* interface fixes */
1149 dev_priv->has_gem = 0;
1150#else
1151 /* enable GEM by default */
1152 dev_priv->has_gem = 1;
1153#endif
1154
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001155 dev->driver->get_vblank_counter = i915_get_vblank_counter;
1156 if (IS_GM45(dev))
1157 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
1158
Eric Anholt673a3942008-07-30 12:06:12 -07001159 i915_gem_load(dev);
1160
Keith Packard398c9cb2008-07-30 13:03:43 -07001161 /* Init HWS */
1162 if (!I915_NEED_GFX_HWS(dev)) {
1163 ret = i915_init_phys_hws(dev);
1164 if (ret != 0)
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001165 goto out_iomapfree;
Keith Packard398c9cb2008-07-30 13:03:43 -07001166 }
Eric Anholted4cb412008-07-29 12:10:39 -07001167
1168 /* On the 945G/GM, the chipset reports the MSI capability on the
1169 * integrated graphics even though the support isn't actually there
1170 * according to the published specs. It doesn't appear to function
1171 * correctly in testing on 945G.
1172 * This may be a side effect of MSI having been made available for PEG
1173 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001174 *
1175 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001176 * be lost or delayed, but we use them anyways to avoid
1177 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001178 */
Keith Packardb60678a2008-12-08 11:12:28 -08001179 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001180 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001181
1182 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes79e53942008-11-07 14:24:08 -08001183 dev_priv->user_irq_refcount = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001184
Keith Packard52440212008-11-18 09:30:25 -08001185 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1186
1187 if (ret) {
1188 (void) i915_driver_unload(dev);
1189 return ret;
1190 }
1191
Jesse Barnes79e53942008-11-07 14:24:08 -08001192 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1193 ret = i915_load_modeset_init(dev);
1194 if (ret < 0) {
1195 DRM_ERROR("failed to init modeset\n");
1196 goto out_rmmap;
1197 }
1198 }
1199
Matthew Garrett74a365b2009-03-19 21:35:39 +00001200 /* Must be done after probing outputs */
1201 intel_opregion_init(dev, 0);
1202
Jesse Barnes79e53942008-11-07 14:24:08 -08001203 return 0;
1204
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001205out_iomapfree:
1206 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001207out_rmmap:
1208 iounmap(dev_priv->regs);
1209free_priv:
1210 drm_free(dev_priv, sizeof(struct drm_i915_private), DRM_MEM_DRIVER);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001211 return ret;
1212}
1213
1214int i915_driver_unload(struct drm_device *dev)
1215{
1216 struct drm_i915_private *dev_priv = dev->dev_private;
1217
Eric Anholtab657db12009-01-23 12:57:47 -08001218 io_mapping_free(dev_priv->mm.gtt_mapping);
1219 if (dev_priv->mm.gtt_mtrr >= 0) {
1220 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1221 dev->agp->agp_info.aper_size * 1024 * 1024);
1222 dev_priv->mm.gtt_mtrr = -1;
1223 }
1224
Jesse Barnes79e53942008-11-07 14:24:08 -08001225 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001226 drm_irq_uninstall(dev);
1227 }
1228
Eric Anholted4cb412008-07-29 12:10:39 -07001229 if (dev->pdev->msi_enabled)
1230 pci_disable_msi(dev->pdev);
1231
Eric Anholt3043c602008-10-02 12:24:47 -07001232 if (dev_priv->regs != NULL)
1233 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001234
Matthew Garrett3b1c1c12009-04-01 19:52:29 +01001235 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001236
Jesse Barnes79e53942008-11-07 14:24:08 -08001237 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1238 intel_modeset_cleanup(dev);
1239
Dave Airlie71acb5e2008-12-30 20:31:46 +10001240 i915_gem_free_all_phys_object(dev);
1241
Jesse Barnes79e53942008-11-07 14:24:08 -08001242 mutex_lock(&dev->struct_mutex);
1243 i915_gem_cleanup_ringbuffer(dev);
1244 mutex_unlock(&dev->struct_mutex);
1245 drm_mm_takedown(&dev_priv->vram);
1246 i915_gem_lastclose(dev);
1247 }
1248
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001249 drm_free(dev->dev_private, sizeof(drm_i915_private_t),
1250 DRM_MEM_DRIVER);
1251
Dave Airlie22eae942005-11-10 22:16:34 +11001252 return 0;
1253}
1254
Eric Anholt673a3942008-07-30 12:06:12 -07001255int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1256{
1257 struct drm_i915_file_private *i915_file_priv;
1258
1259 DRM_DEBUG("\n");
1260 i915_file_priv = (struct drm_i915_file_private *)
1261 drm_alloc(sizeof(*i915_file_priv), DRM_MEM_FILES);
1262
1263 if (!i915_file_priv)
1264 return -ENOMEM;
1265
1266 file_priv->driver_priv = i915_file_priv;
1267
1268 i915_file_priv->mm.last_gem_seqno = 0;
1269 i915_file_priv->mm.last_gem_throttle_seqno = 0;
1270
1271 return 0;
1272}
1273
Jesse Barnes79e53942008-11-07 14:24:08 -08001274/**
1275 * i915_driver_lastclose - clean up after all DRM clients have exited
1276 * @dev: DRM device
1277 *
1278 * Take care of cleaning up after all DRM clients have exited. In the
1279 * mode setting case, we want to restore the kernel's initial mode (just
1280 * in case the last client left us in a bad state).
1281 *
1282 * Additionally, in the non-mode setting case, we'll tear down the AGP
1283 * and DMA structures, since the kernel won't be using them, and clea
1284 * up any GEM state.
1285 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001286void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001288 drm_i915_private_t *dev_priv = dev->dev_private;
1289
Jesse Barnes79e53942008-11-07 14:24:08 -08001290 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
1291 intelfb_restore();
Dave Airlie144a75f2008-03-30 07:53:58 +10001292 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001293 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001294
Eric Anholt673a3942008-07-30 12:06:12 -07001295 i915_gem_lastclose(dev);
1296
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001297 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001298 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001299
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001300 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Eric Anholt6c340ea2007-08-25 20:23:09 +10001303void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001305 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001306 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1307 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Eric Anholt673a3942008-07-30 12:06:12 -07001310void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1311{
1312 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1313
1314 drm_free(i915_file_priv, sizeof(*i915_file_priv), DRM_MEM_FILES);
1315}
1316
Eric Anholtc153f452007-09-03 12:06:45 +10001317struct drm_ioctl_desc i915_ioctls[] = {
1318 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1319 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1320 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1321 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1322 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1323 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1324 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1325 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1326 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1327 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1328 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1329 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1330 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1331 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1332 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1333 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001334 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 +10001335 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 -07001336 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
1337 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1338 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1339 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
1340 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
Dave Airlie2bdf00b2008-10-07 13:40:10 +10001341 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1342 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 -07001343 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1344 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
1345 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
1346 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
Jesse Barnesde151cf2008-11-12 10:03:55 -08001347 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0),
Eric Anholt673a3942008-07-30 12:06:12 -07001348 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
1349 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
1350 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1351 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
Eric Anholt5a125c32008-10-22 21:40:13 -07001352 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0),
Dave Airliec94f7022005-07-07 21:03:38 +10001353};
1354
1355int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001356
1357/**
1358 * Determine if the device really is AGP or not.
1359 *
1360 * All Intel graphics chipsets are treated as AGP, even if they are really
1361 * PCI-e.
1362 *
1363 * \param dev The device to be tested.
1364 *
1365 * \returns
1366 * A value of 1 is always retured to indictate every i9x5 is AGP.
1367 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001368int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001369{
1370 return 1;
1371}