blob: 5a49a1867b357841d24342859db0b79fab3a982d [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"
Dave Airlie28d52042009-09-21 14:33:58 +100036#include <linux/vgaarb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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
Chris Wilson0ef82af2009-09-05 18:07:06 +010083/* As a ringbuffer is only allowed to wrap between instructions, fill
84 * the tail with NOOPs.
85 */
86int i915_wrap_ring(struct drm_device *dev)
87{
88 drm_i915_private_t *dev_priv = dev->dev_private;
89 volatile unsigned int *virt;
90 int rem;
91
92 rem = dev_priv->ring.Size - dev_priv->ring.tail;
93 if (dev_priv->ring.space < rem) {
94 int ret = i915_wait_ring(dev, rem, __func__);
95 if (ret)
96 return ret;
97 }
98 dev_priv->ring.space -= rem;
99
100 virt = (unsigned int *)
101 (dev_priv->ring.virtual_start + dev_priv->ring.tail);
102 rem /= 4;
103 while (rem--)
104 *virt++ = MI_NOOP;
105
106 dev_priv->ring.tail = 0;
107
108 return 0;
109}
110
Keith Packard398c9cb2008-07-30 13:03:43 -0700111/**
112 * Sets up the hardware status page for devices that need a physical address
113 * in the register.
114 */
Eric Anholt3043c602008-10-02 12:24:47 -0700115static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700116{
117 drm_i915_private_t *dev_priv = dev->dev_private;
118 /* Program Hardware Status Page */
119 dev_priv->status_page_dmah =
120 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE, 0xffffffff);
121
122 if (!dev_priv->status_page_dmah) {
123 DRM_ERROR("Can not allocate hardware status page\n");
124 return -ENOMEM;
125 }
126 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
127 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
128
129 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
130
131 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800132 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700133 return 0;
134}
135
136/**
137 * Frees the hardware status page, whether it's a physical address or a virtual
138 * address set up by the X Server.
139 */
Eric Anholt3043c602008-10-02 12:24:47 -0700140static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700141{
142 drm_i915_private_t *dev_priv = dev->dev_private;
143 if (dev_priv->status_page_dmah) {
144 drm_pci_free(dev, dev_priv->status_page_dmah);
145 dev_priv->status_page_dmah = NULL;
146 }
147
148 if (dev_priv->status_gfx_addr) {
149 dev_priv->status_gfx_addr = 0;
150 drm_core_ioremapfree(&dev_priv->hws_map, dev);
151 }
152
153 /* Need to rewrite hardware status page */
154 I915_WRITE(HWS_PGA, 0x1ffff000);
155}
156
Dave Airlie84b1fd12007-07-11 15:53:27 +1000157void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000160 struct drm_i915_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
162
Jesse Barnes79e53942008-11-07 14:24:08 -0800163 /*
164 * We should never lose context on the ring with modesetting
165 * as we don't expose it to userspace
166 */
167 if (drm_core_check_feature(dev, DRIVER_MODESET))
168 return;
169
Jesse Barnes585fb112008-07-29 11:54:06 -0700170 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
171 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ring->space = ring->head - (ring->tail + 8);
173 if (ring->space < 0)
174 ring->space += ring->Size;
175
Dave Airlie7c1c2872008-11-28 14:22:24 +1000176 if (!dev->primary->master)
177 return;
178
179 master_priv = dev->primary->master->driver_priv;
180 if (ring->head == ring->tail && master_priv->sarea_priv)
181 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Dave Airlie84b1fd12007-07-11 15:53:27 +1000184static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000186 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* Make sure interrupts are disabled here because the uninstall ioctl
188 * may not have been called from userspace and after dev_private
189 * is freed, it's too late.
190 */
Eric Anholted4cb412008-07-29 12:10:39 -0700191 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000192 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000194 if (dev_priv->ring.virtual_start) {
195 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Eric Anholt3043c602008-10-02 12:24:47 -0700196 dev_priv->ring.virtual_start = NULL;
197 dev_priv->ring.map.handle = NULL;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000198 dev_priv->ring.map.size = 0;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Keith Packard398c9cb2008-07-30 13:03:43 -0700201 /* Clear the HWS virtual address at teardown */
202 if (I915_NEED_GFX_HWS(dev))
203 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 return 0;
206}
207
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000208static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000210 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000211 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Dave Airlie3a03ac12009-01-11 09:03:49 +1000213 master_priv->sarea = drm_getsarea(dev);
214 if (master_priv->sarea) {
215 master_priv->sarea_priv = (drm_i915_sarea_t *)
216 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
217 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800218 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000219 }
220
Eric Anholt673a3942008-07-30 12:06:12 -0700221 if (init->ring_size != 0) {
222 if (dev_priv->ring.ring_obj != NULL) {
223 i915_dma_cleanup(dev);
224 DRM_ERROR("Client tried to initialize ringbuffer in "
225 "GEM mode\n");
226 return -EINVAL;
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Eric Anholt673a3942008-07-30 12:06:12 -0700229 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Eric Anholt673a3942008-07-30 12:06:12 -0700231 dev_priv->ring.map.offset = init->ring_start;
232 dev_priv->ring.map.size = init->ring_size;
233 dev_priv->ring.map.type = 0;
234 dev_priv->ring.map.flags = 0;
235 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jesse Barnes6fb88582009-02-23 10:08:21 +1000237 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700238
239 if (dev_priv->ring.map.handle == NULL) {
240 i915_dma_cleanup(dev);
241 DRM_ERROR("can not ioremap virtual address for"
242 " ring buffer\n");
243 return -ENOMEM;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
248
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000249 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 dev_priv->back_offset = init->back_offset;
251 dev_priv->front_offset = init->front_offset;
252 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000253 if (master_priv->sarea_priv)
254 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* Allow hardware batchbuffers unless told otherwise.
257 */
258 dev_priv->allow_batchbuffer = 1;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261}
262
Dave Airlie84b1fd12007-07-11 15:53:27 +1000263static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
266
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800267 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (dev_priv->ring.map.handle == NULL) {
270 DRM_ERROR("can not ioremap virtual address for"
271 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000272 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
275 /* Program Hardware Status Page */
276 if (!dev_priv->hw_status_page) {
277 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000278 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800280 DRM_DEBUG_DRIVER("hw status page @ %p\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800281 dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000283 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700284 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000285 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700286 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800287 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 return 0;
290}
291
Eric Anholtc153f452007-09-03 12:06:45 +1000292static int i915_dma_init(struct drm_device *dev, void *data,
293 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Eric Anholtc153f452007-09-03 12:06:45 +1000295 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int retcode = 0;
297
Eric Anholtc153f452007-09-03 12:06:45 +1000298 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000300 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 case I915_CLEANUP_DMA:
303 retcode = i915_dma_cleanup(dev);
304 break;
305 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100306 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000309 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 }
312
313 return retcode;
314}
315
316/* Implement basically the same security restrictions as hardware does
317 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
318 *
319 * Most of the calculations below involve calculating the size of a
320 * particular instruction. It's important to get the size right as
321 * that tells us where the next instruction to check is. Any illegal
322 * instruction detected will be given a size of zero, which is a
323 * signal to abort the rest of the buffer.
324 */
325static int do_validate_cmd(int cmd)
326{
327 switch (((cmd >> 29) & 0x7)) {
328 case 0x0:
329 switch ((cmd >> 23) & 0x3f) {
330 case 0x0:
331 return 1; /* MI_NOOP */
332 case 0x4:
333 return 1; /* MI_FLUSH */
334 default:
335 return 0; /* disallow everything else */
336 }
337 break;
338 case 0x1:
339 return 0; /* reserved */
340 case 0x2:
341 return (cmd & 0xff) + 2; /* 2d commands */
342 case 0x3:
343 if (((cmd >> 24) & 0x1f) <= 0x18)
344 return 1;
345
346 switch ((cmd >> 24) & 0x1f) {
347 case 0x1c:
348 return 1;
349 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 case 0x3:
352 return (cmd & 0x1f) + 2;
353 case 0x4:
354 return (cmd & 0xf) + 2;
355 default:
356 return (cmd & 0xffff) + 2;
357 }
358 case 0x1e:
359 if (cmd & (1 << 23))
360 return (cmd & 0xffff) + 1;
361 else
362 return 1;
363 case 0x1f:
364 if ((cmd & (1 << 23)) == 0) /* inline vertices */
365 return (cmd & 0x1ffff) + 2;
366 else if (cmd & (1 << 17)) /* indirect random */
367 if ((cmd & 0xffff) == 0)
368 return 0; /* unknown length, too hard */
369 else
370 return (((cmd & 0xffff) + 1) / 2) + 1;
371 else
372 return 2; /* indirect sequential */
373 default:
374 return 0;
375 }
376 default:
377 return 0;
378 }
379
380 return 0;
381}
382
383static int validate_cmd(int cmd)
384{
385 int ret = do_validate_cmd(cmd);
386
Dave Airliebc5f4522007-11-05 12:50:58 +1000387/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 return ret;
390}
391
Eric Anholt201361a2009-03-11 12:30:04 -0700392static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 drm_i915_private_t *dev_priv = dev->dev_private;
395 int i;
396 RING_LOCALS;
397
Dave Airliede227f52006-01-25 15:31:43 +1100398 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000399 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100400
Alan Hourihanec29b6692006-08-12 16:29:24 +1000401 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 for (i = 0; i < dwords;) {
404 int cmd, sz;
405
Eric Anholt201361a2009-03-11 12:30:04 -0700406 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000409 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 OUT_RING(cmd);
412
413 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700414 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
Dave Airliede227f52006-01-25 15:31:43 +1100418 if (dwords & 1)
419 OUT_RING(0);
420
421 ADVANCE_LP_RING();
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
424}
425
Eric Anholt673a3942008-07-30 12:06:12 -0700426int
427i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700428 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700429 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt201361a2009-03-11 12:30:04 -0700432 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 RING_LOCALS;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
436 DRM_ERROR("Bad box %d,%d..%d,%d\n",
437 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000438 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Alan Hourihanec29b6692006-08-12 16:29:24 +1000441 if (IS_I965G(dev)) {
442 BEGIN_LP_RING(4);
443 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
444 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000445 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000446 OUT_RING(DR4);
447 ADVANCE_LP_RING();
448 } else {
449 BEGIN_LP_RING(6);
450 OUT_RING(GFX_OP_DRAWRECT_INFO);
451 OUT_RING(DR1);
452 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
453 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
454 OUT_RING(DR4);
455 OUT_RING(0);
456 ADVANCE_LP_RING();
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 return 0;
460}
461
Alan Hourihanec29b6692006-08-12 16:29:24 +1000462/* XXX: Emitting the counter should really be moved to part of the IRQ
463 * emit. For now, do it in both places:
464 */
465
Dave Airlie84b1fd12007-07-11 15:53:27 +1000466static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100467{
468 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000469 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100470 RING_LOCALS;
471
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400472 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000473 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400474 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000475 if (master_priv->sarea_priv)
476 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100477
478 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700479 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000480 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100481 OUT_RING(dev_priv->counter);
482 OUT_RING(0);
483 ADVANCE_LP_RING();
484}
485
Dave Airlie84b1fd12007-07-11 15:53:27 +1000486static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700487 drm_i915_cmdbuffer_t *cmd,
488 struct drm_clip_rect *cliprects,
489 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 int nbox = cmd->num_cliprects;
492 int i = 0, count, ret;
493
494 if (cmd->sz & 0x3) {
495 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
499 i915_kernel_lost_context(dev);
500
501 count = nbox ? nbox : 1;
502
503 for (i = 0; i < count; i++) {
504 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700505 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 cmd->DR1, cmd->DR4);
507 if (ret)
508 return ret;
509 }
510
Eric Anholt201361a2009-03-11 12:30:04 -0700511 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (ret)
513 return ret;
514 }
515
Dave Airliede227f52006-01-25 15:31:43 +1100516 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return 0;
518}
519
Dave Airlie84b1fd12007-07-11 15:53:27 +1000520static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700521 drm_i915_batchbuffer_t * batch,
522 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int nbox = batch->num_cliprects;
526 int i = 0, count;
527 RING_LOCALS;
528
529 if ((batch->start | batch->used) & 0x7) {
530 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000531 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
534 i915_kernel_lost_context(dev);
535
536 count = nbox ? nbox : 1;
537
538 for (i = 0; i < count; i++) {
539 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700540 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 batch->DR1, batch->DR4);
542 if (ret)
543 return ret;
544 }
545
Keith Packard0790d5e2008-07-30 12:28:47 -0700546 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000548 if (IS_I965G(dev)) {
549 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
550 OUT_RING(batch->start);
551 } else {
552 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
553 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ADVANCE_LP_RING();
556 } else {
557 BEGIN_LP_RING(4);
558 OUT_RING(MI_BATCH_BUFFER);
559 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
560 OUT_RING(batch->start + batch->used - 4);
561 OUT_RING(0);
562 ADVANCE_LP_RING();
563 }
564 }
565
Dave Airliede227f52006-01-25 15:31:43 +1100566 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 return 0;
569}
570
Dave Airlieaf6061a2008-05-07 12:15:39 +1000571static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000574 struct drm_i915_master_private *master_priv =
575 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 RING_LOCALS;
577
Dave Airlie7c1c2872008-11-28 14:22:24 +1000578 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400579 return -EINVAL;
580
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800581 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800582 __func__,
583 dev_priv->current_page,
584 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Dave Airlieaf6061a2008-05-07 12:15:39 +1000586 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Dave Airlieaf6061a2008-05-07 12:15:39 +1000588 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700589 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000590 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ADVANCE_LP_RING();
592
Dave Airlieaf6061a2008-05-07 12:15:39 +1000593 BEGIN_LP_RING(6);
594 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
595 OUT_RING(0);
596 if (dev_priv->current_page == 0) {
597 OUT_RING(dev_priv->back_offset);
598 dev_priv->current_page = 1;
599 } else {
600 OUT_RING(dev_priv->front_offset);
601 dev_priv->current_page = 0;
602 }
603 OUT_RING(0);
604 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000605
Dave Airlieaf6061a2008-05-07 12:15:39 +1000606 BEGIN_LP_RING(2);
607 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
608 OUT_RING(0);
609 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000610
Dave Airlie7c1c2872008-11-28 14:22:24 +1000611 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000612
Dave Airlieaf6061a2008-05-07 12:15:39 +1000613 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700614 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000615 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000616 OUT_RING(dev_priv->counter);
617 OUT_RING(0);
618 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000619
Dave Airlie7c1c2872008-11-28 14:22:24 +1000620 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Dave Airlie84b1fd12007-07-11 15:53:27 +1000624static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 drm_i915_private_t *dev_priv = dev->dev_private;
627
628 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700629 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Eric Anholtc153f452007-09-03 12:06:45 +1000632static int i915_flush_ioctl(struct drm_device *dev, void *data,
633 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Eric Anholt546b0972008-09-01 16:45:29 -0700635 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Anholt546b0972008-09-01 16:45:29 -0700637 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
638
639 mutex_lock(&dev->struct_mutex);
640 ret = i915_quiescent(dev);
641 mutex_unlock(&dev->struct_mutex);
642
643 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Eric Anholtc153f452007-09-03 12:06:45 +1000646static int i915_batchbuffer(struct drm_device *dev, void *data,
647 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000650 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000652 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000653 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700655 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 if (!dev_priv->allow_batchbuffer) {
658 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800662 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800663 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Eric Anholt546b0972008-09-01 16:45:29 -0700665 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Eric Anholt201361a2009-03-11 12:30:04 -0700667 if (batch->num_cliprects < 0)
668 return -EINVAL;
669
670 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700671 cliprects = kcalloc(batch->num_cliprects,
672 sizeof(struct drm_clip_rect),
673 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700674 if (cliprects == NULL)
675 return -ENOMEM;
676
677 ret = copy_from_user(cliprects, batch->cliprects,
678 batch->num_cliprects *
679 sizeof(struct drm_clip_rect));
680 if (ret != 0)
681 goto fail_free;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Eric Anholt546b0972008-09-01 16:45:29 -0700684 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700685 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700686 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400688 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000689 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700690
691fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700692 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return ret;
695}
696
Eric Anholtc153f452007-09-03 12:06:45 +1000697static int i915_cmdbuffer(struct drm_device *dev, void *data,
698 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000701 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000703 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000704 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700705 struct drm_clip_rect *cliprects = NULL;
706 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 int ret;
708
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800709 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800710 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Eric Anholt546b0972008-09-01 16:45:29 -0700712 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Eric Anholt201361a2009-03-11 12:30:04 -0700714 if (cmdbuf->num_cliprects < 0)
715 return -EINVAL;
716
Eric Anholt9a298b22009-03-24 12:23:04 -0700717 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700718 if (batch_data == NULL)
719 return -ENOMEM;
720
721 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
722 if (ret != 0)
723 goto fail_batch_free;
724
725 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700726 cliprects = kcalloc(cmdbuf->num_cliprects,
727 sizeof(struct drm_clip_rect), GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700728 if (cliprects == NULL)
729 goto fail_batch_free;
730
731 ret = copy_from_user(cliprects, cmdbuf->cliprects,
732 cmdbuf->num_cliprects *
733 sizeof(struct drm_clip_rect));
734 if (ret != 0)
735 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
Eric Anholt546b0972008-09-01 16:45:29 -0700738 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700739 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700740 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (ret) {
742 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000743 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400746 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000747 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700748
Eric Anholt201361a2009-03-11 12:30:04 -0700749fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700750 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000751fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700752 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700753
754 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Eric Anholtc153f452007-09-03 12:06:45 +1000757static int i915_flip_bufs(struct drm_device *dev, void *data,
758 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Eric Anholt546b0972008-09-01 16:45:29 -0700760 int ret;
761
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800762 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Eric Anholt546b0972008-09-01 16:45:29 -0700764 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Eric Anholt546b0972008-09-01 16:45:29 -0700766 mutex_lock(&dev->struct_mutex);
767 ret = i915_dispatch_flip(dev);
768 mutex_unlock(&dev->struct_mutex);
769
770 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Eric Anholtc153f452007-09-03 12:06:45 +1000773static int i915_getparam(struct drm_device *dev, void *data,
774 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000777 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int value;
779
780 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000781 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000782 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
Eric Anholtc153f452007-09-03 12:06:45 +1000785 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700787 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
789 case I915_PARAM_ALLOW_BATCHBUFFER:
790 value = dev_priv->allow_batchbuffer ? 1 : 0;
791 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100792 case I915_PARAM_LAST_DISPATCH:
793 value = READ_BREADCRUMB(dev_priv);
794 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400795 case I915_PARAM_CHIPSET_ID:
796 value = dev->pci_device;
797 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700798 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000799 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700800 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800801 case I915_PARAM_NUM_FENCES_AVAIL:
802 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800805 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800806 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000807 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
Eric Anholtc153f452007-09-03 12:06:45 +1000810 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000812 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814
815 return 0;
816}
817
Eric Anholtc153f452007-09-03 12:06:45 +1000818static int i915_setparam(struct drm_device *dev, void *data,
819 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000822 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000825 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Eric Anholtc153f452007-09-03 12:06:45 +1000829 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 break;
832 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000833 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
835 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000836 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800838 case I915_SETPARAM_NUM_USED_FENCES:
839 if (param->value > dev_priv->num_fence_regs ||
840 param->value < 0)
841 return -EINVAL;
842 /* Userspace can use first N regs */
843 dev_priv->fence_reg_start = param->value;
844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800846 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800847 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000848 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
851 return 0;
852}
853
Eric Anholtc153f452007-09-03 12:06:45 +1000854static int i915_set_status_page(struct drm_device *dev, void *data,
855 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000856{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000857 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000858 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000859
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000860 if (!I915_NEED_GFX_HWS(dev))
861 return -EINVAL;
862
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000863 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000864 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000865 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000866 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000867
Jesse Barnes79e53942008-11-07 14:24:08 -0800868 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
869 WARN(1, "tried to set status page when mode setting active\n");
870 return 0;
871 }
872
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800873 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000874
Eric Anholtc153f452007-09-03 12:06:45 +1000875 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
876
Eric Anholt8b409582007-11-22 16:40:37 +1000877 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000878 dev_priv->hws_map.size = 4*1024;
879 dev_priv->hws_map.type = 0;
880 dev_priv->hws_map.flags = 0;
881 dev_priv->hws_map.mtrr = 0;
882
Dave Airliedd0910b2009-02-25 14:49:21 +1000883 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000884 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000885 i915_dma_cleanup(dev);
886 dev_priv->status_gfx_addr = 0;
887 DRM_ERROR("can not ioremap virtual address for"
888 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000889 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000890 }
891 dev_priv->hw_status_page = dev_priv->hws_map.handle;
892
893 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700894 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800895 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800896 dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800897 DRM_DEBUG_DRIVER("load hws at %p\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800898 dev_priv->hw_status_page);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000899 return 0;
900}
901
Dave Airlieec2a4c32009-08-04 11:43:41 +1000902static int i915_get_bridge_dev(struct drm_device *dev)
903{
904 struct drm_i915_private *dev_priv = dev->dev_private;
905
906 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
907 if (!dev_priv->bridge_dev) {
908 DRM_ERROR("bridge device not found\n");
909 return -1;
910 }
911 return 0;
912}
913
Jesse Barnes79e53942008-11-07 14:24:08 -0800914/**
915 * i915_probe_agp - get AGP bootup configuration
916 * @pdev: PCI device
917 * @aperture_size: returns AGP aperture configured size
918 * @preallocated_size: returns size of BIOS preallocated AGP space
919 *
920 * Since Intel integrated graphics are UMA, the BIOS has to set aside
921 * some RAM for the framebuffer at early boot. This code figures out
922 * how much was set aside so we can use it for our own purposes.
923 */
Eric Anholt2a34f5e62009-07-02 09:30:50 -0700924static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,
925 uint32_t *preallocated_size)
Jesse Barnes79e53942008-11-07 14:24:08 -0800926{
Dave Airlieec2a4c32009-08-04 11:43:41 +1000927 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800928 u16 tmp = 0;
929 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -0800930 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -0800931
Jesse Barnes79e53942008-11-07 14:24:08 -0800932 /* Get the fb aperture size and "stolen" memory amount. */
Dave Airlieec2a4c32009-08-04 11:43:41 +1000933 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &tmp);
Jesse Barnes79e53942008-11-07 14:24:08 -0800934
935 *aperture_size = 1024 * 1024;
936 *preallocated_size = 1024 * 1024;
937
Eric Anholt60fd99e2008-12-03 22:50:02 -0800938 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800939 case PCI_DEVICE_ID_INTEL_82830_CGC:
940 case PCI_DEVICE_ID_INTEL_82845G_IG:
941 case PCI_DEVICE_ID_INTEL_82855GM_IG:
942 case PCI_DEVICE_ID_INTEL_82865_IG:
943 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
944 *aperture_size *= 64;
945 else
946 *aperture_size *= 128;
947 break;
948 default:
949 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -0800950 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -0800951 break;
952 }
953
954 /*
955 * Some of the preallocated space is taken by the GTT
956 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
957 */
Zhenyu Wang2c072452009-06-05 15:38:42 +0800958 if (IS_G4X(dev) || IS_IGD(dev) || IS_IGDNG(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -0800959 overhead = 4096;
960 else
961 overhead = (*aperture_size / 1024) + 4096;
962
Eric Anholt241fa852009-01-02 18:05:51 -0800963 switch (tmp & INTEL_GMCH_GMS_MASK) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800964 case INTEL_855_GMCH_GMS_DISABLED:
965 DRM_ERROR("video memory is disabled\n");
966 return -1;
Eric Anholt241fa852009-01-02 18:05:51 -0800967 case INTEL_855_GMCH_GMS_STOLEN_1M:
968 stolen = 1 * 1024 * 1024;
969 break;
970 case INTEL_855_GMCH_GMS_STOLEN_4M:
971 stolen = 4 * 1024 * 1024;
972 break;
973 case INTEL_855_GMCH_GMS_STOLEN_8M:
974 stolen = 8 * 1024 * 1024;
975 break;
976 case INTEL_855_GMCH_GMS_STOLEN_16M:
977 stolen = 16 * 1024 * 1024;
978 break;
979 case INTEL_855_GMCH_GMS_STOLEN_32M:
980 stolen = 32 * 1024 * 1024;
981 break;
982 case INTEL_915G_GMCH_GMS_STOLEN_48M:
983 stolen = 48 * 1024 * 1024;
984 break;
985 case INTEL_915G_GMCH_GMS_STOLEN_64M:
986 stolen = 64 * 1024 * 1024;
987 break;
988 case INTEL_GMCH_GMS_STOLEN_128M:
989 stolen = 128 * 1024 * 1024;
990 break;
991 case INTEL_GMCH_GMS_STOLEN_256M:
992 stolen = 256 * 1024 * 1024;
993 break;
994 case INTEL_GMCH_GMS_STOLEN_96M:
995 stolen = 96 * 1024 * 1024;
996 break;
997 case INTEL_GMCH_GMS_STOLEN_160M:
998 stolen = 160 * 1024 * 1024;
999 break;
1000 case INTEL_GMCH_GMS_STOLEN_224M:
1001 stolen = 224 * 1024 * 1024;
1002 break;
1003 case INTEL_GMCH_GMS_STOLEN_352M:
1004 stolen = 352 * 1024 * 1024;
1005 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08001006 default:
1007 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
Eric Anholt241fa852009-01-02 18:05:51 -08001008 tmp & INTEL_GMCH_GMS_MASK);
Jesse Barnes79e53942008-11-07 14:24:08 -08001009 return -1;
1010 }
Eric Anholt241fa852009-01-02 18:05:51 -08001011 *preallocated_size = stolen - overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -08001012
1013 return 0;
1014}
1015
Dave Airlie28d52042009-09-21 14:33:58 +10001016/* true = enable decode, false = disable decoder */
1017static unsigned int i915_vga_set_decode(void *cookie, bool state)
1018{
1019 struct drm_device *dev = cookie;
1020
1021 intel_modeset_vga_set_state(dev, state);
1022 if (state)
1023 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1024 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1025 else
1026 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1027}
1028
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001029static int i915_load_modeset_init(struct drm_device *dev,
1030 unsigned long prealloc_size,
1031 unsigned long agp_size)
Jesse Barnes79e53942008-11-07 14:24:08 -08001032{
1033 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001034 int fb_bar = IS_I9XX(dev) ? 2 : 0;
1035 int ret = 0;
1036
1037 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
1038 0xff000000;
1039
Jesse Barnes2906f022009-01-20 19:10:54 -08001040 if (IS_MOBILE(dev) || IS_I9XX(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08001041 dev_priv->cursor_needs_physical = true;
1042 else
1043 dev_priv->cursor_needs_physical = false;
1044
Jesse Barnes2906f022009-01-20 19:10:54 -08001045 if (IS_I965G(dev) || IS_G33(dev))
1046 dev_priv->cursor_needs_physical = false;
1047
Jesse Barnes79e53942008-11-07 14:24:08 -08001048 /* Basic memrange allocator for stolen space (aka vram) */
1049 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
1050
Eric Anholt13f4c432009-05-12 15:27:36 -07001051 /* Let GEM Manage from end of prealloc space to end of aperture.
1052 *
1053 * However, leave one page at the end still bound to the scratch page.
1054 * There are a number of places where the hardware apparently
1055 * prefetches past the end of the object, and we've seen multiple
1056 * hangs with the GPU head pointer stuck in a batchbuffer bound
1057 * at the last page of the aperture. One page should be enough to
1058 * keep any prefetching inside of the aperture.
1059 */
1060 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001061
1062 ret = i915_gem_init_ringbuffer(dev);
1063 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001064 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001065
Jesse Barnes79e53942008-11-07 14:24:08 -08001066 /* Allow hardware batchbuffers unless told otherwise.
1067 */
1068 dev_priv->allow_batchbuffer = 1;
1069
1070 ret = intel_init_bios(dev);
1071 if (ret)
1072 DRM_INFO("failed to find VBIOS tables\n");
1073
Dave Airlie28d52042009-09-21 14:33:58 +10001074 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1075 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1076 if (ret)
1077 goto destroy_ringbuffer;
1078
Jesse Barnes79e53942008-11-07 14:24:08 -08001079 ret = drm_irq_install(dev);
1080 if (ret)
1081 goto destroy_ringbuffer;
1082
Jesse Barnes79e53942008-11-07 14:24:08 -08001083 /* Always safe in the mode setting case. */
1084 /* FIXME: do pre/post-mode set stuff in core KMS code */
1085 dev->vblank_disable_allowed = 1;
1086
1087 /*
1088 * Initialize the hardware status page IRQ location.
1089 */
1090
1091 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1092
1093 intel_modeset_init(dev);
1094
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001095 drm_helper_initial_config(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001096
Jesse Barnes79e53942008-11-07 14:24:08 -08001097 return 0;
1098
Jesse Barnes79e53942008-11-07 14:24:08 -08001099destroy_ringbuffer:
1100 i915_gem_cleanup_ringbuffer(dev);
1101out:
1102 return ret;
1103}
1104
Dave Airlie7c1c2872008-11-28 14:22:24 +10001105int i915_master_create(struct drm_device *dev, struct drm_master *master)
1106{
1107 struct drm_i915_master_private *master_priv;
1108
Eric Anholt9a298b22009-03-24 12:23:04 -07001109 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001110 if (!master_priv)
1111 return -ENOMEM;
1112
1113 master->driver_priv = master_priv;
1114 return 0;
1115}
1116
1117void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1118{
1119 struct drm_i915_master_private *master_priv = master->driver_priv;
1120
1121 if (!master_priv)
1122 return;
1123
Eric Anholt9a298b22009-03-24 12:23:04 -07001124 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001125
1126 master->driver_priv = NULL;
1127}
1128
Shaohua Li7662c8b2009-06-26 11:23:55 +08001129static void i915_get_mem_freq(struct drm_device *dev)
1130{
1131 drm_i915_private_t *dev_priv = dev->dev_private;
1132 u32 tmp;
1133
1134 if (!IS_IGD(dev))
1135 return;
1136
1137 tmp = I915_READ(CLKCFG);
1138
1139 switch (tmp & CLKCFG_FSB_MASK) {
1140 case CLKCFG_FSB_533:
1141 dev_priv->fsb_freq = 533; /* 133*4 */
1142 break;
1143 case CLKCFG_FSB_800:
1144 dev_priv->fsb_freq = 800; /* 200*4 */
1145 break;
1146 case CLKCFG_FSB_667:
1147 dev_priv->fsb_freq = 667; /* 167*4 */
1148 break;
1149 case CLKCFG_FSB_400:
1150 dev_priv->fsb_freq = 400; /* 100*4 */
1151 break;
1152 }
1153
1154 switch (tmp & CLKCFG_MEM_MASK) {
1155 case CLKCFG_MEM_533:
1156 dev_priv->mem_freq = 533;
1157 break;
1158 case CLKCFG_MEM_667:
1159 dev_priv->mem_freq = 667;
1160 break;
1161 case CLKCFG_MEM_800:
1162 dev_priv->mem_freq = 800;
1163 break;
1164 }
1165}
1166
Jesse Barnes79e53942008-11-07 14:24:08 -08001167/**
1168 * i915_driver_load - setup chip and create an initial config
1169 * @dev: DRM device
1170 * @flags: startup flags
1171 *
1172 * The driver load routine has to do several things:
1173 * - drive output discovery via intel_modeset_init()
1174 * - initialize the memory manager
1175 * - allocate initial config memory
1176 * - setup the DRM framebuffer with the allocated memory
1177 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001178int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001179{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001180 struct drm_i915_private *dev_priv = dev->dev_private;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001181 resource_size_t base, size;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001182 int ret = 0, mmio_bar = IS_I9XX(dev) ? 0 : 1;
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001183 uint32_t agp_size, prealloc_size;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001184
Dave Airlie22eae942005-11-10 22:16:34 +11001185 /* i915 has 4 more counters */
1186 dev->counters += 4;
1187 dev->types[6] = _DRM_STAT_IRQ;
1188 dev->types[7] = _DRM_STAT_PRIMARY;
1189 dev->types[8] = _DRM_STAT_SECONDARY;
1190 dev->types[9] = _DRM_STAT_DMA;
1191
Eric Anholt9a298b22009-03-24 12:23:04 -07001192 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001193 if (dev_priv == NULL)
1194 return -ENOMEM;
1195
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001196 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001197 dev_priv->dev = dev;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001198
1199 /* Add register map (needed for suspend/resume) */
1200 base = drm_get_resource_start(dev, mmio_bar);
1201 size = drm_get_resource_len(dev, mmio_bar);
1202
Dave Airlieec2a4c32009-08-04 11:43:41 +10001203 if (i915_get_bridge_dev(dev)) {
1204 ret = -EIO;
1205 goto free_priv;
1206 }
1207
Eric Anholt3043c602008-10-02 12:24:47 -07001208 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001209 if (!dev_priv->regs) {
1210 DRM_ERROR("failed to map registers\n");
1211 ret = -EIO;
Dave Airlieec2a4c32009-08-04 11:43:41 +10001212 goto put_bridge;
Jesse Barnes79e53942008-11-07 14:24:08 -08001213 }
Eric Anholted4cb412008-07-29 12:10:39 -07001214
Eric Anholtab657db12009-01-23 12:57:47 -08001215 dev_priv->mm.gtt_mapping =
1216 io_mapping_create_wc(dev->agp->base,
1217 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001218 if (dev_priv->mm.gtt_mapping == NULL) {
1219 ret = -EIO;
1220 goto out_rmmap;
1221 }
1222
Eric Anholtab657db12009-01-23 12:57:47 -08001223 /* Set up a WC MTRR for non-PAT systems. This is more common than
1224 * one would think, because the kernel disables PAT on first
1225 * generation Core chips because WC PAT gets overridden by a UC
1226 * MTRR if present. Even if a UC MTRR isn't present.
1227 */
1228 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1229 dev->agp->agp_info.aper_size *
1230 1024 * 1024,
1231 MTRR_TYPE_WRCOMB, 1);
1232 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001233 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001234 "performance may suffer.\n");
1235 }
1236
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001237 ret = i915_probe_agp(dev, &agp_size, &prealloc_size);
1238 if (ret)
1239 goto out_iomapfree;
1240
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001241 dev_priv->wq = create_workqueue("i915");
1242 if (dev_priv->wq == NULL) {
1243 DRM_ERROR("Failed to create our workqueue.\n");
1244 ret = -ENOMEM;
1245 goto out_iomapfree;
1246 }
1247
Dave Airlieac5c4e72008-12-19 15:38:34 +10001248 /* enable GEM by default */
1249 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10001250
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001251 if (prealloc_size > agp_size * 3 / 4) {
1252 DRM_ERROR("Detected broken video BIOS with %d/%dkB of video "
1253 "memory stolen.\n",
1254 prealloc_size / 1024, agp_size / 1024);
1255 DRM_ERROR("Disabling GEM. (try reducing stolen memory or "
1256 "updating the BIOS to fix).\n");
1257 dev_priv->has_gem = 0;
1258 }
1259
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001260 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001261 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Zhenyu Wang036a4a72009-06-08 14:40:19 +08001262 if (IS_G4X(dev) || IS_IGDNG(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001263 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001264 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001265 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001266
Eric Anholt673a3942008-07-30 12:06:12 -07001267 i915_gem_load(dev);
1268
Keith Packard398c9cb2008-07-30 13:03:43 -07001269 /* Init HWS */
1270 if (!I915_NEED_GFX_HWS(dev)) {
1271 ret = i915_init_phys_hws(dev);
1272 if (ret != 0)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001273 goto out_workqueue_free;
Keith Packard398c9cb2008-07-30 13:03:43 -07001274 }
Eric Anholted4cb412008-07-29 12:10:39 -07001275
Shaohua Li7662c8b2009-06-26 11:23:55 +08001276 i915_get_mem_freq(dev);
1277
Eric Anholted4cb412008-07-29 12:10:39 -07001278 /* On the 945G/GM, the chipset reports the MSI capability on the
1279 * integrated graphics even though the support isn't actually there
1280 * according to the published specs. It doesn't appear to function
1281 * correctly in testing on 945G.
1282 * This may be a side effect of MSI having been made available for PEG
1283 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001284 *
1285 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001286 * be lost or delayed, but we use them anyways to avoid
1287 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001288 */
Keith Packardb60678a2008-12-08 11:12:28 -08001289 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001290 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001291
1292 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001293 spin_lock_init(&dev_priv->error_lock);
Jesse Barnes79e53942008-11-07 14:24:08 -08001294 dev_priv->user_irq_refcount = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001295
Keith Packard52440212008-11-18 09:30:25 -08001296 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1297
1298 if (ret) {
1299 (void) i915_driver_unload(dev);
1300 return ret;
1301 }
1302
Jesse Barnes79e53942008-11-07 14:24:08 -08001303 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001304 ret = i915_load_modeset_init(dev, prealloc_size, agp_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001305 if (ret < 0) {
1306 DRM_ERROR("failed to init modeset\n");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001307 goto out_workqueue_free;
Jesse Barnes79e53942008-11-07 14:24:08 -08001308 }
1309 }
1310
Matthew Garrett74a365b2009-03-19 21:35:39 +00001311 /* Must be done after probing outputs */
Zhenyu Wange170b032009-06-05 15:38:40 +08001312 /* FIXME: verify on IGDNG */
1313 if (!IS_IGDNG(dev))
1314 intel_opregion_init(dev, 0);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001315
Jesse Barnes79e53942008-11-07 14:24:08 -08001316 return 0;
1317
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001318out_workqueue_free:
1319 destroy_workqueue(dev_priv->wq);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001320out_iomapfree:
1321 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001322out_rmmap:
1323 iounmap(dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001324put_bridge:
1325 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001326free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001327 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001328 return ret;
1329}
1330
1331int i915_driver_unload(struct drm_device *dev)
1332{
1333 struct drm_i915_private *dev_priv = dev->dev_private;
1334
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001335 destroy_workqueue(dev_priv->wq);
1336
Eric Anholtab657db12009-01-23 12:57:47 -08001337 io_mapping_free(dev_priv->mm.gtt_mapping);
1338 if (dev_priv->mm.gtt_mtrr >= 0) {
1339 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1340 dev->agp->agp_info.aper_size * 1024 * 1024);
1341 dev_priv->mm.gtt_mtrr = -1;
1342 }
1343
Jesse Barnes79e53942008-11-07 14:24:08 -08001344 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001345 drm_irq_uninstall(dev);
Dave Airlie28d52042009-09-21 14:33:58 +10001346 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001347 }
1348
Eric Anholted4cb412008-07-29 12:10:39 -07001349 if (dev->pdev->msi_enabled)
1350 pci_disable_msi(dev->pdev);
1351
Eric Anholt3043c602008-10-02 12:24:47 -07001352 if (dev_priv->regs != NULL)
1353 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001354
Zhenyu Wange170b032009-06-05 15:38:40 +08001355 if (!IS_IGDNG(dev))
1356 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001357
Jesse Barnes79e53942008-11-07 14:24:08 -08001358 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1359 intel_modeset_cleanup(dev);
1360
Dave Airlie71acb5e2008-12-30 20:31:46 +10001361 i915_gem_free_all_phys_object(dev);
1362
Jesse Barnes79e53942008-11-07 14:24:08 -08001363 mutex_lock(&dev->struct_mutex);
1364 i915_gem_cleanup_ringbuffer(dev);
1365 mutex_unlock(&dev->struct_mutex);
1366 drm_mm_takedown(&dev_priv->vram);
1367 i915_gem_lastclose(dev);
1368 }
1369
Dave Airlieec2a4c32009-08-04 11:43:41 +10001370 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07001371 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001372
Dave Airlie22eae942005-11-10 22:16:34 +11001373 return 0;
1374}
1375
Eric Anholt673a3942008-07-30 12:06:12 -07001376int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1377{
1378 struct drm_i915_file_private *i915_file_priv;
1379
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001380 DRM_DEBUG_DRIVER("\n");
Eric Anholt673a3942008-07-30 12:06:12 -07001381 i915_file_priv = (struct drm_i915_file_private *)
Eric Anholt9a298b22009-03-24 12:23:04 -07001382 kmalloc(sizeof(*i915_file_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001383
1384 if (!i915_file_priv)
1385 return -ENOMEM;
1386
1387 file_priv->driver_priv = i915_file_priv;
1388
Eric Anholtb9624422009-06-03 07:27:35 +00001389 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001390
1391 return 0;
1392}
1393
Jesse Barnes79e53942008-11-07 14:24:08 -08001394/**
1395 * i915_driver_lastclose - clean up after all DRM clients have exited
1396 * @dev: DRM device
1397 *
1398 * Take care of cleaning up after all DRM clients have exited. In the
1399 * mode setting case, we want to restore the kernel's initial mode (just
1400 * in case the last client left us in a bad state).
1401 *
1402 * Additionally, in the non-mode setting case, we'll tear down the AGP
1403 * and DMA structures, since the kernel won't be using them, and clea
1404 * up any GEM state.
1405 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001406void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001408 drm_i915_private_t *dev_priv = dev->dev_private;
1409
Jesse Barnes79e53942008-11-07 14:24:08 -08001410 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001411 drm_fb_helper_restore();
Dave Airlie144a75f2008-03-30 07:53:58 +10001412 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001413 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001414
Eric Anholt673a3942008-07-30 12:06:12 -07001415 i915_gem_lastclose(dev);
1416
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001417 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001418 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001419
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001420 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Eric Anholt6c340ea2007-08-25 20:23:09 +10001423void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001425 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001426 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08001427 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1428 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Eric Anholt673a3942008-07-30 12:06:12 -07001431void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1432{
1433 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1434
Eric Anholt9a298b22009-03-24 12:23:04 -07001435 kfree(i915_file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001436}
1437
Eric Anholtc153f452007-09-03 12:06:45 +10001438struct drm_ioctl_desc i915_ioctls[] = {
1439 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1440 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1441 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1442 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1443 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1444 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1445 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1446 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1447 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1448 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1449 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1450 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1451 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1452 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1453 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1454 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001455 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 +10001456 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 -07001457 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
1458 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1459 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1460 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
1461 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
Dave Airlie2bdf00b2008-10-07 13:40:10 +10001462 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1463 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 -07001464 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1465 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
1466 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
1467 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
Jesse Barnesde151cf2008-11-12 10:03:55 -08001468 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0),
Eric Anholt673a3942008-07-30 12:06:12 -07001469 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
1470 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
1471 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1472 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
Eric Anholt5a125c32008-10-22 21:40:13 -07001473 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0),
Carl Worth08d7b3d2009-04-29 14:43:54 -07001474 DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
Dave Airliec94f7022005-07-07 21:03:38 +10001475};
1476
1477int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001478
1479/**
1480 * Determine if the device really is AGP or not.
1481 *
1482 * All Intel graphics chipsets are treated as AGP, even if they are really
1483 * PCI-e.
1484 *
1485 * \param dev The device to be tested.
1486 *
1487 * \returns
1488 * A value of 1 is always retured to indictate every i9x5 is AGP.
1489 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001490int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001491{
1492 return 1;
1493}