blob: 1b81b6a6d81b56262fde04dcf07f390034df0c78 [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;
Dave Airlie7c1c2872008-11-28 14:22:24 +100042 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
Keith Packardd3a6d442008-07-30 12:21:20 -070044 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
45 u32 last_acthd = I915_READ(acthd_reg);
46 u32 acthd;
Jesse Barnes585fb112008-07-29 11:54:06 -070047 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int i;
49
Keith Packardd3a6d442008-07-30 12:21:20 -070050 for (i = 0; i < 100000; i++) {
Jesse Barnes585fb112008-07-29 11:54:06 -070051 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Keith Packardd3a6d442008-07-30 12:21:20 -070052 acthd = I915_READ(acthd_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 ring->space = ring->head - (ring->tail + 8);
54 if (ring->space < 0)
55 ring->space += ring->Size;
56 if (ring->space >= n)
57 return 0;
58
Dave Airlie7c1c2872008-11-28 14:22:24 +100059 if (master_priv->sarea_priv)
60 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (ring->head != last_head)
63 i = 0;
Keith Packardd3a6d442008-07-30 12:21:20 -070064 if (acthd != last_acthd)
65 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 last_head = ring->head;
Keith Packardd3a6d442008-07-30 12:21:20 -070068 last_acthd = acthd;
69 msleep_interruptible(10);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72
Eric Anholt20caafa2007-08-25 19:22:43 +100073 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Keith Packard398c9cb2008-07-30 13:03:43 -070076/**
77 * Sets up the hardware status page for devices that need a physical address
78 * in the register.
79 */
Eric Anholt3043c602008-10-02 12:24:47 -070080static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070081{
82 drm_i915_private_t *dev_priv = dev->dev_private;
83 /* Program Hardware Status Page */
84 dev_priv->status_page_dmah =
85 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
86
87 if (!dev_priv->status_page_dmah) {
88 DRM_ERROR("Can not allocate hardware status page\n");
89 return -ENOMEM;
90 }
91 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
92 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
93
94 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
95
96 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
97 DRM_DEBUG("Enabled hardware status page\n");
98 return 0;
99}
100
101/**
102 * Frees the hardware status page, whether it's a physical address or a virtual
103 * address set up by the X Server.
104 */
Eric Anholt3043c602008-10-02 12:24:47 -0700105static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700106{
107 drm_i915_private_t *dev_priv = dev->dev_private;
108 if (dev_priv->status_page_dmah) {
109 drm_pci_free(dev, dev_priv->status_page_dmah);
110 dev_priv->status_page_dmah = NULL;
111 }
112
113 if (dev_priv->status_gfx_addr) {
114 dev_priv->status_gfx_addr = 0;
115 drm_core_ioremapfree(&dev_priv->hws_map, dev);
116 }
117
118 /* Need to rewrite hardware status page */
119 I915_WRITE(HWS_PGA, 0x1ffff000);
120}
121
Dave Airlie84b1fd12007-07-11 15:53:27 +1000122void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000125 struct drm_i915_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
127
Jesse Barnes585fb112008-07-29 11:54:06 -0700128 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
129 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 ring->space = ring->head - (ring->tail + 8);
131 if (ring->space < 0)
132 ring->space += ring->Size;
133
Dave Airlie7c1c2872008-11-28 14:22:24 +1000134 if (!dev->primary->master)
135 return;
136
137 master_priv = dev->primary->master->driver_priv;
138 if (ring->head == ring->tail && master_priv->sarea_priv)
139 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Dave Airlie84b1fd12007-07-11 15:53:27 +1000142static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000144 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Make sure interrupts are disabled here because the uninstall ioctl
146 * may not have been called from userspace and after dev_private
147 * is freed, it's too late.
148 */
Eric Anholted4cb412008-07-29 12:10:39 -0700149 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000152 if (dev_priv->ring.virtual_start) {
153 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Eric Anholt3043c602008-10-02 12:24:47 -0700154 dev_priv->ring.virtual_start = NULL;
155 dev_priv->ring.map.handle = NULL;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000156 dev_priv->ring.map.size = 0;
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Keith Packard398c9cb2008-07-30 13:03:43 -0700159 /* Clear the HWS virtual address at teardown */
160 if (I915_NEED_GFX_HWS(dev))
161 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 return 0;
164}
165
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000166static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000168 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000169 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Eric Anholt673a3942008-07-30 12:06:12 -0700171 if (init->ring_size != 0) {
172 if (dev_priv->ring.ring_obj != NULL) {
173 i915_dma_cleanup(dev);
174 DRM_ERROR("Client tried to initialize ringbuffer in "
175 "GEM mode\n");
176 return -EINVAL;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Eric Anholt673a3942008-07-30 12:06:12 -0700179 dev_priv->ring.Size = init->ring_size;
180 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Eric Anholt673a3942008-07-30 12:06:12 -0700182 dev_priv->ring.map.offset = init->ring_start;
183 dev_priv->ring.map.size = init->ring_size;
184 dev_priv->ring.map.type = 0;
185 dev_priv->ring.map.flags = 0;
186 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Eric Anholt673a3942008-07-30 12:06:12 -0700188 drm_core_ioremap(&dev_priv->ring.map, dev);
189
190 if (dev_priv->ring.map.handle == NULL) {
191 i915_dma_cleanup(dev);
192 DRM_ERROR("can not ioremap virtual address for"
193 " ring buffer\n");
194 return -ENOMEM;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
198 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
199
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000200 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 dev_priv->back_offset = init->back_offset;
202 dev_priv->front_offset = init->front_offset;
203 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000204 if (master_priv->sarea_priv)
205 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Allow hardware batchbuffers unless told otherwise.
208 */
209 dev_priv->allow_batchbuffer = 1;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
212}
213
Dave Airlie84b1fd12007-07-11 15:53:27 +1000214static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
217
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700218 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (dev_priv->ring.map.handle == NULL) {
221 DRM_ERROR("can not ioremap virtual address for"
222 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000223 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
226 /* Program Hardware Status Page */
227 if (!dev_priv->hw_status_page) {
228 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000229 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 DRM_DEBUG("hw status page @ %p\n", dev_priv->hw_status_page);
232
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000233 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700234 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000235 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700236 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 DRM_DEBUG("Enabled hardware status page\n");
238
239 return 0;
240}
241
Eric Anholtc153f452007-09-03 12:06:45 +1000242static int i915_dma_init(struct drm_device *dev, void *data,
243 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Eric Anholtc153f452007-09-03 12:06:45 +1000245 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 int retcode = 0;
247
Eric Anholtc153f452007-09-03 12:06:45 +1000248 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000250 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252 case I915_CLEANUP_DMA:
253 retcode = i915_dma_cleanup(dev);
254 break;
255 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100256 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
258 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000259 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
261 }
262
263 return retcode;
264}
265
266/* Implement basically the same security restrictions as hardware does
267 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
268 *
269 * Most of the calculations below involve calculating the size of a
270 * particular instruction. It's important to get the size right as
271 * that tells us where the next instruction to check is. Any illegal
272 * instruction detected will be given a size of zero, which is a
273 * signal to abort the rest of the buffer.
274 */
275static int do_validate_cmd(int cmd)
276{
277 switch (((cmd >> 29) & 0x7)) {
278 case 0x0:
279 switch ((cmd >> 23) & 0x3f) {
280 case 0x0:
281 return 1; /* MI_NOOP */
282 case 0x4:
283 return 1; /* MI_FLUSH */
284 default:
285 return 0; /* disallow everything else */
286 }
287 break;
288 case 0x1:
289 return 0; /* reserved */
290 case 0x2:
291 return (cmd & 0xff) + 2; /* 2d commands */
292 case 0x3:
293 if (((cmd >> 24) & 0x1f) <= 0x18)
294 return 1;
295
296 switch ((cmd >> 24) & 0x1f) {
297 case 0x1c:
298 return 1;
299 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 case 0x3:
302 return (cmd & 0x1f) + 2;
303 case 0x4:
304 return (cmd & 0xf) + 2;
305 default:
306 return (cmd & 0xffff) + 2;
307 }
308 case 0x1e:
309 if (cmd & (1 << 23))
310 return (cmd & 0xffff) + 1;
311 else
312 return 1;
313 case 0x1f:
314 if ((cmd & (1 << 23)) == 0) /* inline vertices */
315 return (cmd & 0x1ffff) + 2;
316 else if (cmd & (1 << 17)) /* indirect random */
317 if ((cmd & 0xffff) == 0)
318 return 0; /* unknown length, too hard */
319 else
320 return (((cmd & 0xffff) + 1) / 2) + 1;
321 else
322 return 2; /* indirect sequential */
323 default:
324 return 0;
325 }
326 default:
327 return 0;
328 }
329
330 return 0;
331}
332
333static int validate_cmd(int cmd)
334{
335 int ret = do_validate_cmd(cmd);
336
Dave Airliebc5f4522007-11-05 12:50:58 +1000337/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 return ret;
340}
341
Dave Airlie84b1fd12007-07-11 15:53:27 +1000342static int i915_emit_cmds(struct drm_device * dev, int __user * buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 drm_i915_private_t *dev_priv = dev->dev_private;
345 int i;
346 RING_LOCALS;
347
Dave Airliede227f52006-01-25 15:31:43 +1100348 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000349 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100350
Alan Hourihanec29b6692006-08-12 16:29:24 +1000351 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 for (i = 0; i < dwords;) {
354 int cmd, sz;
355
356 if (DRM_COPY_FROM_USER_UNCHECKED(&cmd, &buffer[i], sizeof(cmd)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000360 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 OUT_RING(cmd);
363
364 while (++i, --sz) {
365 if (DRM_COPY_FROM_USER_UNCHECKED(&cmd, &buffer[i],
366 sizeof(cmd))) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000367 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 OUT_RING(cmd);
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Dave Airliede227f52006-01-25 15:31:43 +1100373 if (dwords & 1)
374 OUT_RING(0);
375
376 ADVANCE_LP_RING();
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379}
380
Eric Anholt673a3942008-07-30 12:06:12 -0700381int
382i915_emit_box(struct drm_device *dev,
383 struct drm_clip_rect __user *boxes,
384 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airliec60ce622007-07-11 15:27:12 +1000387 struct drm_clip_rect box;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 RING_LOCALS;
389
390 if (DRM_COPY_FROM_USER_UNCHECKED(&box, &boxes[i], sizeof(box))) {
Eric Anholt20caafa2007-08-25 19:22:43 +1000391 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
395 DRM_ERROR("Bad box %d,%d..%d,%d\n",
396 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000397 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Alan Hourihanec29b6692006-08-12 16:29:24 +1000400 if (IS_I965G(dev)) {
401 BEGIN_LP_RING(4);
402 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
403 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000404 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000405 OUT_RING(DR4);
406 ADVANCE_LP_RING();
407 } else {
408 BEGIN_LP_RING(6);
409 OUT_RING(GFX_OP_DRAWRECT_INFO);
410 OUT_RING(DR1);
411 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
412 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
413 OUT_RING(DR4);
414 OUT_RING(0);
415 ADVANCE_LP_RING();
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 return 0;
419}
420
Alan Hourihanec29b6692006-08-12 16:29:24 +1000421/* XXX: Emitting the counter should really be moved to part of the IRQ
422 * emit. For now, do it in both places:
423 */
424
Dave Airlie84b1fd12007-07-11 15:53:27 +1000425static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100426{
427 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000428 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100429 RING_LOCALS;
430
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400431 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000432 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400433 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000434 if (master_priv->sarea_priv)
435 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100436
437 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700438 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000439 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100440 OUT_RING(dev_priv->counter);
441 OUT_RING(0);
442 ADVANCE_LP_RING();
443}
444
Dave Airlie84b1fd12007-07-11 15:53:27 +1000445static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 drm_i915_cmdbuffer_t * cmd)
447{
448 int nbox = cmd->num_cliprects;
449 int i = 0, count, ret;
450
451 if (cmd->sz & 0x3) {
452 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 i915_kernel_lost_context(dev);
457
458 count = nbox ? nbox : 1;
459
460 for (i = 0; i < count; i++) {
461 if (i < nbox) {
462 ret = i915_emit_box(dev, cmd->cliprects, i,
463 cmd->DR1, cmd->DR4);
464 if (ret)
465 return ret;
466 }
467
468 ret = i915_emit_cmds(dev, (int __user *)cmd->buf, cmd->sz / 4);
469 if (ret)
470 return ret;
471 }
472
Dave Airliede227f52006-01-25 15:31:43 +1100473 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475}
476
Dave Airlie84b1fd12007-07-11 15:53:27 +1000477static int i915_dispatch_batchbuffer(struct drm_device * dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 drm_i915_batchbuffer_t * batch)
479{
480 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airliec60ce622007-07-11 15:27:12 +1000481 struct drm_clip_rect __user *boxes = batch->cliprects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int nbox = batch->num_cliprects;
483 int i = 0, count;
484 RING_LOCALS;
485
486 if ((batch->start | batch->used) & 0x7) {
487 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000488 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
491 i915_kernel_lost_context(dev);
492
493 count = nbox ? nbox : 1;
494
495 for (i = 0; i < count; i++) {
496 if (i < nbox) {
497 int ret = i915_emit_box(dev, boxes, i,
498 batch->DR1, batch->DR4);
499 if (ret)
500 return ret;
501 }
502
Keith Packard0790d5e2008-07-30 12:28:47 -0700503 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000505 if (IS_I965G(dev)) {
506 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
507 OUT_RING(batch->start);
508 } else {
509 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
510 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 ADVANCE_LP_RING();
513 } else {
514 BEGIN_LP_RING(4);
515 OUT_RING(MI_BATCH_BUFFER);
516 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
517 OUT_RING(batch->start + batch->used - 4);
518 OUT_RING(0);
519 ADVANCE_LP_RING();
520 }
521 }
522
Dave Airliede227f52006-01-25 15:31:43 +1100523 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 return 0;
526}
527
Dave Airlieaf6061a2008-05-07 12:15:39 +1000528static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000531 struct drm_i915_master_private *master_priv =
532 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 RING_LOCALS;
534
Dave Airlie7c1c2872008-11-28 14:22:24 +1000535 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400536 return -EINVAL;
537
Dave Airlieaf6061a2008-05-07 12:15:39 +1000538 DRM_DEBUG("%s: page=%d pfCurrentPage=%d\n",
Harvey Harrison80a914d2008-10-15 22:01:25 -0700539 __func__,
Dave Airlieaf6061a2008-05-07 12:15:39 +1000540 dev_priv->current_page,
Dave Airlie7c1c2872008-11-28 14:22:24 +1000541 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dave Airlieaf6061a2008-05-07 12:15:39 +1000543 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Airlieaf6061a2008-05-07 12:15:39 +1000545 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700546 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000547 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ADVANCE_LP_RING();
549
Dave Airlieaf6061a2008-05-07 12:15:39 +1000550 BEGIN_LP_RING(6);
551 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
552 OUT_RING(0);
553 if (dev_priv->current_page == 0) {
554 OUT_RING(dev_priv->back_offset);
555 dev_priv->current_page = 1;
556 } else {
557 OUT_RING(dev_priv->front_offset);
558 dev_priv->current_page = 0;
559 }
560 OUT_RING(0);
561 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000562
Dave Airlieaf6061a2008-05-07 12:15:39 +1000563 BEGIN_LP_RING(2);
564 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
565 OUT_RING(0);
566 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000567
Dave Airlie7c1c2872008-11-28 14:22:24 +1000568 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000569
Dave Airlieaf6061a2008-05-07 12:15:39 +1000570 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700571 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000572 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000573 OUT_RING(dev_priv->counter);
574 OUT_RING(0);
575 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000576
Dave Airlie7c1c2872008-11-28 14:22:24 +1000577 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Dave Airlie84b1fd12007-07-11 15:53:27 +1000581static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 drm_i915_private_t *dev_priv = dev->dev_private;
584
585 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700586 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Eric Anholtc153f452007-09-03 12:06:45 +1000589static int i915_flush_ioctl(struct drm_device *dev, void *data,
590 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Eric Anholt546b0972008-09-01 16:45:29 -0700592 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Eric Anholt546b0972008-09-01 16:45:29 -0700594 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
595
596 mutex_lock(&dev->struct_mutex);
597 ret = i915_quiescent(dev);
598 mutex_unlock(&dev->struct_mutex);
599
600 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Eric Anholtc153f452007-09-03 12:06:45 +1000603static int i915_batchbuffer(struct drm_device *dev, void *data,
604 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000607 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000609 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000610 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int ret;
612
613 if (!dev_priv->allow_batchbuffer) {
614 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000615 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 DRM_DEBUG("i915 batchbuffer, start %x used %d cliprects %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000619 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Eric Anholt546b0972008-09-01 16:45:29 -0700621 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Eric Anholtc153f452007-09-03 12:06:45 +1000623 if (batch->num_cliprects && DRM_VERIFYAREA_READ(batch->cliprects,
624 batch->num_cliprects *
Dave Airliec60ce622007-07-11 15:27:12 +1000625 sizeof(struct drm_clip_rect)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000626 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Eric Anholt546b0972008-09-01 16:45:29 -0700628 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000629 ret = i915_dispatch_batchbuffer(dev, batch);
Eric Anholt546b0972008-09-01 16:45:29 -0700630 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400632 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000633 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return ret;
635}
636
Eric Anholtc153f452007-09-03 12:06:45 +1000637static int i915_cmdbuffer(struct drm_device *dev, void *data,
638 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000641 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000643 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000644 drm_i915_cmdbuffer_t *cmdbuf = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int ret;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 DRM_DEBUG("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000648 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric Anholt546b0972008-09-01 16:45:29 -0700650 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric Anholtc153f452007-09-03 12:06:45 +1000652 if (cmdbuf->num_cliprects &&
653 DRM_VERIFYAREA_READ(cmdbuf->cliprects,
654 cmdbuf->num_cliprects *
Dave Airliec60ce622007-07-11 15:27:12 +1000655 sizeof(struct drm_clip_rect))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 DRM_ERROR("Fault accessing cliprects\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000657 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659
Eric Anholt546b0972008-09-01 16:45:29 -0700660 mutex_lock(&dev->struct_mutex);
Eric Anholtc153f452007-09-03 12:06:45 +1000661 ret = i915_dispatch_cmdbuffer(dev, cmdbuf);
Eric Anholt546b0972008-09-01 16:45:29 -0700662 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (ret) {
664 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
665 return ret;
666 }
667
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400668 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000669 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return 0;
671}
672
Eric Anholtc153f452007-09-03 12:06:45 +1000673static int i915_flip_bufs(struct drm_device *dev, void *data,
674 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Eric Anholt546b0972008-09-01 16:45:29 -0700676 int ret;
677
Harvey Harrison80a914d2008-10-15 22:01:25 -0700678 DRM_DEBUG("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Eric Anholt546b0972008-09-01 16:45:29 -0700680 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Eric Anholt546b0972008-09-01 16:45:29 -0700682 mutex_lock(&dev->struct_mutex);
683 ret = i915_dispatch_flip(dev);
684 mutex_unlock(&dev->struct_mutex);
685
686 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Eric Anholtc153f452007-09-03 12:06:45 +1000689static int i915_getparam(struct drm_device *dev, void *data,
690 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000693 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int value;
695
696 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000697 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000698 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
Eric Anholtc153f452007-09-03 12:06:45 +1000701 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700703 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 case I915_PARAM_ALLOW_BATCHBUFFER:
706 value = dev_priv->allow_batchbuffer ? 1 : 0;
707 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100708 case I915_PARAM_LAST_DISPATCH:
709 value = READ_BREADCRUMB(dev_priv);
710 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400711 case I915_PARAM_CHIPSET_ID:
712 value = dev->pci_device;
713 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700714 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000715 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 default:
Eric Anholtc153f452007-09-03 12:06:45 +1000718 DRM_ERROR("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000719 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
Eric Anholtc153f452007-09-03 12:06:45 +1000722 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000724 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
727 return 0;
728}
729
Eric Anholtc153f452007-09-03 12:06:45 +1000730static int i915_setparam(struct drm_device *dev, void *data,
731 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000734 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000737 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000738 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Eric Anholtc153f452007-09-03 12:06:45 +1000741 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
744 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000745 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 break;
747 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000748 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 break;
750 default:
Eric Anholtc153f452007-09-03 12:06:45 +1000751 DRM_ERROR("unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 return 0;
756}
757
Eric Anholtc153f452007-09-03 12:06:45 +1000758static int i915_set_status_page(struct drm_device *dev, void *data,
759 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000760{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000761 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000762 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000763
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000764 if (!I915_NEED_GFX_HWS(dev))
765 return -EINVAL;
766
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000767 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000768 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000769 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000770 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000771
Eric Anholtc153f452007-09-03 12:06:45 +1000772 printk(KERN_DEBUG "set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000773
Eric Anholtc153f452007-09-03 12:06:45 +1000774 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
775
Eric Anholt8b409582007-11-22 16:40:37 +1000776 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000777 dev_priv->hws_map.size = 4*1024;
778 dev_priv->hws_map.type = 0;
779 dev_priv->hws_map.flags = 0;
780 dev_priv->hws_map.mtrr = 0;
781
782 drm_core_ioremap(&dev_priv->hws_map, dev);
783 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000784 i915_dma_cleanup(dev);
785 dev_priv->status_gfx_addr = 0;
786 DRM_ERROR("can not ioremap virtual address for"
787 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000788 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000789 }
790 dev_priv->hw_status_page = dev_priv->hws_map.handle;
791
792 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700793 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
794 DRM_DEBUG("load hws HWS_PGA with gfx mem 0x%x\n",
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000795 dev_priv->status_gfx_addr);
796 DRM_DEBUG("load hws at %p\n", dev_priv->hw_status_page);
797 return 0;
798}
799
Dave Airlie7c1c2872008-11-28 14:22:24 +1000800int i915_master_create(struct drm_device *dev, struct drm_master *master)
801{
802 struct drm_i915_master_private *master_priv;
803
804 master_priv = drm_calloc(1, sizeof(*master_priv), DRM_MEM_DRIVER);
805 if (!master_priv)
806 return -ENOMEM;
807
808 master->driver_priv = master_priv;
809 return 0;
810}
811
812void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
813{
814 struct drm_i915_master_private *master_priv = master->driver_priv;
815
816 if (!master_priv)
817 return;
818
819 drm_free(master_priv, sizeof(*master_priv), DRM_MEM_DRIVER);
820
821 master->driver_priv = NULL;
822}
823
Dave Airlie84b1fd12007-07-11 15:53:27 +1000824int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +1100825{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000826 struct drm_i915_private *dev_priv = dev->dev_private;
827 unsigned long base, size;
828 int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1;
829
Dave Airlie22eae942005-11-10 22:16:34 +1100830 /* i915 has 4 more counters */
831 dev->counters += 4;
832 dev->types[6] = _DRM_STAT_IRQ;
833 dev->types[7] = _DRM_STAT_PRIMARY;
834 dev->types[8] = _DRM_STAT_SECONDARY;
835 dev->types[9] = _DRM_STAT_DMA;
836
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000837 dev_priv = drm_alloc(sizeof(drm_i915_private_t), DRM_MEM_DRIVER);
838 if (dev_priv == NULL)
839 return -ENOMEM;
840
841 memset(dev_priv, 0, sizeof(drm_i915_private_t));
842
843 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -0700844 dev_priv->dev = dev;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000845
846 /* Add register map (needed for suspend/resume) */
847 base = drm_get_resource_start(dev, mmio_bar);
848 size = drm_get_resource_len(dev, mmio_bar);
849
Eric Anholt3043c602008-10-02 12:24:47 -0700850 dev_priv->regs = ioremap(base, size);
Eric Anholted4cb412008-07-29 12:10:39 -0700851
Dave Airlieac5c4e72008-12-19 15:38:34 +1000852#ifdef CONFIG_HIGHMEM64G
853 /* don't enable GEM on PAE - needs agp + set_memory_* interface fixes */
854 dev_priv->has_gem = 0;
855#else
856 /* enable GEM by default */
857 dev_priv->has_gem = 1;
858#endif
859
Eric Anholt673a3942008-07-30 12:06:12 -0700860 i915_gem_load(dev);
861
Keith Packard398c9cb2008-07-30 13:03:43 -0700862 /* Init HWS */
863 if (!I915_NEED_GFX_HWS(dev)) {
864 ret = i915_init_phys_hws(dev);
865 if (ret != 0)
866 return ret;
867 }
Eric Anholted4cb412008-07-29 12:10:39 -0700868
869 /* On the 945G/GM, the chipset reports the MSI capability on the
870 * integrated graphics even though the support isn't actually there
871 * according to the published specs. It doesn't appear to function
872 * correctly in testing on 945G.
873 * This may be a side effect of MSI having been made available for PEG
874 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -0700875 *
876 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -0800877 * be lost or delayed, but we use them anyways to avoid
878 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -0700879 */
Keith Packardb60678a2008-12-08 11:12:28 -0800880 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -0800881 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -0700882
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100883 intel_opregion_init(dev);
884
Eric Anholted4cb412008-07-29 12:10:39 -0700885 spin_lock_init(&dev_priv->user_irq_lock);
886
Keith Packard52440212008-11-18 09:30:25 -0800887 ret = drm_vblank_init(dev, I915_NUM_PIPE);
888
889 if (ret) {
890 (void) i915_driver_unload(dev);
891 return ret;
892 }
893
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000894 return ret;
895}
896
897int i915_driver_unload(struct drm_device *dev)
898{
899 struct drm_i915_private *dev_priv = dev->dev_private;
900
Eric Anholted4cb412008-07-29 12:10:39 -0700901 if (dev->pdev->msi_enabled)
902 pci_disable_msi(dev->pdev);
903
Keith Packard398c9cb2008-07-30 13:03:43 -0700904 i915_free_hws(dev);
905
Eric Anholt3043c602008-10-02 12:24:47 -0700906 if (dev_priv->regs != NULL)
907 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000908
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +0100909 intel_opregion_free(dev);
910
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000911 drm_free(dev->dev_private, sizeof(drm_i915_private_t),
912 DRM_MEM_DRIVER);
913
Dave Airlie22eae942005-11-10 22:16:34 +1100914 return 0;
915}
916
Eric Anholt673a3942008-07-30 12:06:12 -0700917int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
918{
919 struct drm_i915_file_private *i915_file_priv;
920
921 DRM_DEBUG("\n");
922 i915_file_priv = (struct drm_i915_file_private *)
923 drm_alloc(sizeof(*i915_file_priv), DRM_MEM_FILES);
924
925 if (!i915_file_priv)
926 return -ENOMEM;
927
928 file_priv->driver_priv = i915_file_priv;
929
930 i915_file_priv->mm.last_gem_seqno = 0;
931 i915_file_priv->mm.last_gem_throttle_seqno = 0;
932
933 return 0;
934}
935
Dave Airlie84b1fd12007-07-11 15:53:27 +1000936void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000938 drm_i915_private_t *dev_priv = dev->dev_private;
939
Dave Airlie144a75f2008-03-30 07:53:58 +1000940 if (!dev_priv)
941 return;
942
Eric Anholt673a3942008-07-30 12:06:12 -0700943 i915_gem_lastclose(dev);
944
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000945 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000946 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000947
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000948 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Eric Anholt6c340ea2007-08-25 20:23:09 +1000951void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000953 drm_i915_private_t *dev_priv = dev->dev_private;
954 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Eric Anholt673a3942008-07-30 12:06:12 -0700957void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
958{
959 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
960
961 drm_free(i915_file_priv, sizeof(*i915_file_priv), DRM_MEM_FILES);
962}
963
Eric Anholtc153f452007-09-03 12:06:45 +1000964struct drm_ioctl_desc i915_ioctls[] = {
965 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
966 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
967 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
968 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
969 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
970 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
971 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
972 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
973 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
974 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
975 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
976 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
977 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
978 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
979 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
980 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +1000981 DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie2bdf00b22008-10-07 13:40:10 +1000982 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 -0700983 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
984 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
985 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
986 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
987 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
Dave Airlie2bdf00b22008-10-07 13:40:10 +1000988 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
989 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 -0700990 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
991 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
992 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
993 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
Jesse Barnesde151cf2008-11-12 10:03:55 -0800994 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0),
Eric Anholt673a3942008-07-30 12:06:12 -0700995 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
996 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
997 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
998 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
Eric Anholt5a125c32008-10-22 21:40:13 -0700999 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0),
Dave Airliec94f7022005-07-07 21:03:38 +10001000};
1001
1002int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001003
1004/**
1005 * Determine if the device really is AGP or not.
1006 *
1007 * All Intel graphics chipsets are treated as AGP, even if they are really
1008 * PCI-e.
1009 *
1010 * \param dev The device to be tested.
1011 *
1012 * \returns
1013 * A value of 1 is always retured to indictate every i9x5 is AGP.
1014 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001015int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001016{
1017 return 1;
1018}