blob: 048da791ca664c6086d381f08e3715eb25703b32 [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"
31#include "i915_drm.h"
32#include "i915_drv.h"
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Really want an OS-independent resettable timer. Would like to have
35 * this loop run for (eg) 3 sec, but have the timer reset every time
36 * the head pointer changes, so that EBUSY only happens if the ring
37 * actually stalls for (eg) 3 seconds.
38 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100039int i915_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 drm_i915_private_t *dev_priv = dev->dev_private;
42 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
Keith Packardd3a6d442008-07-30 12:21:20 -070043 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
44 u32 last_acthd = I915_READ(acthd_reg);
45 u32 acthd;
Jesse Barnes585fb112008-07-29 11:54:06 -070046 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int i;
48
Keith Packardd3a6d442008-07-30 12:21:20 -070049 for (i = 0; i < 100000; i++) {
Jesse Barnes585fb112008-07-29 11:54:06 -070050 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Keith Packardd3a6d442008-07-30 12:21:20 -070051 acthd = I915_READ(acthd_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 ring->space = ring->head - (ring->tail + 8);
53 if (ring->space < 0)
54 ring->space += ring->Size;
55 if (ring->space >= n)
56 return 0;
57
Kristian Høgsbergc99b0582008-08-20 11:20:13 -040058 if (dev_priv->sarea_priv)
59 dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (ring->head != last_head)
62 i = 0;
Keith Packardd3a6d442008-07-30 12:21:20 -070063 if (acthd != last_acthd)
64 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 last_head = ring->head;
Keith Packardd3a6d442008-07-30 12:21:20 -070067 last_acthd = acthd;
68 msleep_interruptible(10);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
Eric Anholt20caafa2007-08-25 19:22:43 +100072 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Keith Packard398c9cb2008-07-30 13:03:43 -070075/**
76 * Sets up the hardware status page for devices that need a physical address
77 * in the register.
78 */
79int i915_init_phys_hws(struct drm_device *dev)
80{
81 drm_i915_private_t *dev_priv = dev->dev_private;
82 /* Program Hardware Status Page */
83 dev_priv->status_page_dmah =
84 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
85
86 if (!dev_priv->status_page_dmah) {
87 DRM_ERROR("Can not allocate hardware status page\n");
88 return -ENOMEM;
89 }
90 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
91 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
92
93 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
94
95 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
96 DRM_DEBUG("Enabled hardware status page\n");
97 return 0;
98}
99
100/**
101 * Frees the hardware status page, whether it's a physical address or a virtual
102 * address set up by the X Server.
103 */
104void i915_free_hws(struct drm_device *dev)
105{
106 drm_i915_private_t *dev_priv = dev->dev_private;
107 if (dev_priv->status_page_dmah) {
108 drm_pci_free(dev, dev_priv->status_page_dmah);
109 dev_priv->status_page_dmah = NULL;
110 }
111
112 if (dev_priv->status_gfx_addr) {
113 dev_priv->status_gfx_addr = 0;
114 drm_core_ioremapfree(&dev_priv->hws_map, dev);
115 }
116
117 /* Need to rewrite hardware status page */
118 I915_WRITE(HWS_PGA, 0x1ffff000);
119}
120
Dave Airlie84b1fd12007-07-11 15:53:27 +1000121void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 drm_i915_private_t *dev_priv = dev->dev_private;
124 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
125
Jesse Barnes585fb112008-07-29 11:54:06 -0700126 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
127 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ring->space = ring->head - (ring->tail + 8);
129 if (ring->space < 0)
130 ring->space += ring->Size;
131
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400132 if (ring->head == ring->tail && dev_priv->sarea_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 dev_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
134}
135
Dave Airlie84b1fd12007-07-11 15:53:27 +1000136static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000138 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* Make sure interrupts are disabled here because the uninstall ioctl
140 * may not have been called from userspace and after dev_private
141 * is freed, it's too late.
142 */
Eric Anholted4cb412008-07-29 12:10:39 -0700143 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000144 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000146 if (dev_priv->ring.virtual_start) {
147 drm_core_ioremapfree(&dev_priv->ring.map, dev);
148 dev_priv->ring.virtual_start = 0;
149 dev_priv->ring.map.handle = 0;
150 dev_priv->ring.map.size = 0;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Keith Packard398c9cb2008-07-30 13:03:43 -0700153 /* Clear the HWS virtual address at teardown */
154 if (I915_NEED_GFX_HWS(dev))
155 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return 0;
158}
159
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000160static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000162 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Dave Airlieda509d72007-05-26 05:04:51 +1000164 dev_priv->sarea = drm_getsarea(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!dev_priv->sarea) {
166 DRM_ERROR("can not find sarea!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 i915_dma_cleanup(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000168 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 dev_priv->sarea_priv = (drm_i915_sarea_t *)
172 ((u8 *) dev_priv->sarea->handle + init->sarea_priv_offset);
173
Eric Anholt673a3942008-07-30 12:06:12 -0700174 if (init->ring_size != 0) {
175 if (dev_priv->ring.ring_obj != NULL) {
176 i915_dma_cleanup(dev);
177 DRM_ERROR("Client tried to initialize ringbuffer in "
178 "GEM mode\n");
179 return -EINVAL;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Eric Anholt673a3942008-07-30 12:06:12 -0700182 dev_priv->ring.Size = init->ring_size;
183 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Eric Anholt673a3942008-07-30 12:06:12 -0700185 dev_priv->ring.map.offset = init->ring_start;
186 dev_priv->ring.map.size = init->ring_size;
187 dev_priv->ring.map.type = 0;
188 dev_priv->ring.map.flags = 0;
189 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Eric Anholt673a3942008-07-30 12:06:12 -0700191 drm_core_ioremap(&dev_priv->ring.map, dev);
192
193 if (dev_priv->ring.map.handle == NULL) {
194 i915_dma_cleanup(dev);
195 DRM_ERROR("can not ioremap virtual address for"
196 " ring buffer\n");
197 return -ENOMEM;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
201 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
202
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000203 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 dev_priv->back_offset = init->back_offset;
205 dev_priv->front_offset = init->front_offset;
206 dev_priv->current_page = 0;
207 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* Allow hardware batchbuffers unless told otherwise.
210 */
211 dev_priv->allow_batchbuffer = 1;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
214}
215
Dave Airlie84b1fd12007-07-11 15:53:27 +1000216static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
219
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700220 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (!dev_priv->sarea) {
223 DRM_ERROR("can not find sarea!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (dev_priv->ring.map.handle == NULL) {
228 DRM_ERROR("can not ioremap virtual address for"
229 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000230 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
233 /* Program Hardware Status Page */
234 if (!dev_priv->hw_status_page) {
235 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000236 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
239
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000240 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700241 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000242 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700243 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 DRM_DEBUG("Enabled hardware status page\n");
245
246 return 0;
247}
248
Eric Anholtc153f452007-09-03 12:06:45 +1000249static int i915_dma_init(struct drm_device *dev, void *data,
250 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Eric Anholtc153f452007-09-03 12:06:45 +1000252 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int retcode = 0;
254
Eric Anholtc153f452007-09-03 12:06:45 +1000255 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000257 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
259 case I915_CLEANUP_DMA:
260 retcode = i915_dma_cleanup(dev);
261 break;
262 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100263 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 break;
265 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000266 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
268 }
269
270 return retcode;
271}
272
273/* Implement basically the same security restrictions as hardware does
274 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
275 *
276 * Most of the calculations below involve calculating the size of a
277 * particular instruction. It's important to get the size right as
278 * that tells us where the next instruction to check is. Any illegal
279 * instruction detected will be given a size of zero, which is a
280 * signal to abort the rest of the buffer.
281 */
282static int do_validate_cmd(int cmd)
283{
284 switch (((cmd >> 29) & 0x7)) {
285 case 0x0:
286 switch ((cmd >> 23) & 0x3f) {
287 case 0x0:
288 return 1; /* MI_NOOP */
289 case 0x4:
290 return 1; /* MI_FLUSH */
291 default:
292 return 0; /* disallow everything else */
293 }
294 break;
295 case 0x1:
296 return 0; /* reserved */
297 case 0x2:
298 return (cmd & 0xff) + 2; /* 2d commands */
299 case 0x3:
300 if (((cmd >> 24) & 0x1f) <= 0x18)
301 return 1;
302
303 switch ((cmd >> 24) & 0x1f) {
304 case 0x1c:
305 return 1;
306 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000307 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case 0x3:
309 return (cmd & 0x1f) + 2;
310 case 0x4:
311 return (cmd & 0xf) + 2;
312 default:
313 return (cmd & 0xffff) + 2;
314 }
315 case 0x1e:
316 if (cmd & (1 << 23))
317 return (cmd & 0xffff) + 1;
318 else
319 return 1;
320 case 0x1f:
321 if ((cmd & (1 << 23)) == 0) /* inline vertices */
322 return (cmd & 0x1ffff) + 2;
323 else if (cmd & (1 << 17)) /* indirect random */
324 if ((cmd & 0xffff) == 0)
325 return 0; /* unknown length, too hard */
326 else
327 return (((cmd & 0xffff) + 1) / 2) + 1;
328 else
329 return 2; /* indirect sequential */
330 default:
331 return 0;
332 }
333 default:
334 return 0;
335 }
336
337 return 0;
338}
339
340static int validate_cmd(int cmd)
341{
342 int ret = do_validate_cmd(cmd);
343
Dave Airliebc5f4522007-11-05 12:50:58 +1000344/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 return ret;
347}
348
Dave Airlie84b1fd12007-07-11 15:53:27 +1000349static int i915_emit_cmds(struct drm_device * dev, int __user * buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 drm_i915_private_t *dev_priv = dev->dev_private;
352 int i;
353 RING_LOCALS;
354
Dave Airliede227f52006-01-25 15:31:43 +1100355 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000356 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100357
Alan Hourihanec29b6692006-08-12 16:29:24 +1000358 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 for (i = 0; i < dwords;) {
361 int cmd, sz;
362
363 if (DRM_COPY_FROM_USER_UNCHECKED(&cmd, &buffer[i], sizeof(cmd)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000367 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 OUT_RING(cmd);
370
371 while (++i, --sz) {
372 if (DRM_COPY_FROM_USER_UNCHECKED(&cmd, &buffer[i],
373 sizeof(cmd))) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000374 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376 OUT_RING(cmd);
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Dave Airliede227f52006-01-25 15:31:43 +1100380 if (dwords & 1)
381 OUT_RING(0);
382
383 ADVANCE_LP_RING();
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386}
387
Eric Anholt673a3942008-07-30 12:06:12 -0700388int
389i915_emit_box(struct drm_device *dev,
390 struct drm_clip_rect __user *boxes,
391 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airliec60ce622007-07-11 15:27:12 +1000394 struct drm_clip_rect box;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 RING_LOCALS;
396
397 if (DRM_COPY_FROM_USER_UNCHECKED(&box, &boxes[i], sizeof(box))) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000398 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
402 DRM_ERROR("Bad box %d,%d..%d,%d\n",
403 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000404 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
Alan Hourihanec29b6692006-08-12 16:29:24 +1000407 if (IS_I965G(dev)) {
408 BEGIN_LP_RING(4);
409 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
410 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000411 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000412 OUT_RING(DR4);
413 ADVANCE_LP_RING();
414 } else {
415 BEGIN_LP_RING(6);
416 OUT_RING(GFX_OP_DRAWRECT_INFO);
417 OUT_RING(DR1);
418 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
419 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
420 OUT_RING(DR4);
421 OUT_RING(0);
422 ADVANCE_LP_RING();
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 return 0;
426}
427
Alan Hourihanec29b6692006-08-12 16:29:24 +1000428/* XXX: Emitting the counter should really be moved to part of the IRQ
429 * emit. For now, do it in both places:
430 */
431
Dave Airlie84b1fd12007-07-11 15:53:27 +1000432static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100433{
434 drm_i915_private_t *dev_priv = dev->dev_private;
435 RING_LOCALS;
436
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400437 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000438 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400439 dev_priv->counter = 0;
440 if (dev_priv->sarea_priv)
441 dev_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100442
443 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700444 OUT_RING(MI_STORE_DWORD_INDEX);
445 OUT_RING(5 << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100446 OUT_RING(dev_priv->counter);
447 OUT_RING(0);
448 ADVANCE_LP_RING();
449}
450
Dave Airlie84b1fd12007-07-11 15:53:27 +1000451static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 drm_i915_cmdbuffer_t * cmd)
453{
454 int nbox = cmd->num_cliprects;
455 int i = 0, count, ret;
456
457 if (cmd->sz & 0x3) {
458 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462 i915_kernel_lost_context(dev);
463
464 count = nbox ? nbox : 1;
465
466 for (i = 0; i < count; i++) {
467 if (i < nbox) {
468 ret = i915_emit_box(dev, cmd->cliprects, i,
469 cmd->DR1, cmd->DR4);
470 if (ret)
471 return ret;
472 }
473
474 ret = i915_emit_cmds(dev, (int __user *)cmd->buf, cmd->sz / 4);
475 if (ret)
476 return ret;
477 }
478
Dave Airliede227f52006-01-25 15:31:43 +1100479 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
481}
482
Dave Airlie84b1fd12007-07-11 15:53:27 +1000483static int i915_dispatch_batchbuffer(struct drm_device * dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 drm_i915_batchbuffer_t * batch)
485{
486 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airliec60ce622007-07-11 15:27:12 +1000487 struct drm_clip_rect __user *boxes = batch->cliprects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int nbox = batch->num_cliprects;
489 int i = 0, count;
490 RING_LOCALS;
491
492 if ((batch->start | batch->used) & 0x7) {
493 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000494 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
497 i915_kernel_lost_context(dev);
498
499 count = nbox ? nbox : 1;
500
501 for (i = 0; i < count; i++) {
502 if (i < nbox) {
503 int ret = i915_emit_box(dev, boxes, i,
504 batch->DR1, batch->DR4);
505 if (ret)
506 return ret;
507 }
508
Keith Packard0790d5e2008-07-30 12:28:47 -0700509 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000511 if (IS_I965G(dev)) {
512 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
513 OUT_RING(batch->start);
514 } else {
515 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
516 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ADVANCE_LP_RING();
519 } else {
520 BEGIN_LP_RING(4);
521 OUT_RING(MI_BATCH_BUFFER);
522 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
523 OUT_RING(batch->start + batch->used - 4);
524 OUT_RING(0);
525 ADVANCE_LP_RING();
526 }
527 }
528
Dave Airliede227f52006-01-25 15:31:43 +1100529 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 return 0;
532}
533
Dave Airlieaf6061a2008-05-07 12:15:39 +1000534static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 drm_i915_private_t *dev_priv = dev->dev_private;
537 RING_LOCALS;
538
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400539 if (!dev_priv->sarea_priv)
540 return -EINVAL;
541
Dave Airlieaf6061a2008-05-07 12:15:39 +1000542 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700543 __func__,
Dave Airlieaf6061a2008-05-07 12:15:39 +1000544 dev_priv->current_page,
545 dev_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Dave Airlieaf6061a2008-05-07 12:15:39 +1000547 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Dave Airlieaf6061a2008-05-07 12:15:39 +1000549 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700550 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000551 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 ADVANCE_LP_RING();
553
Dave Airlieaf6061a2008-05-07 12:15:39 +1000554 BEGIN_LP_RING(6);
555 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
556 OUT_RING(0);
557 if (dev_priv->current_page == 0) {
558 OUT_RING(dev_priv->back_offset);
559 dev_priv->current_page = 1;
560 } else {
561 OUT_RING(dev_priv->front_offset);
562 dev_priv->current_page = 0;
563 }
564 OUT_RING(0);
565 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000566
Dave Airlieaf6061a2008-05-07 12:15:39 +1000567 BEGIN_LP_RING(2);
568 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
569 OUT_RING(0);
570 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000571
Dave Airlieaf6061a2008-05-07 12:15:39 +1000572 dev_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000573
Dave Airlieaf6061a2008-05-07 12:15:39 +1000574 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700575 OUT_RING(MI_STORE_DWORD_INDEX);
576 OUT_RING(5 << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000577 OUT_RING(dev_priv->counter);
578 OUT_RING(0);
579 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000580
Dave Airlieaf6061a2008-05-07 12:15:39 +1000581 dev_priv->sarea_priv->pf_current_page = dev_priv->current_page;
582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Dave Airlie84b1fd12007-07-11 15:53:27 +1000585static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 drm_i915_private_t *dev_priv = dev->dev_private;
588
589 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700590 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Eric Anholtc153f452007-09-03 12:06:45 +1000593static int i915_flush_ioctl(struct drm_device *dev, void *data,
594 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Eric Anholt546b0972008-09-01 16:45:29 -0700596 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Eric Anholt546b0972008-09-01 16:45:29 -0700598 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
599
600 mutex_lock(&dev->struct_mutex);
601 ret = i915_quiescent(dev);
602 mutex_unlock(&dev->struct_mutex);
603
604 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Eric Anholtc153f452007-09-03 12:06:45 +1000607static int i915_batchbuffer(struct drm_device *dev, void *data,
608 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000611 u32 *hw_status = dev_priv->hw_status_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
613 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000614 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int ret;
616
617 if (!dev_priv->allow_batchbuffer) {
618 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000619 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 DRM_DEBUG("i915 batchbuffer, start %x used %d cliprects %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000623 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Eric Anholt546b0972008-09-01 16:45:29 -0700625 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Eric Anholtc153f452007-09-03 12:06:45 +1000627 if (batch->num_cliprects && DRM_VERIFYAREA_READ(batch->cliprects,
628 batch->num_cliprects *
Dave Airliec60ce622007-07-11 15:27:12 +1000629 sizeof(struct drm_clip_rect)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000630 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Eric Anholt546b0972008-09-01 16:45:29 -0700632 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000633 ret = i915_dispatch_batchbuffer(dev, batch);
Eric Anholt546b0972008-09-01 16:45:29 -0700634 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400636 if (sarea_priv)
637 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return ret;
639}
640
Eric Anholtc153f452007-09-03 12:06:45 +1000641static int i915_cmdbuffer(struct drm_device *dev, void *data,
642 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000645 u32 *hw_status = dev_priv->hw_status_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
647 dev_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000648 drm_i915_cmdbuffer_t *cmdbuf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 int ret;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 DRM_DEBUG("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000652 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Eric Anholt546b0972008-09-01 16:45:29 -0700654 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Eric Anholtc153f452007-09-03 12:06:45 +1000656 if (cmdbuf->num_cliprects &&
657 DRM_VERIFYAREA_READ(cmdbuf->cliprects,
658 cmdbuf->num_cliprects *
Dave Airliec60ce622007-07-11 15:27:12 +1000659 sizeof(struct drm_clip_rect))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 DRM_ERROR("Fault accessing cliprects\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000661 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
Eric Anholt546b0972008-09-01 16:45:29 -0700664 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000665 ret = i915_dispatch_cmdbuffer(dev, cmdbuf);
Eric Anholt546b0972008-09-01 16:45:29 -0700666 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (ret) {
668 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
669 return ret;
670 }
671
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400672 if (sarea_priv)
673 sarea_priv->last_dispatch = (int)hw_status[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return 0;
675}
676
Eric Anholtc153f452007-09-03 12:06:45 +1000677static int i915_flip_bufs(struct drm_device *dev, void *data,
678 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Eric Anholt546b0972008-09-01 16:45:29 -0700680 int ret;
681
Harvey Harrison80a914d2008-10-15 22:01:25 -0700682 DRM_DEBUG("%s\n", __func__);
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 Anholt546b0972008-09-01 16:45:29 -0700686 mutex_lock(&dev->struct_mutex);
687 ret = i915_dispatch_flip(dev);
688 mutex_unlock(&dev->struct_mutex);
689
690 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Eric Anholtc153f452007-09-03 12:06:45 +1000693static int i915_getparam(struct drm_device *dev, void *data,
694 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000697 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 int value;
699
700 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000701 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000702 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Eric Anholtc153f452007-09-03 12:06:45 +1000705 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700707 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 break;
709 case I915_PARAM_ALLOW_BATCHBUFFER:
710 value = dev_priv->allow_batchbuffer ? 1 : 0;
711 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100712 case I915_PARAM_LAST_DISPATCH:
713 value = READ_BREADCRUMB(dev_priv);
714 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400715 case I915_PARAM_CHIPSET_ID:
716 value = dev->pci_device;
717 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700718 case I915_PARAM_HAS_GEM:
719 value = 1;
720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 default:
Eric Anholtc153f452007-09-03 12:06:45 +1000722 DRM_ERROR("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000723 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
Eric Anholtc153f452007-09-03 12:06:45 +1000726 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000728 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
731 return 0;
732}
733
Eric Anholtc153f452007-09-03 12:06:45 +1000734static int i915_setparam(struct drm_device *dev, void *data,
735 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000738 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000741 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000742 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Eric Anholtc153f452007-09-03 12:06:45 +1000745 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
748 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000749 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000752 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 default:
Eric Anholtc153f452007-09-03 12:06:45 +1000755 DRM_ERROR("unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 return 0;
760}
761
Eric Anholtc153f452007-09-03 12:06:45 +1000762static int i915_set_status_page(struct drm_device *dev, void *data,
763 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000764{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000765 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000766 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000767
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000768 if (!I915_NEED_GFX_HWS(dev))
769 return -EINVAL;
770
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000771 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000772 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000773 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000774 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000775
Eric Anholtc153f452007-09-03 12:06:45 +1000776 printk(KERN_DEBUG "set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000777
Eric Anholtc153f452007-09-03 12:06:45 +1000778 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
779
Eric Anholt8b409582007-11-22 16:40:37 +1000780 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000781 dev_priv->hws_map.size = 4*1024;
782 dev_priv->hws_map.type = 0;
783 dev_priv->hws_map.flags = 0;
784 dev_priv->hws_map.mtrr = 0;
785
786 drm_core_ioremap(&dev_priv->hws_map, dev);
787 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000788 i915_dma_cleanup(dev);
789 dev_priv->status_gfx_addr = 0;
790 DRM_ERROR("can not ioremap virtual address for"
791 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000792 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000793 }
794 dev_priv->hw_status_page = dev_priv->hws_map.handle;
795
796 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700797 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
798 DRM_DEBUG("load hws HWS_PGA with gfx mem 0x%x\n",
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000799 dev_priv->status_gfx_addr);
800 DRM_DEBUG("load hws at %p\n", dev_priv->hw_status_page);
801 return 0;
802}
803
Dave Airlie84b1fd12007-07-11 15:53:27 +1000804int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +1100805{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000806 struct drm_i915_private *dev_priv = dev->dev_private;
807 unsigned long base, size;
808 int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1;
809
Dave Airlie22eae942005-11-10 22:16:34 +1100810 /* i915 has 4 more counters */
811 dev->counters += 4;
812 dev->types[6] = _DRM_STAT_IRQ;
813 dev->types[7] = _DRM_STAT_PRIMARY;
814 dev->types[8] = _DRM_STAT_SECONDARY;
815 dev->types[9] = _DRM_STAT_DMA;
816
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000817 dev_priv = drm_alloc(sizeof(drm_i915_private_t), DRM_MEM_DRIVER);
818 if (dev_priv == NULL)
819 return -ENOMEM;
820
821 memset(dev_priv, 0, sizeof(drm_i915_private_t));
822
823 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700824 dev_priv->dev = dev;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000825
826 /* Add register map (needed for suspend/resume) */
827 base = drm_get_resource_start(dev, mmio_bar);
828 size = drm_get_resource_len(dev, mmio_bar);
829
Dave Airliee3236a12007-12-17 09:41:56 +1000830 ret = drm_addmap(dev, base, size, _DRM_REGISTERS,
831 _DRM_KERNEL | _DRM_DRIVER,
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000832 &dev_priv->mmio_map);
Eric Anholted4cb412008-07-29 12:10:39 -0700833
Eric Anholt673a3942008-07-30 12:06:12 -0700834 i915_gem_load(dev);
835
Keith Packard398c9cb2008-07-30 13:03:43 -0700836 /* Init HWS */
837 if (!I915_NEED_GFX_HWS(dev)) {
838 ret = i915_init_phys_hws(dev);
839 if (ret != 0)
840 return ret;
841 }
Eric Anholted4cb412008-07-29 12:10:39 -0700842
843 /* On the 945G/GM, the chipset reports the MSI capability on the
844 * integrated graphics even though the support isn't actually there
845 * according to the published specs. It doesn't appear to function
846 * correctly in testing on 945G.
847 * This may be a side effect of MSI having been made available for PEG
848 * and the registers being closely associated.
849 */
850 if (!IS_I945G(dev) && !IS_I945GM(dev))
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700851 if (pci_enable_msi(dev->pdev))
852 DRM_ERROR("failed to enable MSI\n");
Eric Anholted4cb412008-07-29 12:10:39 -0700853
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100854 intel_opregion_init(dev);
855
Eric Anholted4cb412008-07-29 12:10:39 -0700856 spin_lock_init(&dev_priv->user_irq_lock);
857
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000858 return ret;
859}
860
861int i915_driver_unload(struct drm_device *dev)
862{
863 struct drm_i915_private *dev_priv = dev->dev_private;
864
Eric Anholted4cb412008-07-29 12:10:39 -0700865 if (dev->pdev->msi_enabled)
866 pci_disable_msi(dev->pdev);
867
Keith Packard398c9cb2008-07-30 13:03:43 -0700868 i915_free_hws(dev);
869
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000870 if (dev_priv->mmio_map)
871 drm_rmmap(dev, dev_priv->mmio_map);
872
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100873 intel_opregion_free(dev);
874
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000875 drm_free(dev->dev_private, sizeof(drm_i915_private_t),
876 DRM_MEM_DRIVER);
877
Dave Airlie22eae942005-11-10 22:16:34 +1100878 return 0;
879}
880
Eric Anholt673a3942008-07-30 12:06:12 -0700881int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
882{
883 struct drm_i915_file_private *i915_file_priv;
884
885 DRM_DEBUG("\n");
886 i915_file_priv = (struct drm_i915_file_private *)
887 drm_alloc(sizeof(*i915_file_priv), DRM_MEM_FILES);
888
889 if (!i915_file_priv)
890 return -ENOMEM;
891
892 file_priv->driver_priv = i915_file_priv;
893
894 i915_file_priv->mm.last_gem_seqno = 0;
895 i915_file_priv->mm.last_gem_throttle_seqno = 0;
896
897 return 0;
898}
899
Dave Airlie84b1fd12007-07-11 15:53:27 +1000900void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000902 drm_i915_private_t *dev_priv = dev->dev_private;
903
Dave Airlie144a75f2008-03-30 07:53:58 +1000904 if (!dev_priv)
905 return;
906
Eric Anholt673a3942008-07-30 12:06:12 -0700907 i915_gem_lastclose(dev);
908
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000909 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000910 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000911
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Eric Anholt6c340ea2007-08-25 20:23:09 +1000915void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000917 drm_i915_private_t *dev_priv = dev->dev_private;
918 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Eric Anholt673a3942008-07-30 12:06:12 -0700921void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
922{
923 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
924
925 drm_free(i915_file_priv, sizeof(*i915_file_priv), DRM_MEM_FILES);
926}
927
Eric Anholtc153f452007-09-03 12:06:45 +1000928struct drm_ioctl_desc i915_ioctls[] = {
929 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
930 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
931 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
932 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
933 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
934 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
935 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
936 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
937 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
938 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
939 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
940 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
941 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
942 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
943 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
944 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
945 DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH),
Eric Anholt673a3942008-07-30 12:06:12 -0700946 DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH),
947 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
948 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
949 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
950 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
951 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
952 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH),
953 DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH),
954 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
955 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
956 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
957 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
958 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
959 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
960 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
961 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
Dave Airliec94f7022005-07-07 21:03:38 +1000962};
963
964int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +1000965
966/**
967 * Determine if the device really is AGP or not.
968 *
969 * All Intel graphics chipsets are treated as AGP, even if they are really
970 * PCI-e.
971 *
972 * \param dev The device to be tested.
973 *
974 * \returns
975 * A value of 1 is always retured to indictate every i9x5 is AGP.
976 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000977int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +1000978{
979 return 1;
980}