blob: 3afe361ec55261e69c6e17db60ed45d03ec5d9b9 [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"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010036#include "i915_trace.h"
Dave Airlie28d52042009-09-21 14:33:58 +100037#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080038#include <linux/acpi.h>
39#include <linux/pnp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* Really want an OS-independent resettable timer. Would like to have
42 * this loop run for (eg) 3 sec, but have the timer reset every time
43 * the head pointer changes, so that EBUSY only happens if the ring
44 * actually stalls for (eg) 3 seconds.
45 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100046int i915_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 drm_i915_private_t *dev_priv = dev->dev_private;
49 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
Keith Packardd3a6d442008-07-30 12:21:20 -070050 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
51 u32 last_acthd = I915_READ(acthd_reg);
52 u32 acthd;
Jesse Barnes585fb112008-07-29 11:54:06 -070053 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int i;
55
Chris Wilson1c5d22f2009-08-25 11:15:50 +010056 trace_i915_ring_wait_begin (dev);
57
Keith Packardd3a6d442008-07-30 12:21:20 -070058 for (i = 0; i < 100000; i++) {
Jesse Barnes585fb112008-07-29 11:54:06 -070059 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Keith Packardd3a6d442008-07-30 12:21:20 -070060 acthd = I915_READ(acthd_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 ring->space = ring->head - (ring->tail + 8);
62 if (ring->space < 0)
63 ring->space += ring->Size;
Chris Wilson1c5d22f2009-08-25 11:15:50 +010064 if (ring->space >= n) {
65 trace_i915_ring_wait_end (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +010067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Chris Wilson98787c02009-03-06 23:27:52 +000069 if (dev->primary->master) {
70 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
71 if (master_priv->sarea_priv)
72 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
73 }
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (ring->head != last_head)
77 i = 0;
Keith Packardd3a6d442008-07-30 12:21:20 -070078 if (acthd != last_acthd)
79 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 last_head = ring->head;
Keith Packardd3a6d442008-07-30 12:21:20 -070082 last_acthd = acthd;
83 msleep_interruptible(10);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86
Chris Wilson1c5d22f2009-08-25 11:15:50 +010087 trace_i915_ring_wait_end (dev);
Eric Anholt20caafa2007-08-25 19:22:43 +100088 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Chris Wilson0ef82af2009-09-05 18:07:06 +010091/* As a ringbuffer is only allowed to wrap between instructions, fill
92 * the tail with NOOPs.
93 */
94int i915_wrap_ring(struct drm_device *dev)
95{
96 drm_i915_private_t *dev_priv = dev->dev_private;
97 volatile unsigned int *virt;
98 int rem;
99
100 rem = dev_priv->ring.Size - dev_priv->ring.tail;
101 if (dev_priv->ring.space < rem) {
102 int ret = i915_wait_ring(dev, rem, __func__);
103 if (ret)
104 return ret;
105 }
106 dev_priv->ring.space -= rem;
107
108 virt = (unsigned int *)
109 (dev_priv->ring.virtual_start + dev_priv->ring.tail);
110 rem /= 4;
111 while (rem--)
112 *virt++ = MI_NOOP;
113
114 dev_priv->ring.tail = 0;
115
116 return 0;
117}
118
Keith Packard398c9cb2008-07-30 13:03:43 -0700119/**
120 * Sets up the hardware status page for devices that need a physical address
121 * in the register.
122 */
Eric Anholt3043c602008-10-02 12:24:47 -0700123static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700124{
125 drm_i915_private_t *dev_priv = dev->dev_private;
126 /* Program Hardware Status Page */
127 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800128 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -0700129
130 if (!dev_priv->status_page_dmah) {
131 DRM_ERROR("Can not allocate hardware status page\n");
132 return -ENOMEM;
133 }
134 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
135 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
136
137 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
138
Zhenyu Wang9b974cc2010-01-05 11:25:06 +0800139 if (IS_I965G(dev))
140 dev_priv->dma_status_page |= (dev_priv->dma_status_page >> 28) &
141 0xf0;
142
Keith Packard398c9cb2008-07-30 13:03:43 -0700143 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800144 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700145 return 0;
146}
147
148/**
149 * Frees the hardware status page, whether it's a physical address or a virtual
150 * address set up by the X Server.
151 */
Eric Anholt3043c602008-10-02 12:24:47 -0700152static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700153{
154 drm_i915_private_t *dev_priv = dev->dev_private;
155 if (dev_priv->status_page_dmah) {
156 drm_pci_free(dev, dev_priv->status_page_dmah);
157 dev_priv->status_page_dmah = NULL;
158 }
159
160 if (dev_priv->status_gfx_addr) {
161 dev_priv->status_gfx_addr = 0;
162 drm_core_ioremapfree(&dev_priv->hws_map, dev);
163 }
164
165 /* Need to rewrite hardware status page */
166 I915_WRITE(HWS_PGA, 0x1ffff000);
167}
168
Dave Airlie84b1fd12007-07-11 15:53:27 +1000169void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000172 struct drm_i915_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
174
Jesse Barnes79e53942008-11-07 14:24:08 -0800175 /*
176 * We should never lose context on the ring with modesetting
177 * as we don't expose it to userspace
178 */
179 if (drm_core_check_feature(dev, DRIVER_MODESET))
180 return;
181
Jesse Barnes585fb112008-07-29 11:54:06 -0700182 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
183 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 ring->space = ring->head - (ring->tail + 8);
185 if (ring->space < 0)
186 ring->space += ring->Size;
187
Dave Airlie7c1c2872008-11-28 14:22:24 +1000188 if (!dev->primary->master)
189 return;
190
191 master_priv = dev->primary->master->driver_priv;
192 if (ring->head == ring->tail && master_priv->sarea_priv)
193 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Dave Airlie84b1fd12007-07-11 15:53:27 +1000196static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000198 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Make sure interrupts are disabled here because the uninstall ioctl
200 * may not have been called from userspace and after dev_private
201 * is freed, it's too late.
202 */
Eric Anholted4cb412008-07-29 12:10:39 -0700203 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000206 if (dev_priv->ring.virtual_start) {
207 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Eric Anholt3043c602008-10-02 12:24:47 -0700208 dev_priv->ring.virtual_start = NULL;
209 dev_priv->ring.map.handle = NULL;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000210 dev_priv->ring.map.size = 0;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Keith Packard398c9cb2008-07-30 13:03:43 -0700213 /* Clear the HWS virtual address at teardown */
214 if (I915_NEED_GFX_HWS(dev))
215 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 return 0;
218}
219
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000220static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000222 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000223 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Dave Airlie3a03ac12009-01-11 09:03:49 +1000225 master_priv->sarea = drm_getsarea(dev);
226 if (master_priv->sarea) {
227 master_priv->sarea_priv = (drm_i915_sarea_t *)
228 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
229 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800230 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000231 }
232
Eric Anholt673a3942008-07-30 12:06:12 -0700233 if (init->ring_size != 0) {
234 if (dev_priv->ring.ring_obj != NULL) {
235 i915_dma_cleanup(dev);
236 DRM_ERROR("Client tried to initialize ringbuffer in "
237 "GEM mode\n");
238 return -EINVAL;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Eric Anholt673a3942008-07-30 12:06:12 -0700241 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Eric Anholt673a3942008-07-30 12:06:12 -0700243 dev_priv->ring.map.offset = init->ring_start;
244 dev_priv->ring.map.size = init->ring_size;
245 dev_priv->ring.map.type = 0;
246 dev_priv->ring.map.flags = 0;
247 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jesse Barnes6fb88582009-02-23 10:08:21 +1000249 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700250
251 if (dev_priv->ring.map.handle == NULL) {
252 i915_dma_cleanup(dev);
253 DRM_ERROR("can not ioremap virtual address for"
254 " ring buffer\n");
255 return -ENOMEM;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
259 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
260
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000261 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 dev_priv->back_offset = init->back_offset;
263 dev_priv->front_offset = init->front_offset;
264 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000265 if (master_priv->sarea_priv)
266 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* Allow hardware batchbuffers unless told otherwise.
269 */
270 dev_priv->allow_batchbuffer = 1;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273}
274
Dave Airlie84b1fd12007-07-11 15:53:27 +1000275static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
278
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800279 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (dev_priv->ring.map.handle == NULL) {
282 DRM_ERROR("can not ioremap virtual address for"
283 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000284 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
287 /* Program Hardware Status Page */
288 if (!dev_priv->hw_status_page) {
289 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000290 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800292 DRM_DEBUG_DRIVER("hw status page @ %p\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800293 dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000295 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700296 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000297 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700298 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800299 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 return 0;
302}
303
Eric Anholtc153f452007-09-03 12:06:45 +1000304static int i915_dma_init(struct drm_device *dev, void *data,
305 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Eric Anholtc153f452007-09-03 12:06:45 +1000307 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int retcode = 0;
309
Eric Anholtc153f452007-09-03 12:06:45 +1000310 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000312 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
314 case I915_CLEANUP_DMA:
315 retcode = i915_dma_cleanup(dev);
316 break;
317 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100318 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
320 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000321 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
323 }
324
325 return retcode;
326}
327
328/* Implement basically the same security restrictions as hardware does
329 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
330 *
331 * Most of the calculations below involve calculating the size of a
332 * particular instruction. It's important to get the size right as
333 * that tells us where the next instruction to check is. Any illegal
334 * instruction detected will be given a size of zero, which is a
335 * signal to abort the rest of the buffer.
336 */
337static int do_validate_cmd(int cmd)
338{
339 switch (((cmd >> 29) & 0x7)) {
340 case 0x0:
341 switch ((cmd >> 23) & 0x3f) {
342 case 0x0:
343 return 1; /* MI_NOOP */
344 case 0x4:
345 return 1; /* MI_FLUSH */
346 default:
347 return 0; /* disallow everything else */
348 }
349 break;
350 case 0x1:
351 return 0; /* reserved */
352 case 0x2:
353 return (cmd & 0xff) + 2; /* 2d commands */
354 case 0x3:
355 if (((cmd >> 24) & 0x1f) <= 0x18)
356 return 1;
357
358 switch ((cmd >> 24) & 0x1f) {
359 case 0x1c:
360 return 1;
361 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000362 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 case 0x3:
364 return (cmd & 0x1f) + 2;
365 case 0x4:
366 return (cmd & 0xf) + 2;
367 default:
368 return (cmd & 0xffff) + 2;
369 }
370 case 0x1e:
371 if (cmd & (1 << 23))
372 return (cmd & 0xffff) + 1;
373 else
374 return 1;
375 case 0x1f:
376 if ((cmd & (1 << 23)) == 0) /* inline vertices */
377 return (cmd & 0x1ffff) + 2;
378 else if (cmd & (1 << 17)) /* indirect random */
379 if ((cmd & 0xffff) == 0)
380 return 0; /* unknown length, too hard */
381 else
382 return (((cmd & 0xffff) + 1) / 2) + 1;
383 else
384 return 2; /* indirect sequential */
385 default:
386 return 0;
387 }
388 default:
389 return 0;
390 }
391
392 return 0;
393}
394
395static int validate_cmd(int cmd)
396{
397 int ret = do_validate_cmd(cmd);
398
Dave Airliebc5f4522007-11-05 12:50:58 +1000399/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 return ret;
402}
403
Eric Anholt201361a2009-03-11 12:30:04 -0700404static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 drm_i915_private_t *dev_priv = dev->dev_private;
407 int i;
408 RING_LOCALS;
409
Dave Airliede227f52006-01-25 15:31:43 +1100410 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000411 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100412
Alan Hourihanec29b6692006-08-12 16:29:24 +1000413 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 for (i = 0; i < dwords;) {
416 int cmd, sz;
417
Eric Anholt201361a2009-03-11 12:30:04 -0700418 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000421 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 OUT_RING(cmd);
424
425 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700426 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
Dave Airliede227f52006-01-25 15:31:43 +1100430 if (dwords & 1)
431 OUT_RING(0);
432
433 ADVANCE_LP_RING();
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 0;
436}
437
Eric Anholt673a3942008-07-30 12:06:12 -0700438int
439i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700440 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700441 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt201361a2009-03-11 12:30:04 -0700444 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 RING_LOCALS;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
448 DRM_ERROR("Bad box %d,%d..%d,%d\n",
449 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000450 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
Alan Hourihanec29b6692006-08-12 16:29:24 +1000453 if (IS_I965G(dev)) {
454 BEGIN_LP_RING(4);
455 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
456 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000457 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000458 OUT_RING(DR4);
459 ADVANCE_LP_RING();
460 } else {
461 BEGIN_LP_RING(6);
462 OUT_RING(GFX_OP_DRAWRECT_INFO);
463 OUT_RING(DR1);
464 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
465 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
466 OUT_RING(DR4);
467 OUT_RING(0);
468 ADVANCE_LP_RING();
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 return 0;
472}
473
Alan Hourihanec29b6692006-08-12 16:29:24 +1000474/* XXX: Emitting the counter should really be moved to part of the IRQ
475 * emit. For now, do it in both places:
476 */
477
Dave Airlie84b1fd12007-07-11 15:53:27 +1000478static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100479{
480 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000481 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100482 RING_LOCALS;
483
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400484 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000485 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400486 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000487 if (master_priv->sarea_priv)
488 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100489
490 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700491 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000492 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100493 OUT_RING(dev_priv->counter);
494 OUT_RING(0);
495 ADVANCE_LP_RING();
496}
497
Dave Airlie84b1fd12007-07-11 15:53:27 +1000498static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700499 drm_i915_cmdbuffer_t *cmd,
500 struct drm_clip_rect *cliprects,
501 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 int nbox = cmd->num_cliprects;
504 int i = 0, count, ret;
505
506 if (cmd->sz & 0x3) {
507 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000508 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 i915_kernel_lost_context(dev);
512
513 count = nbox ? nbox : 1;
514
515 for (i = 0; i < count; i++) {
516 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700517 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 cmd->DR1, cmd->DR4);
519 if (ret)
520 return ret;
521 }
522
Eric Anholt201361a2009-03-11 12:30:04 -0700523 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (ret)
525 return ret;
526 }
527
Dave Airliede227f52006-01-25 15:31:43 +1100528 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
530}
531
Dave Airlie84b1fd12007-07-11 15:53:27 +1000532static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700533 drm_i915_batchbuffer_t * batch,
534 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int nbox = batch->num_cliprects;
538 int i = 0, count;
539 RING_LOCALS;
540
541 if ((batch->start | batch->used) & 0x7) {
542 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000543 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
546 i915_kernel_lost_context(dev);
547
548 count = nbox ? nbox : 1;
549
550 for (i = 0; i < count; i++) {
551 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700552 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 batch->DR1, batch->DR4);
554 if (ret)
555 return ret;
556 }
557
Keith Packard0790d5e2008-07-30 12:28:47 -0700558 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000560 if (IS_I965G(dev)) {
561 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
562 OUT_RING(batch->start);
563 } else {
564 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
565 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ADVANCE_LP_RING();
568 } else {
569 BEGIN_LP_RING(4);
570 OUT_RING(MI_BATCH_BUFFER);
571 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
572 OUT_RING(batch->start + batch->used - 4);
573 OUT_RING(0);
574 ADVANCE_LP_RING();
575 }
576 }
577
Dave Airliede227f52006-01-25 15:31:43 +1100578 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return 0;
581}
582
Dave Airlieaf6061a2008-05-07 12:15:39 +1000583static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000586 struct drm_i915_master_private *master_priv =
587 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 RING_LOCALS;
589
Dave Airlie7c1c2872008-11-28 14:22:24 +1000590 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400591 return -EINVAL;
592
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800593 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800594 __func__,
595 dev_priv->current_page,
596 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Dave Airlieaf6061a2008-05-07 12:15:39 +1000598 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Dave Airlieaf6061a2008-05-07 12:15:39 +1000600 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700601 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000602 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 ADVANCE_LP_RING();
604
Dave Airlieaf6061a2008-05-07 12:15:39 +1000605 BEGIN_LP_RING(6);
606 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
607 OUT_RING(0);
608 if (dev_priv->current_page == 0) {
609 OUT_RING(dev_priv->back_offset);
610 dev_priv->current_page = 1;
611 } else {
612 OUT_RING(dev_priv->front_offset);
613 dev_priv->current_page = 0;
614 }
615 OUT_RING(0);
616 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000617
Dave Airlieaf6061a2008-05-07 12:15:39 +1000618 BEGIN_LP_RING(2);
619 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
620 OUT_RING(0);
621 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000622
Dave Airlie7c1c2872008-11-28 14:22:24 +1000623 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000624
Dave Airlieaf6061a2008-05-07 12:15:39 +1000625 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700626 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000627 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000628 OUT_RING(dev_priv->counter);
629 OUT_RING(0);
630 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000631
Dave Airlie7c1c2872008-11-28 14:22:24 +1000632 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Dave Airlie84b1fd12007-07-11 15:53:27 +1000636static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 drm_i915_private_t *dev_priv = dev->dev_private;
639
640 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700641 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Eric Anholtc153f452007-09-03 12:06:45 +1000644static int i915_flush_ioctl(struct drm_device *dev, void *data,
645 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Eric Anholt546b0972008-09-01 16:45:29 -0700647 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Eric Anholt546b0972008-09-01 16:45:29 -0700649 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
650
651 mutex_lock(&dev->struct_mutex);
652 ret = i915_quiescent(dev);
653 mutex_unlock(&dev->struct_mutex);
654
655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Eric Anholtc153f452007-09-03 12:06:45 +1000658static int i915_batchbuffer(struct drm_device *dev, void *data,
659 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000662 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000664 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000665 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700667 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 if (!dev_priv->allow_batchbuffer) {
670 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000671 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800674 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800675 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Eric Anholt546b0972008-09-01 16:45:29 -0700677 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Eric Anholt201361a2009-03-11 12:30:04 -0700679 if (batch->num_cliprects < 0)
680 return -EINVAL;
681
682 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700683 cliprects = kcalloc(batch->num_cliprects,
684 sizeof(struct drm_clip_rect),
685 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700686 if (cliprects == NULL)
687 return -ENOMEM;
688
689 ret = copy_from_user(cliprects, batch->cliprects,
690 batch->num_cliprects *
691 sizeof(struct drm_clip_rect));
692 if (ret != 0)
693 goto fail_free;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric Anholt546b0972008-09-01 16:45:29 -0700696 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700697 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700698 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400700 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000701 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700702
703fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700704 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return ret;
707}
708
Eric Anholtc153f452007-09-03 12:06:45 +1000709static int i915_cmdbuffer(struct drm_device *dev, void *data,
710 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000713 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000715 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000716 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700717 struct drm_clip_rect *cliprects = NULL;
718 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 int ret;
720
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800721 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800722 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Eric Anholt546b0972008-09-01 16:45:29 -0700724 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Eric Anholt201361a2009-03-11 12:30:04 -0700726 if (cmdbuf->num_cliprects < 0)
727 return -EINVAL;
728
Eric Anholt9a298b22009-03-24 12:23:04 -0700729 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700730 if (batch_data == NULL)
731 return -ENOMEM;
732
733 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
734 if (ret != 0)
735 goto fail_batch_free;
736
737 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700738 cliprects = kcalloc(cmdbuf->num_cliprects,
739 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000740 if (cliprects == NULL) {
741 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700742 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000743 }
Eric Anholt201361a2009-03-11 12:30:04 -0700744
745 ret = copy_from_user(cliprects, cmdbuf->cliprects,
746 cmdbuf->num_cliprects *
747 sizeof(struct drm_clip_rect));
748 if (ret != 0)
749 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Eric Anholt546b0972008-09-01 16:45:29 -0700752 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700753 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700754 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (ret) {
756 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000757 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400760 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000761 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700762
Eric Anholt201361a2009-03-11 12:30:04 -0700763fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700764 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000765fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700766 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700767
768 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Eric Anholtc153f452007-09-03 12:06:45 +1000771static int i915_flip_bufs(struct drm_device *dev, void *data,
772 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Eric Anholt546b0972008-09-01 16:45:29 -0700774 int ret;
775
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800776 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Eric Anholt546b0972008-09-01 16:45:29 -0700778 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Eric Anholt546b0972008-09-01 16:45:29 -0700780 mutex_lock(&dev->struct_mutex);
781 ret = i915_dispatch_flip(dev);
782 mutex_unlock(&dev->struct_mutex);
783
784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Eric Anholtc153f452007-09-03 12:06:45 +1000787static int i915_getparam(struct drm_device *dev, void *data,
788 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000791 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int value;
793
794 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000795 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000796 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Eric Anholtc153f452007-09-03 12:06:45 +1000799 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700801 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
803 case I915_PARAM_ALLOW_BATCHBUFFER:
804 value = dev_priv->allow_batchbuffer ? 1 : 0;
805 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100806 case I915_PARAM_LAST_DISPATCH:
807 value = READ_BREADCRUMB(dev_priv);
808 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400809 case I915_PARAM_CHIPSET_ID:
810 value = dev->pci_device;
811 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700812 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000813 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700814 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800815 case I915_PARAM_NUM_FENCES_AVAIL:
816 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
817 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200818 case I915_PARAM_HAS_OVERLAY:
819 value = dev_priv->overlay ? 1 : 0;
820 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800821 case I915_PARAM_HAS_PAGEFLIPPING:
822 value = 1;
823 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500824 case I915_PARAM_HAS_EXECBUF2:
825 /* depends on GEM */
826 value = dev_priv->has_gem;
827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800829 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -0500830 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000831 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Eric Anholtc153f452007-09-03 12:06:45 +1000834 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000836 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
839 return 0;
840}
841
Eric Anholtc153f452007-09-03 12:06:45 +1000842static int i915_setparam(struct drm_device *dev, void *data,
843 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000846 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000849 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000850 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Eric Anholtc153f452007-09-03 12:06:45 +1000853 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 break;
856 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000857 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000860 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800862 case I915_SETPARAM_NUM_USED_FENCES:
863 if (param->value > dev_priv->num_fence_regs ||
864 param->value < 0)
865 return -EINVAL;
866 /* Userspace can use first N regs */
867 dev_priv->fence_reg_start = param->value;
868 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800870 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800871 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000872 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 return 0;
876}
877
Eric Anholtc153f452007-09-03 12:06:45 +1000878static int i915_set_status_page(struct drm_device *dev, void *data,
879 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000880{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000881 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000882 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000883
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000884 if (!I915_NEED_GFX_HWS(dev))
885 return -EINVAL;
886
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000887 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000888 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000889 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000890 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000891
Jesse Barnes79e53942008-11-07 14:24:08 -0800892 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
893 WARN(1, "tried to set status page when mode setting active\n");
894 return 0;
895 }
896
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800897 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000898
Eric Anholtc153f452007-09-03 12:06:45 +1000899 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
900
Eric Anholt8b409582007-11-22 16:40:37 +1000901 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000902 dev_priv->hws_map.size = 4*1024;
903 dev_priv->hws_map.type = 0;
904 dev_priv->hws_map.flags = 0;
905 dev_priv->hws_map.mtrr = 0;
906
Dave Airliedd0910b2009-02-25 14:49:21 +1000907 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000908 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000909 i915_dma_cleanup(dev);
910 dev_priv->status_gfx_addr = 0;
911 DRM_ERROR("can not ioremap virtual address for"
912 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000913 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000914 }
915 dev_priv->hw_status_page = dev_priv->hws_map.handle;
916
917 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700918 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800919 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800920 dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800921 DRM_DEBUG_DRIVER("load hws at %p\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800922 dev_priv->hw_status_page);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000923 return 0;
924}
925
Dave Airlieec2a4c32009-08-04 11:43:41 +1000926static int i915_get_bridge_dev(struct drm_device *dev)
927{
928 struct drm_i915_private *dev_priv = dev->dev_private;
929
930 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
931 if (!dev_priv->bridge_dev) {
932 DRM_ERROR("bridge device not found\n");
933 return -1;
934 }
935 return 0;
936}
937
Zhenyu Wangc48044112009-12-17 14:48:43 +0800938#define MCHBAR_I915 0x44
939#define MCHBAR_I965 0x48
940#define MCHBAR_SIZE (4*4096)
941
942#define DEVEN_REG 0x54
943#define DEVEN_MCHBAR_EN (1 << 28)
944
945/* Allocate space for the MCH regs if needed, return nonzero on error */
946static int
947intel_alloc_mchbar_resource(struct drm_device *dev)
948{
949 drm_i915_private_t *dev_priv = dev->dev_private;
950 int reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
951 u32 temp_lo, temp_hi = 0;
952 u64 mchbar_addr;
953 int ret = 0;
954
955 if (IS_I965G(dev))
956 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
957 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
958 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
959
960 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
961#ifdef CONFIG_PNP
962 if (mchbar_addr &&
963 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE)) {
964 ret = 0;
965 goto out;
966 }
967#endif
968
969 /* Get some space for it */
970 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus, &dev_priv->mch_res,
971 MCHBAR_SIZE, MCHBAR_SIZE,
972 PCIBIOS_MIN_MEM,
973 0, pcibios_align_resource,
974 dev_priv->bridge_dev);
975 if (ret) {
976 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
977 dev_priv->mch_res.start = 0;
978 goto out;
979 }
980
981 if (IS_I965G(dev))
982 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
983 upper_32_bits(dev_priv->mch_res.start));
984
985 pci_write_config_dword(dev_priv->bridge_dev, reg,
986 lower_32_bits(dev_priv->mch_res.start));
987out:
988 return ret;
989}
990
991/* Setup MCHBAR if possible, return true if we should disable it again */
992static void
993intel_setup_mchbar(struct drm_device *dev)
994{
995 drm_i915_private_t *dev_priv = dev->dev_private;
996 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
997 u32 temp;
998 bool enabled;
999
1000 dev_priv->mchbar_need_disable = false;
1001
1002 if (IS_I915G(dev) || IS_I915GM(dev)) {
1003 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1004 enabled = !!(temp & DEVEN_MCHBAR_EN);
1005 } else {
1006 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1007 enabled = temp & 1;
1008 }
1009
1010 /* If it's already enabled, don't have to do anything */
1011 if (enabled)
1012 return;
1013
1014 if (intel_alloc_mchbar_resource(dev))
1015 return;
1016
1017 dev_priv->mchbar_need_disable = true;
1018
1019 /* Space is allocated or reserved, so enable it. */
1020 if (IS_I915G(dev) || IS_I915GM(dev)) {
1021 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1022 temp | DEVEN_MCHBAR_EN);
1023 } else {
1024 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1025 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1026 }
1027}
1028
1029static void
1030intel_teardown_mchbar(struct drm_device *dev)
1031{
1032 drm_i915_private_t *dev_priv = dev->dev_private;
1033 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
1034 u32 temp;
1035
1036 if (dev_priv->mchbar_need_disable) {
1037 if (IS_I915G(dev) || IS_I915GM(dev)) {
1038 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1039 temp &= ~DEVEN_MCHBAR_EN;
1040 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1041 } else {
1042 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1043 temp &= ~1;
1044 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1045 }
1046 }
1047
1048 if (dev_priv->mch_res.start)
1049 release_resource(&dev_priv->mch_res);
1050}
1051
Jesse Barnes79e53942008-11-07 14:24:08 -08001052/**
1053 * i915_probe_agp - get AGP bootup configuration
1054 * @pdev: PCI device
1055 * @aperture_size: returns AGP aperture configured size
1056 * @preallocated_size: returns size of BIOS preallocated AGP space
1057 *
1058 * Since Intel integrated graphics are UMA, the BIOS has to set aside
1059 * some RAM for the framebuffer at early boot. This code figures out
1060 * how much was set aside so we can use it for our own purposes.
1061 */
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001062static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,
Jesse Barnes80824002009-09-10 15:28:06 -07001063 uint32_t *preallocated_size,
1064 uint32_t *start)
Jesse Barnes79e53942008-11-07 14:24:08 -08001065{
Dave Airlieec2a4c32009-08-04 11:43:41 +10001066 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001067 u16 tmp = 0;
1068 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -08001069 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -08001070
Jesse Barnes79e53942008-11-07 14:24:08 -08001071 /* Get the fb aperture size and "stolen" memory amount. */
Dave Airlieec2a4c32009-08-04 11:43:41 +10001072 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &tmp);
Jesse Barnes79e53942008-11-07 14:24:08 -08001073
1074 *aperture_size = 1024 * 1024;
1075 *preallocated_size = 1024 * 1024;
1076
Eric Anholt60fd99e2008-12-03 22:50:02 -08001077 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001078 case PCI_DEVICE_ID_INTEL_82830_CGC:
1079 case PCI_DEVICE_ID_INTEL_82845G_IG:
1080 case PCI_DEVICE_ID_INTEL_82855GM_IG:
1081 case PCI_DEVICE_ID_INTEL_82865_IG:
1082 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
1083 *aperture_size *= 64;
1084 else
1085 *aperture_size *= 128;
1086 break;
1087 default:
1088 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -08001089 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001090 break;
1091 }
1092
1093 /*
1094 * Some of the preallocated space is taken by the GTT
1095 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
1096 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001097 if (IS_G4X(dev) || IS_PINEVIEW(dev) || IS_IRONLAKE(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -08001098 overhead = 4096;
1099 else
1100 overhead = (*aperture_size / 1024) + 4096;
1101
Eric Anholt241fa852009-01-02 18:05:51 -08001102 switch (tmp & INTEL_GMCH_GMS_MASK) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001103 case INTEL_855_GMCH_GMS_DISABLED:
1104 DRM_ERROR("video memory is disabled\n");
1105 return -1;
Eric Anholt241fa852009-01-02 18:05:51 -08001106 case INTEL_855_GMCH_GMS_STOLEN_1M:
1107 stolen = 1 * 1024 * 1024;
1108 break;
1109 case INTEL_855_GMCH_GMS_STOLEN_4M:
1110 stolen = 4 * 1024 * 1024;
1111 break;
1112 case INTEL_855_GMCH_GMS_STOLEN_8M:
1113 stolen = 8 * 1024 * 1024;
1114 break;
1115 case INTEL_855_GMCH_GMS_STOLEN_16M:
1116 stolen = 16 * 1024 * 1024;
1117 break;
1118 case INTEL_855_GMCH_GMS_STOLEN_32M:
1119 stolen = 32 * 1024 * 1024;
1120 break;
1121 case INTEL_915G_GMCH_GMS_STOLEN_48M:
1122 stolen = 48 * 1024 * 1024;
1123 break;
1124 case INTEL_915G_GMCH_GMS_STOLEN_64M:
1125 stolen = 64 * 1024 * 1024;
1126 break;
1127 case INTEL_GMCH_GMS_STOLEN_128M:
1128 stolen = 128 * 1024 * 1024;
1129 break;
1130 case INTEL_GMCH_GMS_STOLEN_256M:
1131 stolen = 256 * 1024 * 1024;
1132 break;
1133 case INTEL_GMCH_GMS_STOLEN_96M:
1134 stolen = 96 * 1024 * 1024;
1135 break;
1136 case INTEL_GMCH_GMS_STOLEN_160M:
1137 stolen = 160 * 1024 * 1024;
1138 break;
1139 case INTEL_GMCH_GMS_STOLEN_224M:
1140 stolen = 224 * 1024 * 1024;
1141 break;
1142 case INTEL_GMCH_GMS_STOLEN_352M:
1143 stolen = 352 * 1024 * 1024;
1144 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08001145 default:
1146 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
Eric Anholt241fa852009-01-02 18:05:51 -08001147 tmp & INTEL_GMCH_GMS_MASK);
Jesse Barnes79e53942008-11-07 14:24:08 -08001148 return -1;
1149 }
Eric Anholt241fa852009-01-02 18:05:51 -08001150 *preallocated_size = stolen - overhead;
Jesse Barnes80824002009-09-10 15:28:06 -07001151 *start = overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -08001152
1153 return 0;
1154}
1155
Jesse Barnes80824002009-09-10 15:28:06 -07001156#define PTE_ADDRESS_MASK 0xfffff000
1157#define PTE_ADDRESS_MASK_HIGH 0x000000f0 /* i915+ */
1158#define PTE_MAPPING_TYPE_UNCACHED (0 << 1)
1159#define PTE_MAPPING_TYPE_DCACHE (1 << 1) /* i830 only */
1160#define PTE_MAPPING_TYPE_CACHED (3 << 1)
1161#define PTE_MAPPING_TYPE_MASK (3 << 1)
1162#define PTE_VALID (1 << 0)
1163
1164/**
1165 * i915_gtt_to_phys - take a GTT address and turn it into a physical one
1166 * @dev: drm device
1167 * @gtt_addr: address to translate
1168 *
1169 * Some chip functions require allocations from stolen space but need the
1170 * physical address of the memory in question. We use this routine
1171 * to get a physical address suitable for register programming from a given
1172 * GTT address.
1173 */
1174static unsigned long i915_gtt_to_phys(struct drm_device *dev,
1175 unsigned long gtt_addr)
1176{
1177 unsigned long *gtt;
1178 unsigned long entry, phys;
1179 int gtt_bar = IS_I9XX(dev) ? 0 : 1;
1180 int gtt_offset, gtt_size;
1181
1182 if (IS_I965G(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001183 if (IS_G4X(dev) || IS_IRONLAKE(dev)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001184 gtt_offset = 2*1024*1024;
1185 gtt_size = 2*1024*1024;
1186 } else {
1187 gtt_offset = 512*1024;
1188 gtt_size = 512*1024;
1189 }
1190 } else {
1191 gtt_bar = 3;
1192 gtt_offset = 0;
1193 gtt_size = pci_resource_len(dev->pdev, gtt_bar);
1194 }
1195
1196 gtt = ioremap_wc(pci_resource_start(dev->pdev, gtt_bar) + gtt_offset,
1197 gtt_size);
1198 if (!gtt) {
1199 DRM_ERROR("ioremap of GTT failed\n");
1200 return 0;
1201 }
1202
1203 entry = *(volatile u32 *)(gtt + (gtt_addr / 1024));
1204
Zhao Yakui44d98a62009-10-09 11:39:40 +08001205 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, PTE: 0x%08lx\n", gtt_addr, entry);
Jesse Barnes80824002009-09-10 15:28:06 -07001206
1207 /* Mask out these reserved bits on this hardware. */
1208 if (!IS_I9XX(dev) || IS_I915G(dev) || IS_I915GM(dev) ||
1209 IS_I945G(dev) || IS_I945GM(dev)) {
1210 entry &= ~PTE_ADDRESS_MASK_HIGH;
1211 }
1212
1213 /* If it's not a mapping type we know, then bail. */
1214 if ((entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_UNCACHED &&
1215 (entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_CACHED) {
1216 iounmap(gtt);
1217 return 0;
1218 }
1219
1220 if (!(entry & PTE_VALID)) {
1221 DRM_ERROR("bad GTT entry in stolen space\n");
1222 iounmap(gtt);
1223 return 0;
1224 }
1225
1226 iounmap(gtt);
1227
1228 phys =(entry & PTE_ADDRESS_MASK) |
1229 ((uint64_t)(entry & PTE_ADDRESS_MASK_HIGH) << (32 - 4));
1230
Zhao Yakui44d98a62009-10-09 11:39:40 +08001231 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, phys addr: 0x%08lx\n", gtt_addr, phys);
Jesse Barnes80824002009-09-10 15:28:06 -07001232
1233 return phys;
1234}
1235
1236static void i915_warn_stolen(struct drm_device *dev)
1237{
1238 DRM_ERROR("not enough stolen space for compressed buffer, disabling\n");
1239 DRM_ERROR("hint: you may be able to increase stolen memory size in the BIOS to avoid this\n");
1240}
1241
1242static void i915_setup_compression(struct drm_device *dev, int size)
1243{
1244 struct drm_i915_private *dev_priv = dev->dev_private;
1245 struct drm_mm_node *compressed_fb, *compressed_llb;
Andrew Morton29bd0ae2009-11-17 14:08:52 -08001246 unsigned long cfb_base;
1247 unsigned long ll_base = 0;
Jesse Barnes80824002009-09-10 15:28:06 -07001248
1249 /* Leave 1M for line length buffer & misc. */
1250 compressed_fb = drm_mm_search_free(&dev_priv->vram, size, 4096, 0);
1251 if (!compressed_fb) {
1252 i915_warn_stolen(dev);
1253 return;
1254 }
1255
1256 compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
1257 if (!compressed_fb) {
1258 i915_warn_stolen(dev);
1259 return;
1260 }
1261
Jesse Barnes74dff282009-09-14 15:39:40 -07001262 cfb_base = i915_gtt_to_phys(dev, compressed_fb->start);
1263 if (!cfb_base) {
1264 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1265 drm_mm_put_block(compressed_fb);
Jesse Barnes80824002009-09-10 15:28:06 -07001266 }
1267
Jesse Barnes74dff282009-09-14 15:39:40 -07001268 if (!IS_GM45(dev)) {
1269 compressed_llb = drm_mm_search_free(&dev_priv->vram, 4096,
1270 4096, 0);
1271 if (!compressed_llb) {
1272 i915_warn_stolen(dev);
1273 return;
1274 }
1275
1276 compressed_llb = drm_mm_get_block(compressed_llb, 4096, 4096);
1277 if (!compressed_llb) {
1278 i915_warn_stolen(dev);
1279 return;
1280 }
1281
1282 ll_base = i915_gtt_to_phys(dev, compressed_llb->start);
1283 if (!ll_base) {
1284 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1285 drm_mm_put_block(compressed_fb);
1286 drm_mm_put_block(compressed_llb);
1287 }
Jesse Barnes80824002009-09-10 15:28:06 -07001288 }
1289
1290 dev_priv->cfb_size = size;
1291
Jesse Barnes74dff282009-09-14 15:39:40 -07001292 if (IS_GM45(dev)) {
1293 g4x_disable_fbc(dev);
1294 I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
1295 } else {
1296 i8xx_disable_fbc(dev);
1297 I915_WRITE(FBC_CFB_BASE, cfb_base);
1298 I915_WRITE(FBC_LL_BASE, ll_base);
Jesse Barnes80824002009-09-10 15:28:06 -07001299 }
1300
Jesse Barnes80824002009-09-10 15:28:06 -07001301 DRM_DEBUG("FBC base 0x%08lx, ll base 0x%08lx, size %dM\n", cfb_base,
1302 ll_base, size >> 20);
Jesse Barnes80824002009-09-10 15:28:06 -07001303}
1304
Dave Airlie28d52042009-09-21 14:33:58 +10001305/* true = enable decode, false = disable decoder */
1306static unsigned int i915_vga_set_decode(void *cookie, bool state)
1307{
1308 struct drm_device *dev = cookie;
1309
1310 intel_modeset_vga_set_state(dev, state);
1311 if (state)
1312 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1313 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1314 else
1315 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1316}
1317
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001318static int i915_load_modeset_init(struct drm_device *dev,
Jesse Barnes80824002009-09-10 15:28:06 -07001319 unsigned long prealloc_start,
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001320 unsigned long prealloc_size,
1321 unsigned long agp_size)
Jesse Barnes79e53942008-11-07 14:24:08 -08001322{
1323 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001324 int fb_bar = IS_I9XX(dev) ? 2 : 0;
1325 int ret = 0;
1326
1327 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
1328 0xff000000;
1329
Jesse Barnes79e53942008-11-07 14:24:08 -08001330 /* Basic memrange allocator for stolen space (aka vram) */
1331 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
Jesse Barnes80824002009-09-10 15:28:06 -07001332 DRM_INFO("set up %ldM of stolen space\n", prealloc_size / (1024*1024));
Jesse Barnes79e53942008-11-07 14:24:08 -08001333
Ben Gamari11ed50e2009-09-14 17:48:45 -04001334 /* We're off and running w/KMS */
1335 dev_priv->mm.suspended = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001336
Eric Anholt13f4c432009-05-12 15:27:36 -07001337 /* Let GEM Manage from end of prealloc space to end of aperture.
1338 *
1339 * However, leave one page at the end still bound to the scratch page.
1340 * There are a number of places where the hardware apparently
1341 * prefetches past the end of the object, and we've seen multiple
1342 * hangs with the GPU head pointer stuck in a batchbuffer bound
1343 * at the last page of the aperture. One page should be enough to
1344 * keep any prefetching inside of the aperture.
1345 */
1346 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001347
Ben Gamari11ed50e2009-09-14 17:48:45 -04001348 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001349 ret = i915_gem_init_ringbuffer(dev);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001350 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001351 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001352 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001353
Jesse Barnes80824002009-09-10 15:28:06 -07001354 /* Try to set up FBC with a reasonable compressed buffer size */
Shaohua Li9216d442009-10-10 15:20:55 +08001355 if (I915_HAS_FBC(dev) && i915_powersave) {
Jesse Barnes80824002009-09-10 15:28:06 -07001356 int cfb_size;
1357
1358 /* Try to get an 8M buffer... */
1359 if (prealloc_size > (9*1024*1024))
1360 cfb_size = 8*1024*1024;
1361 else /* fall back to 7/8 of the stolen space */
1362 cfb_size = prealloc_size * 7 / 8;
1363 i915_setup_compression(dev, cfb_size);
1364 }
1365
Jesse Barnes79e53942008-11-07 14:24:08 -08001366 /* Allow hardware batchbuffers unless told otherwise.
1367 */
1368 dev_priv->allow_batchbuffer = 1;
1369
1370 ret = intel_init_bios(dev);
1371 if (ret)
1372 DRM_INFO("failed to find VBIOS tables\n");
1373
Dave Airlie28d52042009-09-21 14:33:58 +10001374 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1375 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1376 if (ret)
1377 goto destroy_ringbuffer;
1378
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001379 intel_modeset_init(dev);
1380
Jesse Barnes79e53942008-11-07 14:24:08 -08001381 ret = drm_irq_install(dev);
1382 if (ret)
1383 goto destroy_ringbuffer;
1384
Jesse Barnes79e53942008-11-07 14:24:08 -08001385 /* Always safe in the mode setting case. */
1386 /* FIXME: do pre/post-mode set stuff in core KMS code */
1387 dev->vblank_disable_allowed = 1;
1388
1389 /*
1390 * Initialize the hardware status page IRQ location.
1391 */
1392
1393 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1394
Jesse Barnes7a1fb5d2009-03-27 13:05:19 -07001395 drm_helper_initial_config(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001396
Jesse Barnes79e53942008-11-07 14:24:08 -08001397 return 0;
1398
Jesse Barnes79e53942008-11-07 14:24:08 -08001399destroy_ringbuffer:
1400 i915_gem_cleanup_ringbuffer(dev);
1401out:
1402 return ret;
1403}
1404
Dave Airlie7c1c2872008-11-28 14:22:24 +10001405int i915_master_create(struct drm_device *dev, struct drm_master *master)
1406{
1407 struct drm_i915_master_private *master_priv;
1408
Eric Anholt9a298b22009-03-24 12:23:04 -07001409 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001410 if (!master_priv)
1411 return -ENOMEM;
1412
1413 master->driver_priv = master_priv;
1414 return 0;
1415}
1416
1417void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1418{
1419 struct drm_i915_master_private *master_priv = master->driver_priv;
1420
1421 if (!master_priv)
1422 return;
1423
Eric Anholt9a298b22009-03-24 12:23:04 -07001424 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001425
1426 master->driver_priv = NULL;
1427}
1428
Shaohua Li7662c8b2009-06-26 11:23:55 +08001429static void i915_get_mem_freq(struct drm_device *dev)
1430{
1431 drm_i915_private_t *dev_priv = dev->dev_private;
1432 u32 tmp;
1433
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001434 if (!IS_PINEVIEW(dev))
Shaohua Li7662c8b2009-06-26 11:23:55 +08001435 return;
1436
1437 tmp = I915_READ(CLKCFG);
1438
1439 switch (tmp & CLKCFG_FSB_MASK) {
1440 case CLKCFG_FSB_533:
1441 dev_priv->fsb_freq = 533; /* 133*4 */
1442 break;
1443 case CLKCFG_FSB_800:
1444 dev_priv->fsb_freq = 800; /* 200*4 */
1445 break;
1446 case CLKCFG_FSB_667:
1447 dev_priv->fsb_freq = 667; /* 167*4 */
1448 break;
1449 case CLKCFG_FSB_400:
1450 dev_priv->fsb_freq = 400; /* 100*4 */
1451 break;
1452 }
1453
1454 switch (tmp & CLKCFG_MEM_MASK) {
1455 case CLKCFG_MEM_533:
1456 dev_priv->mem_freq = 533;
1457 break;
1458 case CLKCFG_MEM_667:
1459 dev_priv->mem_freq = 667;
1460 break;
1461 case CLKCFG_MEM_800:
1462 dev_priv->mem_freq = 800;
1463 break;
1464 }
1465}
1466
Jesse Barnes79e53942008-11-07 14:24:08 -08001467/**
1468 * i915_driver_load - setup chip and create an initial config
1469 * @dev: DRM device
1470 * @flags: startup flags
1471 *
1472 * The driver load routine has to do several things:
1473 * - drive output discovery via intel_modeset_init()
1474 * - initialize the memory manager
1475 * - allocate initial config memory
1476 * - setup the DRM framebuffer with the allocated memory
1477 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001478int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001479{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001480 struct drm_i915_private *dev_priv = dev->dev_private;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001481 resource_size_t base, size;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001482 int ret = 0, mmio_bar;
Jesse Barnes80824002009-09-10 15:28:06 -07001483 uint32_t agp_size, prealloc_size, prealloc_start;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001484
Dave Airlie22eae942005-11-10 22:16:34 +11001485 /* i915 has 4 more counters */
1486 dev->counters += 4;
1487 dev->types[6] = _DRM_STAT_IRQ;
1488 dev->types[7] = _DRM_STAT_PRIMARY;
1489 dev->types[8] = _DRM_STAT_SECONDARY;
1490 dev->types[9] = _DRM_STAT_DMA;
1491
Eric Anholt9a298b22009-03-24 12:23:04 -07001492 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001493 if (dev_priv == NULL)
1494 return -ENOMEM;
1495
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001496 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001497 dev_priv->dev = dev;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001498 dev_priv->info = (struct intel_device_info *) flags;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001499
1500 /* Add register map (needed for suspend/resume) */
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001501 mmio_bar = IS_I9XX(dev) ? 0 : 1;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001502 base = drm_get_resource_start(dev, mmio_bar);
1503 size = drm_get_resource_len(dev, mmio_bar);
1504
Dave Airlieec2a4c32009-08-04 11:43:41 +10001505 if (i915_get_bridge_dev(dev)) {
1506 ret = -EIO;
1507 goto free_priv;
1508 }
1509
Eric Anholt3043c602008-10-02 12:24:47 -07001510 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001511 if (!dev_priv->regs) {
1512 DRM_ERROR("failed to map registers\n");
1513 ret = -EIO;
Dave Airlieec2a4c32009-08-04 11:43:41 +10001514 goto put_bridge;
Jesse Barnes79e53942008-11-07 14:24:08 -08001515 }
Eric Anholted4cb412008-07-29 12:10:39 -07001516
Eric Anholtab657db12009-01-23 12:57:47 -08001517 dev_priv->mm.gtt_mapping =
1518 io_mapping_create_wc(dev->agp->base,
1519 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001520 if (dev_priv->mm.gtt_mapping == NULL) {
1521 ret = -EIO;
1522 goto out_rmmap;
1523 }
1524
Eric Anholtab657db12009-01-23 12:57:47 -08001525 /* Set up a WC MTRR for non-PAT systems. This is more common than
1526 * one would think, because the kernel disables PAT on first
1527 * generation Core chips because WC PAT gets overridden by a UC
1528 * MTRR if present. Even if a UC MTRR isn't present.
1529 */
1530 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1531 dev->agp->agp_info.aper_size *
1532 1024 * 1024,
1533 MTRR_TYPE_WRCOMB, 1);
1534 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001535 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001536 "performance may suffer.\n");
1537 }
1538
Jesse Barnes80824002009-09-10 15:28:06 -07001539 ret = i915_probe_agp(dev, &agp_size, &prealloc_size, &prealloc_start);
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001540 if (ret)
1541 goto out_iomapfree;
1542
Chris Wilsonaed5f1d2009-10-14 13:40:04 +01001543 dev_priv->wq = create_singlethread_workqueue("i915");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001544 if (dev_priv->wq == NULL) {
1545 DRM_ERROR("Failed to create our workqueue.\n");
1546 ret = -ENOMEM;
1547 goto out_iomapfree;
1548 }
1549
Dave Airlieac5c4e72008-12-19 15:38:34 +10001550 /* enable GEM by default */
1551 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10001552
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001553 if (prealloc_size > agp_size * 3 / 4) {
1554 DRM_ERROR("Detected broken video BIOS with %d/%dkB of video "
1555 "memory stolen.\n",
1556 prealloc_size / 1024, agp_size / 1024);
1557 DRM_ERROR("Disabling GEM. (try reducing stolen memory or "
1558 "updating the BIOS to fix).\n");
1559 dev_priv->has_gem = 0;
1560 }
1561
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001562 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001563 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001564 if (IS_G4X(dev) || IS_IRONLAKE(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001565 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001566 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001567 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001568
Zhenyu Wangc48044112009-12-17 14:48:43 +08001569 /* Try to make sure MCHBAR is enabled before poking at it */
1570 intel_setup_mchbar(dev);
1571
Eric Anholt673a3942008-07-30 12:06:12 -07001572 i915_gem_load(dev);
1573
Keith Packard398c9cb2008-07-30 13:03:43 -07001574 /* Init HWS */
1575 if (!I915_NEED_GFX_HWS(dev)) {
1576 ret = i915_init_phys_hws(dev);
1577 if (ret != 0)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001578 goto out_workqueue_free;
Keith Packard398c9cb2008-07-30 13:03:43 -07001579 }
Eric Anholted4cb412008-07-29 12:10:39 -07001580
Shaohua Li7662c8b2009-06-26 11:23:55 +08001581 i915_get_mem_freq(dev);
1582
Eric Anholted4cb412008-07-29 12:10:39 -07001583 /* On the 945G/GM, the chipset reports the MSI capability on the
1584 * integrated graphics even though the support isn't actually there
1585 * according to the published specs. It doesn't appear to function
1586 * correctly in testing on 945G.
1587 * This may be a side effect of MSI having been made available for PEG
1588 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001589 *
1590 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001591 * be lost or delayed, but we use them anyways to avoid
1592 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001593 */
Keith Packardb60678a2008-12-08 11:12:28 -08001594 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001595 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001596
1597 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001598 spin_lock_init(&dev_priv->error_lock);
Jesse Barnes79e53942008-11-07 14:24:08 -08001599 dev_priv->user_irq_refcount = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001600 dev_priv->trace_irq_seqno = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001601
Keith Packard52440212008-11-18 09:30:25 -08001602 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1603
1604 if (ret) {
1605 (void) i915_driver_unload(dev);
1606 return ret;
1607 }
1608
Ben Gamari11ed50e2009-09-14 17:48:45 -04001609 /* Start out suspended */
1610 dev_priv->mm.suspended = 1;
1611
Jesse Barnes79e53942008-11-07 14:24:08 -08001612 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001613 ret = i915_load_modeset_init(dev, prealloc_start,
1614 prealloc_size, agp_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001615 if (ret < 0) {
1616 DRM_ERROR("failed to init modeset\n");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001617 goto out_workqueue_free;
Jesse Barnes79e53942008-11-07 14:24:08 -08001618 }
1619 }
1620
Matthew Garrett74a365b2009-03-19 21:35:39 +00001621 /* Must be done after probing outputs */
Zhao Yakui01c66882009-10-28 05:10:00 +00001622 intel_opregion_init(dev, 0);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001623
Ben Gamarif65d9422009-09-14 17:48:44 -04001624 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
1625 (unsigned long) dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001626 return 0;
1627
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001628out_workqueue_free:
1629 destroy_workqueue(dev_priv->wq);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001630out_iomapfree:
1631 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001632out_rmmap:
1633 iounmap(dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001634put_bridge:
1635 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001636free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001637 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001638 return ret;
1639}
1640
1641int i915_driver_unload(struct drm_device *dev)
1642{
1643 struct drm_i915_private *dev_priv = dev->dev_private;
1644
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001645 destroy_workqueue(dev_priv->wq);
Ben Gamarif65d9422009-09-14 17:48:44 -04001646 del_timer_sync(&dev_priv->hangcheck_timer);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001647
Eric Anholtab657db12009-01-23 12:57:47 -08001648 io_mapping_free(dev_priv->mm.gtt_mapping);
1649 if (dev_priv->mm.gtt_mtrr >= 0) {
1650 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1651 dev->agp->agp_info.aper_size * 1024 * 1024);
1652 dev_priv->mm.gtt_mtrr = -1;
1653 }
1654
Jesse Barnes79e53942008-11-07 14:24:08 -08001655 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Zhao Yakui6363ee62009-11-24 09:48:44 +08001656 /*
1657 * free the memory space allocated for the child device
1658 * config parsed from VBT
1659 */
1660 if (dev_priv->child_dev && dev_priv->child_dev_num) {
1661 kfree(dev_priv->child_dev);
1662 dev_priv->child_dev = NULL;
1663 dev_priv->child_dev_num = 0;
1664 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001665 drm_irq_uninstall(dev);
Dave Airlie28d52042009-09-21 14:33:58 +10001666 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001667 }
1668
Eric Anholted4cb412008-07-29 12:10:39 -07001669 if (dev->pdev->msi_enabled)
1670 pci_disable_msi(dev->pdev);
1671
Eric Anholt3043c602008-10-02 12:24:47 -07001672 if (dev_priv->regs != NULL)
1673 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001674
Zhao Yakui01c66882009-10-28 05:10:00 +00001675 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001676
Jesse Barnes79e53942008-11-07 14:24:08 -08001677 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1678 intel_modeset_cleanup(dev);
1679
Dave Airlie71acb5e2008-12-30 20:31:46 +10001680 i915_gem_free_all_phys_object(dev);
1681
Jesse Barnes79e53942008-11-07 14:24:08 -08001682 mutex_lock(&dev->struct_mutex);
1683 i915_gem_cleanup_ringbuffer(dev);
1684 mutex_unlock(&dev->struct_mutex);
1685 drm_mm_takedown(&dev_priv->vram);
1686 i915_gem_lastclose(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001687
1688 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001689 }
1690
Zhenyu Wangc48044112009-12-17 14:48:43 +08001691 intel_teardown_mchbar(dev);
1692
Dave Airlieec2a4c32009-08-04 11:43:41 +10001693 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07001694 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001695
Dave Airlie22eae942005-11-10 22:16:34 +11001696 return 0;
1697}
1698
Eric Anholt673a3942008-07-30 12:06:12 -07001699int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1700{
1701 struct drm_i915_file_private *i915_file_priv;
1702
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001703 DRM_DEBUG_DRIVER("\n");
Eric Anholt673a3942008-07-30 12:06:12 -07001704 i915_file_priv = (struct drm_i915_file_private *)
Eric Anholt9a298b22009-03-24 12:23:04 -07001705 kmalloc(sizeof(*i915_file_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001706
1707 if (!i915_file_priv)
1708 return -ENOMEM;
1709
1710 file_priv->driver_priv = i915_file_priv;
1711
Eric Anholtb9624422009-06-03 07:27:35 +00001712 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001713
1714 return 0;
1715}
1716
Jesse Barnes79e53942008-11-07 14:24:08 -08001717/**
1718 * i915_driver_lastclose - clean up after all DRM clients have exited
1719 * @dev: DRM device
1720 *
1721 * Take care of cleaning up after all DRM clients have exited. In the
1722 * mode setting case, we want to restore the kernel's initial mode (just
1723 * in case the last client left us in a bad state).
1724 *
1725 * Additionally, in the non-mode setting case, we'll tear down the AGP
1726 * and DMA structures, since the kernel won't be using them, and clea
1727 * up any GEM state.
1728 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001729void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001731 drm_i915_private_t *dev_priv = dev->dev_private;
1732
Jesse Barnes79e53942008-11-07 14:24:08 -08001733 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001734 drm_fb_helper_restore();
Dave Airlie144a75f2008-03-30 07:53:58 +10001735 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001736 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001737
Eric Anholt673a3942008-07-30 12:06:12 -07001738 i915_gem_lastclose(dev);
1739
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001740 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001741 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001742
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001743 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Eric Anholt6c340ea2007-08-25 20:23:09 +10001746void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001748 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001749 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08001750 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1751 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Eric Anholt673a3942008-07-30 12:06:12 -07001754void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1755{
1756 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1757
Eric Anholt9a298b22009-03-24 12:23:04 -07001758 kfree(i915_file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001759}
1760
Eric Anholtc153f452007-09-03 12:06:45 +10001761struct drm_ioctl_desc i915_ioctls[] = {
1762 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1763 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1764 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1765 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1766 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1767 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1768 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1769 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1770 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1771 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1772 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1773 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1774 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1775 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1776 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1777 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001778 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 +10001779 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 -07001780 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
Jesse Barnes76446ca2009-12-17 22:05:42 -05001781 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH),
Eric Anholt673a3942008-07-30 12:06:12 -07001782 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1783 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
1784 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH),
1785 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH),
Dave Airlie2bdf00b2008-10-07 13:40:10 +10001786 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1787 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 -07001788 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, 0),
1789 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, 0),
1790 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, 0),
1791 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, 0),
Jesse Barnesde151cf2008-11-12 10:03:55 -08001792 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, 0),
Eric Anholt673a3942008-07-30 12:06:12 -07001793 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, 0),
1794 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, 0),
1795 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, 0),
1796 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, 0),
Eric Anholt5a125c32008-10-22 21:40:13 -07001797 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, 0),
Carl Worth08d7b3d2009-04-29 14:43:54 -07001798 DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
Chris Wilson3ef94da2009-09-14 16:50:29 +01001799 DRM_IOCTL_DEF(DRM_I915_GEM_MADVISE, i915_gem_madvise_ioctl, 0),
Daniel Vetter02e792f2009-09-15 22:57:34 +02001800 DRM_IOCTL_DEF(DRM_I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW),
1801 DRM_IOCTL_DEF(DRM_I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW),
Dave Airliec94f7022005-07-07 21:03:38 +10001802};
1803
1804int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001805
1806/**
1807 * Determine if the device really is AGP or not.
1808 *
1809 * All Intel graphics chipsets are treated as AGP, even if they are really
1810 * PCI-e.
1811 *
1812 * \param dev The device to be tested.
1813 *
1814 * \returns
1815 * A value of 1 is always retured to indictate every i9x5 is AGP.
1816 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001817int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001818{
1819 return 1;
1820}