blob: 8c4783180bf653e9a1885b40896c711c074f78d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
2 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "drmP.h"
30#include "drm.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080031#include "drm_crtc_helper.h"
32#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "i915_drm.h"
34#include "i915_drv.h"
35
yakui_zhaobe25ed92009-06-02 14:13:55 +080036#define I915_DRV "i915_drv"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* Really want an OS-independent resettable timer. Would like to have
39 * this loop run for (eg) 3 sec, but have the timer reset every time
40 * the head pointer changes, so that EBUSY only happens if the ring
41 * actually stalls for (eg) 3 seconds.
42 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100043int i915_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 drm_i915_private_t *dev_priv = dev->dev_private;
46 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
Keith Packardd3a6d442008-07-30 12:21:20 -070047 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
48 u32 last_acthd = I915_READ(acthd_reg);
49 u32 acthd;
Jesse Barnes585fb112008-07-29 11:54:06 -070050 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int i;
52
Keith Packardd3a6d442008-07-30 12:21:20 -070053 for (i = 0; i < 100000; i++) {
Jesse Barnes585fb112008-07-29 11:54:06 -070054 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Keith Packardd3a6d442008-07-30 12:21:20 -070055 acthd = I915_READ(acthd_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 ring->space = ring->head - (ring->tail + 8);
57 if (ring->space < 0)
58 ring->space += ring->Size;
59 if (ring->space >= n)
60 return 0;
61
Chris Wilson98787c02009-03-06 23:27:52 +000062 if (dev->primary->master) {
63 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
64 if (master_priv->sarea_priv)
65 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
66 }
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if (ring->head != last_head)
70 i = 0;
Keith Packardd3a6d442008-07-30 12:21:20 -070071 if (acthd != last_acthd)
72 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 last_head = ring->head;
Keith Packardd3a6d442008-07-30 12:21:20 -070075 last_acthd = acthd;
76 msleep_interruptible(10);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
Eric Anholt20caafa2007-08-25 19:22:43 +100080 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Keith Packard398c9cb2008-07-30 13:03:43 -070083/**
84 * Sets up the hardware status page for devices that need a physical address
85 * in the register.
86 */
Eric Anholt3043c602008-10-02 12:24:47 -070087static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070088{
89 drm_i915_private_t *dev_priv = dev->dev_private;
90 /* Program Hardware Status Page */
91 dev_priv->status_page_dmah =
92 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
93
94 if (!dev_priv->status_page_dmah) {
95 DRM_ERROR("Can not allocate hardware status page\n");
96 return -ENOMEM;
97 }
98 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
99 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
100
101 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
102
103 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
yakui_zhaobe25ed92009-06-02 14:13:55 +0800104 DRM_DEBUG_DRIVER(I915_DRV, "Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700105 return 0;
106}
107
108/**
109 * Frees the hardware status page, whether it's a physical address or a virtual
110 * address set up by the X Server.
111 */
Eric Anholt3043c602008-10-02 12:24:47 -0700112static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700113{
114 drm_i915_private_t *dev_priv = dev->dev_private;
115 if (dev_priv->status_page_dmah) {
116 drm_pci_free(dev, dev_priv->status_page_dmah);
117 dev_priv->status_page_dmah = NULL;
118 }
119
120 if (dev_priv->status_gfx_addr) {
121 dev_priv->status_gfx_addr = 0;
122 drm_core_ioremapfree(&dev_priv->hws_map, dev);
123 }
124
125 /* Need to rewrite hardware status page */
126 I915_WRITE(HWS_PGA, 0x1ffff000);
127}
128
Dave Airlie84b1fd12007-07-11 15:53:27 +1000129void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000132 struct drm_i915_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
134
Jesse Barnes79e53942008-11-07 14:24:08 -0800135 /*
136 * We should never lose context on the ring with modesetting
137 * as we don't expose it to userspace
138 */
139 if (drm_core_check_feature(dev, DRIVER_MODESET))
140 return;
141
Jesse Barnes585fb112008-07-29 11:54:06 -0700142 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
143 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ring->space = ring->head - (ring->tail + 8);
145 if (ring->space < 0)
146 ring->space += ring->Size;
147
Dave Airlie7c1c2872008-11-28 14:22:24 +1000148 if (!dev->primary->master)
149 return;
150
151 master_priv = dev->primary->master->driver_priv;
152 if (ring->head == ring->tail && master_priv->sarea_priv)
153 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Dave Airlie84b1fd12007-07-11 15:53:27 +1000156static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000158 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* Make sure interrupts are disabled here because the uninstall ioctl
160 * may not have been called from userspace and after dev_private
161 * is freed, it's too late.
162 */
Eric Anholted4cb412008-07-29 12:10:39 -0700163 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000166 if (dev_priv->ring.virtual_start) {
167 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Eric Anholt3043c602008-10-02 12:24:47 -0700168 dev_priv->ring.virtual_start = NULL;
169 dev_priv->ring.map.handle = NULL;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000170 dev_priv->ring.map.size = 0;
171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Keith Packard398c9cb2008-07-30 13:03:43 -0700173 /* Clear the HWS virtual address at teardown */
174 if (I915_NEED_GFX_HWS(dev))
175 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 return 0;
178}
179
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000180static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000182 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000183 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Dave Airlie3a03ac12009-01-11 09:03:49 +1000185 master_priv->sarea = drm_getsarea(dev);
186 if (master_priv->sarea) {
187 master_priv->sarea_priv = (drm_i915_sarea_t *)
188 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
189 } else {
yakui_zhaobe25ed92009-06-02 14:13:55 +0800190 DRM_DEBUG_DRIVER(I915_DRV,
191 "sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000192 }
193
Eric Anholt673a3942008-07-30 12:06:12 -0700194 if (init->ring_size != 0) {
195 if (dev_priv->ring.ring_obj != NULL) {
196 i915_dma_cleanup(dev);
197 DRM_ERROR("Client tried to initialize ringbuffer in "
198 "GEM mode\n");
199 return -EINVAL;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Eric Anholt673a3942008-07-30 12:06:12 -0700202 dev_priv->ring.Size = init->ring_size;
203 dev_priv->ring.tail_mask = dev_priv->ring.Size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Eric Anholt673a3942008-07-30 12:06:12 -0700205 dev_priv->ring.map.offset = init->ring_start;
206 dev_priv->ring.map.size = init->ring_size;
207 dev_priv->ring.map.type = 0;
208 dev_priv->ring.map.flags = 0;
209 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Jesse Barnes6fb88582009-02-23 10:08:21 +1000211 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700212
213 if (dev_priv->ring.map.handle == NULL) {
214 i915_dma_cleanup(dev);
215 DRM_ERROR("can not ioremap virtual address for"
216 " ring buffer\n");
217 return -ENOMEM;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
221 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
222
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000223 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 dev_priv->back_offset = init->back_offset;
225 dev_priv->front_offset = init->front_offset;
226 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000227 if (master_priv->sarea_priv)
228 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Allow hardware batchbuffers unless told otherwise.
231 */
232 dev_priv->allow_batchbuffer = 1;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
235}
236
Dave Airlie84b1fd12007-07-11 15:53:27 +1000237static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
240
yakui_zhaobe25ed92009-06-02 14:13:55 +0800241 DRM_DEBUG_DRIVER(I915_DRV, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (dev_priv->ring.map.handle == NULL) {
244 DRM_ERROR("can not ioremap virtual address for"
245 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000246 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 /* Program Hardware Status Page */
250 if (!dev_priv->hw_status_page) {
251 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000252 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
yakui_zhaobe25ed92009-06-02 14:13:55 +0800254 DRM_DEBUG_DRIVER(I915_DRV, "hw status page @ %p\n",
255 dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000257 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700258 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000259 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700260 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
yakui_zhaobe25ed92009-06-02 14:13:55 +0800261 DRM_DEBUG_DRIVER(I915_DRV, "Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return 0;
264}
265
Eric Anholtc153f452007-09-03 12:06:45 +1000266static int i915_dma_init(struct drm_device *dev, void *data,
267 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Eric Anholtc153f452007-09-03 12:06:45 +1000269 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int retcode = 0;
271
Eric Anholtc153f452007-09-03 12:06:45 +1000272 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000274 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 case I915_CLEANUP_DMA:
277 retcode = i915_dma_cleanup(dev);
278 break;
279 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100280 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000283 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 }
286
287 return retcode;
288}
289
290/* Implement basically the same security restrictions as hardware does
291 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
292 *
293 * Most of the calculations below involve calculating the size of a
294 * particular instruction. It's important to get the size right as
295 * that tells us where the next instruction to check is. Any illegal
296 * instruction detected will be given a size of zero, which is a
297 * signal to abort the rest of the buffer.
298 */
299static int do_validate_cmd(int cmd)
300{
301 switch (((cmd >> 29) & 0x7)) {
302 case 0x0:
303 switch ((cmd >> 23) & 0x3f) {
304 case 0x0:
305 return 1; /* MI_NOOP */
306 case 0x4:
307 return 1; /* MI_FLUSH */
308 default:
309 return 0; /* disallow everything else */
310 }
311 break;
312 case 0x1:
313 return 0; /* reserved */
314 case 0x2:
315 return (cmd & 0xff) + 2; /* 2d commands */
316 case 0x3:
317 if (((cmd >> 24) & 0x1f) <= 0x18)
318 return 1;
319
320 switch ((cmd >> 24) & 0x1f) {
321 case 0x1c:
322 return 1;
323 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 case 0x3:
326 return (cmd & 0x1f) + 2;
327 case 0x4:
328 return (cmd & 0xf) + 2;
329 default:
330 return (cmd & 0xffff) + 2;
331 }
332 case 0x1e:
333 if (cmd & (1 << 23))
334 return (cmd & 0xffff) + 1;
335 else
336 return 1;
337 case 0x1f:
338 if ((cmd & (1 << 23)) == 0) /* inline vertices */
339 return (cmd & 0x1ffff) + 2;
340 else if (cmd & (1 << 17)) /* indirect random */
341 if ((cmd & 0xffff) == 0)
342 return 0; /* unknown length, too hard */
343 else
344 return (((cmd & 0xffff) + 1) / 2) + 1;
345 else
346 return 2; /* indirect sequential */
347 default:
348 return 0;
349 }
350 default:
351 return 0;
352 }
353
354 return 0;
355}
356
357static int validate_cmd(int cmd)
358{
359 int ret = do_validate_cmd(cmd);
360
Dave Airliebc5f4522007-11-05 12:50:58 +1000361/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 return ret;
364}
365
Eric Anholt201361a2009-03-11 12:30:04 -0700366static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 drm_i915_private_t *dev_priv = dev->dev_private;
369 int i;
370 RING_LOCALS;
371
Dave Airliede227f52006-01-25 15:31:43 +1100372 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000373 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100374
Alan Hourihanec29b6692006-08-12 16:29:24 +1000375 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 for (i = 0; i < dwords;) {
378 int cmd, sz;
379
Eric Anholt201361a2009-03-11 12:30:04 -0700380 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000383 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 OUT_RING(cmd);
386
387 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700388 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
Dave Airliede227f52006-01-25 15:31:43 +1100392 if (dwords & 1)
393 OUT_RING(0);
394
395 ADVANCE_LP_RING();
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 0;
398}
399
Eric Anholt673a3942008-07-30 12:06:12 -0700400int
401i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700402 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700403 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt201361a2009-03-11 12:30:04 -0700406 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 RING_LOCALS;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
410 DRM_ERROR("Bad box %d,%d..%d,%d\n",
411 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
Alan Hourihanec29b6692006-08-12 16:29:24 +1000415 if (IS_I965G(dev)) {
416 BEGIN_LP_RING(4);
417 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
418 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000419 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000420 OUT_RING(DR4);
421 ADVANCE_LP_RING();
422 } else {
423 BEGIN_LP_RING(6);
424 OUT_RING(GFX_OP_DRAWRECT_INFO);
425 OUT_RING(DR1);
426 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
427 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
428 OUT_RING(DR4);
429 OUT_RING(0);
430 ADVANCE_LP_RING();
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 return 0;
434}
435
Alan Hourihanec29b6692006-08-12 16:29:24 +1000436/* XXX: Emitting the counter should really be moved to part of the IRQ
437 * emit. For now, do it in both places:
438 */
439
Dave Airlie84b1fd12007-07-11 15:53:27 +1000440static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100441{
442 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000443 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100444 RING_LOCALS;
445
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400446 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000447 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400448 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000449 if (master_priv->sarea_priv)
450 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100451
452 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700453 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000454 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100455 OUT_RING(dev_priv->counter);
456 OUT_RING(0);
457 ADVANCE_LP_RING();
458}
459
Dave Airlie84b1fd12007-07-11 15:53:27 +1000460static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700461 drm_i915_cmdbuffer_t *cmd,
462 struct drm_clip_rect *cliprects,
463 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 int nbox = cmd->num_cliprects;
466 int i = 0, count, ret;
467
468 if (cmd->sz & 0x3) {
469 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000470 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 i915_kernel_lost_context(dev);
474
475 count = nbox ? nbox : 1;
476
477 for (i = 0; i < count; i++) {
478 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700479 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 cmd->DR1, cmd->DR4);
481 if (ret)
482 return ret;
483 }
484
Eric Anholt201361a2009-03-11 12:30:04 -0700485 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (ret)
487 return ret;
488 }
489
Dave Airliede227f52006-01-25 15:31:43 +1100490 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 0;
492}
493
Dave Airlie84b1fd12007-07-11 15:53:27 +1000494static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700495 drm_i915_batchbuffer_t * batch,
496 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int nbox = batch->num_cliprects;
500 int i = 0, count;
501 RING_LOCALS;
502
503 if ((batch->start | batch->used) & 0x7) {
504 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000505 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
508 i915_kernel_lost_context(dev);
509
510 count = nbox ? nbox : 1;
511
512 for (i = 0; i < count; i++) {
513 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700514 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 batch->DR1, batch->DR4);
516 if (ret)
517 return ret;
518 }
519
Keith Packard0790d5e2008-07-30 12:28:47 -0700520 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000522 if (IS_I965G(dev)) {
523 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
524 OUT_RING(batch->start);
525 } else {
526 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
527 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ADVANCE_LP_RING();
530 } else {
531 BEGIN_LP_RING(4);
532 OUT_RING(MI_BATCH_BUFFER);
533 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
534 OUT_RING(batch->start + batch->used - 4);
535 OUT_RING(0);
536 ADVANCE_LP_RING();
537 }
538 }
539
Dave Airliede227f52006-01-25 15:31:43 +1100540 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 return 0;
543}
544
Dave Airlieaf6061a2008-05-07 12:15:39 +1000545static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000548 struct drm_i915_master_private *master_priv =
549 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 RING_LOCALS;
551
Dave Airlie7c1c2872008-11-28 14:22:24 +1000552 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400553 return -EINVAL;
554
yakui_zhaobe25ed92009-06-02 14:13:55 +0800555 DRM_DEBUG_DRIVER(I915_DRV, "%s: page=%d pfCurrentPage=%d\n",
556 __func__,
557 dev_priv->current_page,
558 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Dave Airlieaf6061a2008-05-07 12:15:39 +1000560 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dave Airlieaf6061a2008-05-07 12:15:39 +1000562 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700563 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000564 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ADVANCE_LP_RING();
566
Dave Airlieaf6061a2008-05-07 12:15:39 +1000567 BEGIN_LP_RING(6);
568 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
569 OUT_RING(0);
570 if (dev_priv->current_page == 0) {
571 OUT_RING(dev_priv->back_offset);
572 dev_priv->current_page = 1;
573 } else {
574 OUT_RING(dev_priv->front_offset);
575 dev_priv->current_page = 0;
576 }
577 OUT_RING(0);
578 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000579
Dave Airlieaf6061a2008-05-07 12:15:39 +1000580 BEGIN_LP_RING(2);
581 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
582 OUT_RING(0);
583 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000584
Dave Airlie7c1c2872008-11-28 14:22:24 +1000585 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000586
Dave Airlieaf6061a2008-05-07 12:15:39 +1000587 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700588 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000589 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000590 OUT_RING(dev_priv->counter);
591 OUT_RING(0);
592 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000593
Dave Airlie7c1c2872008-11-28 14:22:24 +1000594 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Dave Airlie84b1fd12007-07-11 15:53:27 +1000598static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 drm_i915_private_t *dev_priv = dev->dev_private;
601
602 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700603 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Eric Anholtc153f452007-09-03 12:06:45 +1000606static int i915_flush_ioctl(struct drm_device *dev, void *data,
607 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Eric Anholt546b0972008-09-01 16:45:29 -0700609 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Eric Anholt546b0972008-09-01 16:45:29 -0700611 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
612
613 mutex_lock(&dev->struct_mutex);
614 ret = i915_quiescent(dev);
615 mutex_unlock(&dev->struct_mutex);
616
617 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Eric Anholtc153f452007-09-03 12:06:45 +1000620static int i915_batchbuffer(struct drm_device *dev, void *data,
621 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000624 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000626 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000627 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700629 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (!dev_priv->allow_batchbuffer) {
632 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000633 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
yakui_zhaobe25ed92009-06-02 14:13:55 +0800636 DRM_DEBUG_DRIVER(I915_DRV,
637 "i915 batchbuffer, start %x used %d cliprects %d\n",
638 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Eric Anholt546b0972008-09-01 16:45:29 -0700640 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric Anholt201361a2009-03-11 12:30:04 -0700642 if (batch->num_cliprects < 0)
643 return -EINVAL;
644
645 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700646 cliprects = kcalloc(batch->num_cliprects,
647 sizeof(struct drm_clip_rect),
648 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700649 if (cliprects == NULL)
650 return -ENOMEM;
651
652 ret = copy_from_user(cliprects, batch->cliprects,
653 batch->num_cliprects *
654 sizeof(struct drm_clip_rect));
655 if (ret != 0)
656 goto fail_free;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Eric Anholt546b0972008-09-01 16:45:29 -0700659 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700660 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700661 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400663 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000664 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700665
666fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700667 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return ret;
670}
671
Eric Anholtc153f452007-09-03 12:06:45 +1000672static int i915_cmdbuffer(struct drm_device *dev, void *data,
673 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000676 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000678 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000679 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700680 struct drm_clip_rect *cliprects = NULL;
681 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int ret;
683
yakui_zhaobe25ed92009-06-02 14:13:55 +0800684 DRM_DEBUG_DRIVER(I915_DRV,
685 "i915 cmdbuffer, buf %p sz %d cliprects %d\n",
686 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Eric Anholt546b0972008-09-01 16:45:29 -0700688 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Eric Anholt201361a2009-03-11 12:30:04 -0700690 if (cmdbuf->num_cliprects < 0)
691 return -EINVAL;
692
Eric Anholt9a298b22009-03-24 12:23:04 -0700693 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700694 if (batch_data == NULL)
695 return -ENOMEM;
696
697 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
698 if (ret != 0)
699 goto fail_batch_free;
700
701 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700702 cliprects = kcalloc(cmdbuf->num_cliprects,
703 sizeof(struct drm_clip_rect), GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700704 if (cliprects == NULL)
705 goto fail_batch_free;
706
707 ret = copy_from_user(cliprects, cmdbuf->cliprects,
708 cmdbuf->num_cliprects *
709 sizeof(struct drm_clip_rect));
710 if (ret != 0)
711 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
Eric Anholt546b0972008-09-01 16:45:29 -0700714 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700715 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700716 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (ret) {
718 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000719 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400722 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000723 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700724
Eric Anholt201361a2009-03-11 12:30:04 -0700725fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700726 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000727fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700728 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700729
730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Eric Anholtc153f452007-09-03 12:06:45 +1000733static int i915_flip_bufs(struct drm_device *dev, void *data,
734 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Eric Anholt546b0972008-09-01 16:45:29 -0700736 int ret;
737
yakui_zhaobe25ed92009-06-02 14:13:55 +0800738 DRM_DEBUG_DRIVER(I915_DRV, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Eric Anholt546b0972008-09-01 16:45:29 -0700740 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Eric Anholt546b0972008-09-01 16:45:29 -0700742 mutex_lock(&dev->struct_mutex);
743 ret = i915_dispatch_flip(dev);
744 mutex_unlock(&dev->struct_mutex);
745
746 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Eric Anholtc153f452007-09-03 12:06:45 +1000749static int i915_getparam(struct drm_device *dev, void *data,
750 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000753 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int value;
755
756 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000757 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000758 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Eric Anholtc153f452007-09-03 12:06:45 +1000761 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700763 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
765 case I915_PARAM_ALLOW_BATCHBUFFER:
766 value = dev_priv->allow_batchbuffer ? 1 : 0;
767 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100768 case I915_PARAM_LAST_DISPATCH:
769 value = READ_BREADCRUMB(dev_priv);
770 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400771 case I915_PARAM_CHIPSET_ID:
772 value = dev->pci_device;
773 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700774 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000775 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700776 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800777 case I915_PARAM_NUM_FENCES_AVAIL:
778 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
779 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 default:
yakui_zhaobe25ed92009-06-02 14:13:55 +0800781 DRM_DEBUG_DRIVER(I915_DRV, "Unknown parameter %d\n",
782 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000783 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
Eric Anholtc153f452007-09-03 12:06:45 +1000786 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000788 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
791 return 0;
792}
793
Eric Anholtc153f452007-09-03 12:06:45 +1000794static int i915_setparam(struct drm_device *dev, void *data,
795 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000798 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000801 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000802 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
Eric Anholtc153f452007-09-03 12:06:45 +1000805 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
808 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000809 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
811 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000812 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800814 case I915_SETPARAM_NUM_USED_FENCES:
815 if (param->value > dev_priv->num_fence_regs ||
816 param->value < 0)
817 return -EINVAL;
818 /* Userspace can use first N regs */
819 dev_priv->fence_reg_start = param->value;
820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 default:
yakui_zhaobe25ed92009-06-02 14:13:55 +0800822 DRM_DEBUG_DRIVER(I915_DRV, "unknown parameter %d\n",
823 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000824 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
827 return 0;
828}
829
Eric Anholtc153f452007-09-03 12:06:45 +1000830static int i915_set_status_page(struct drm_device *dev, void *data,
831 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000832{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000833 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000834 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000835
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000836 if (!I915_NEED_GFX_HWS(dev))
837 return -EINVAL;
838
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000839 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000840 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000841 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000842 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000843
Jesse Barnes79e53942008-11-07 14:24:08 -0800844 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
845 WARN(1, "tried to set status page when mode setting active\n");
846 return 0;
847 }
848
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700849 DRM_DEBUG("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000850
Eric Anholtc153f452007-09-03 12:06:45 +1000851 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
852
Eric Anholt8b409582007-11-22 16:40:37 +1000853 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000854 dev_priv->hws_map.size = 4*1024;
855 dev_priv->hws_map.type = 0;
856 dev_priv->hws_map.flags = 0;
857 dev_priv->hws_map.mtrr = 0;
858
Dave Airliedd0910b2009-02-25 14:49:21 +1000859 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000860 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000861 i915_dma_cleanup(dev);
862 dev_priv->status_gfx_addr = 0;
863 DRM_ERROR("can not ioremap virtual address for"
864 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000865 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000866 }
867 dev_priv->hw_status_page = dev_priv->hws_map.handle;
868
869 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700870 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
yakui_zhaobe25ed92009-06-02 14:13:55 +0800871 DRM_DEBUG_DRIVER(I915_DRV, "load hws HWS_PGA with gfx mem 0x%x\n",
872 dev_priv->status_gfx_addr);
873 DRM_DEBUG_DRIVER(I915_DRV, "load hws at %p\n",
874 dev_priv->hw_status_page);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000875 return 0;
876}
877
Jesse Barnes79e53942008-11-07 14:24:08 -0800878/**
879 * i915_probe_agp - get AGP bootup configuration
880 * @pdev: PCI device
881 * @aperture_size: returns AGP aperture configured size
882 * @preallocated_size: returns size of BIOS preallocated AGP space
883 *
884 * Since Intel integrated graphics are UMA, the BIOS has to set aside
885 * some RAM for the framebuffer at early boot. This code figures out
886 * how much was set aside so we can use it for our own purposes.
887 */
Eric Anholt2a34f5e62009-07-02 09:30:50 -0700888static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,
889 uint32_t *preallocated_size)
Jesse Barnes79e53942008-11-07 14:24:08 -0800890{
891 struct pci_dev *bridge_dev;
892 u16 tmp = 0;
893 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -0800894 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -0800895
896 bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
897 if (!bridge_dev) {
898 DRM_ERROR("bridge device not found\n");
899 return -1;
900 }
901
902 /* Get the fb aperture size and "stolen" memory amount. */
903 pci_read_config_word(bridge_dev, INTEL_GMCH_CTRL, &tmp);
904 pci_dev_put(bridge_dev);
905
906 *aperture_size = 1024 * 1024;
907 *preallocated_size = 1024 * 1024;
908
Eric Anholt60fd99e2008-12-03 22:50:02 -0800909 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800910 case PCI_DEVICE_ID_INTEL_82830_CGC:
911 case PCI_DEVICE_ID_INTEL_82845G_IG:
912 case PCI_DEVICE_ID_INTEL_82855GM_IG:
913 case PCI_DEVICE_ID_INTEL_82865_IG:
914 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
915 *aperture_size *= 64;
916 else
917 *aperture_size *= 128;
918 break;
919 default:
920 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -0800921 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -0800922 break;
923 }
924
925 /*
926 * Some of the preallocated space is taken by the GTT
927 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
928 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800929 if (IS_G4X(dev) || IS_IGD(dev) || IS_IGDNG(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -0800930 overhead = 4096;
931 else
932 overhead = (*aperture_size / 1024) + 4096;
933
Eric Anholt241fa852009-01-02 18:05:51 -0800934 switch (tmp & INTEL_GMCH_GMS_MASK) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800935 case INTEL_855_GMCH_GMS_DISABLED:
936 DRM_ERROR("video memory is disabled\n");
937 return -1;
Eric Anholt241fa852009-01-02 18:05:51 -0800938 case INTEL_855_GMCH_GMS_STOLEN_1M:
939 stolen = 1 * 1024 * 1024;
940 break;
941 case INTEL_855_GMCH_GMS_STOLEN_4M:
942 stolen = 4 * 1024 * 1024;
943 break;
944 case INTEL_855_GMCH_GMS_STOLEN_8M:
945 stolen = 8 * 1024 * 1024;
946 break;
947 case INTEL_855_GMCH_GMS_STOLEN_16M:
948 stolen = 16 * 1024 * 1024;
949 break;
950 case INTEL_855_GMCH_GMS_STOLEN_32M:
951 stolen = 32 * 1024 * 1024;
952 break;
953 case INTEL_915G_GMCH_GMS_STOLEN_48M:
954 stolen = 48 * 1024 * 1024;
955 break;
956 case INTEL_915G_GMCH_GMS_STOLEN_64M:
957 stolen = 64 * 1024 * 1024;
958 break;
959 case INTEL_GMCH_GMS_STOLEN_128M:
960 stolen = 128 * 1024 * 1024;
961 break;
962 case INTEL_GMCH_GMS_STOLEN_256M:
963 stolen = 256 * 1024 * 1024;
964 break;
965 case INTEL_GMCH_GMS_STOLEN_96M:
966 stolen = 96 * 1024 * 1024;
967 break;
968 case INTEL_GMCH_GMS_STOLEN_160M:
969 stolen = 160 * 1024 * 1024;
970 break;
971 case INTEL_GMCH_GMS_STOLEN_224M:
972 stolen = 224 * 1024 * 1024;
973 break;
974 case INTEL_GMCH_GMS_STOLEN_352M:
975 stolen = 352 * 1024 * 1024;
976 break;
Jesse Barnes79e53942008-11-07 14:24:08 -0800977 default:
978 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
Eric Anholt241fa852009-01-02 18:05:51 -0800979 tmp & INTEL_GMCH_GMS_MASK);
Jesse Barnes79e53942008-11-07 14:24:08 -0800980 return -1;
981 }
Eric Anholt241fa852009-01-02 18:05:51 -0800982 *preallocated_size = stolen - overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -0800983
984 return 0;
985}
986
Eric Anholt2a34f5e62009-07-02 09:30:50 -0700987static int i915_load_modeset_init(struct drm_device *dev,
988 unsigned long prealloc_size,
989 unsigned long agp_size)
Jesse Barnes79e53942008-11-07 14:24:08 -0800990{
991 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800992 int fb_bar = IS_I9XX(dev) ? 2 : 0;
993 int ret = 0;
994
995 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
996 0xff000000;
997
Jesse Barnes2906f022009-01-20 19:10:54 -0800998 if (IS_MOBILE(dev) || IS_I9XX(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800999 dev_priv->cursor_needs_physical = true;
1000 else
1001 dev_priv->cursor_needs_physical = false;
1002
Jesse Barnes2906f022009-01-20 19:10:54 -08001003 if (IS_I965G(dev) || IS_G33(dev))
1004 dev_priv->cursor_needs_physical = false;
1005
Jesse Barnes79e53942008-11-07 14:24:08 -08001006 /* Basic memrange allocator for stolen space (aka vram) */
1007 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
1008
Eric Anholt13f4c432009-05-12 15:27:36 -07001009 /* Let GEM Manage from end of prealloc space to end of aperture.
1010 *
1011 * However, leave one page at the end still bound to the scratch page.
1012 * There are a number of places where the hardware apparently
1013 * prefetches past the end of the object, and we've seen multiple
1014 * hangs with the GPU head pointer stuck in a batchbuffer bound
1015 * at the last page of the aperture. One page should be enough to
1016 * keep any prefetching inside of the aperture.
1017 */
1018 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001019
1020 ret = i915_gem_init_ringbuffer(dev);
1021 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001022 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001023
Jesse Barnes79e53942008-11-07 14:24:08 -08001024 /* Allow hardware batchbuffers unless told otherwise.
1025 */
1026 dev_priv->allow_batchbuffer = 1;
1027
1028 ret = intel_init_bios(dev);
1029 if (ret)
1030 DRM_INFO("failed to find VBIOS tables\n");
1031
1032 ret = drm_irq_install(dev);
1033 if (ret)
1034 goto destroy_ringbuffer;
1035
Jesse Barnes79e53942008-11-07 14:24:08 -08001036 /* Always safe in the mode setting case. */
1037 /* FIXME: do pre/post-mode set stuff in core KMS code */
1038 dev->vblank_disable_allowed = 1;
1039
1040 /*
1041 * Initialize the hardware status page IRQ location.
1042 */
1043
1044 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1045
1046 intel_modeset_init(dev);
1047
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001048 drm_helper_initial_config(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001049
Jesse Barnes79e53942008-11-07 14:24:08 -08001050 return 0;
1051
Jesse Barnes79e53942008-11-07 14:24:08 -08001052destroy_ringbuffer:
1053 i915_gem_cleanup_ringbuffer(dev);
1054out:
1055 return ret;
1056}
1057
Dave Airlie7c1c2872008-11-28 14:22:24 +10001058int i915_master_create(struct drm_device *dev, struct drm_master *master)
1059{
1060 struct drm_i915_master_private *master_priv;
1061
Eric Anholt9a298b22009-03-24 12:23:04 -07001062 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001063 if (!master_priv)
1064 return -ENOMEM;
1065
1066 master->driver_priv = master_priv;
1067 return 0;
1068}
1069
1070void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1071{
1072 struct drm_i915_master_private *master_priv = master->driver_priv;
1073
1074 if (!master_priv)
1075 return;
1076
Eric Anholt9a298b22009-03-24 12:23:04 -07001077 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001078
1079 master->driver_priv = NULL;
1080}
1081
Shaohua Li7662c8b2009-06-26 11:23:55 +08001082static void i915_get_mem_freq(struct drm_device *dev)
1083{
1084 drm_i915_private_t *dev_priv = dev->dev_private;
1085 u32 tmp;
1086
1087 if (!IS_IGD(dev))
1088 return;
1089
1090 tmp = I915_READ(CLKCFG);
1091
1092 switch (tmp & CLKCFG_FSB_MASK) {
1093 case CLKCFG_FSB_533:
1094 dev_priv->fsb_freq = 533; /* 133*4 */
1095 break;
1096 case CLKCFG_FSB_800:
1097 dev_priv->fsb_freq = 800; /* 200*4 */
1098 break;
1099 case CLKCFG_FSB_667:
1100 dev_priv->fsb_freq = 667; /* 167*4 */
1101 break;
1102 case CLKCFG_FSB_400:
1103 dev_priv->fsb_freq = 400; /* 100*4 */
1104 break;
1105 }
1106
1107 switch (tmp & CLKCFG_MEM_MASK) {
1108 case CLKCFG_MEM_533:
1109 dev_priv->mem_freq = 533;
1110 break;
1111 case CLKCFG_MEM_667:
1112 dev_priv->mem_freq = 667;
1113 break;
1114 case CLKCFG_MEM_800:
1115 dev_priv->mem_freq = 800;
1116 break;
1117 }
1118}
1119
Jesse Barnes79e53942008-11-07 14:24:08 -08001120/**
1121 * i915_driver_load - setup chip and create an initial config
1122 * @dev: DRM device
1123 * @flags: startup flags
1124 *
1125 * The driver load routine has to do several things:
1126 * - drive output discovery via intel_modeset_init()
1127 * - initialize the memory manager
1128 * - allocate initial config memory
1129 * - setup the DRM framebuffer with the allocated memory
1130 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001131int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001132{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001133 struct drm_i915_private *dev_priv = dev->dev_private;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001134 resource_size_t base, size;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001135 int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1;
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001136 uint32_t agp_size, prealloc_size;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001137
Dave Airlie22eae942005-11-10 22:16:34 +11001138 /* i915 has 4 more counters */
1139 dev->counters += 4;
1140 dev->types[6] = _DRM_STAT_IRQ;
1141 dev->types[7] = _DRM_STAT_PRIMARY;
1142 dev->types[8] = _DRM_STAT_SECONDARY;
1143 dev->types[9] = _DRM_STAT_DMA;
1144
Eric Anholt9a298b22009-03-24 12:23:04 -07001145 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001146 if (dev_priv == NULL)
1147 return -ENOMEM;
1148
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001149 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001150 dev_priv->dev = dev;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001151
1152 /* Add register map (needed for suspend/resume) */
1153 base = drm_get_resource_start(dev, mmio_bar);
1154 size = drm_get_resource_len(dev, mmio_bar);
1155
Eric Anholt3043c602008-10-02 12:24:47 -07001156 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001157 if (!dev_priv->regs) {
1158 DRM_ERROR("failed to map registers\n");
1159 ret = -EIO;
1160 goto free_priv;
1161 }
Eric Anholted4cb412008-07-29 12:10:39 -07001162
Eric Anholtab657db12009-01-23 12:57:47 -08001163 dev_priv->mm.gtt_mapping =
1164 io_mapping_create_wc(dev->agp->base,
1165 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001166 if (dev_priv->mm.gtt_mapping == NULL) {
1167 ret = -EIO;
1168 goto out_rmmap;
1169 }
1170
Eric Anholtab657db12009-01-23 12:57:47 -08001171 /* Set up a WC MTRR for non-PAT systems. This is more common than
1172 * one would think, because the kernel disables PAT on first
1173 * generation Core chips because WC PAT gets overridden by a UC
1174 * MTRR if present. Even if a UC MTRR isn't present.
1175 */
1176 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1177 dev->agp->agp_info.aper_size *
1178 1024 * 1024,
1179 MTRR_TYPE_WRCOMB, 1);
1180 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001181 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001182 "performance may suffer.\n");
1183 }
1184
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001185 ret = i915_probe_agp(dev, &agp_size, &prealloc_size);
1186 if (ret)
1187 goto out_iomapfree;
1188
Dave Airlieac5c4e72008-12-19 15:38:34 +10001189 /* enable GEM by default */
1190 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10001191
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001192 if (prealloc_size > agp_size * 3 / 4) {
1193 DRM_ERROR("Detected broken video BIOS with %d/%dkB of video "
1194 "memory stolen.\n",
1195 prealloc_size / 1024, agp_size / 1024);
1196 DRM_ERROR("Disabling GEM. (try reducing stolen memory or "
1197 "updating the BIOS to fix).\n");
1198 dev_priv->has_gem = 0;
1199 }
1200
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001201 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001202 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001203 if (IS_G4X(dev) || IS_IGDNG(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001204 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001205 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001206 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001207
Eric Anholt673a3942008-07-30 12:06:12 -07001208 i915_gem_load(dev);
1209
Keith Packard398c9cb2008-07-30 13:03:43 -07001210 /* Init HWS */
1211 if (!I915_NEED_GFX_HWS(dev)) {
1212 ret = i915_init_phys_hws(dev);
1213 if (ret != 0)
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001214 goto out_iomapfree;
Keith Packard398c9cb2008-07-30 13:03:43 -07001215 }
Eric Anholted4cb412008-07-29 12:10:39 -07001216
Shaohua Li7662c8b2009-06-26 11:23:55 +08001217 i915_get_mem_freq(dev);
1218
Eric Anholted4cb412008-07-29 12:10:39 -07001219 /* On the 945G/GM, the chipset reports the MSI capability on the
1220 * integrated graphics even though the support isn't actually there
1221 * according to the published specs. It doesn't appear to function
1222 * correctly in testing on 945G.
1223 * This may be a side effect of MSI having been made available for PEG
1224 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001225 *
1226 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001227 * be lost or delayed, but we use them anyways to avoid
1228 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001229 */
Keith Packardb60678a2008-12-08 11:12:28 -08001230 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001231 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001232
1233 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001234 spin_lock_init(&dev_priv->error_lock);
Jesse Barnes79e53942008-11-07 14:24:08 -08001235 dev_priv->user_irq_refcount = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001236
Keith Packard52440212008-11-18 09:30:25 -08001237 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1238
1239 if (ret) {
1240 (void) i915_driver_unload(dev);
1241 return ret;
1242 }
1243
Jesse Barnes79e53942008-11-07 14:24:08 -08001244 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001245 ret = i915_load_modeset_init(dev, prealloc_size, agp_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001246 if (ret < 0) {
1247 DRM_ERROR("failed to init modeset\n");
1248 goto out_rmmap;
1249 }
1250 }
1251
Matthew Garrett74a365b2009-03-19 21:35:39 +00001252 /* Must be done after probing outputs */
Zhenyu Wange170b032009-06-05 15:38:40 +08001253 /* FIXME: verify on IGDNG */
1254 if (!IS_IGDNG(dev))
1255 intel_opregion_init(dev, 0);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001256
Jesse Barnes79e53942008-11-07 14:24:08 -08001257 return 0;
1258
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001259out_iomapfree:
1260 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001261out_rmmap:
1262 iounmap(dev_priv->regs);
1263free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001264 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001265 return ret;
1266}
1267
1268int i915_driver_unload(struct drm_device *dev)
1269{
1270 struct drm_i915_private *dev_priv = dev->dev_private;
1271
Eric Anholtab657db12009-01-23 12:57:47 -08001272 io_mapping_free(dev_priv->mm.gtt_mapping);
1273 if (dev_priv->mm.gtt_mtrr >= 0) {
1274 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1275 dev->agp->agp_info.aper_size * 1024 * 1024);
1276 dev_priv->mm.gtt_mtrr = -1;
1277 }
1278
Jesse Barnes79e53942008-11-07 14:24:08 -08001279 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001280 drm_irq_uninstall(dev);
1281 }
1282
Eric Anholted4cb412008-07-29 12:10:39 -07001283 if (dev->pdev->msi_enabled)
1284 pci_disable_msi(dev->pdev);
1285
Eric Anholt3043c602008-10-02 12:24:47 -07001286 if (dev_priv->regs != NULL)
1287 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001288
Zhenyu Wange170b032009-06-05 15:38:40 +08001289 if (!IS_IGDNG(dev))
1290 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001291
Jesse Barnes79e53942008-11-07 14:24:08 -08001292 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1293 intel_modeset_cleanup(dev);
1294
Dave Airlie71acb5e2008-12-30 20:31:46 +10001295 i915_gem_free_all_phys_object(dev);
1296
Jesse Barnes79e53942008-11-07 14:24:08 -08001297 mutex_lock(&dev->struct_mutex);
1298 i915_gem_cleanup_ringbuffer(dev);
1299 mutex_unlock(&dev->struct_mutex);
1300 drm_mm_takedown(&dev_priv->vram);
1301 i915_gem_lastclose(dev);
1302 }
1303
Eric Anholt9a298b22009-03-24 12:23:04 -07001304 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001305
Dave Airlie22eae942005-11-10 22:16:34 +11001306 return 0;
1307}
1308
Eric Anholt673a3942008-07-30 12:06:12 -07001309int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1310{
1311 struct drm_i915_file_private *i915_file_priv;
1312
yakui_zhaobe25ed92009-06-02 14:13:55 +08001313 DRM_DEBUG_DRIVER(I915_DRV, "\n");
Eric Anholt673a3942008-07-30 12:06:12 -07001314 i915_file_priv = (struct drm_i915_file_private *)
Eric Anholt9a298b22009-03-24 12:23:04 -07001315 kmalloc(sizeof(*i915_file_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001316
1317 if (!i915_file_priv)
1318 return -ENOMEM;
1319
1320 file_priv->driver_priv = i915_file_priv;
1321
Eric Anholtb9624422009-06-03 07:27:35 +00001322 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001323
1324 return 0;
1325}
1326
Jesse Barnes79e53942008-11-07 14:24:08 -08001327/**
1328 * i915_driver_lastclose - clean up after all DRM clients have exited
1329 * @dev: DRM device
1330 *
1331 * Take care of cleaning up after all DRM clients have exited. In the
1332 * mode setting case, we want to restore the kernel's initial mode (just
1333 * in case the last client left us in a bad state).
1334 *
1335 * Additionally, in the non-mode setting case, we'll tear down the AGP
1336 * and DMA structures, since the kernel won't be using them, and clea
1337 * up any GEM state.
1338 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001339void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001341 drm_i915_private_t *dev_priv = dev->dev_private;
1342
Jesse Barnes79e53942008-11-07 14:24:08 -08001343 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
1344 intelfb_restore();
Dave Airlie144a75f2008-03-30 07:53:58 +10001345 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001346 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001347
Eric Anholt673a3942008-07-30 12:06:12 -07001348 i915_gem_lastclose(dev);
1349
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001350 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001351 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001352
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001353 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Eric Anholt6c340ea2007-08-25 20:23:09 +10001356void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001358 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001359 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08001360 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1361 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Eric Anholt673a3942008-07-30 12:06:12 -07001364void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1365{
1366 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1367
Eric Anholt9a298b22009-03-24 12:23:04 -07001368 kfree(i915_file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001369}
1370
Eric Anholtc153f452007-09-03 12:06:45 +10001371struct drm_ioctl_desc i915_ioctls[] = {
1372 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1373 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1374 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1375 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1376 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1377 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1378 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1379 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1380 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1381 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1382 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1383 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1384 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1385 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1386 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1387 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001388 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 +10001389 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 -07001390 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
1391 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1392 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1393 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
1394 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
Dave Airlie2bdf00b2008-10-07 13:40:10 +10001395 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1396 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 -07001397 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1398 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
1399 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
1400 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
Jesse Barnesde151cf2008-11-12 10:03:55 -08001401 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0),
Eric Anholt673a3942008-07-30 12:06:12 -07001402 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
1403 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
1404 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1405 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
Eric Anholt5a125c32008-10-22 21:40:13 -07001406 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0),
Carl Worth08d7b3d2009-04-29 14:43:54 -07001407 DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
Dave Airliec94f7022005-07-07 21:03:38 +10001408};
1409
1410int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001411
1412/**
1413 * Determine if the device really is AGP or not.
1414 *
1415 * All Intel graphics chipsets are treated as AGP, even if they are really
1416 * PCI-e.
1417 *
1418 * \param dev The device to be tested.
1419 *
1420 * \returns
1421 * A value of 1 is always retured to indictate every i9x5 is AGP.
1422 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001423int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001424{
1425 return 1;
1426}