blob: 9909505d070a237026774a54d614a102d17c82bb [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"
Dave Airlie785b93e2009-08-28 15:46:53 +100032#include "drm_fb_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "i915_drm.h"
35#include "i915_drv.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* Really want an OS-independent resettable timer. Would like to have
38 * this loop run for (eg) 3 sec, but have the timer reset every time
39 * the head pointer changes, so that EBUSY only happens if the ring
40 * actually stalls for (eg) 3 seconds.
41 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100042int i915_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 drm_i915_private_t *dev_priv = dev->dev_private;
45 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
Keith Packardd3a6d442008-07-30 12:21:20 -070046 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
47 u32 last_acthd = I915_READ(acthd_reg);
48 u32 acthd;
Jesse Barnes585fb112008-07-29 11:54:06 -070049 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int i;
51
Keith Packardd3a6d442008-07-30 12:21:20 -070052 for (i = 0; i < 100000; i++) {
Jesse Barnes585fb112008-07-29 11:54:06 -070053 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Keith Packardd3a6d442008-07-30 12:21:20 -070054 acthd = I915_READ(acthd_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 ring->space = ring->head - (ring->tail + 8);
56 if (ring->space < 0)
57 ring->space += ring->Size;
58 if (ring->space >= n)
59 return 0;
60
Chris Wilson98787c02009-03-06 23:27:52 +000061 if (dev->primary->master) {
62 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
63 if (master_priv->sarea_priv)
64 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
65 }
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 if (ring->head != last_head)
69 i = 0;
Keith Packardd3a6d442008-07-30 12:21:20 -070070 if (acthd != last_acthd)
71 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 last_head = ring->head;
Keith Packardd3a6d442008-07-30 12:21:20 -070074 last_acthd = acthd;
75 msleep_interruptible(10);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
Eric Anholt20caafa2007-08-25 19:22:43 +100079 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Chris Wilson0ef82af2009-09-05 18:07:06 +010082/* As a ringbuffer is only allowed to wrap between instructions, fill
83 * the tail with NOOPs.
84 */
85int i915_wrap_ring(struct drm_device *dev)
86{
87 drm_i915_private_t *dev_priv = dev->dev_private;
88 volatile unsigned int *virt;
89 int rem;
90
91 rem = dev_priv->ring.Size - dev_priv->ring.tail;
92 if (dev_priv->ring.space < rem) {
93 int ret = i915_wait_ring(dev, rem, __func__);
94 if (ret)
95 return ret;
96 }
97 dev_priv->ring.space -= rem;
98
99 virt = (unsigned int *)
100 (dev_priv->ring.virtual_start + dev_priv->ring.tail);
101 rem /= 4;
102 while (rem--)
103 *virt++ = MI_NOOP;
104
105 dev_priv->ring.tail = 0;
106
107 return 0;
108}
109
Keith Packard398c9cb2008-07-30 13:03:43 -0700110/**
111 * Sets up the hardware status page for devices that need a physical address
112 * in the register.
113 */
Eric Anholt3043c602008-10-02 12:24:47 -0700114static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700115{
116 drm_i915_private_t *dev_priv = dev->dev_private;
117 /* Program Hardware Status Page */
118 dev_priv->status_page_dmah =
119 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
120
121 if (!dev_priv->status_page_dmah) {
122 DRM_ERROR("Can not allocate hardware status page\n");
123 return -ENOMEM;
124 }
125 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
126 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
127
128 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
129
130 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800131 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700132 return 0;
133}
134
135/**
136 * Frees the hardware status page, whether it's a physical address or a virtual
137 * address set up by the X Server.
138 */
Eric Anholt3043c602008-10-02 12:24:47 -0700139static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700140{
141 drm_i915_private_t *dev_priv = dev->dev_private;
142 if (dev_priv->status_page_dmah) {
143 drm_pci_free(dev, dev_priv->status_page_dmah);
144 dev_priv->status_page_dmah = NULL;
145 }
146
147 if (dev_priv->status_gfx_addr) {
148 dev_priv->status_gfx_addr = 0;
149 drm_core_ioremapfree(&dev_priv->hws_map, dev);
150 }
151
152 /* Need to rewrite hardware status page */
153 I915_WRITE(HWS_PGA, 0x1ffff000);
154}
155
Dave Airlie84b1fd12007-07-11 15:53:27 +1000156void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000159 struct drm_i915_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
161
Jesse Barnes79e53942008-11-07 14:24:08 -0800162 /*
163 * We should never lose context on the ring with modesetting
164 * as we don't expose it to userspace
165 */
166 if (drm_core_check_feature(dev, DRIVER_MODESET))
167 return;
168
Jesse Barnes585fb112008-07-29 11:54:06 -0700169 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
170 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 ring->space = ring->head - (ring->tail + 8);
172 if (ring->space < 0)
173 ring->space += ring->Size;
174
Dave Airlie7c1c2872008-11-28 14:22:24 +1000175 if (!dev->primary->master)
176 return;
177
178 master_priv = dev->primary->master->driver_priv;
179 if (ring->head == ring->tail && master_priv->sarea_priv)
180 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Dave Airlie84b1fd12007-07-11 15:53:27 +1000183static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000185 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Make sure interrupts are disabled here because the uninstall ioctl
187 * may not have been called from userspace and after dev_private
188 * is freed, it's too late.
189 */
Eric Anholted4cb412008-07-29 12:10:39 -0700190 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000193 if (dev_priv->ring.virtual_start) {
194 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Eric Anholt3043c602008-10-02 12:24:47 -0700195 dev_priv->ring.virtual_start = NULL;
196 dev_priv->ring.map.handle = NULL;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000197 dev_priv->ring.map.size = 0;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Keith Packard398c9cb2008-07-30 13:03:43 -0700200 /* Clear the HWS virtual address at teardown */
201 if (I915_NEED_GFX_HWS(dev))
202 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 return 0;
205}
206
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000207static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000209 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000210 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Dave Airlie3a03ac12009-01-11 09:03:49 +1000212 master_priv->sarea = drm_getsarea(dev);
213 if (master_priv->sarea) {
214 master_priv->sarea_priv = (drm_i915_sarea_t *)
215 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
216 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800217 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000218 }
219
Eric Anholt673a3942008-07-30 12:06:12 -0700220 if (init->ring_size != 0) {
221 if (dev_priv->ring.ring_obj != NULL) {
222 i915_dma_cleanup(dev);
223 DRM_ERROR("Client tried to initialize ringbuffer in "
224 "GEM mode\n");
225 return -EINVAL;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Eric Anholt673a3942008-07-30 12:06:12 -0700228 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric Anholt673a3942008-07-30 12:06:12 -0700230 dev_priv->ring.map.offset = init->ring_start;
231 dev_priv->ring.map.size = init->ring_size;
232 dev_priv->ring.map.type = 0;
233 dev_priv->ring.map.flags = 0;
234 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jesse Barnes6fb88582009-02-23 10:08:21 +1000236 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700237
238 if (dev_priv->ring.map.handle == NULL) {
239 i915_dma_cleanup(dev);
240 DRM_ERROR("can not ioremap virtual address for"
241 " ring buffer\n");
242 return -ENOMEM;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
247
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000248 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dev_priv->back_offset = init->back_offset;
250 dev_priv->front_offset = init->front_offset;
251 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000252 if (master_priv->sarea_priv)
253 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* Allow hardware batchbuffers unless told otherwise.
256 */
257 dev_priv->allow_batchbuffer = 1;
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260}
261
Dave Airlie84b1fd12007-07-11 15:53:27 +1000262static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
265
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800266 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (dev_priv->ring.map.handle == NULL) {
269 DRM_ERROR("can not ioremap virtual address for"
270 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000271 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 /* Program Hardware Status Page */
275 if (!dev_priv->hw_status_page) {
276 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000277 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800279 DRM_DEBUG_DRIVER("hw status page @ %p\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800280 dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000282 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700283 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000284 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700285 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800286 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return 0;
289}
290
Eric Anholtc153f452007-09-03 12:06:45 +1000291static int i915_dma_init(struct drm_device *dev, void *data,
292 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Eric Anholtc153f452007-09-03 12:06:45 +1000294 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int retcode = 0;
296
Eric Anholtc153f452007-09-03 12:06:45 +1000297 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000299 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 case I915_CLEANUP_DMA:
302 retcode = i915_dma_cleanup(dev);
303 break;
304 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100305 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000308 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 break;
310 }
311
312 return retcode;
313}
314
315/* Implement basically the same security restrictions as hardware does
316 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
317 *
318 * Most of the calculations below involve calculating the size of a
319 * particular instruction. It's important to get the size right as
320 * that tells us where the next instruction to check is. Any illegal
321 * instruction detected will be given a size of zero, which is a
322 * signal to abort the rest of the buffer.
323 */
324static int do_validate_cmd(int cmd)
325{
326 switch (((cmd >> 29) & 0x7)) {
327 case 0x0:
328 switch ((cmd >> 23) & 0x3f) {
329 case 0x0:
330 return 1; /* MI_NOOP */
331 case 0x4:
332 return 1; /* MI_FLUSH */
333 default:
334 return 0; /* disallow everything else */
335 }
336 break;
337 case 0x1:
338 return 0; /* reserved */
339 case 0x2:
340 return (cmd & 0xff) + 2; /* 2d commands */
341 case 0x3:
342 if (((cmd >> 24) & 0x1f) <= 0x18)
343 return 1;
344
345 switch ((cmd >> 24) & 0x1f) {
346 case 0x1c:
347 return 1;
348 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 case 0x3:
351 return (cmd & 0x1f) + 2;
352 case 0x4:
353 return (cmd & 0xf) + 2;
354 default:
355 return (cmd & 0xffff) + 2;
356 }
357 case 0x1e:
358 if (cmd & (1 << 23))
359 return (cmd & 0xffff) + 1;
360 else
361 return 1;
362 case 0x1f:
363 if ((cmd & (1 << 23)) == 0) /* inline vertices */
364 return (cmd & 0x1ffff) + 2;
365 else if (cmd & (1 << 17)) /* indirect random */
366 if ((cmd & 0xffff) == 0)
367 return 0; /* unknown length, too hard */
368 else
369 return (((cmd & 0xffff) + 1) / 2) + 1;
370 else
371 return 2; /* indirect sequential */
372 default:
373 return 0;
374 }
375 default:
376 return 0;
377 }
378
379 return 0;
380}
381
382static int validate_cmd(int cmd)
383{
384 int ret = do_validate_cmd(cmd);
385
Dave Airliebc5f4522007-11-05 12:50:58 +1000386/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 return ret;
389}
390
Eric Anholt201361a2009-03-11 12:30:04 -0700391static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 drm_i915_private_t *dev_priv = dev->dev_private;
394 int i;
395 RING_LOCALS;
396
Dave Airliede227f52006-01-25 15:31:43 +1100397 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000398 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100399
Alan Hourihanec29b6692006-08-12 16:29:24 +1000400 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 for (i = 0; i < dwords;) {
403 int cmd, sz;
404
Eric Anholt201361a2009-03-11 12:30:04 -0700405 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000408 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 OUT_RING(cmd);
411
412 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700413 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Dave Airliede227f52006-01-25 15:31:43 +1100417 if (dwords & 1)
418 OUT_RING(0);
419
420 ADVANCE_LP_RING();
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 0;
423}
424
Eric Anholt673a3942008-07-30 12:06:12 -0700425int
426i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700427 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700428 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt201361a2009-03-11 12:30:04 -0700431 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 RING_LOCALS;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
435 DRM_ERROR("Bad box %d,%d..%d,%d\n",
436 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Alan Hourihanec29b6692006-08-12 16:29:24 +1000440 if (IS_I965G(dev)) {
441 BEGIN_LP_RING(4);
442 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
443 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000444 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000445 OUT_RING(DR4);
446 ADVANCE_LP_RING();
447 } else {
448 BEGIN_LP_RING(6);
449 OUT_RING(GFX_OP_DRAWRECT_INFO);
450 OUT_RING(DR1);
451 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
452 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
453 OUT_RING(DR4);
454 OUT_RING(0);
455 ADVANCE_LP_RING();
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return 0;
459}
460
Alan Hourihanec29b6692006-08-12 16:29:24 +1000461/* XXX: Emitting the counter should really be moved to part of the IRQ
462 * emit. For now, do it in both places:
463 */
464
Dave Airlie84b1fd12007-07-11 15:53:27 +1000465static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100466{
467 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000468 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100469 RING_LOCALS;
470
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400471 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000472 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400473 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000474 if (master_priv->sarea_priv)
475 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100476
477 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700478 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000479 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100480 OUT_RING(dev_priv->counter);
481 OUT_RING(0);
482 ADVANCE_LP_RING();
483}
484
Dave Airlie84b1fd12007-07-11 15:53:27 +1000485static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700486 drm_i915_cmdbuffer_t *cmd,
487 struct drm_clip_rect *cliprects,
488 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 int nbox = cmd->num_cliprects;
491 int i = 0, count, ret;
492
493 if (cmd->sz & 0x3) {
494 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000495 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
498 i915_kernel_lost_context(dev);
499
500 count = nbox ? nbox : 1;
501
502 for (i = 0; i < count; i++) {
503 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700504 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 cmd->DR1, cmd->DR4);
506 if (ret)
507 return ret;
508 }
509
Eric Anholt201361a2009-03-11 12:30:04 -0700510 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (ret)
512 return ret;
513 }
514
Dave Airliede227f52006-01-25 15:31:43 +1100515 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return 0;
517}
518
Dave Airlie84b1fd12007-07-11 15:53:27 +1000519static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700520 drm_i915_batchbuffer_t * batch,
521 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int nbox = batch->num_cliprects;
525 int i = 0, count;
526 RING_LOCALS;
527
528 if ((batch->start | batch->used) & 0x7) {
529 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000530 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
533 i915_kernel_lost_context(dev);
534
535 count = nbox ? nbox : 1;
536
537 for (i = 0; i < count; i++) {
538 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700539 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 batch->DR1, batch->DR4);
541 if (ret)
542 return ret;
543 }
544
Keith Packard0790d5e2008-07-30 12:28:47 -0700545 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000547 if (IS_I965G(dev)) {
548 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
549 OUT_RING(batch->start);
550 } else {
551 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
552 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 ADVANCE_LP_RING();
555 } else {
556 BEGIN_LP_RING(4);
557 OUT_RING(MI_BATCH_BUFFER);
558 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
559 OUT_RING(batch->start + batch->used - 4);
560 OUT_RING(0);
561 ADVANCE_LP_RING();
562 }
563 }
564
Dave Airliede227f52006-01-25 15:31:43 +1100565 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 return 0;
568}
569
Dave Airlieaf6061a2008-05-07 12:15:39 +1000570static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000573 struct drm_i915_master_private *master_priv =
574 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 RING_LOCALS;
576
Dave Airlie7c1c2872008-11-28 14:22:24 +1000577 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400578 return -EINVAL;
579
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800580 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800581 __func__,
582 dev_priv->current_page,
583 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Dave Airlieaf6061a2008-05-07 12:15:39 +1000585 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Dave Airlieaf6061a2008-05-07 12:15:39 +1000587 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700588 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000589 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ADVANCE_LP_RING();
591
Dave Airlieaf6061a2008-05-07 12:15:39 +1000592 BEGIN_LP_RING(6);
593 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
594 OUT_RING(0);
595 if (dev_priv->current_page == 0) {
596 OUT_RING(dev_priv->back_offset);
597 dev_priv->current_page = 1;
598 } else {
599 OUT_RING(dev_priv->front_offset);
600 dev_priv->current_page = 0;
601 }
602 OUT_RING(0);
603 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000604
Dave Airlieaf6061a2008-05-07 12:15:39 +1000605 BEGIN_LP_RING(2);
606 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
607 OUT_RING(0);
608 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000609
Dave Airlie7c1c2872008-11-28 14:22:24 +1000610 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000611
Dave Airlieaf6061a2008-05-07 12:15:39 +1000612 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700613 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000614 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000615 OUT_RING(dev_priv->counter);
616 OUT_RING(0);
617 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000618
Dave Airlie7c1c2872008-11-28 14:22:24 +1000619 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Dave Airlie84b1fd12007-07-11 15:53:27 +1000623static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 drm_i915_private_t *dev_priv = dev->dev_private;
626
627 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700628 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Eric Anholtc153f452007-09-03 12:06:45 +1000631static int i915_flush_ioctl(struct drm_device *dev, void *data,
632 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Eric Anholt546b0972008-09-01 16:45:29 -0700634 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Eric Anholt546b0972008-09-01 16:45:29 -0700636 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
637
638 mutex_lock(&dev->struct_mutex);
639 ret = i915_quiescent(dev);
640 mutex_unlock(&dev->struct_mutex);
641
642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Eric Anholtc153f452007-09-03 12:06:45 +1000645static int i915_batchbuffer(struct drm_device *dev, void *data,
646 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000649 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000651 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000652 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700654 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (!dev_priv->allow_batchbuffer) {
657 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800661 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800662 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Eric Anholt546b0972008-09-01 16:45:29 -0700664 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Eric Anholt201361a2009-03-11 12:30:04 -0700666 if (batch->num_cliprects < 0)
667 return -EINVAL;
668
669 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700670 cliprects = kcalloc(batch->num_cliprects,
671 sizeof(struct drm_clip_rect),
672 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700673 if (cliprects == NULL)
674 return -ENOMEM;
675
676 ret = copy_from_user(cliprects, batch->cliprects,
677 batch->num_cliprects *
678 sizeof(struct drm_clip_rect));
679 if (ret != 0)
680 goto fail_free;
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Eric Anholt546b0972008-09-01 16:45:29 -0700683 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700684 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700685 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400687 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000688 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700689
690fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700691 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return ret;
694}
695
Eric Anholtc153f452007-09-03 12:06:45 +1000696static int i915_cmdbuffer(struct drm_device *dev, void *data,
697 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000700 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000702 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000703 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700704 struct drm_clip_rect *cliprects = NULL;
705 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 int ret;
707
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800708 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800709 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Eric Anholt546b0972008-09-01 16:45:29 -0700711 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Eric Anholt201361a2009-03-11 12:30:04 -0700713 if (cmdbuf->num_cliprects < 0)
714 return -EINVAL;
715
Eric Anholt9a298b22009-03-24 12:23:04 -0700716 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700717 if (batch_data == NULL)
718 return -ENOMEM;
719
720 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
721 if (ret != 0)
722 goto fail_batch_free;
723
724 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700725 cliprects = kcalloc(cmdbuf->num_cliprects,
726 sizeof(struct drm_clip_rect), GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700727 if (cliprects == NULL)
728 goto fail_batch_free;
729
730 ret = copy_from_user(cliprects, cmdbuf->cliprects,
731 cmdbuf->num_cliprects *
732 sizeof(struct drm_clip_rect));
733 if (ret != 0)
734 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
Eric Anholt546b0972008-09-01 16:45:29 -0700737 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700738 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700739 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (ret) {
741 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000742 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400745 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000746 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700747
Eric Anholt201361a2009-03-11 12:30:04 -0700748fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700749 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000750fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700751 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700752
753 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Eric Anholtc153f452007-09-03 12:06:45 +1000756static int i915_flip_bufs(struct drm_device *dev, void *data,
757 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Eric Anholt546b0972008-09-01 16:45:29 -0700759 int ret;
760
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800761 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Eric Anholt546b0972008-09-01 16:45:29 -0700763 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Eric Anholt546b0972008-09-01 16:45:29 -0700765 mutex_lock(&dev->struct_mutex);
766 ret = i915_dispatch_flip(dev);
767 mutex_unlock(&dev->struct_mutex);
768
769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Eric Anholtc153f452007-09-03 12:06:45 +1000772static int i915_getparam(struct drm_device *dev, void *data,
773 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000776 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int value;
778
779 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000780 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000781 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Eric Anholtc153f452007-09-03 12:06:45 +1000784 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700786 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 break;
788 case I915_PARAM_ALLOW_BATCHBUFFER:
789 value = dev_priv->allow_batchbuffer ? 1 : 0;
790 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100791 case I915_PARAM_LAST_DISPATCH:
792 value = READ_BREADCRUMB(dev_priv);
793 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400794 case I915_PARAM_CHIPSET_ID:
795 value = dev->pci_device;
796 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700797 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000798 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700799 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800800 case I915_PARAM_NUM_FENCES_AVAIL:
801 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800804 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800805 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000806 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
Eric Anholtc153f452007-09-03 12:06:45 +1000809 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000811 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 return 0;
815}
816
Eric Anholtc153f452007-09-03 12:06:45 +1000817static int i915_setparam(struct drm_device *dev, void *data,
818 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000821 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000824 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Eric Anholtc153f452007-09-03 12:06:45 +1000828 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
831 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000832 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 break;
834 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000835 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800837 case I915_SETPARAM_NUM_USED_FENCES:
838 if (param->value > dev_priv->num_fence_regs ||
839 param->value < 0)
840 return -EINVAL;
841 /* Userspace can use first N regs */
842 dev_priv->fence_reg_start = param->value;
843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800845 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800846 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000847 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
850 return 0;
851}
852
Eric Anholtc153f452007-09-03 12:06:45 +1000853static int i915_set_status_page(struct drm_device *dev, void *data,
854 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000855{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000856 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000857 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000858
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000859 if (!I915_NEED_GFX_HWS(dev))
860 return -EINVAL;
861
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000862 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000863 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000864 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000865 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000866
Jesse Barnes79e53942008-11-07 14:24:08 -0800867 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
868 WARN(1, "tried to set status page when mode setting active\n");
869 return 0;
870 }
871
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800872 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000873
Eric Anholtc153f452007-09-03 12:06:45 +1000874 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
875
Eric Anholt8b409582007-11-22 16:40:37 +1000876 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000877 dev_priv->hws_map.size = 4*1024;
878 dev_priv->hws_map.type = 0;
879 dev_priv->hws_map.flags = 0;
880 dev_priv->hws_map.mtrr = 0;
881
Dave Airliedd0910b2009-02-25 14:49:21 +1000882 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000883 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000884 i915_dma_cleanup(dev);
885 dev_priv->status_gfx_addr = 0;
886 DRM_ERROR("can not ioremap virtual address for"
887 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000888 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000889 }
890 dev_priv->hw_status_page = dev_priv->hws_map.handle;
891
892 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700893 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800894 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800895 dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800896 DRM_DEBUG_DRIVER("load hws at %p\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800897 dev_priv->hw_status_page);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000898 return 0;
899}
900
Dave Airlieec2a4c32009-08-04 11:43:41 +1000901static int i915_get_bridge_dev(struct drm_device *dev)
902{
903 struct drm_i915_private *dev_priv = dev->dev_private;
904
905 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
906 if (!dev_priv->bridge_dev) {
907 DRM_ERROR("bridge device not found\n");
908 return -1;
909 }
910 return 0;
911}
912
Jesse Barnes79e53942008-11-07 14:24:08 -0800913/**
914 * i915_probe_agp - get AGP bootup configuration
915 * @pdev: PCI device
916 * @aperture_size: returns AGP aperture configured size
917 * @preallocated_size: returns size of BIOS preallocated AGP space
918 *
919 * Since Intel integrated graphics are UMA, the BIOS has to set aside
920 * some RAM for the framebuffer at early boot. This code figures out
921 * how much was set aside so we can use it for our own purposes.
922 */
Eric Anholt2a34f5e62009-07-02 09:30:50 -0700923static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,
924 uint32_t *preallocated_size)
Jesse Barnes79e53942008-11-07 14:24:08 -0800925{
Dave Airlieec2a4c32009-08-04 11:43:41 +1000926 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800927 u16 tmp = 0;
928 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -0800929 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -0800930
Jesse Barnes79e53942008-11-07 14:24:08 -0800931 /* Get the fb aperture size and "stolen" memory amount. */
Dave Airlieec2a4c32009-08-04 11:43:41 +1000932 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &tmp);
Jesse Barnes79e53942008-11-07 14:24:08 -0800933
934 *aperture_size = 1024 * 1024;
935 *preallocated_size = 1024 * 1024;
936
Eric Anholt60fd99e2008-12-03 22:50:02 -0800937 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800938 case PCI_DEVICE_ID_INTEL_82830_CGC:
939 case PCI_DEVICE_ID_INTEL_82845G_IG:
940 case PCI_DEVICE_ID_INTEL_82855GM_IG:
941 case PCI_DEVICE_ID_INTEL_82865_IG:
942 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
943 *aperture_size *= 64;
944 else
945 *aperture_size *= 128;
946 break;
947 default:
948 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -0800949 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -0800950 break;
951 }
952
953 /*
954 * Some of the preallocated space is taken by the GTT
955 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
956 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800957 if (IS_G4X(dev) || IS_IGD(dev) || IS_IGDNG(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -0800958 overhead = 4096;
959 else
960 overhead = (*aperture_size / 1024) + 4096;
961
Eric Anholt241fa852009-01-02 18:05:51 -0800962 switch (tmp & INTEL_GMCH_GMS_MASK) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800963 case INTEL_855_GMCH_GMS_DISABLED:
964 DRM_ERROR("video memory is disabled\n");
965 return -1;
Eric Anholt241fa852009-01-02 18:05:51 -0800966 case INTEL_855_GMCH_GMS_STOLEN_1M:
967 stolen = 1 * 1024 * 1024;
968 break;
969 case INTEL_855_GMCH_GMS_STOLEN_4M:
970 stolen = 4 * 1024 * 1024;
971 break;
972 case INTEL_855_GMCH_GMS_STOLEN_8M:
973 stolen = 8 * 1024 * 1024;
974 break;
975 case INTEL_855_GMCH_GMS_STOLEN_16M:
976 stolen = 16 * 1024 * 1024;
977 break;
978 case INTEL_855_GMCH_GMS_STOLEN_32M:
979 stolen = 32 * 1024 * 1024;
980 break;
981 case INTEL_915G_GMCH_GMS_STOLEN_48M:
982 stolen = 48 * 1024 * 1024;
983 break;
984 case INTEL_915G_GMCH_GMS_STOLEN_64M:
985 stolen = 64 * 1024 * 1024;
986 break;
987 case INTEL_GMCH_GMS_STOLEN_128M:
988 stolen = 128 * 1024 * 1024;
989 break;
990 case INTEL_GMCH_GMS_STOLEN_256M:
991 stolen = 256 * 1024 * 1024;
992 break;
993 case INTEL_GMCH_GMS_STOLEN_96M:
994 stolen = 96 * 1024 * 1024;
995 break;
996 case INTEL_GMCH_GMS_STOLEN_160M:
997 stolen = 160 * 1024 * 1024;
998 break;
999 case INTEL_GMCH_GMS_STOLEN_224M:
1000 stolen = 224 * 1024 * 1024;
1001 break;
1002 case INTEL_GMCH_GMS_STOLEN_352M:
1003 stolen = 352 * 1024 * 1024;
1004 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08001005 default:
1006 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
Eric Anholt241fa852009-01-02 18:05:51 -08001007 tmp & INTEL_GMCH_GMS_MASK);
Jesse Barnes79e53942008-11-07 14:24:08 -08001008 return -1;
1009 }
Eric Anholt241fa852009-01-02 18:05:51 -08001010 *preallocated_size = stolen - overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -08001011
1012 return 0;
1013}
1014
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001015static int i915_load_modeset_init(struct drm_device *dev,
1016 unsigned long prealloc_size,
1017 unsigned long agp_size)
Jesse Barnes79e53942008-11-07 14:24:08 -08001018{
1019 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001020 int fb_bar = IS_I9XX(dev) ? 2 : 0;
1021 int ret = 0;
1022
1023 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
1024 0xff000000;
1025
Jesse Barnes2906f022009-01-20 19:10:54 -08001026 if (IS_MOBILE(dev) || IS_I9XX(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08001027 dev_priv->cursor_needs_physical = true;
1028 else
1029 dev_priv->cursor_needs_physical = false;
1030
Jesse Barnes2906f022009-01-20 19:10:54 -08001031 if (IS_I965G(dev) || IS_G33(dev))
1032 dev_priv->cursor_needs_physical = false;
1033
Jesse Barnes79e53942008-11-07 14:24:08 -08001034 /* Basic memrange allocator for stolen space (aka vram) */
1035 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
1036
Eric Anholt13f4c432009-05-12 15:27:36 -07001037 /* Let GEM Manage from end of prealloc space to end of aperture.
1038 *
1039 * However, leave one page at the end still bound to the scratch page.
1040 * There are a number of places where the hardware apparently
1041 * prefetches past the end of the object, and we've seen multiple
1042 * hangs with the GPU head pointer stuck in a batchbuffer bound
1043 * at the last page of the aperture. One page should be enough to
1044 * keep any prefetching inside of the aperture.
1045 */
1046 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001047
1048 ret = i915_gem_init_ringbuffer(dev);
1049 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001050 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001051
Jesse Barnes79e53942008-11-07 14:24:08 -08001052 /* Allow hardware batchbuffers unless told otherwise.
1053 */
1054 dev_priv->allow_batchbuffer = 1;
1055
1056 ret = intel_init_bios(dev);
1057 if (ret)
1058 DRM_INFO("failed to find VBIOS tables\n");
1059
1060 ret = drm_irq_install(dev);
1061 if (ret)
1062 goto destroy_ringbuffer;
1063
Jesse Barnes79e53942008-11-07 14:24:08 -08001064 /* Always safe in the mode setting case. */
1065 /* FIXME: do pre/post-mode set stuff in core KMS code */
1066 dev->vblank_disable_allowed = 1;
1067
1068 /*
1069 * Initialize the hardware status page IRQ location.
1070 */
1071
1072 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1073
1074 intel_modeset_init(dev);
1075
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001076 drm_helper_initial_config(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001077
Jesse Barnes79e53942008-11-07 14:24:08 -08001078 return 0;
1079
Jesse Barnes79e53942008-11-07 14:24:08 -08001080destroy_ringbuffer:
1081 i915_gem_cleanup_ringbuffer(dev);
1082out:
1083 return ret;
1084}
1085
Dave Airlie7c1c2872008-11-28 14:22:24 +10001086int i915_master_create(struct drm_device *dev, struct drm_master *master)
1087{
1088 struct drm_i915_master_private *master_priv;
1089
Eric Anholt9a298b22009-03-24 12:23:04 -07001090 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001091 if (!master_priv)
1092 return -ENOMEM;
1093
1094 master->driver_priv = master_priv;
1095 return 0;
1096}
1097
1098void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1099{
1100 struct drm_i915_master_private *master_priv = master->driver_priv;
1101
1102 if (!master_priv)
1103 return;
1104
Eric Anholt9a298b22009-03-24 12:23:04 -07001105 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001106
1107 master->driver_priv = NULL;
1108}
1109
Shaohua Li7662c8b2009-06-26 11:23:55 +08001110static void i915_get_mem_freq(struct drm_device *dev)
1111{
1112 drm_i915_private_t *dev_priv = dev->dev_private;
1113 u32 tmp;
1114
1115 if (!IS_IGD(dev))
1116 return;
1117
1118 tmp = I915_READ(CLKCFG);
1119
1120 switch (tmp & CLKCFG_FSB_MASK) {
1121 case CLKCFG_FSB_533:
1122 dev_priv->fsb_freq = 533; /* 133*4 */
1123 break;
1124 case CLKCFG_FSB_800:
1125 dev_priv->fsb_freq = 800; /* 200*4 */
1126 break;
1127 case CLKCFG_FSB_667:
1128 dev_priv->fsb_freq = 667; /* 167*4 */
1129 break;
1130 case CLKCFG_FSB_400:
1131 dev_priv->fsb_freq = 400; /* 100*4 */
1132 break;
1133 }
1134
1135 switch (tmp & CLKCFG_MEM_MASK) {
1136 case CLKCFG_MEM_533:
1137 dev_priv->mem_freq = 533;
1138 break;
1139 case CLKCFG_MEM_667:
1140 dev_priv->mem_freq = 667;
1141 break;
1142 case CLKCFG_MEM_800:
1143 dev_priv->mem_freq = 800;
1144 break;
1145 }
1146}
1147
Jesse Barnes79e53942008-11-07 14:24:08 -08001148/**
1149 * i915_driver_load - setup chip and create an initial config
1150 * @dev: DRM device
1151 * @flags: startup flags
1152 *
1153 * The driver load routine has to do several things:
1154 * - drive output discovery via intel_modeset_init()
1155 * - initialize the memory manager
1156 * - allocate initial config memory
1157 * - setup the DRM framebuffer with the allocated memory
1158 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001159int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001160{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001161 struct drm_i915_private *dev_priv = dev->dev_private;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001162 resource_size_t base, size;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001163 int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1;
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001164 uint32_t agp_size, prealloc_size;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001165
Dave Airlie22eae942005-11-10 22:16:34 +11001166 /* i915 has 4 more counters */
1167 dev->counters += 4;
1168 dev->types[6] = _DRM_STAT_IRQ;
1169 dev->types[7] = _DRM_STAT_PRIMARY;
1170 dev->types[8] = _DRM_STAT_SECONDARY;
1171 dev->types[9] = _DRM_STAT_DMA;
1172
Eric Anholt9a298b22009-03-24 12:23:04 -07001173 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001174 if (dev_priv == NULL)
1175 return -ENOMEM;
1176
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001177 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001178 dev_priv->dev = dev;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001179
1180 /* Add register map (needed for suspend/resume) */
1181 base = drm_get_resource_start(dev, mmio_bar);
1182 size = drm_get_resource_len(dev, mmio_bar);
1183
Dave Airlieec2a4c32009-08-04 11:43:41 +10001184 if (i915_get_bridge_dev(dev)) {
1185 ret = -EIO;
1186 goto free_priv;
1187 }
1188
Eric Anholt3043c602008-10-02 12:24:47 -07001189 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001190 if (!dev_priv->regs) {
1191 DRM_ERROR("failed to map registers\n");
1192 ret = -EIO;
Dave Airlieec2a4c32009-08-04 11:43:41 +10001193 goto put_bridge;
Jesse Barnes79e53942008-11-07 14:24:08 -08001194 }
Eric Anholted4cb412008-07-29 12:10:39 -07001195
Eric Anholtab657db12009-01-23 12:57:47 -08001196 dev_priv->mm.gtt_mapping =
1197 io_mapping_create_wc(dev->agp->base,
1198 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001199 if (dev_priv->mm.gtt_mapping == NULL) {
1200 ret = -EIO;
1201 goto out_rmmap;
1202 }
1203
Eric Anholtab657db12009-01-23 12:57:47 -08001204 /* Set up a WC MTRR for non-PAT systems. This is more common than
1205 * one would think, because the kernel disables PAT on first
1206 * generation Core chips because WC PAT gets overridden by a UC
1207 * MTRR if present. Even if a UC MTRR isn't present.
1208 */
1209 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1210 dev->agp->agp_info.aper_size *
1211 1024 * 1024,
1212 MTRR_TYPE_WRCOMB, 1);
1213 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001214 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001215 "performance may suffer.\n");
1216 }
1217
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001218 ret = i915_probe_agp(dev, &agp_size, &prealloc_size);
1219 if (ret)
1220 goto out_iomapfree;
1221
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001222 dev_priv->wq = create_workqueue("i915");
1223 if (dev_priv->wq == NULL) {
1224 DRM_ERROR("Failed to create our workqueue.\n");
1225 ret = -ENOMEM;
1226 goto out_iomapfree;
1227 }
1228
Dave Airlieac5c4e72008-12-19 15:38:34 +10001229 /* enable GEM by default */
1230 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10001231
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001232 if (prealloc_size > agp_size * 3 / 4) {
1233 DRM_ERROR("Detected broken video BIOS with %d/%dkB of video "
1234 "memory stolen.\n",
1235 prealloc_size / 1024, agp_size / 1024);
1236 DRM_ERROR("Disabling GEM. (try reducing stolen memory or "
1237 "updating the BIOS to fix).\n");
1238 dev_priv->has_gem = 0;
1239 }
1240
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001241 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001242 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001243 if (IS_G4X(dev) || IS_IGDNG(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001244 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001245 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001246 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001247
Eric Anholt673a3942008-07-30 12:06:12 -07001248 i915_gem_load(dev);
1249
Keith Packard398c9cb2008-07-30 13:03:43 -07001250 /* Init HWS */
1251 if (!I915_NEED_GFX_HWS(dev)) {
1252 ret = i915_init_phys_hws(dev);
1253 if (ret != 0)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001254 goto out_workqueue_free;
Keith Packard398c9cb2008-07-30 13:03:43 -07001255 }
Eric Anholted4cb412008-07-29 12:10:39 -07001256
Shaohua Li7662c8b2009-06-26 11:23:55 +08001257 i915_get_mem_freq(dev);
1258
Eric Anholted4cb412008-07-29 12:10:39 -07001259 /* On the 945G/GM, the chipset reports the MSI capability on the
1260 * integrated graphics even though the support isn't actually there
1261 * according to the published specs. It doesn't appear to function
1262 * correctly in testing on 945G.
1263 * This may be a side effect of MSI having been made available for PEG
1264 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001265 *
1266 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001267 * be lost or delayed, but we use them anyways to avoid
1268 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001269 */
Keith Packardb60678a2008-12-08 11:12:28 -08001270 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001271 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001272
1273 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001274 spin_lock_init(&dev_priv->error_lock);
Jesse Barnes79e53942008-11-07 14:24:08 -08001275 dev_priv->user_irq_refcount = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001276
Keith Packard52440212008-11-18 09:30:25 -08001277 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1278
1279 if (ret) {
1280 (void) i915_driver_unload(dev);
1281 return ret;
1282 }
1283
Jesse Barnes79e53942008-11-07 14:24:08 -08001284 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001285 ret = i915_load_modeset_init(dev, prealloc_size, agp_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001286 if (ret < 0) {
1287 DRM_ERROR("failed to init modeset\n");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001288 goto out_workqueue_free;
Jesse Barnes79e53942008-11-07 14:24:08 -08001289 }
1290 }
1291
Matthew Garrett74a365b2009-03-19 21:35:39 +00001292 /* Must be done after probing outputs */
Zhenyu Wange170b032009-06-05 15:38:40 +08001293 /* FIXME: verify on IGDNG */
1294 if (!IS_IGDNG(dev))
1295 intel_opregion_init(dev, 0);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001296
Jesse Barnes79e53942008-11-07 14:24:08 -08001297 return 0;
1298
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001299out_workqueue_free:
1300 destroy_workqueue(dev_priv->wq);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001301out_iomapfree:
1302 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001303out_rmmap:
1304 iounmap(dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001305put_bridge:
1306 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001307free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001308 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001309 return ret;
1310}
1311
1312int i915_driver_unload(struct drm_device *dev)
1313{
1314 struct drm_i915_private *dev_priv = dev->dev_private;
1315
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001316 destroy_workqueue(dev_priv->wq);
1317
Eric Anholtab657db12009-01-23 12:57:47 -08001318 io_mapping_free(dev_priv->mm.gtt_mapping);
1319 if (dev_priv->mm.gtt_mtrr >= 0) {
1320 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1321 dev->agp->agp_info.aper_size * 1024 * 1024);
1322 dev_priv->mm.gtt_mtrr = -1;
1323 }
1324
Jesse Barnes79e53942008-11-07 14:24:08 -08001325 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001326 drm_irq_uninstall(dev);
1327 }
1328
Eric Anholted4cb412008-07-29 12:10:39 -07001329 if (dev->pdev->msi_enabled)
1330 pci_disable_msi(dev->pdev);
1331
Eric Anholt3043c602008-10-02 12:24:47 -07001332 if (dev_priv->regs != NULL)
1333 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001334
Zhenyu Wange170b032009-06-05 15:38:40 +08001335 if (!IS_IGDNG(dev))
1336 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001337
Jesse Barnes79e53942008-11-07 14:24:08 -08001338 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1339 intel_modeset_cleanup(dev);
1340
Dave Airlie71acb5e2008-12-30 20:31:46 +10001341 i915_gem_free_all_phys_object(dev);
1342
Jesse Barnes79e53942008-11-07 14:24:08 -08001343 mutex_lock(&dev->struct_mutex);
1344 i915_gem_cleanup_ringbuffer(dev);
1345 mutex_unlock(&dev->struct_mutex);
1346 drm_mm_takedown(&dev_priv->vram);
1347 i915_gem_lastclose(dev);
1348 }
1349
Dave Airlieec2a4c32009-08-04 11:43:41 +10001350 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07001351 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001352
Dave Airlie22eae942005-11-10 22:16:34 +11001353 return 0;
1354}
1355
Eric Anholt673a3942008-07-30 12:06:12 -07001356int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1357{
1358 struct drm_i915_file_private *i915_file_priv;
1359
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001360 DRM_DEBUG_DRIVER("\n");
Eric Anholt673a3942008-07-30 12:06:12 -07001361 i915_file_priv = (struct drm_i915_file_private *)
Eric Anholt9a298b22009-03-24 12:23:04 -07001362 kmalloc(sizeof(*i915_file_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001363
1364 if (!i915_file_priv)
1365 return -ENOMEM;
1366
1367 file_priv->driver_priv = i915_file_priv;
1368
Eric Anholtb9624422009-06-03 07:27:35 +00001369 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001370
1371 return 0;
1372}
1373
Jesse Barnes79e53942008-11-07 14:24:08 -08001374/**
1375 * i915_driver_lastclose - clean up after all DRM clients have exited
1376 * @dev: DRM device
1377 *
1378 * Take care of cleaning up after all DRM clients have exited. In the
1379 * mode setting case, we want to restore the kernel's initial mode (just
1380 * in case the last client left us in a bad state).
1381 *
1382 * Additionally, in the non-mode setting case, we'll tear down the AGP
1383 * and DMA structures, since the kernel won't be using them, and clea
1384 * up any GEM state.
1385 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001386void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001388 drm_i915_private_t *dev_priv = dev->dev_private;
1389
Jesse Barnes79e53942008-11-07 14:24:08 -08001390 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001391 drm_fb_helper_restore();
Dave Airlie144a75f2008-03-30 07:53:58 +10001392 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001393 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001394
Eric Anholt673a3942008-07-30 12:06:12 -07001395 i915_gem_lastclose(dev);
1396
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001397 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001398 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001399
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001400 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Eric Anholt6c340ea2007-08-25 20:23:09 +10001403void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001405 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001406 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08001407 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1408 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Eric Anholt673a3942008-07-30 12:06:12 -07001411void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1412{
1413 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1414
Eric Anholt9a298b22009-03-24 12:23:04 -07001415 kfree(i915_file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001416}
1417
Eric Anholtc153f452007-09-03 12:06:45 +10001418struct drm_ioctl_desc i915_ioctls[] = {
1419 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1420 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1421 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1422 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1423 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1424 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1425 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1426 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1427 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1428 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1429 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1430 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1431 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1432 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1433 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1434 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001435 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 +10001436 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 -07001437 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
1438 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1439 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1440 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
1441 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
Dave Airlie2bdf00b2008-10-07 13:40:10 +10001442 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1443 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 -07001444 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1445 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
1446 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
1447 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
Jesse Barnesde151cf2008-11-12 10:03:55 -08001448 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0),
Eric Anholt673a3942008-07-30 12:06:12 -07001449 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
1450 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
1451 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1452 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
Eric Anholt5a125c32008-10-22 21:40:13 -07001453 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0),
Carl Worth08d7b3d2009-04-29 14:43:54 -07001454 DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
Dave Airliec94f7022005-07-07 21:03:38 +10001455};
1456
1457int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001458
1459/**
1460 * Determine if the device really is AGP or not.
1461 *
1462 * All Intel graphics chipsets are treated as AGP, even if they are really
1463 * PCI-e.
1464 *
1465 * \param dev The device to be tested.
1466 *
1467 * \returns
1468 * A value of 1 is always retured to indictate every i9x5 is AGP.
1469 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001470int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001471{
1472 return 1;
1473}