blob: 9bed5617e0ead2cca0339ab51ed3bf54442c0259 [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"
Jordan Crousedcdb1672010-05-27 13:40:25 -060037#include <linux/pci.h>
Dave Airlie28d52042009-09-21 14:33:58 +100038#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080039#include <linux/acpi.h>
40#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100041#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* Really want an OS-independent resettable timer. Would like to have
45 * this loop run for (eg) 3 sec, but have the timer reset every time
46 * the head pointer changes, so that EBUSY only happens if the ring
47 * actually stalls for (eg) 3 seconds.
48 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100049int i915_wait_ring(struct drm_device * dev, int n, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 drm_i915_private_t *dev_priv = dev->dev_private;
52 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
Keith Packardd3a6d442008-07-30 12:21:20 -070053 u32 acthd_reg = IS_I965G(dev) ? ACTHD_I965 : ACTHD;
54 u32 last_acthd = I915_READ(acthd_reg);
55 u32 acthd;
Jesse Barnes585fb112008-07-29 11:54:06 -070056 u32 last_head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int i;
58
Chris Wilson1c5d22f2009-08-25 11:15:50 +010059 trace_i915_ring_wait_begin (dev);
60
Keith Packardd3a6d442008-07-30 12:21:20 -070061 for (i = 0; i < 100000; i++) {
Jesse Barnes585fb112008-07-29 11:54:06 -070062 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
Keith Packardd3a6d442008-07-30 12:21:20 -070063 acthd = I915_READ(acthd_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ring->space = ring->head - (ring->tail + 8);
65 if (ring->space < 0)
66 ring->space += ring->Size;
Chris Wilson1c5d22f2009-08-25 11:15:50 +010067 if (ring->space >= n) {
68 trace_i915_ring_wait_end (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return 0;
Chris Wilson1c5d22f2009-08-25 11:15:50 +010070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Chris Wilson98787c02009-03-06 23:27:52 +000072 if (dev->primary->master) {
73 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
74 if (master_priv->sarea_priv)
75 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
76 }
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (ring->head != last_head)
80 i = 0;
Keith Packardd3a6d442008-07-30 12:21:20 -070081 if (acthd != last_acthd)
82 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 last_head = ring->head;
Keith Packardd3a6d442008-07-30 12:21:20 -070085 last_acthd = acthd;
86 msleep_interruptible(10);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89
Chris Wilson1c5d22f2009-08-25 11:15:50 +010090 trace_i915_ring_wait_end (dev);
Eric Anholt20caafa2007-08-25 19:22:43 +100091 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Chris Wilson0ef82af2009-09-05 18:07:06 +010094/* As a ringbuffer is only allowed to wrap between instructions, fill
95 * the tail with NOOPs.
96 */
97int i915_wrap_ring(struct drm_device *dev)
98{
99 drm_i915_private_t *dev_priv = dev->dev_private;
100 volatile unsigned int *virt;
101 int rem;
102
103 rem = dev_priv->ring.Size - dev_priv->ring.tail;
104 if (dev_priv->ring.space < rem) {
105 int ret = i915_wait_ring(dev, rem, __func__);
106 if (ret)
107 return ret;
108 }
109 dev_priv->ring.space -= rem;
110
111 virt = (unsigned int *)
112 (dev_priv->ring.virtual_start + dev_priv->ring.tail);
113 rem /= 4;
114 while (rem--)
115 *virt++ = MI_NOOP;
116
117 dev_priv->ring.tail = 0;
118
119 return 0;
120}
121
Keith Packard398c9cb2008-07-30 13:03:43 -0700122/**
123 * Sets up the hardware status page for devices that need a physical address
124 * in the register.
125 */
Eric Anholt3043c602008-10-02 12:24:47 -0700126static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700127{
128 drm_i915_private_t *dev_priv = dev->dev_private;
129 /* Program Hardware Status Page */
130 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800131 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -0700132
133 if (!dev_priv->status_page_dmah) {
134 DRM_ERROR("Can not allocate hardware status page\n");
135 return -ENOMEM;
136 }
137 dev_priv->hw_status_page = dev_priv->status_page_dmah->vaddr;
138 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
139
140 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
141
Zhenyu Wang9b974cc2010-01-05 11:25:06 +0800142 if (IS_I965G(dev))
143 dev_priv->dma_status_page |= (dev_priv->dma_status_page >> 28) &
144 0xf0;
145
Keith Packard398c9cb2008-07-30 13:03:43 -0700146 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800147 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700148 return 0;
149}
150
151/**
152 * Frees the hardware status page, whether it's a physical address or a virtual
153 * address set up by the X Server.
154 */
Eric Anholt3043c602008-10-02 12:24:47 -0700155static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700156{
157 drm_i915_private_t *dev_priv = dev->dev_private;
158 if (dev_priv->status_page_dmah) {
159 drm_pci_free(dev, dev_priv->status_page_dmah);
160 dev_priv->status_page_dmah = NULL;
161 }
162
163 if (dev_priv->status_gfx_addr) {
164 dev_priv->status_gfx_addr = 0;
165 drm_core_ioremapfree(&dev_priv->hws_map, dev);
166 }
167
168 /* Need to rewrite hardware status page */
169 I915_WRITE(HWS_PGA, 0x1ffff000);
170}
171
Dave Airlie84b1fd12007-07-11 15:53:27 +1000172void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000175 struct drm_i915_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 drm_i915_ring_buffer_t *ring = &(dev_priv->ring);
177
Jesse Barnes79e53942008-11-07 14:24:08 -0800178 /*
179 * We should never lose context on the ring with modesetting
180 * as we don't expose it to userspace
181 */
182 if (drm_core_check_feature(dev, DRIVER_MODESET))
183 return;
184
Jesse Barnes585fb112008-07-29 11:54:06 -0700185 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
186 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ring->space = ring->head - (ring->tail + 8);
188 if (ring->space < 0)
189 ring->space += ring->Size;
190
Dave Airlie7c1c2872008-11-28 14:22:24 +1000191 if (!dev->primary->master)
192 return;
193
194 master_priv = dev->primary->master->driver_priv;
195 if (ring->head == ring->tail && master_priv->sarea_priv)
196 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Dave Airlie84b1fd12007-07-11 15:53:27 +1000199static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000201 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Make sure interrupts are disabled here because the uninstall ioctl
203 * may not have been called from userspace and after dev_private
204 * is freed, it's too late.
205 */
Eric Anholted4cb412008-07-29 12:10:39 -0700206 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000209 if (dev_priv->ring.virtual_start) {
210 drm_core_ioremapfree(&dev_priv->ring.map, dev);
Eric Anholt3043c602008-10-02 12:24:47 -0700211 dev_priv->ring.virtual_start = NULL;
212 dev_priv->ring.map.handle = NULL;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000213 dev_priv->ring.map.size = 0;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Keith Packard398c9cb2008-07-30 13:03:43 -0700216 /* Clear the HWS virtual address at teardown */
217 if (I915_NEED_GFX_HWS(dev))
218 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 return 0;
221}
222
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000223static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000225 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000226 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Dave Airlie3a03ac12009-01-11 09:03:49 +1000228 master_priv->sarea = drm_getsarea(dev);
229 if (master_priv->sarea) {
230 master_priv->sarea_priv = (drm_i915_sarea_t *)
231 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
232 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800233 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000234 }
235
Eric Anholt673a3942008-07-30 12:06:12 -0700236 if (init->ring_size != 0) {
237 if (dev_priv->ring.ring_obj != NULL) {
238 i915_dma_cleanup(dev);
239 DRM_ERROR("Client tried to initialize ringbuffer in "
240 "GEM mode\n");
241 return -EINVAL;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Eric Anholt673a3942008-07-30 12:06:12 -0700244 dev_priv->ring.Size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Eric Anholt673a3942008-07-30 12:06:12 -0700246 dev_priv->ring.map.offset = init->ring_start;
247 dev_priv->ring.map.size = init->ring_size;
248 dev_priv->ring.map.type = 0;
249 dev_priv->ring.map.flags = 0;
250 dev_priv->ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jesse Barnes6fb88582009-02-23 10:08:21 +1000252 drm_core_ioremap_wc(&dev_priv->ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700253
254 if (dev_priv->ring.map.handle == NULL) {
255 i915_dma_cleanup(dev);
256 DRM_ERROR("can not ioremap virtual address for"
257 " ring buffer\n");
258 return -ENOMEM;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
262 dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
263
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000264 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 dev_priv->back_offset = init->back_offset;
266 dev_priv->front_offset = init->front_offset;
267 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000268 if (master_priv->sarea_priv)
269 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Allow hardware batchbuffers unless told otherwise.
272 */
273 dev_priv->allow_batchbuffer = 1;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276}
277
Dave Airlie84b1fd12007-07-11 15:53:27 +1000278static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
281
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800282 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (dev_priv->ring.map.handle == NULL) {
285 DRM_ERROR("can not ioremap virtual address for"
286 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000287 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 /* Program Hardware Status Page */
291 if (!dev_priv->hw_status_page) {
292 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000293 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800295 DRM_DEBUG_DRIVER("hw status page @ %p\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800296 dev_priv->hw_status_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000298 if (dev_priv->status_gfx_addr != 0)
Jesse Barnes585fb112008-07-29 11:54:06 -0700299 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000300 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700301 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800302 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return 0;
305}
306
Eric Anholtc153f452007-09-03 12:06:45 +1000307static int i915_dma_init(struct drm_device *dev, void *data,
308 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Eric Anholtc153f452007-09-03 12:06:45 +1000310 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 int retcode = 0;
312
Eric Anholtc153f452007-09-03 12:06:45 +1000313 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000315 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 case I915_CLEANUP_DMA:
318 retcode = i915_dma_cleanup(dev);
319 break;
320 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100321 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
323 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000324 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 }
327
328 return retcode;
329}
330
331/* Implement basically the same security restrictions as hardware does
332 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
333 *
334 * Most of the calculations below involve calculating the size of a
335 * particular instruction. It's important to get the size right as
336 * that tells us where the next instruction to check is. Any illegal
337 * instruction detected will be given a size of zero, which is a
338 * signal to abort the rest of the buffer.
339 */
340static int do_validate_cmd(int cmd)
341{
342 switch (((cmd >> 29) & 0x7)) {
343 case 0x0:
344 switch ((cmd >> 23) & 0x3f) {
345 case 0x0:
346 return 1; /* MI_NOOP */
347 case 0x4:
348 return 1; /* MI_FLUSH */
349 default:
350 return 0; /* disallow everything else */
351 }
352 break;
353 case 0x1:
354 return 0; /* reserved */
355 case 0x2:
356 return (cmd & 0xff) + 2; /* 2d commands */
357 case 0x3:
358 if (((cmd >> 24) & 0x1f) <= 0x18)
359 return 1;
360
361 switch ((cmd >> 24) & 0x1f) {
362 case 0x1c:
363 return 1;
364 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 case 0x3:
367 return (cmd & 0x1f) + 2;
368 case 0x4:
369 return (cmd & 0xf) + 2;
370 default:
371 return (cmd & 0xffff) + 2;
372 }
373 case 0x1e:
374 if (cmd & (1 << 23))
375 return (cmd & 0xffff) + 1;
376 else
377 return 1;
378 case 0x1f:
379 if ((cmd & (1 << 23)) == 0) /* inline vertices */
380 return (cmd & 0x1ffff) + 2;
381 else if (cmd & (1 << 17)) /* indirect random */
382 if ((cmd & 0xffff) == 0)
383 return 0; /* unknown length, too hard */
384 else
385 return (((cmd & 0xffff) + 1) / 2) + 1;
386 else
387 return 2; /* indirect sequential */
388 default:
389 return 0;
390 }
391 default:
392 return 0;
393 }
394
395 return 0;
396}
397
398static int validate_cmd(int cmd)
399{
400 int ret = do_validate_cmd(cmd);
401
Dave Airliebc5f4522007-11-05 12:50:58 +1000402/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return ret;
405}
406
Eric Anholt201361a2009-03-11 12:30:04 -0700407static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 drm_i915_private_t *dev_priv = dev->dev_private;
410 int i;
411 RING_LOCALS;
412
Dave Airliede227f52006-01-25 15:31:43 +1100413 if ((dwords+1) * sizeof(int) >= dev_priv->ring.Size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000414 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100415
Alan Hourihanec29b6692006-08-12 16:29:24 +1000416 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 for (i = 0; i < dwords;) {
419 int cmd, sz;
420
Eric Anholt201361a2009-03-11 12:30:04 -0700421 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 OUT_RING(cmd);
427
428 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700429 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
Dave Airliede227f52006-01-25 15:31:43 +1100433 if (dwords & 1)
434 OUT_RING(0);
435
436 ADVANCE_LP_RING();
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439}
440
Eric Anholt673a3942008-07-30 12:06:12 -0700441int
442i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700443 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700444 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholt201361a2009-03-11 12:30:04 -0700447 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 RING_LOCALS;
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
451 DRM_ERROR("Bad box %d,%d..%d,%d\n",
452 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Alan Hourihanec29b6692006-08-12 16:29:24 +1000456 if (IS_I965G(dev)) {
457 BEGIN_LP_RING(4);
458 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
459 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000460 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000461 OUT_RING(DR4);
462 ADVANCE_LP_RING();
463 } else {
464 BEGIN_LP_RING(6);
465 OUT_RING(GFX_OP_DRAWRECT_INFO);
466 OUT_RING(DR1);
467 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
468 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
469 OUT_RING(DR4);
470 OUT_RING(0);
471 ADVANCE_LP_RING();
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 return 0;
475}
476
Alan Hourihanec29b6692006-08-12 16:29:24 +1000477/* XXX: Emitting the counter should really be moved to part of the IRQ
478 * emit. For now, do it in both places:
479 */
480
Dave Airlie84b1fd12007-07-11 15:53:27 +1000481static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100482{
483 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000484 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100485 RING_LOCALS;
486
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400487 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000488 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400489 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000490 if (master_priv->sarea_priv)
491 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100492
493 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700494 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000495 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100496 OUT_RING(dev_priv->counter);
497 OUT_RING(0);
498 ADVANCE_LP_RING();
499}
500
Dave Airlie84b1fd12007-07-11 15:53:27 +1000501static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700502 drm_i915_cmdbuffer_t *cmd,
503 struct drm_clip_rect *cliprects,
504 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 int nbox = cmd->num_cliprects;
507 int i = 0, count, ret;
508
509 if (cmd->sz & 0x3) {
510 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000511 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 i915_kernel_lost_context(dev);
515
516 count = nbox ? nbox : 1;
517
518 for (i = 0; i < count; i++) {
519 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700520 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 cmd->DR1, cmd->DR4);
522 if (ret)
523 return ret;
524 }
525
Eric Anholt201361a2009-03-11 12:30:04 -0700526 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (ret)
528 return ret;
529 }
530
Dave Airliede227f52006-01-25 15:31:43 +1100531 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return 0;
533}
534
Dave Airlie84b1fd12007-07-11 15:53:27 +1000535static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700536 drm_i915_batchbuffer_t * batch,
537 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int nbox = batch->num_cliprects;
541 int i = 0, count;
542 RING_LOCALS;
543
544 if ((batch->start | batch->used) & 0x7) {
545 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000546 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 i915_kernel_lost_context(dev);
550
551 count = nbox ? nbox : 1;
552
553 for (i = 0; i < count; i++) {
554 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700555 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 batch->DR1, batch->DR4);
557 if (ret)
558 return ret;
559 }
560
Keith Packard0790d5e2008-07-30 12:28:47 -0700561 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000563 if (IS_I965G(dev)) {
564 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
565 OUT_RING(batch->start);
566 } else {
567 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
568 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ADVANCE_LP_RING();
571 } else {
572 BEGIN_LP_RING(4);
573 OUT_RING(MI_BATCH_BUFFER);
574 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
575 OUT_RING(batch->start + batch->used - 4);
576 OUT_RING(0);
577 ADVANCE_LP_RING();
578 }
579 }
580
Dave Airliede227f52006-01-25 15:31:43 +1100581 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 return 0;
584}
585
Dave Airlieaf6061a2008-05-07 12:15:39 +1000586static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000589 struct drm_i915_master_private *master_priv =
590 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 RING_LOCALS;
592
Dave Airlie7c1c2872008-11-28 14:22:24 +1000593 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400594 return -EINVAL;
595
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800596 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800597 __func__,
598 dev_priv->current_page,
599 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Dave Airlieaf6061a2008-05-07 12:15:39 +1000601 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Dave Airlieaf6061a2008-05-07 12:15:39 +1000603 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700604 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000605 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ADVANCE_LP_RING();
607
Dave Airlieaf6061a2008-05-07 12:15:39 +1000608 BEGIN_LP_RING(6);
609 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
610 OUT_RING(0);
611 if (dev_priv->current_page == 0) {
612 OUT_RING(dev_priv->back_offset);
613 dev_priv->current_page = 1;
614 } else {
615 OUT_RING(dev_priv->front_offset);
616 dev_priv->current_page = 0;
617 }
618 OUT_RING(0);
619 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000620
Dave Airlieaf6061a2008-05-07 12:15:39 +1000621 BEGIN_LP_RING(2);
622 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
623 OUT_RING(0);
624 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000625
Dave Airlie7c1c2872008-11-28 14:22:24 +1000626 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000627
Dave Airlieaf6061a2008-05-07 12:15:39 +1000628 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700629 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000630 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000631 OUT_RING(dev_priv->counter);
632 OUT_RING(0);
633 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000634
Dave Airlie7c1c2872008-11-28 14:22:24 +1000635 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Dave Airlie84b1fd12007-07-11 15:53:27 +1000639static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 drm_i915_private_t *dev_priv = dev->dev_private;
642
643 i915_kernel_lost_context(dev);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700644 return i915_wait_ring(dev, dev_priv->ring.Size - 8, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Eric Anholtc153f452007-09-03 12:06:45 +1000647static int i915_flush_ioctl(struct drm_device *dev, void *data,
648 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Eric Anholt546b0972008-09-01 16:45:29 -0700650 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric Anholt546b0972008-09-01 16:45:29 -0700652 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
653
654 mutex_lock(&dev->struct_mutex);
655 ret = i915_quiescent(dev);
656 mutex_unlock(&dev->struct_mutex);
657
658 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Eric Anholtc153f452007-09-03 12:06:45 +1000661static int i915_batchbuffer(struct drm_device *dev, void *data,
662 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000665 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000667 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000668 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700670 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (!dev_priv->allow_batchbuffer) {
673 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800677 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800678 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Eric Anholt546b0972008-09-01 16:45:29 -0700680 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Eric Anholt201361a2009-03-11 12:30:04 -0700682 if (batch->num_cliprects < 0)
683 return -EINVAL;
684
685 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700686 cliprects = kcalloc(batch->num_cliprects,
687 sizeof(struct drm_clip_rect),
688 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700689 if (cliprects == NULL)
690 return -ENOMEM;
691
692 ret = copy_from_user(cliprects, batch->cliprects,
693 batch->num_cliprects *
694 sizeof(struct drm_clip_rect));
695 if (ret != 0)
696 goto fail_free;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Eric Anholt546b0972008-09-01 16:45:29 -0700699 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700700 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700701 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400703 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000704 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700705
706fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700707 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return ret;
710}
711
Eric Anholtc153f452007-09-03 12:06:45 +1000712static int i915_cmdbuffer(struct drm_device *dev, void *data,
713 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000716 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000718 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000719 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700720 struct drm_clip_rect *cliprects = NULL;
721 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int ret;
723
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800724 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800725 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Eric Anholt546b0972008-09-01 16:45:29 -0700727 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Eric Anholt201361a2009-03-11 12:30:04 -0700729 if (cmdbuf->num_cliprects < 0)
730 return -EINVAL;
731
Eric Anholt9a298b22009-03-24 12:23:04 -0700732 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700733 if (batch_data == NULL)
734 return -ENOMEM;
735
736 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
737 if (ret != 0)
738 goto fail_batch_free;
739
740 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700741 cliprects = kcalloc(cmdbuf->num_cliprects,
742 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000743 if (cliprects == NULL) {
744 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700745 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000746 }
Eric Anholt201361a2009-03-11 12:30:04 -0700747
748 ret = copy_from_user(cliprects, cmdbuf->cliprects,
749 cmdbuf->num_cliprects *
750 sizeof(struct drm_clip_rect));
751 if (ret != 0)
752 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
Eric Anholt546b0972008-09-01 16:45:29 -0700755 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700756 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700757 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (ret) {
759 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000760 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400763 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000764 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700765
Eric Anholt201361a2009-03-11 12:30:04 -0700766fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700767 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000768fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700769 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700770
771 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Eric Anholtc153f452007-09-03 12:06:45 +1000774static int i915_flip_bufs(struct drm_device *dev, void *data,
775 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Eric Anholt546b0972008-09-01 16:45:29 -0700777 int ret;
778
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800779 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Eric Anholt546b0972008-09-01 16:45:29 -0700781 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Eric Anholt546b0972008-09-01 16:45:29 -0700783 mutex_lock(&dev->struct_mutex);
784 ret = i915_dispatch_flip(dev);
785 mutex_unlock(&dev->struct_mutex);
786
787 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Eric Anholtc153f452007-09-03 12:06:45 +1000790static int i915_getparam(struct drm_device *dev, void *data,
791 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000794 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int value;
796
797 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000798 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000799 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Eric Anholtc153f452007-09-03 12:06:45 +1000802 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700804 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
806 case I915_PARAM_ALLOW_BATCHBUFFER:
807 value = dev_priv->allow_batchbuffer ? 1 : 0;
808 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100809 case I915_PARAM_LAST_DISPATCH:
810 value = READ_BREADCRUMB(dev_priv);
811 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400812 case I915_PARAM_CHIPSET_ID:
813 value = dev->pci_device;
814 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700815 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000816 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700817 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800818 case I915_PARAM_NUM_FENCES_AVAIL:
819 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
820 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200821 case I915_PARAM_HAS_OVERLAY:
822 value = dev_priv->overlay ? 1 : 0;
823 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800824 case I915_PARAM_HAS_PAGEFLIPPING:
825 value = 1;
826 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500827 case I915_PARAM_HAS_EXECBUF2:
828 /* depends on GEM */
829 value = dev_priv->has_gem;
830 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800832 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -0500833 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000834 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
Eric Anholtc153f452007-09-03 12:06:45 +1000837 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000839 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 return 0;
843}
844
Eric Anholtc153f452007-09-03 12:06:45 +1000845static int i915_setparam(struct drm_device *dev, void *data,
846 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000849 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000852 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000853 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Eric Anholtc153f452007-09-03 12:06:45 +1000856 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000860 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000863 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800865 case I915_SETPARAM_NUM_USED_FENCES:
866 if (param->value > dev_priv->num_fence_regs ||
867 param->value < 0)
868 return -EINVAL;
869 /* Userspace can use first N regs */
870 dev_priv->fence_reg_start = param->value;
871 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800873 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800874 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
878 return 0;
879}
880
Eric Anholtc153f452007-09-03 12:06:45 +1000881static int i915_set_status_page(struct drm_device *dev, void *data,
882 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000883{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000884 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000885 drm_i915_hws_addr_t *hws = data;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000886
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000887 if (!I915_NEED_GFX_HWS(dev))
888 return -EINVAL;
889
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000890 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000891 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000892 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000893 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000894
Jesse Barnes79e53942008-11-07 14:24:08 -0800895 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
896 WARN(1, "tried to set status page when mode setting active\n");
897 return 0;
898 }
899
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800900 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000901
Eric Anholtc153f452007-09-03 12:06:45 +1000902 dev_priv->status_gfx_addr = hws->addr & (0x1ffff<<12);
903
Eric Anholt8b409582007-11-22 16:40:37 +1000904 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000905 dev_priv->hws_map.size = 4*1024;
906 dev_priv->hws_map.type = 0;
907 dev_priv->hws_map.flags = 0;
908 dev_priv->hws_map.mtrr = 0;
909
Dave Airliedd0910b2009-02-25 14:49:21 +1000910 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000911 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000912 i915_dma_cleanup(dev);
913 dev_priv->status_gfx_addr = 0;
914 DRM_ERROR("can not ioremap virtual address for"
915 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000916 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000917 }
918 dev_priv->hw_status_page = dev_priv->hws_map.handle;
919
920 memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
Jesse Barnes585fb112008-07-29 11:54:06 -0700921 I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800922 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800923 dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800924 DRM_DEBUG_DRIVER("load hws at %p\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800925 dev_priv->hw_status_page);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000926 return 0;
927}
928
Dave Airlieec2a4c32009-08-04 11:43:41 +1000929static int i915_get_bridge_dev(struct drm_device *dev)
930{
931 struct drm_i915_private *dev_priv = dev->dev_private;
932
933 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
934 if (!dev_priv->bridge_dev) {
935 DRM_ERROR("bridge device not found\n");
936 return -1;
937 }
938 return 0;
939}
940
Zhenyu Wangc48044112009-12-17 14:48:43 +0800941#define MCHBAR_I915 0x44
942#define MCHBAR_I965 0x48
943#define MCHBAR_SIZE (4*4096)
944
945#define DEVEN_REG 0x54
946#define DEVEN_MCHBAR_EN (1 << 28)
947
948/* Allocate space for the MCH regs if needed, return nonzero on error */
949static int
950intel_alloc_mchbar_resource(struct drm_device *dev)
951{
952 drm_i915_private_t *dev_priv = dev->dev_private;
953 int reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
954 u32 temp_lo, temp_hi = 0;
955 u64 mchbar_addr;
956 int ret = 0;
957
958 if (IS_I965G(dev))
959 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
960 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
961 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
962
963 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
964#ifdef CONFIG_PNP
965 if (mchbar_addr &&
966 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE)) {
967 ret = 0;
968 goto out;
969 }
970#endif
971
972 /* Get some space for it */
973 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus, &dev_priv->mch_res,
974 MCHBAR_SIZE, MCHBAR_SIZE,
975 PCIBIOS_MIN_MEM,
976 0, pcibios_align_resource,
977 dev_priv->bridge_dev);
978 if (ret) {
979 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
980 dev_priv->mch_res.start = 0;
981 goto out;
982 }
983
984 if (IS_I965G(dev))
985 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
986 upper_32_bits(dev_priv->mch_res.start));
987
988 pci_write_config_dword(dev_priv->bridge_dev, reg,
989 lower_32_bits(dev_priv->mch_res.start));
990out:
991 return ret;
992}
993
994/* Setup MCHBAR if possible, return true if we should disable it again */
995static void
996intel_setup_mchbar(struct drm_device *dev)
997{
998 drm_i915_private_t *dev_priv = dev->dev_private;
999 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
1000 u32 temp;
1001 bool enabled;
1002
1003 dev_priv->mchbar_need_disable = false;
1004
1005 if (IS_I915G(dev) || IS_I915GM(dev)) {
1006 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1007 enabled = !!(temp & DEVEN_MCHBAR_EN);
1008 } else {
1009 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1010 enabled = temp & 1;
1011 }
1012
1013 /* If it's already enabled, don't have to do anything */
1014 if (enabled)
1015 return;
1016
1017 if (intel_alloc_mchbar_resource(dev))
1018 return;
1019
1020 dev_priv->mchbar_need_disable = true;
1021
1022 /* Space is allocated or reserved, so enable it. */
1023 if (IS_I915G(dev) || IS_I915GM(dev)) {
1024 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1025 temp | DEVEN_MCHBAR_EN);
1026 } else {
1027 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1028 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1029 }
1030}
1031
1032static void
1033intel_teardown_mchbar(struct drm_device *dev)
1034{
1035 drm_i915_private_t *dev_priv = dev->dev_private;
1036 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
1037 u32 temp;
1038
1039 if (dev_priv->mchbar_need_disable) {
1040 if (IS_I915G(dev) || IS_I915GM(dev)) {
1041 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1042 temp &= ~DEVEN_MCHBAR_EN;
1043 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1044 } else {
1045 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1046 temp &= ~1;
1047 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1048 }
1049 }
1050
1051 if (dev_priv->mch_res.start)
1052 release_resource(&dev_priv->mch_res);
1053}
1054
Jesse Barnes79e53942008-11-07 14:24:08 -08001055/**
1056 * i915_probe_agp - get AGP bootup configuration
1057 * @pdev: PCI device
1058 * @aperture_size: returns AGP aperture configured size
1059 * @preallocated_size: returns size of BIOS preallocated AGP space
1060 *
1061 * Since Intel integrated graphics are UMA, the BIOS has to set aside
1062 * some RAM for the framebuffer at early boot. This code figures out
1063 * how much was set aside so we can use it for our own purposes.
1064 */
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001065static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,
Jesse Barnes80824002009-09-10 15:28:06 -07001066 uint32_t *preallocated_size,
1067 uint32_t *start)
Jesse Barnes79e53942008-11-07 14:24:08 -08001068{
Dave Airlieec2a4c32009-08-04 11:43:41 +10001069 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001070 u16 tmp = 0;
1071 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -08001072 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -08001073
Jesse Barnes79e53942008-11-07 14:24:08 -08001074 /* Get the fb aperture size and "stolen" memory amount. */
Dave Airlieec2a4c32009-08-04 11:43:41 +10001075 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &tmp);
Jesse Barnes79e53942008-11-07 14:24:08 -08001076
1077 *aperture_size = 1024 * 1024;
1078 *preallocated_size = 1024 * 1024;
1079
Eric Anholt60fd99e2008-12-03 22:50:02 -08001080 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001081 case PCI_DEVICE_ID_INTEL_82830_CGC:
1082 case PCI_DEVICE_ID_INTEL_82845G_IG:
1083 case PCI_DEVICE_ID_INTEL_82855GM_IG:
1084 case PCI_DEVICE_ID_INTEL_82865_IG:
1085 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
1086 *aperture_size *= 64;
1087 else
1088 *aperture_size *= 128;
1089 break;
1090 default:
1091 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -08001092 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001093 break;
1094 }
1095
1096 /*
1097 * Some of the preallocated space is taken by the GTT
1098 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
1099 */
Eric Anholtbad720f2009-10-22 16:11:14 -07001100 if (IS_G4X(dev) || IS_PINEVIEW(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -08001101 overhead = 4096;
1102 else
1103 overhead = (*aperture_size / 1024) + 4096;
1104
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001105 if (IS_GEN6(dev)) {
1106 /* SNB has memory control reg at 0x50.w */
1107 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &tmp);
1108
1109 switch (tmp & SNB_GMCH_GMS_STOLEN_MASK) {
1110 case INTEL_855_GMCH_GMS_DISABLED:
Eric Anholtbad720f2009-10-22 16:11:14 -07001111 DRM_ERROR("video memory is disabled\n");
1112 return -1;
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001113 case SNB_GMCH_GMS_STOLEN_32M:
1114 stolen = 32 * 1024 * 1024;
1115 break;
1116 case SNB_GMCH_GMS_STOLEN_64M:
1117 stolen = 64 * 1024 * 1024;
1118 break;
1119 case SNB_GMCH_GMS_STOLEN_96M:
1120 stolen = 96 * 1024 * 1024;
1121 break;
1122 case SNB_GMCH_GMS_STOLEN_128M:
1123 stolen = 128 * 1024 * 1024;
1124 break;
1125 case SNB_GMCH_GMS_STOLEN_160M:
1126 stolen = 160 * 1024 * 1024;
1127 break;
1128 case SNB_GMCH_GMS_STOLEN_192M:
1129 stolen = 192 * 1024 * 1024;
1130 break;
1131 case SNB_GMCH_GMS_STOLEN_224M:
1132 stolen = 224 * 1024 * 1024;
1133 break;
1134 case SNB_GMCH_GMS_STOLEN_256M:
1135 stolen = 256 * 1024 * 1024;
1136 break;
1137 case SNB_GMCH_GMS_STOLEN_288M:
1138 stolen = 288 * 1024 * 1024;
1139 break;
1140 case SNB_GMCH_GMS_STOLEN_320M:
1141 stolen = 320 * 1024 * 1024;
1142 break;
1143 case SNB_GMCH_GMS_STOLEN_352M:
1144 stolen = 352 * 1024 * 1024;
1145 break;
1146 case SNB_GMCH_GMS_STOLEN_384M:
1147 stolen = 384 * 1024 * 1024;
1148 break;
1149 case SNB_GMCH_GMS_STOLEN_416M:
1150 stolen = 416 * 1024 * 1024;
1151 break;
1152 case SNB_GMCH_GMS_STOLEN_448M:
1153 stolen = 448 * 1024 * 1024;
1154 break;
1155 case SNB_GMCH_GMS_STOLEN_480M:
1156 stolen = 480 * 1024 * 1024;
1157 break;
1158 case SNB_GMCH_GMS_STOLEN_512M:
1159 stolen = 512 * 1024 * 1024;
1160 break;
1161 default:
1162 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
1163 tmp & SNB_GMCH_GMS_STOLEN_MASK);
1164 return -1;
Eric Anholtbad720f2009-10-22 16:11:14 -07001165 }
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001166 } else {
1167 switch (tmp & INTEL_GMCH_GMS_MASK) {
1168 case INTEL_855_GMCH_GMS_DISABLED:
1169 DRM_ERROR("video memory is disabled\n");
1170 return -1;
1171 case INTEL_855_GMCH_GMS_STOLEN_1M:
1172 stolen = 1 * 1024 * 1024;
1173 break;
1174 case INTEL_855_GMCH_GMS_STOLEN_4M:
1175 stolen = 4 * 1024 * 1024;
1176 break;
1177 case INTEL_855_GMCH_GMS_STOLEN_8M:
1178 stolen = 8 * 1024 * 1024;
1179 break;
1180 case INTEL_855_GMCH_GMS_STOLEN_16M:
1181 stolen = 16 * 1024 * 1024;
1182 break;
1183 case INTEL_855_GMCH_GMS_STOLEN_32M:
1184 stolen = 32 * 1024 * 1024;
1185 break;
1186 case INTEL_915G_GMCH_GMS_STOLEN_48M:
1187 stolen = 48 * 1024 * 1024;
1188 break;
1189 case INTEL_915G_GMCH_GMS_STOLEN_64M:
1190 stolen = 64 * 1024 * 1024;
1191 break;
1192 case INTEL_GMCH_GMS_STOLEN_128M:
1193 stolen = 128 * 1024 * 1024;
1194 break;
1195 case INTEL_GMCH_GMS_STOLEN_256M:
1196 stolen = 256 * 1024 * 1024;
1197 break;
1198 case INTEL_GMCH_GMS_STOLEN_96M:
1199 stolen = 96 * 1024 * 1024;
1200 break;
1201 case INTEL_GMCH_GMS_STOLEN_160M:
1202 stolen = 160 * 1024 * 1024;
1203 break;
1204 case INTEL_GMCH_GMS_STOLEN_224M:
1205 stolen = 224 * 1024 * 1024;
1206 break;
1207 case INTEL_GMCH_GMS_STOLEN_352M:
1208 stolen = 352 * 1024 * 1024;
1209 break;
1210 default:
1211 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
1212 tmp & INTEL_GMCH_GMS_MASK);
1213 return -1;
1214 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001215 }
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001216
Eric Anholt241fa852009-01-02 18:05:51 -08001217 *preallocated_size = stolen - overhead;
Jesse Barnes80824002009-09-10 15:28:06 -07001218 *start = overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -08001219
1220 return 0;
1221}
1222
Jesse Barnes80824002009-09-10 15:28:06 -07001223#define PTE_ADDRESS_MASK 0xfffff000
1224#define PTE_ADDRESS_MASK_HIGH 0x000000f0 /* i915+ */
1225#define PTE_MAPPING_TYPE_UNCACHED (0 << 1)
1226#define PTE_MAPPING_TYPE_DCACHE (1 << 1) /* i830 only */
1227#define PTE_MAPPING_TYPE_CACHED (3 << 1)
1228#define PTE_MAPPING_TYPE_MASK (3 << 1)
1229#define PTE_VALID (1 << 0)
1230
1231/**
1232 * i915_gtt_to_phys - take a GTT address and turn it into a physical one
1233 * @dev: drm device
1234 * @gtt_addr: address to translate
1235 *
1236 * Some chip functions require allocations from stolen space but need the
1237 * physical address of the memory in question. We use this routine
1238 * to get a physical address suitable for register programming from a given
1239 * GTT address.
1240 */
1241static unsigned long i915_gtt_to_phys(struct drm_device *dev,
1242 unsigned long gtt_addr)
1243{
1244 unsigned long *gtt;
1245 unsigned long entry, phys;
1246 int gtt_bar = IS_I9XX(dev) ? 0 : 1;
1247 int gtt_offset, gtt_size;
1248
1249 if (IS_I965G(dev)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001250 if (IS_G4X(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001251 gtt_offset = 2*1024*1024;
1252 gtt_size = 2*1024*1024;
1253 } else {
1254 gtt_offset = 512*1024;
1255 gtt_size = 512*1024;
1256 }
1257 } else {
1258 gtt_bar = 3;
1259 gtt_offset = 0;
1260 gtt_size = pci_resource_len(dev->pdev, gtt_bar);
1261 }
1262
1263 gtt = ioremap_wc(pci_resource_start(dev->pdev, gtt_bar) + gtt_offset,
1264 gtt_size);
1265 if (!gtt) {
1266 DRM_ERROR("ioremap of GTT failed\n");
1267 return 0;
1268 }
1269
1270 entry = *(volatile u32 *)(gtt + (gtt_addr / 1024));
1271
Zhao Yakui44d98a62009-10-09 11:39:40 +08001272 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, PTE: 0x%08lx\n", gtt_addr, entry);
Jesse Barnes80824002009-09-10 15:28:06 -07001273
1274 /* Mask out these reserved bits on this hardware. */
1275 if (!IS_I9XX(dev) || IS_I915G(dev) || IS_I915GM(dev) ||
1276 IS_I945G(dev) || IS_I945GM(dev)) {
1277 entry &= ~PTE_ADDRESS_MASK_HIGH;
1278 }
1279
1280 /* If it's not a mapping type we know, then bail. */
1281 if ((entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_UNCACHED &&
1282 (entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_CACHED) {
1283 iounmap(gtt);
1284 return 0;
1285 }
1286
1287 if (!(entry & PTE_VALID)) {
1288 DRM_ERROR("bad GTT entry in stolen space\n");
1289 iounmap(gtt);
1290 return 0;
1291 }
1292
1293 iounmap(gtt);
1294
1295 phys =(entry & PTE_ADDRESS_MASK) |
1296 ((uint64_t)(entry & PTE_ADDRESS_MASK_HIGH) << (32 - 4));
1297
Zhao Yakui44d98a62009-10-09 11:39:40 +08001298 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, phys addr: 0x%08lx\n", gtt_addr, phys);
Jesse Barnes80824002009-09-10 15:28:06 -07001299
1300 return phys;
1301}
1302
1303static void i915_warn_stolen(struct drm_device *dev)
1304{
1305 DRM_ERROR("not enough stolen space for compressed buffer, disabling\n");
1306 DRM_ERROR("hint: you may be able to increase stolen memory size in the BIOS to avoid this\n");
1307}
1308
1309static void i915_setup_compression(struct drm_device *dev, int size)
1310{
1311 struct drm_i915_private *dev_priv = dev->dev_private;
1312 struct drm_mm_node *compressed_fb, *compressed_llb;
Andrew Morton29bd0ae2009-11-17 14:08:52 -08001313 unsigned long cfb_base;
1314 unsigned long ll_base = 0;
Jesse Barnes80824002009-09-10 15:28:06 -07001315
1316 /* Leave 1M for line length buffer & misc. */
1317 compressed_fb = drm_mm_search_free(&dev_priv->vram, size, 4096, 0);
1318 if (!compressed_fb) {
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001319 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001320 i915_warn_stolen(dev);
1321 return;
1322 }
1323
1324 compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
1325 if (!compressed_fb) {
1326 i915_warn_stolen(dev);
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001327 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001328 return;
1329 }
1330
Jesse Barnes74dff282009-09-14 15:39:40 -07001331 cfb_base = i915_gtt_to_phys(dev, compressed_fb->start);
1332 if (!cfb_base) {
1333 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1334 drm_mm_put_block(compressed_fb);
Jesse Barnes80824002009-09-10 15:28:06 -07001335 }
1336
Jesse Barnes74dff282009-09-14 15:39:40 -07001337 if (!IS_GM45(dev)) {
1338 compressed_llb = drm_mm_search_free(&dev_priv->vram, 4096,
1339 4096, 0);
1340 if (!compressed_llb) {
1341 i915_warn_stolen(dev);
1342 return;
1343 }
1344
1345 compressed_llb = drm_mm_get_block(compressed_llb, 4096, 4096);
1346 if (!compressed_llb) {
1347 i915_warn_stolen(dev);
1348 return;
1349 }
1350
1351 ll_base = i915_gtt_to_phys(dev, compressed_llb->start);
1352 if (!ll_base) {
1353 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1354 drm_mm_put_block(compressed_fb);
1355 drm_mm_put_block(compressed_llb);
1356 }
Jesse Barnes80824002009-09-10 15:28:06 -07001357 }
1358
1359 dev_priv->cfb_size = size;
1360
Adam Jacksonee5382a2010-04-23 11:17:39 -04001361 intel_disable_fbc(dev);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001362 dev_priv->compressed_fb = compressed_fb;
1363
Jesse Barnes74dff282009-09-14 15:39:40 -07001364 if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001365 I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
1366 } else {
Jesse Barnes74dff282009-09-14 15:39:40 -07001367 I915_WRITE(FBC_CFB_BASE, cfb_base);
1368 I915_WRITE(FBC_LL_BASE, ll_base);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001369 dev_priv->compressed_llb = compressed_llb;
Jesse Barnes80824002009-09-10 15:28:06 -07001370 }
1371
Jesse Barnes80824002009-09-10 15:28:06 -07001372 DRM_DEBUG("FBC base 0x%08lx, ll base 0x%08lx, size %dM\n", cfb_base,
1373 ll_base, size >> 20);
Jesse Barnes80824002009-09-10 15:28:06 -07001374}
1375
Jesse Barnes20bf3772010-04-21 11:39:22 -07001376static void i915_cleanup_compression(struct drm_device *dev)
1377{
1378 struct drm_i915_private *dev_priv = dev->dev_private;
1379
1380 drm_mm_put_block(dev_priv->compressed_fb);
1381 if (!IS_GM45(dev))
1382 drm_mm_put_block(dev_priv->compressed_llb);
1383}
1384
Dave Airlie28d52042009-09-21 14:33:58 +10001385/* true = enable decode, false = disable decoder */
1386static unsigned int i915_vga_set_decode(void *cookie, bool state)
1387{
1388 struct drm_device *dev = cookie;
1389
1390 intel_modeset_vga_set_state(dev, state);
1391 if (state)
1392 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1393 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1394 else
1395 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1396}
1397
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001398static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1399{
1400 struct drm_device *dev = pci_get_drvdata(pdev);
1401 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1402 if (state == VGA_SWITCHEROO_ON) {
1403 printk(KERN_INFO "i915: switched off\n");
1404 /* i915 resume handler doesn't set to D0 */
1405 pci_set_power_state(dev->pdev, PCI_D0);
1406 i915_resume(dev);
1407 } else {
1408 printk(KERN_ERR "i915: switched off\n");
1409 i915_suspend(dev, pmm);
1410 }
1411}
1412
1413static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1414{
1415 struct drm_device *dev = pci_get_drvdata(pdev);
1416 bool can_switch;
1417
1418 spin_lock(&dev->count_lock);
1419 can_switch = (dev->open_count == 0);
1420 spin_unlock(&dev->count_lock);
1421 return can_switch;
1422}
1423
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001424static int i915_load_modeset_init(struct drm_device *dev,
Jesse Barnes80824002009-09-10 15:28:06 -07001425 unsigned long prealloc_start,
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001426 unsigned long prealloc_size,
1427 unsigned long agp_size)
Jesse Barnes79e53942008-11-07 14:24:08 -08001428{
1429 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001430 int fb_bar = IS_I9XX(dev) ? 2 : 0;
1431 int ret = 0;
1432
Jordan Crouse01d73a62010-05-27 13:40:24 -06001433 dev->mode_config.fb_base = pci_resource_start(dev->pdev, fb_bar) &
Jesse Barnes79e53942008-11-07 14:24:08 -08001434 0xff000000;
1435
Jesse Barnes79e53942008-11-07 14:24:08 -08001436 /* Basic memrange allocator for stolen space (aka vram) */
1437 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
Jesse Barnes80824002009-09-10 15:28:06 -07001438 DRM_INFO("set up %ldM of stolen space\n", prealloc_size / (1024*1024));
Jesse Barnes79e53942008-11-07 14:24:08 -08001439
Ben Gamari11ed50e2009-09-14 17:48:45 -04001440 /* We're off and running w/KMS */
1441 dev_priv->mm.suspended = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001442
Eric Anholt13f4c432009-05-12 15:27:36 -07001443 /* Let GEM Manage from end of prealloc space to end of aperture.
1444 *
1445 * However, leave one page at the end still bound to the scratch page.
1446 * There are a number of places where the hardware apparently
1447 * prefetches past the end of the object, and we've seen multiple
1448 * hangs with the GPU head pointer stuck in a batchbuffer bound
1449 * at the last page of the aperture. One page should be enough to
1450 * keep any prefetching inside of the aperture.
1451 */
1452 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001453
Ben Gamari11ed50e2009-09-14 17:48:45 -04001454 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001455 ret = i915_gem_init_ringbuffer(dev);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001456 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001457 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001458 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001459
Jesse Barnes80824002009-09-10 15:28:06 -07001460 /* Try to set up FBC with a reasonable compressed buffer size */
Shaohua Li9216d442009-10-10 15:20:55 +08001461 if (I915_HAS_FBC(dev) && i915_powersave) {
Jesse Barnes80824002009-09-10 15:28:06 -07001462 int cfb_size;
1463
1464 /* Try to get an 8M buffer... */
1465 if (prealloc_size > (9*1024*1024))
1466 cfb_size = 8*1024*1024;
1467 else /* fall back to 7/8 of the stolen space */
1468 cfb_size = prealloc_size * 7 / 8;
1469 i915_setup_compression(dev, cfb_size);
1470 }
1471
Jesse Barnes79e53942008-11-07 14:24:08 -08001472 /* Allow hardware batchbuffers unless told otherwise.
1473 */
1474 dev_priv->allow_batchbuffer = 1;
1475
1476 ret = intel_init_bios(dev);
1477 if (ret)
1478 DRM_INFO("failed to find VBIOS tables\n");
1479
Dave Airlie28d52042009-09-21 14:33:58 +10001480 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1481 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1482 if (ret)
1483 goto destroy_ringbuffer;
1484
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001485 ret = vga_switcheroo_register_client(dev->pdev,
1486 i915_switcheroo_set_state,
1487 i915_switcheroo_can_switch);
1488 if (ret)
1489 goto destroy_ringbuffer;
1490
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001491 intel_modeset_init(dev);
1492
Jesse Barnes79e53942008-11-07 14:24:08 -08001493 ret = drm_irq_install(dev);
1494 if (ret)
1495 goto destroy_ringbuffer;
1496
Jesse Barnes79e53942008-11-07 14:24:08 -08001497 /* Always safe in the mode setting case. */
1498 /* FIXME: do pre/post-mode set stuff in core KMS code */
1499 dev->vblank_disable_allowed = 1;
1500
1501 /*
1502 * Initialize the hardware status page IRQ location.
1503 */
1504
1505 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1506
Dave Airlie38651672010-03-30 05:34:13 +00001507 intel_fbdev_init(dev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001508 drm_kms_helper_poll_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001509 return 0;
1510
Jesse Barnes79e53942008-11-07 14:24:08 -08001511destroy_ringbuffer:
Eric Anholt21099532009-11-09 14:57:34 -08001512 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001513 i915_gem_cleanup_ringbuffer(dev);
Eric Anholt21099532009-11-09 14:57:34 -08001514 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001515out:
1516 return ret;
1517}
1518
Dave Airlie7c1c2872008-11-28 14:22:24 +10001519int i915_master_create(struct drm_device *dev, struct drm_master *master)
1520{
1521 struct drm_i915_master_private *master_priv;
1522
Eric Anholt9a298b22009-03-24 12:23:04 -07001523 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001524 if (!master_priv)
1525 return -ENOMEM;
1526
1527 master->driver_priv = master_priv;
1528 return 0;
1529}
1530
1531void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1532{
1533 struct drm_i915_master_private *master_priv = master->driver_priv;
1534
1535 if (!master_priv)
1536 return;
1537
Eric Anholt9a298b22009-03-24 12:23:04 -07001538 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001539
1540 master->driver_priv = NULL;
1541}
1542
Shaohua Li7662c8b2009-06-26 11:23:55 +08001543static void i915_get_mem_freq(struct drm_device *dev)
1544{
1545 drm_i915_private_t *dev_priv = dev->dev_private;
1546 u32 tmp;
1547
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001548 if (!IS_PINEVIEW(dev))
Shaohua Li7662c8b2009-06-26 11:23:55 +08001549 return;
1550
1551 tmp = I915_READ(CLKCFG);
1552
1553 switch (tmp & CLKCFG_FSB_MASK) {
1554 case CLKCFG_FSB_533:
1555 dev_priv->fsb_freq = 533; /* 133*4 */
1556 break;
1557 case CLKCFG_FSB_800:
1558 dev_priv->fsb_freq = 800; /* 200*4 */
1559 break;
1560 case CLKCFG_FSB_667:
1561 dev_priv->fsb_freq = 667; /* 167*4 */
1562 break;
1563 case CLKCFG_FSB_400:
1564 dev_priv->fsb_freq = 400; /* 100*4 */
1565 break;
1566 }
1567
1568 switch (tmp & CLKCFG_MEM_MASK) {
1569 case CLKCFG_MEM_533:
1570 dev_priv->mem_freq = 533;
1571 break;
1572 case CLKCFG_MEM_667:
1573 dev_priv->mem_freq = 667;
1574 break;
1575 case CLKCFG_MEM_800:
1576 dev_priv->mem_freq = 800;
1577 break;
1578 }
1579}
1580
Jesse Barnes79e53942008-11-07 14:24:08 -08001581/**
1582 * i915_driver_load - setup chip and create an initial config
1583 * @dev: DRM device
1584 * @flags: startup flags
1585 *
1586 * The driver load routine has to do several things:
1587 * - drive output discovery via intel_modeset_init()
1588 * - initialize the memory manager
1589 * - allocate initial config memory
1590 * - setup the DRM framebuffer with the allocated memory
1591 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001592int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001593{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001594 struct drm_i915_private *dev_priv;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001595 resource_size_t base, size;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001596 int ret = 0, mmio_bar;
Jesse Barnes80824002009-09-10 15:28:06 -07001597 uint32_t agp_size, prealloc_size, prealloc_start;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001598
Dave Airlie22eae942005-11-10 22:16:34 +11001599 /* i915 has 4 more counters */
1600 dev->counters += 4;
1601 dev->types[6] = _DRM_STAT_IRQ;
1602 dev->types[7] = _DRM_STAT_PRIMARY;
1603 dev->types[8] = _DRM_STAT_SECONDARY;
1604 dev->types[9] = _DRM_STAT_DMA;
1605
Eric Anholt9a298b22009-03-24 12:23:04 -07001606 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001607 if (dev_priv == NULL)
1608 return -ENOMEM;
1609
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001610 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001611 dev_priv->dev = dev;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001612 dev_priv->info = (struct intel_device_info *) flags;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001613
1614 /* Add register map (needed for suspend/resume) */
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001615 mmio_bar = IS_I9XX(dev) ? 0 : 1;
Jordan Crouse01d73a62010-05-27 13:40:24 -06001616 base = pci_resource_start(dev->pdev, mmio_bar);
1617 size = pci_resource_len(dev->pdev, mmio_bar);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001618
Dave Airlieec2a4c32009-08-04 11:43:41 +10001619 if (i915_get_bridge_dev(dev)) {
1620 ret = -EIO;
1621 goto free_priv;
1622 }
1623
Eric Anholt3043c602008-10-02 12:24:47 -07001624 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001625 if (!dev_priv->regs) {
1626 DRM_ERROR("failed to map registers\n");
1627 ret = -EIO;
Dave Airlieec2a4c32009-08-04 11:43:41 +10001628 goto put_bridge;
Jesse Barnes79e53942008-11-07 14:24:08 -08001629 }
Eric Anholted4cb412008-07-29 12:10:39 -07001630
Eric Anholtab657db12009-01-23 12:57:47 -08001631 dev_priv->mm.gtt_mapping =
1632 io_mapping_create_wc(dev->agp->base,
1633 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001634 if (dev_priv->mm.gtt_mapping == NULL) {
1635 ret = -EIO;
1636 goto out_rmmap;
1637 }
1638
Eric Anholtab657db12009-01-23 12:57:47 -08001639 /* Set up a WC MTRR for non-PAT systems. This is more common than
1640 * one would think, because the kernel disables PAT on first
1641 * generation Core chips because WC PAT gets overridden by a UC
1642 * MTRR if present. Even if a UC MTRR isn't present.
1643 */
1644 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1645 dev->agp->agp_info.aper_size *
1646 1024 * 1024,
1647 MTRR_TYPE_WRCOMB, 1);
1648 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001649 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001650 "performance may suffer.\n");
1651 }
1652
Jesse Barnes80824002009-09-10 15:28:06 -07001653 ret = i915_probe_agp(dev, &agp_size, &prealloc_size, &prealloc_start);
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001654 if (ret)
1655 goto out_iomapfree;
1656
Chris Wilsonaed5f1d2009-10-14 13:40:04 +01001657 dev_priv->wq = create_singlethread_workqueue("i915");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001658 if (dev_priv->wq == NULL) {
1659 DRM_ERROR("Failed to create our workqueue.\n");
1660 ret = -ENOMEM;
1661 goto out_iomapfree;
1662 }
1663
Dave Airlieac5c4e72008-12-19 15:38:34 +10001664 /* enable GEM by default */
1665 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10001666
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001667 if (prealloc_size > agp_size * 3 / 4) {
1668 DRM_ERROR("Detected broken video BIOS with %d/%dkB of video "
1669 "memory stolen.\n",
1670 prealloc_size / 1024, agp_size / 1024);
1671 DRM_ERROR("Disabling GEM. (try reducing stolen memory or "
1672 "updating the BIOS to fix).\n");
1673 dev_priv->has_gem = 0;
1674 }
1675
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001676 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001677 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eric Anholtbad720f2009-10-22 16:11:14 -07001678 if (IS_G4X(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001679 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001680 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001681 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001682
Zhenyu Wangc48044112009-12-17 14:48:43 +08001683 /* Try to make sure MCHBAR is enabled before poking at it */
1684 intel_setup_mchbar(dev);
1685
Eric Anholt673a3942008-07-30 12:06:12 -07001686 i915_gem_load(dev);
1687
Keith Packard398c9cb2008-07-30 13:03:43 -07001688 /* Init HWS */
1689 if (!I915_NEED_GFX_HWS(dev)) {
1690 ret = i915_init_phys_hws(dev);
1691 if (ret != 0)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001692 goto out_workqueue_free;
Keith Packard398c9cb2008-07-30 13:03:43 -07001693 }
Eric Anholted4cb412008-07-29 12:10:39 -07001694
Shaohua Li7662c8b2009-06-26 11:23:55 +08001695 i915_get_mem_freq(dev);
1696
Eric Anholted4cb412008-07-29 12:10:39 -07001697 /* On the 945G/GM, the chipset reports the MSI capability on the
1698 * integrated graphics even though the support isn't actually there
1699 * according to the published specs. It doesn't appear to function
1700 * correctly in testing on 945G.
1701 * This may be a side effect of MSI having been made available for PEG
1702 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001703 *
1704 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001705 * be lost or delayed, but we use them anyways to avoid
1706 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001707 */
Keith Packardb60678a2008-12-08 11:12:28 -08001708 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001709 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001710
1711 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001712 spin_lock_init(&dev_priv->error_lock);
Jesse Barnes79e53942008-11-07 14:24:08 -08001713 dev_priv->user_irq_refcount = 0;
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001714 dev_priv->trace_irq_seqno = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001715
Keith Packard52440212008-11-18 09:30:25 -08001716 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1717
1718 if (ret) {
1719 (void) i915_driver_unload(dev);
1720 return ret;
1721 }
1722
Ben Gamari11ed50e2009-09-14 17:48:45 -04001723 /* Start out suspended */
1724 dev_priv->mm.suspended = 1;
1725
Zhenyu Wang3bad0782010-04-07 16:15:53 +08001726 intel_detect_pch(dev);
1727
Jesse Barnes79e53942008-11-07 14:24:08 -08001728 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001729 ret = i915_load_modeset_init(dev, prealloc_start,
1730 prealloc_size, agp_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001731 if (ret < 0) {
1732 DRM_ERROR("failed to init modeset\n");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001733 goto out_workqueue_free;
Jesse Barnes79e53942008-11-07 14:24:08 -08001734 }
1735 }
1736
Matthew Garrett74a365b2009-03-19 21:35:39 +00001737 /* Must be done after probing outputs */
Zhao Yakui01c66882009-10-28 05:10:00 +00001738 intel_opregion_init(dev, 0);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001739
Ben Gamarif65d9422009-09-14 17:48:44 -04001740 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
1741 (unsigned long) dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001742 return 0;
1743
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001744out_workqueue_free:
1745 destroy_workqueue(dev_priv->wq);
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001746out_iomapfree:
1747 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001748out_rmmap:
1749 iounmap(dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001750put_bridge:
1751 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001752free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001753 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001754 return ret;
1755}
1756
1757int i915_driver_unload(struct drm_device *dev)
1758{
1759 struct drm_i915_private *dev_priv = dev->dev_private;
1760
Chris Wilson9df30792010-02-18 10:24:56 +00001761 i915_destroy_error_state(dev);
1762
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001763 destroy_workqueue(dev_priv->wq);
Ben Gamarif65d9422009-09-14 17:48:44 -04001764 del_timer_sync(&dev_priv->hangcheck_timer);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001765
Eric Anholtab657db12009-01-23 12:57:47 -08001766 io_mapping_free(dev_priv->mm.gtt_mapping);
1767 if (dev_priv->mm.gtt_mtrr >= 0) {
1768 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1769 dev->agp->agp_info.aper_size * 1024 * 1024);
1770 dev_priv->mm.gtt_mtrr = -1;
1771 }
1772
Jesse Barnes79e53942008-11-07 14:24:08 -08001773 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001774 intel_modeset_cleanup(dev);
1775
Zhao Yakui6363ee62009-11-24 09:48:44 +08001776 /*
1777 * free the memory space allocated for the child device
1778 * config parsed from VBT
1779 */
1780 if (dev_priv->child_dev && dev_priv->child_dev_num) {
1781 kfree(dev_priv->child_dev);
1782 dev_priv->child_dev = NULL;
1783 dev_priv->child_dev_num = 0;
1784 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001785 drm_irq_uninstall(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001786 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001787 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001788 }
1789
Eric Anholted4cb412008-07-29 12:10:39 -07001790 if (dev->pdev->msi_enabled)
1791 pci_disable_msi(dev->pdev);
1792
Eric Anholt3043c602008-10-02 12:24:47 -07001793 if (dev_priv->regs != NULL)
1794 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001795
Zhao Yakui01c66882009-10-28 05:10:00 +00001796 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001797
Jesse Barnes79e53942008-11-07 14:24:08 -08001798 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie71acb5e2008-12-30 20:31:46 +10001799 i915_gem_free_all_phys_object(dev);
1800
Jesse Barnes79e53942008-11-07 14:24:08 -08001801 mutex_lock(&dev->struct_mutex);
1802 i915_gem_cleanup_ringbuffer(dev);
1803 mutex_unlock(&dev->struct_mutex);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001804 if (I915_HAS_FBC(dev) && i915_powersave)
1805 i915_cleanup_compression(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001806 drm_mm_takedown(&dev_priv->vram);
1807 i915_gem_lastclose(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001808
1809 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001810 }
1811
Zhenyu Wangc48044112009-12-17 14:48:43 +08001812 intel_teardown_mchbar(dev);
1813
Dave Airlieec2a4c32009-08-04 11:43:41 +10001814 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07001815 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001816
Dave Airlie22eae942005-11-10 22:16:34 +11001817 return 0;
1818}
1819
Eric Anholt673a3942008-07-30 12:06:12 -07001820int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1821{
1822 struct drm_i915_file_private *i915_file_priv;
1823
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001824 DRM_DEBUG_DRIVER("\n");
Eric Anholt673a3942008-07-30 12:06:12 -07001825 i915_file_priv = (struct drm_i915_file_private *)
Eric Anholt9a298b22009-03-24 12:23:04 -07001826 kmalloc(sizeof(*i915_file_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001827
1828 if (!i915_file_priv)
1829 return -ENOMEM;
1830
1831 file_priv->driver_priv = i915_file_priv;
1832
Eric Anholtb9624422009-06-03 07:27:35 +00001833 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001834
1835 return 0;
1836}
1837
Jesse Barnes79e53942008-11-07 14:24:08 -08001838/**
1839 * i915_driver_lastclose - clean up after all DRM clients have exited
1840 * @dev: DRM device
1841 *
1842 * Take care of cleaning up after all DRM clients have exited. In the
1843 * mode setting case, we want to restore the kernel's initial mode (just
1844 * in case the last client left us in a bad state).
1845 *
1846 * Additionally, in the non-mode setting case, we'll tear down the AGP
1847 * and DMA structures, since the kernel won't be using them, and clea
1848 * up any GEM state.
1849 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001850void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001852 drm_i915_private_t *dev_priv = dev->dev_private;
1853
Jesse Barnes79e53942008-11-07 14:24:08 -08001854 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001855 drm_fb_helper_restore();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001856 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001857 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001858 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001859
Eric Anholt673a3942008-07-30 12:06:12 -07001860 i915_gem_lastclose(dev);
1861
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001862 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001863 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001864
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001865 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Eric Anholt6c340ea2007-08-25 20:23:09 +10001868void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001870 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001871 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08001872 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1873 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
Eric Anholt673a3942008-07-30 12:06:12 -07001876void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1877{
1878 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1879
Eric Anholt9a298b22009-03-24 12:23:04 -07001880 kfree(i915_file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001881}
1882
Eric Anholtc153f452007-09-03 12:06:45 +10001883struct drm_ioctl_desc i915_ioctls[] = {
1884 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1885 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1886 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1887 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1888 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1889 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1890 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1891 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1892 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1893 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1894 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1895 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1896 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1897 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1898 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1899 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001900 DRM_IOCTL_DEF(DRM_I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Eric Anholtf05dd2f2010-02-26 13:32:11 -08001901 DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1902 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
1903 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
1904 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1905 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1906 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
1907 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
1908 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1909 DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1910 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
1911 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
1912 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
1913 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
1914 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
1915 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
1916 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
1917 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
1918 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
1919 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
1920 DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
1921 DRM_IOCTL_DEF(DRM_I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
1922 DRM_IOCTL_DEF(DRM_I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1923 DRM_IOCTL_DEF(DRM_I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10001924};
1925
1926int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001927
1928/**
1929 * Determine if the device really is AGP or not.
1930 *
1931 * All Intel graphics chipsets are treated as AGP, even if they are really
1932 * PCI-e.
1933 *
1934 * \param dev The device to be tested.
1935 *
1936 * \returns
1937 * A value of 1 is always retured to indictate every i9x5 is AGP.
1938 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001939int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001940{
1941 return 1;
1942}