blob: 1dbed700800e622311488b7b0809efec435e7fff [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>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100040#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Keith Packard398c9cb2008-07-30 13:03:43 -070043/**
44 * Sets up the hardware status page for devices that need a physical address
45 * in the register.
46 */
Eric Anholt3043c602008-10-02 12:24:47 -070047static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070048{
49 drm_i915_private_t *dev_priv = dev->dev_private;
50 /* Program Hardware Status Page */
51 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +080052 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -070053
54 if (!dev_priv->status_page_dmah) {
55 DRM_ERROR("Can not allocate hardware status page\n");
56 return -ENOMEM;
57 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +080058 dev_priv->render_ring.status_page.page_addr
59 = dev_priv->status_page_dmah->vaddr;
Keith Packard398c9cb2008-07-30 13:03:43 -070060 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
61
Zou Nan hai8187a2b2010-05-21 09:08:55 +080062 memset(dev_priv->render_ring.status_page.page_addr, 0, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -070063
Zhenyu Wang9b974cc2010-01-05 11:25:06 +080064 if (IS_I965G(dev))
65 dev_priv->dma_status_page |= (dev_priv->dma_status_page >> 28) &
66 0xf0;
67
Keith Packard398c9cb2008-07-30 13:03:43 -070068 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +080069 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -070070 return 0;
71}
72
73/**
74 * Frees the hardware status page, whether it's a physical address or a virtual
75 * address set up by the X Server.
76 */
Eric Anholt3043c602008-10-02 12:24:47 -070077static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070078{
79 drm_i915_private_t *dev_priv = dev->dev_private;
80 if (dev_priv->status_page_dmah) {
81 drm_pci_free(dev, dev_priv->status_page_dmah);
82 dev_priv->status_page_dmah = NULL;
83 }
84
Zou Nan hai852835f2010-05-21 09:08:56 +080085 if (dev_priv->render_ring.status_page.gfx_addr) {
86 dev_priv->render_ring.status_page.gfx_addr = 0;
Keith Packard398c9cb2008-07-30 13:03:43 -070087 dev_priv->status_gfx_addr = 0;
88 drm_core_ioremapfree(&dev_priv->hws_map, dev);
89 }
90
91 /* Need to rewrite hardware status page */
92 I915_WRITE(HWS_PGA, 0x1ffff000);
93}
94
Dave Airlie84b1fd12007-07-11 15:53:27 +100095void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +100098 struct drm_i915_master_private *master_priv;
Zou Nan hai8187a2b2010-05-21 09:08:55 +080099 struct intel_ring_buffer *ring = &dev_priv->render_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Jesse Barnes79e53942008-11-07 14:24:08 -0800101 /*
102 * We should never lose context on the ring with modesetting
103 * as we don't expose it to userspace
104 */
105 if (drm_core_check_feature(dev, DRIVER_MODESET))
106 return;
107
Jesse Barnes585fb112008-07-29 11:54:06 -0700108 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
109 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 ring->space = ring->head - (ring->tail + 8);
111 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800112 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Dave Airlie7c1c2872008-11-28 14:22:24 +1000114 if (!dev->primary->master)
115 return;
116
117 master_priv = dev->primary->master->driver_priv;
118 if (ring->head == ring->tail && master_priv->sarea_priv)
119 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Dave Airlie84b1fd12007-07-11 15:53:27 +1000122static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000124 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* Make sure interrupts are disabled here because the uninstall ioctl
126 * may not have been called from userspace and after dev_private
127 * is freed, it's too late.
128 */
Eric Anholted4cb412008-07-29 12:10:39 -0700129 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800132 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800133 if (HAS_BSD(dev))
134 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Keith Packard398c9cb2008-07-30 13:03:43 -0700136 /* Clear the HWS virtual address at teardown */
137 if (I915_NEED_GFX_HWS(dev))
138 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return 0;
141}
142
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000143static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000145 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000146 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Dave Airlie3a03ac12009-01-11 09:03:49 +1000148 master_priv->sarea = drm_getsarea(dev);
149 if (master_priv->sarea) {
150 master_priv->sarea_priv = (drm_i915_sarea_t *)
151 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
152 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800153 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000154 }
155
Eric Anholt673a3942008-07-30 12:06:12 -0700156 if (init->ring_size != 0) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800157 if (dev_priv->render_ring.gem_object != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700158 i915_dma_cleanup(dev);
159 DRM_ERROR("Client tried to initialize ringbuffer in "
160 "GEM mode\n");
161 return -EINVAL;
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800164 dev_priv->render_ring.size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Eric Anholtd3301d82010-05-21 13:55:54 -0700166 dev_priv->render_ring.map.offset = init->ring_start;
167 dev_priv->render_ring.map.size = init->ring_size;
168 dev_priv->render_ring.map.type = 0;
169 dev_priv->render_ring.map.flags = 0;
170 dev_priv->render_ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Eric Anholtd3301d82010-05-21 13:55:54 -0700172 drm_core_ioremap_wc(&dev_priv->render_ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700173
Eric Anholtd3301d82010-05-21 13:55:54 -0700174 if (dev_priv->render_ring.map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700175 i915_dma_cleanup(dev);
176 DRM_ERROR("can not ioremap virtual address for"
177 " ring buffer\n");
178 return -ENOMEM;
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
Eric Anholtd3301d82010-05-21 13:55:54 -0700182 dev_priv->render_ring.virtual_start = dev_priv->render_ring.map.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000184 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 dev_priv->back_offset = init->back_offset;
186 dev_priv->front_offset = init->front_offset;
187 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000188 if (master_priv->sarea_priv)
189 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Allow hardware batchbuffers unless told otherwise.
192 */
193 dev_priv->allow_batchbuffer = 1;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return 0;
196}
197
Dave Airlie84b1fd12007-07-11 15:53:27 +1000198static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
201
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800202 struct intel_ring_buffer *ring;
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800203 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800205 ring = &dev_priv->render_ring;
206
207 if (ring->map.handle == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 DRM_ERROR("can not ioremap virtual address for"
209 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000210 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
213 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800214 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000216 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800218 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800219 ring->status_page.page_addr);
220 if (ring->status_page.gfx_addr != 0)
221 ring->setup_status_page(dev, ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000222 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700223 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800224
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800225 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 return 0;
228}
229
Eric Anholtc153f452007-09-03 12:06:45 +1000230static int i915_dma_init(struct drm_device *dev, void *data,
231 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Eric Anholtc153f452007-09-03 12:06:45 +1000233 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int retcode = 0;
235
Eric Anholtc153f452007-09-03 12:06:45 +1000236 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000238 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 break;
240 case I915_CLEANUP_DMA:
241 retcode = i915_dma_cleanup(dev);
242 break;
243 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100244 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000247 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249 }
250
251 return retcode;
252}
253
254/* Implement basically the same security restrictions as hardware does
255 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
256 *
257 * Most of the calculations below involve calculating the size of a
258 * particular instruction. It's important to get the size right as
259 * that tells us where the next instruction to check is. Any illegal
260 * instruction detected will be given a size of zero, which is a
261 * signal to abort the rest of the buffer.
262 */
263static int do_validate_cmd(int cmd)
264{
265 switch (((cmd >> 29) & 0x7)) {
266 case 0x0:
267 switch ((cmd >> 23) & 0x3f) {
268 case 0x0:
269 return 1; /* MI_NOOP */
270 case 0x4:
271 return 1; /* MI_FLUSH */
272 default:
273 return 0; /* disallow everything else */
274 }
275 break;
276 case 0x1:
277 return 0; /* reserved */
278 case 0x2:
279 return (cmd & 0xff) + 2; /* 2d commands */
280 case 0x3:
281 if (((cmd >> 24) & 0x1f) <= 0x18)
282 return 1;
283
284 switch ((cmd >> 24) & 0x1f) {
285 case 0x1c:
286 return 1;
287 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case 0x3:
290 return (cmd & 0x1f) + 2;
291 case 0x4:
292 return (cmd & 0xf) + 2;
293 default:
294 return (cmd & 0xffff) + 2;
295 }
296 case 0x1e:
297 if (cmd & (1 << 23))
298 return (cmd & 0xffff) + 1;
299 else
300 return 1;
301 case 0x1f:
302 if ((cmd & (1 << 23)) == 0) /* inline vertices */
303 return (cmd & 0x1ffff) + 2;
304 else if (cmd & (1 << 17)) /* indirect random */
305 if ((cmd & 0xffff) == 0)
306 return 0; /* unknown length, too hard */
307 else
308 return (((cmd & 0xffff) + 1) / 2) + 1;
309 else
310 return 2; /* indirect sequential */
311 default:
312 return 0;
313 }
314 default:
315 return 0;
316 }
317
318 return 0;
319}
320
321static int validate_cmd(int cmd)
322{
323 int ret = do_validate_cmd(cmd);
324
Dave Airliebc5f4522007-11-05 12:50:58 +1000325/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return ret;
328}
329
Eric Anholt201361a2009-03-11 12:30:04 -0700330static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 drm_i915_private_t *dev_priv = dev->dev_private;
333 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800335 if ((dwords+1) * sizeof(int) >= dev_priv->render_ring.size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000336 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100337
Alan Hourihanec29b6692006-08-12 16:29:24 +1000338 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 for (i = 0; i < dwords;) {
341 int cmd, sz;
342
Eric Anholt201361a2009-03-11 12:30:04 -0700343 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 OUT_RING(cmd);
349
350 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700351 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Dave Airliede227f52006-01-25 15:31:43 +1100355 if (dwords & 1)
356 OUT_RING(0);
357
358 ADVANCE_LP_RING();
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361}
362
Eric Anholt673a3942008-07-30 12:06:12 -0700363int
364i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700365 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700366 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Eric Anholt201361a2009-03-11 12:30:04 -0700368 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
371 DRM_ERROR("Bad box %d,%d..%d,%d\n",
372 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000373 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Alan Hourihanec29b6692006-08-12 16:29:24 +1000376 if (IS_I965G(dev)) {
377 BEGIN_LP_RING(4);
378 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
379 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000380 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000381 OUT_RING(DR4);
382 ADVANCE_LP_RING();
383 } else {
384 BEGIN_LP_RING(6);
385 OUT_RING(GFX_OP_DRAWRECT_INFO);
386 OUT_RING(DR1);
387 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
388 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
389 OUT_RING(DR4);
390 OUT_RING(0);
391 ADVANCE_LP_RING();
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 return 0;
395}
396
Alan Hourihanec29b6692006-08-12 16:29:24 +1000397/* XXX: Emitting the counter should really be moved to part of the IRQ
398 * emit. For now, do it in both places:
399 */
400
Dave Airlie84b1fd12007-07-11 15:53:27 +1000401static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100402{
403 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000404 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100405
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400406 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000407 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400408 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000409 if (master_priv->sarea_priv)
410 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100411
412 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700413 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000414 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100415 OUT_RING(dev_priv->counter);
416 OUT_RING(0);
417 ADVANCE_LP_RING();
418}
419
Dave Airlie84b1fd12007-07-11 15:53:27 +1000420static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700421 drm_i915_cmdbuffer_t *cmd,
422 struct drm_clip_rect *cliprects,
423 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 int nbox = cmd->num_cliprects;
426 int i = 0, count, ret;
427
428 if (cmd->sz & 0x3) {
429 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 i915_kernel_lost_context(dev);
434
435 count = nbox ? nbox : 1;
436
437 for (i = 0; i < count; i++) {
438 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700439 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 cmd->DR1, cmd->DR4);
441 if (ret)
442 return ret;
443 }
444
Eric Anholt201361a2009-03-11 12:30:04 -0700445 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (ret)
447 return ret;
448 }
449
Dave Airliede227f52006-01-25 15:31:43 +1100450 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
452}
453
Dave Airlie84b1fd12007-07-11 15:53:27 +1000454static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700455 drm_i915_batchbuffer_t * batch,
456 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 int nbox = batch->num_cliprects;
459 int i = 0, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if ((batch->start | batch->used) & 0x7) {
462 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000463 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
466 i915_kernel_lost_context(dev);
467
468 count = nbox ? nbox : 1;
469
470 for (i = 0; i < count; i++) {
471 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700472 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 batch->DR1, batch->DR4);
474 if (ret)
475 return ret;
476 }
477
Keith Packard0790d5e2008-07-30 12:28:47 -0700478 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000480 if (IS_I965G(dev)) {
481 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
482 OUT_RING(batch->start);
483 } else {
484 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
485 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 ADVANCE_LP_RING();
488 } else {
489 BEGIN_LP_RING(4);
490 OUT_RING(MI_BATCH_BUFFER);
491 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
492 OUT_RING(batch->start + batch->used - 4);
493 OUT_RING(0);
494 ADVANCE_LP_RING();
495 }
496 }
497
Dave Airliede227f52006-01-25 15:31:43 +1100498 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 return 0;
501}
502
Dave Airlieaf6061a2008-05-07 12:15:39 +1000503static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000506 struct drm_i915_master_private *master_priv =
507 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Dave Airlie7c1c2872008-11-28 14:22:24 +1000509 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400510 return -EINVAL;
511
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800512 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800513 __func__,
514 dev_priv->current_page,
515 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Dave Airlieaf6061a2008-05-07 12:15:39 +1000517 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Dave Airlieaf6061a2008-05-07 12:15:39 +1000519 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700520 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000521 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 ADVANCE_LP_RING();
523
Dave Airlieaf6061a2008-05-07 12:15:39 +1000524 BEGIN_LP_RING(6);
525 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
526 OUT_RING(0);
527 if (dev_priv->current_page == 0) {
528 OUT_RING(dev_priv->back_offset);
529 dev_priv->current_page = 1;
530 } else {
531 OUT_RING(dev_priv->front_offset);
532 dev_priv->current_page = 0;
533 }
534 OUT_RING(0);
535 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000536
Dave Airlieaf6061a2008-05-07 12:15:39 +1000537 BEGIN_LP_RING(2);
538 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
539 OUT_RING(0);
540 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000541
Dave Airlie7c1c2872008-11-28 14:22:24 +1000542 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000543
Dave Airlieaf6061a2008-05-07 12:15:39 +1000544 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700545 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000546 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000547 OUT_RING(dev_priv->counter);
548 OUT_RING(0);
549 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000550
Dave Airlie7c1c2872008-11-28 14:22:24 +1000551 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Dave Airlie84b1fd12007-07-11 15:53:27 +1000555static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 drm_i915_private_t *dev_priv = dev->dev_private;
558
559 i915_kernel_lost_context(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800560 return intel_wait_ring_buffer(dev, &dev_priv->render_ring,
561 dev_priv->render_ring.size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Eric Anholtc153f452007-09-03 12:06:45 +1000564static int i915_flush_ioctl(struct drm_device *dev, void *data,
565 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Eric Anholt546b0972008-09-01 16:45:29 -0700567 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Eric Anholt546b0972008-09-01 16:45:29 -0700569 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
570
571 mutex_lock(&dev->struct_mutex);
572 ret = i915_quiescent(dev);
573 mutex_unlock(&dev->struct_mutex);
574
575 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Eric Anholtc153f452007-09-03 12:06:45 +1000578static int i915_batchbuffer(struct drm_device *dev, void *data,
579 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000582 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000584 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000585 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700587 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 if (!dev_priv->allow_batchbuffer) {
590 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800594 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800595 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Eric Anholt546b0972008-09-01 16:45:29 -0700597 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Eric Anholt201361a2009-03-11 12:30:04 -0700599 if (batch->num_cliprects < 0)
600 return -EINVAL;
601
602 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700603 cliprects = kcalloc(batch->num_cliprects,
604 sizeof(struct drm_clip_rect),
605 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700606 if (cliprects == NULL)
607 return -ENOMEM;
608
609 ret = copy_from_user(cliprects, batch->cliprects,
610 batch->num_cliprects *
611 sizeof(struct drm_clip_rect));
612 if (ret != 0)
613 goto fail_free;
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Eric Anholt546b0972008-09-01 16:45:29 -0700616 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700617 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700618 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400620 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000621 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700622
623fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700624 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return ret;
627}
628
Eric Anholtc153f452007-09-03 12:06:45 +1000629static int i915_cmdbuffer(struct drm_device *dev, void *data,
630 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000633 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000635 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000636 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700637 struct drm_clip_rect *cliprects = NULL;
638 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int ret;
640
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800641 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800642 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Eric Anholt546b0972008-09-01 16:45:29 -0700644 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Eric Anholt201361a2009-03-11 12:30:04 -0700646 if (cmdbuf->num_cliprects < 0)
647 return -EINVAL;
648
Eric Anholt9a298b22009-03-24 12:23:04 -0700649 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700650 if (batch_data == NULL)
651 return -ENOMEM;
652
653 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
654 if (ret != 0)
655 goto fail_batch_free;
656
657 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700658 cliprects = kcalloc(cmdbuf->num_cliprects,
659 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000660 if (cliprects == NULL) {
661 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700662 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000663 }
Eric Anholt201361a2009-03-11 12:30:04 -0700664
665 ret = copy_from_user(cliprects, cmdbuf->cliprects,
666 cmdbuf->num_cliprects *
667 sizeof(struct drm_clip_rect));
668 if (ret != 0)
669 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
Eric Anholt546b0972008-09-01 16:45:29 -0700672 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700673 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700674 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (ret) {
676 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000677 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400680 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000681 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700682
Eric Anholt201361a2009-03-11 12:30:04 -0700683fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700684 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000685fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700686 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700687
688 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Eric Anholtc153f452007-09-03 12:06:45 +1000691static int i915_flip_bufs(struct drm_device *dev, void *data,
692 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Eric Anholt546b0972008-09-01 16:45:29 -0700694 int ret;
695
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800696 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Eric Anholt546b0972008-09-01 16:45:29 -0700698 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Eric Anholt546b0972008-09-01 16:45:29 -0700700 mutex_lock(&dev->struct_mutex);
701 ret = i915_dispatch_flip(dev);
702 mutex_unlock(&dev->struct_mutex);
703
704 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Eric Anholtc153f452007-09-03 12:06:45 +1000707static int i915_getparam(struct drm_device *dev, void *data,
708 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000711 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int value;
713
714 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000715 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000716 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
Eric Anholtc153f452007-09-03 12:06:45 +1000719 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700721 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723 case I915_PARAM_ALLOW_BATCHBUFFER:
724 value = dev_priv->allow_batchbuffer ? 1 : 0;
725 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100726 case I915_PARAM_LAST_DISPATCH:
727 value = READ_BREADCRUMB(dev_priv);
728 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400729 case I915_PARAM_CHIPSET_ID:
730 value = dev->pci_device;
731 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700732 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000733 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700734 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800735 case I915_PARAM_NUM_FENCES_AVAIL:
736 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
737 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200738 case I915_PARAM_HAS_OVERLAY:
739 value = dev_priv->overlay ? 1 : 0;
740 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800741 case I915_PARAM_HAS_PAGEFLIPPING:
742 value = 1;
743 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500744 case I915_PARAM_HAS_EXECBUF2:
745 /* depends on GEM */
746 value = dev_priv->has_gem;
747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800749 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -0500750 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000751 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Eric Anholtc153f452007-09-03 12:06:45 +1000754 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000756 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 return 0;
760}
761
Eric Anholtc153f452007-09-03 12:06:45 +1000762static int i915_setparam(struct drm_device *dev, void *data,
763 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000766 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000769 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000770 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
Eric Anholtc153f452007-09-03 12:06:45 +1000773 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000777 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 break;
779 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000780 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800782 case I915_SETPARAM_NUM_USED_FENCES:
783 if (param->value > dev_priv->num_fence_regs ||
784 param->value < 0)
785 return -EINVAL;
786 /* Userspace can use first N regs */
787 dev_priv->fence_reg_start = param->value;
788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800790 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800791 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000792 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
795 return 0;
796}
797
Eric Anholtc153f452007-09-03 12:06:45 +1000798static int i915_set_status_page(struct drm_device *dev, void *data,
799 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000800{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000801 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000802 drm_i915_hws_addr_t *hws = data;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800803 struct intel_ring_buffer *ring = &dev_priv->render_ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000804
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000805 if (!I915_NEED_GFX_HWS(dev))
806 return -EINVAL;
807
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000808 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000809 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000810 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000811 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000812
Jesse Barnes79e53942008-11-07 14:24:08 -0800813 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
814 WARN(1, "tried to set status page when mode setting active\n");
815 return 0;
816 }
817
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800818 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000819
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800820 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +1000821
Eric Anholt8b409582007-11-22 16:40:37 +1000822 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000823 dev_priv->hws_map.size = 4*1024;
824 dev_priv->hws_map.type = 0;
825 dev_priv->hws_map.flags = 0;
826 dev_priv->hws_map.mtrr = 0;
827
Dave Airliedd0910b2009-02-25 14:49:21 +1000828 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000829 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000830 i915_dma_cleanup(dev);
831 dev_priv->status_gfx_addr = 0;
832 DRM_ERROR("can not ioremap virtual address for"
833 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000834 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000835 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800836 ring->status_page.page_addr = dev_priv->hws_map.handle;
837 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
838 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000839
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800840 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Zou Nan hai852835f2010-05-21 09:08:56 +0800841 dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800842 DRM_DEBUG_DRIVER("load hws at %p\n",
Zou Nan hai852835f2010-05-21 09:08:56 +0800843 dev_priv->hw_status_page);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000844 return 0;
845}
846
Dave Airlieec2a4c32009-08-04 11:43:41 +1000847static int i915_get_bridge_dev(struct drm_device *dev)
848{
849 struct drm_i915_private *dev_priv = dev->dev_private;
850
851 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
852 if (!dev_priv->bridge_dev) {
853 DRM_ERROR("bridge device not found\n");
854 return -1;
855 }
856 return 0;
857}
858
Zhenyu Wangc48044112009-12-17 14:48:43 +0800859#define MCHBAR_I915 0x44
860#define MCHBAR_I965 0x48
861#define MCHBAR_SIZE (4*4096)
862
863#define DEVEN_REG 0x54
864#define DEVEN_MCHBAR_EN (1 << 28)
865
866/* Allocate space for the MCH regs if needed, return nonzero on error */
867static int
868intel_alloc_mchbar_resource(struct drm_device *dev)
869{
870 drm_i915_private_t *dev_priv = dev->dev_private;
871 int reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
872 u32 temp_lo, temp_hi = 0;
873 u64 mchbar_addr;
874 int ret = 0;
875
876 if (IS_I965G(dev))
877 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
878 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
879 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
880
881 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
882#ifdef CONFIG_PNP
883 if (mchbar_addr &&
884 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE)) {
885 ret = 0;
886 goto out;
887 }
888#endif
889
890 /* Get some space for it */
891 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus, &dev_priv->mch_res,
892 MCHBAR_SIZE, MCHBAR_SIZE,
893 PCIBIOS_MIN_MEM,
894 0, pcibios_align_resource,
895 dev_priv->bridge_dev);
896 if (ret) {
897 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
898 dev_priv->mch_res.start = 0;
899 goto out;
900 }
901
902 if (IS_I965G(dev))
903 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
904 upper_32_bits(dev_priv->mch_res.start));
905
906 pci_write_config_dword(dev_priv->bridge_dev, reg,
907 lower_32_bits(dev_priv->mch_res.start));
908out:
909 return ret;
910}
911
912/* Setup MCHBAR if possible, return true if we should disable it again */
913static void
914intel_setup_mchbar(struct drm_device *dev)
915{
916 drm_i915_private_t *dev_priv = dev->dev_private;
917 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
918 u32 temp;
919 bool enabled;
920
921 dev_priv->mchbar_need_disable = false;
922
923 if (IS_I915G(dev) || IS_I915GM(dev)) {
924 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
925 enabled = !!(temp & DEVEN_MCHBAR_EN);
926 } else {
927 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
928 enabled = temp & 1;
929 }
930
931 /* If it's already enabled, don't have to do anything */
932 if (enabled)
933 return;
934
935 if (intel_alloc_mchbar_resource(dev))
936 return;
937
938 dev_priv->mchbar_need_disable = true;
939
940 /* Space is allocated or reserved, so enable it. */
941 if (IS_I915G(dev) || IS_I915GM(dev)) {
942 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
943 temp | DEVEN_MCHBAR_EN);
944 } else {
945 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
946 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
947 }
948}
949
950static void
951intel_teardown_mchbar(struct drm_device *dev)
952{
953 drm_i915_private_t *dev_priv = dev->dev_private;
954 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
955 u32 temp;
956
957 if (dev_priv->mchbar_need_disable) {
958 if (IS_I915G(dev) || IS_I915GM(dev)) {
959 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
960 temp &= ~DEVEN_MCHBAR_EN;
961 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
962 } else {
963 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
964 temp &= ~1;
965 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
966 }
967 }
968
969 if (dev_priv->mch_res.start)
970 release_resource(&dev_priv->mch_res);
971}
972
Jesse Barnes79e53942008-11-07 14:24:08 -0800973/**
974 * i915_probe_agp - get AGP bootup configuration
975 * @pdev: PCI device
976 * @aperture_size: returns AGP aperture configured size
977 * @preallocated_size: returns size of BIOS preallocated AGP space
978 *
979 * Since Intel integrated graphics are UMA, the BIOS has to set aside
980 * some RAM for the framebuffer at early boot. This code figures out
981 * how much was set aside so we can use it for our own purposes.
982 */
Eric Anholt2a34f5e62009-07-02 09:30:50 -0700983static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,
Jesse Barnes80824002009-09-10 15:28:06 -0700984 uint32_t *preallocated_size,
985 uint32_t *start)
Jesse Barnes79e53942008-11-07 14:24:08 -0800986{
Dave Airlieec2a4c32009-08-04 11:43:41 +1000987 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800988 u16 tmp = 0;
989 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -0800990 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -0800991
Jesse Barnes79e53942008-11-07 14:24:08 -0800992 /* Get the fb aperture size and "stolen" memory amount. */
Dave Airlieec2a4c32009-08-04 11:43:41 +1000993 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &tmp);
Jesse Barnes79e53942008-11-07 14:24:08 -0800994
995 *aperture_size = 1024 * 1024;
996 *preallocated_size = 1024 * 1024;
997
Eric Anholt60fd99e2008-12-03 22:50:02 -0800998 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800999 case PCI_DEVICE_ID_INTEL_82830_CGC:
1000 case PCI_DEVICE_ID_INTEL_82845G_IG:
1001 case PCI_DEVICE_ID_INTEL_82855GM_IG:
1002 case PCI_DEVICE_ID_INTEL_82865_IG:
1003 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
1004 *aperture_size *= 64;
1005 else
1006 *aperture_size *= 128;
1007 break;
1008 default:
1009 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -08001010 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001011 break;
1012 }
1013
1014 /*
1015 * Some of the preallocated space is taken by the GTT
1016 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
1017 */
Eric Anholtbad720f2009-10-22 16:11:14 -07001018 if (IS_G4X(dev) || IS_PINEVIEW(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -08001019 overhead = 4096;
1020 else
1021 overhead = (*aperture_size / 1024) + 4096;
1022
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001023 if (IS_GEN6(dev)) {
1024 /* SNB has memory control reg at 0x50.w */
1025 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &tmp);
1026
1027 switch (tmp & SNB_GMCH_GMS_STOLEN_MASK) {
1028 case INTEL_855_GMCH_GMS_DISABLED:
Eric Anholtbad720f2009-10-22 16:11:14 -07001029 DRM_ERROR("video memory is disabled\n");
1030 return -1;
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001031 case SNB_GMCH_GMS_STOLEN_32M:
1032 stolen = 32 * 1024 * 1024;
1033 break;
1034 case SNB_GMCH_GMS_STOLEN_64M:
1035 stolen = 64 * 1024 * 1024;
1036 break;
1037 case SNB_GMCH_GMS_STOLEN_96M:
1038 stolen = 96 * 1024 * 1024;
1039 break;
1040 case SNB_GMCH_GMS_STOLEN_128M:
1041 stolen = 128 * 1024 * 1024;
1042 break;
1043 case SNB_GMCH_GMS_STOLEN_160M:
1044 stolen = 160 * 1024 * 1024;
1045 break;
1046 case SNB_GMCH_GMS_STOLEN_192M:
1047 stolen = 192 * 1024 * 1024;
1048 break;
1049 case SNB_GMCH_GMS_STOLEN_224M:
1050 stolen = 224 * 1024 * 1024;
1051 break;
1052 case SNB_GMCH_GMS_STOLEN_256M:
1053 stolen = 256 * 1024 * 1024;
1054 break;
1055 case SNB_GMCH_GMS_STOLEN_288M:
1056 stolen = 288 * 1024 * 1024;
1057 break;
1058 case SNB_GMCH_GMS_STOLEN_320M:
1059 stolen = 320 * 1024 * 1024;
1060 break;
1061 case SNB_GMCH_GMS_STOLEN_352M:
1062 stolen = 352 * 1024 * 1024;
1063 break;
1064 case SNB_GMCH_GMS_STOLEN_384M:
1065 stolen = 384 * 1024 * 1024;
1066 break;
1067 case SNB_GMCH_GMS_STOLEN_416M:
1068 stolen = 416 * 1024 * 1024;
1069 break;
1070 case SNB_GMCH_GMS_STOLEN_448M:
1071 stolen = 448 * 1024 * 1024;
1072 break;
1073 case SNB_GMCH_GMS_STOLEN_480M:
1074 stolen = 480 * 1024 * 1024;
1075 break;
1076 case SNB_GMCH_GMS_STOLEN_512M:
1077 stolen = 512 * 1024 * 1024;
1078 break;
1079 default:
1080 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
1081 tmp & SNB_GMCH_GMS_STOLEN_MASK);
1082 return -1;
Eric Anholtbad720f2009-10-22 16:11:14 -07001083 }
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001084 } else {
1085 switch (tmp & INTEL_GMCH_GMS_MASK) {
1086 case INTEL_855_GMCH_GMS_DISABLED:
1087 DRM_ERROR("video memory is disabled\n");
1088 return -1;
1089 case INTEL_855_GMCH_GMS_STOLEN_1M:
1090 stolen = 1 * 1024 * 1024;
1091 break;
1092 case INTEL_855_GMCH_GMS_STOLEN_4M:
1093 stolen = 4 * 1024 * 1024;
1094 break;
1095 case INTEL_855_GMCH_GMS_STOLEN_8M:
1096 stolen = 8 * 1024 * 1024;
1097 break;
1098 case INTEL_855_GMCH_GMS_STOLEN_16M:
1099 stolen = 16 * 1024 * 1024;
1100 break;
1101 case INTEL_855_GMCH_GMS_STOLEN_32M:
1102 stolen = 32 * 1024 * 1024;
1103 break;
1104 case INTEL_915G_GMCH_GMS_STOLEN_48M:
1105 stolen = 48 * 1024 * 1024;
1106 break;
1107 case INTEL_915G_GMCH_GMS_STOLEN_64M:
1108 stolen = 64 * 1024 * 1024;
1109 break;
1110 case INTEL_GMCH_GMS_STOLEN_128M:
1111 stolen = 128 * 1024 * 1024;
1112 break;
1113 case INTEL_GMCH_GMS_STOLEN_256M:
1114 stolen = 256 * 1024 * 1024;
1115 break;
1116 case INTEL_GMCH_GMS_STOLEN_96M:
1117 stolen = 96 * 1024 * 1024;
1118 break;
1119 case INTEL_GMCH_GMS_STOLEN_160M:
1120 stolen = 160 * 1024 * 1024;
1121 break;
1122 case INTEL_GMCH_GMS_STOLEN_224M:
1123 stolen = 224 * 1024 * 1024;
1124 break;
1125 case INTEL_GMCH_GMS_STOLEN_352M:
1126 stolen = 352 * 1024 * 1024;
1127 break;
1128 default:
1129 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
1130 tmp & INTEL_GMCH_GMS_MASK);
1131 return -1;
1132 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001133 }
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001134
Eric Anholt241fa852009-01-02 18:05:51 -08001135 *preallocated_size = stolen - overhead;
Jesse Barnes80824002009-09-10 15:28:06 -07001136 *start = overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -08001137
1138 return 0;
1139}
1140
Jesse Barnes80824002009-09-10 15:28:06 -07001141#define PTE_ADDRESS_MASK 0xfffff000
1142#define PTE_ADDRESS_MASK_HIGH 0x000000f0 /* i915+ */
1143#define PTE_MAPPING_TYPE_UNCACHED (0 << 1)
1144#define PTE_MAPPING_TYPE_DCACHE (1 << 1) /* i830 only */
1145#define PTE_MAPPING_TYPE_CACHED (3 << 1)
1146#define PTE_MAPPING_TYPE_MASK (3 << 1)
1147#define PTE_VALID (1 << 0)
1148
1149/**
1150 * i915_gtt_to_phys - take a GTT address and turn it into a physical one
1151 * @dev: drm device
1152 * @gtt_addr: address to translate
1153 *
1154 * Some chip functions require allocations from stolen space but need the
1155 * physical address of the memory in question. We use this routine
1156 * to get a physical address suitable for register programming from a given
1157 * GTT address.
1158 */
1159static unsigned long i915_gtt_to_phys(struct drm_device *dev,
1160 unsigned long gtt_addr)
1161{
1162 unsigned long *gtt;
1163 unsigned long entry, phys;
1164 int gtt_bar = IS_I9XX(dev) ? 0 : 1;
1165 int gtt_offset, gtt_size;
1166
1167 if (IS_I965G(dev)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001168 if (IS_G4X(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001169 gtt_offset = 2*1024*1024;
1170 gtt_size = 2*1024*1024;
1171 } else {
1172 gtt_offset = 512*1024;
1173 gtt_size = 512*1024;
1174 }
1175 } else {
1176 gtt_bar = 3;
1177 gtt_offset = 0;
1178 gtt_size = pci_resource_len(dev->pdev, gtt_bar);
1179 }
1180
1181 gtt = ioremap_wc(pci_resource_start(dev->pdev, gtt_bar) + gtt_offset,
1182 gtt_size);
1183 if (!gtt) {
1184 DRM_ERROR("ioremap of GTT failed\n");
1185 return 0;
1186 }
1187
1188 entry = *(volatile u32 *)(gtt + (gtt_addr / 1024));
1189
Zhao Yakui44d98a62009-10-09 11:39:40 +08001190 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, PTE: 0x%08lx\n", gtt_addr, entry);
Jesse Barnes80824002009-09-10 15:28:06 -07001191
1192 /* Mask out these reserved bits on this hardware. */
1193 if (!IS_I9XX(dev) || IS_I915G(dev) || IS_I915GM(dev) ||
1194 IS_I945G(dev) || IS_I945GM(dev)) {
1195 entry &= ~PTE_ADDRESS_MASK_HIGH;
1196 }
1197
1198 /* If it's not a mapping type we know, then bail. */
1199 if ((entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_UNCACHED &&
1200 (entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_CACHED) {
1201 iounmap(gtt);
1202 return 0;
1203 }
1204
1205 if (!(entry & PTE_VALID)) {
1206 DRM_ERROR("bad GTT entry in stolen space\n");
1207 iounmap(gtt);
1208 return 0;
1209 }
1210
1211 iounmap(gtt);
1212
1213 phys =(entry & PTE_ADDRESS_MASK) |
1214 ((uint64_t)(entry & PTE_ADDRESS_MASK_HIGH) << (32 - 4));
1215
Zhao Yakui44d98a62009-10-09 11:39:40 +08001216 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, phys addr: 0x%08lx\n", gtt_addr, phys);
Jesse Barnes80824002009-09-10 15:28:06 -07001217
1218 return phys;
1219}
1220
1221static void i915_warn_stolen(struct drm_device *dev)
1222{
1223 DRM_ERROR("not enough stolen space for compressed buffer, disabling\n");
1224 DRM_ERROR("hint: you may be able to increase stolen memory size in the BIOS to avoid this\n");
1225}
1226
1227static void i915_setup_compression(struct drm_device *dev, int size)
1228{
1229 struct drm_i915_private *dev_priv = dev->dev_private;
1230 struct drm_mm_node *compressed_fb, *compressed_llb;
Andrew Morton29bd0ae2009-11-17 14:08:52 -08001231 unsigned long cfb_base;
1232 unsigned long ll_base = 0;
Jesse Barnes80824002009-09-10 15:28:06 -07001233
1234 /* Leave 1M for line length buffer & misc. */
1235 compressed_fb = drm_mm_search_free(&dev_priv->vram, size, 4096, 0);
1236 if (!compressed_fb) {
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001237 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001238 i915_warn_stolen(dev);
1239 return;
1240 }
1241
1242 compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
1243 if (!compressed_fb) {
1244 i915_warn_stolen(dev);
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001245 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001246 return;
1247 }
1248
Jesse Barnes74dff282009-09-14 15:39:40 -07001249 cfb_base = i915_gtt_to_phys(dev, compressed_fb->start);
1250 if (!cfb_base) {
1251 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1252 drm_mm_put_block(compressed_fb);
Jesse Barnes80824002009-09-10 15:28:06 -07001253 }
1254
Jesse Barnes74dff282009-09-14 15:39:40 -07001255 if (!IS_GM45(dev)) {
1256 compressed_llb = drm_mm_search_free(&dev_priv->vram, 4096,
1257 4096, 0);
1258 if (!compressed_llb) {
1259 i915_warn_stolen(dev);
1260 return;
1261 }
1262
1263 compressed_llb = drm_mm_get_block(compressed_llb, 4096, 4096);
1264 if (!compressed_llb) {
1265 i915_warn_stolen(dev);
1266 return;
1267 }
1268
1269 ll_base = i915_gtt_to_phys(dev, compressed_llb->start);
1270 if (!ll_base) {
1271 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1272 drm_mm_put_block(compressed_fb);
1273 drm_mm_put_block(compressed_llb);
1274 }
Jesse Barnes80824002009-09-10 15:28:06 -07001275 }
1276
1277 dev_priv->cfb_size = size;
1278
Adam Jacksonee5382a2010-04-23 11:17:39 -04001279 intel_disable_fbc(dev);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001280 dev_priv->compressed_fb = compressed_fb;
1281
Jesse Barnes74dff282009-09-14 15:39:40 -07001282 if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001283 I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
1284 } else {
Jesse Barnes74dff282009-09-14 15:39:40 -07001285 I915_WRITE(FBC_CFB_BASE, cfb_base);
1286 I915_WRITE(FBC_LL_BASE, ll_base);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001287 dev_priv->compressed_llb = compressed_llb;
Jesse Barnes80824002009-09-10 15:28:06 -07001288 }
1289
Jesse Barnes80824002009-09-10 15:28:06 -07001290 DRM_DEBUG("FBC base 0x%08lx, ll base 0x%08lx, size %dM\n", cfb_base,
1291 ll_base, size >> 20);
Jesse Barnes80824002009-09-10 15:28:06 -07001292}
1293
Jesse Barnes20bf3772010-04-21 11:39:22 -07001294static void i915_cleanup_compression(struct drm_device *dev)
1295{
1296 struct drm_i915_private *dev_priv = dev->dev_private;
1297
1298 drm_mm_put_block(dev_priv->compressed_fb);
1299 if (!IS_GM45(dev))
1300 drm_mm_put_block(dev_priv->compressed_llb);
1301}
1302
Dave Airlie28d52042009-09-21 14:33:58 +10001303/* true = enable decode, false = disable decoder */
1304static unsigned int i915_vga_set_decode(void *cookie, bool state)
1305{
1306 struct drm_device *dev = cookie;
1307
1308 intel_modeset_vga_set_state(dev, state);
1309 if (state)
1310 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1311 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1312 else
1313 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1314}
1315
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001316static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1317{
1318 struct drm_device *dev = pci_get_drvdata(pdev);
1319 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1320 if (state == VGA_SWITCHEROO_ON) {
1321 printk(KERN_INFO "i915: switched off\n");
1322 /* i915 resume handler doesn't set to D0 */
1323 pci_set_power_state(dev->pdev, PCI_D0);
1324 i915_resume(dev);
1325 } else {
1326 printk(KERN_ERR "i915: switched off\n");
1327 i915_suspend(dev, pmm);
1328 }
1329}
1330
1331static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1332{
1333 struct drm_device *dev = pci_get_drvdata(pdev);
1334 bool can_switch;
1335
1336 spin_lock(&dev->count_lock);
1337 can_switch = (dev->open_count == 0);
1338 spin_unlock(&dev->count_lock);
1339 return can_switch;
1340}
1341
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001342static int i915_load_modeset_init(struct drm_device *dev,
Jesse Barnes80824002009-09-10 15:28:06 -07001343 unsigned long prealloc_start,
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001344 unsigned long prealloc_size,
1345 unsigned long agp_size)
Jesse Barnes79e53942008-11-07 14:24:08 -08001346{
1347 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001348 int fb_bar = IS_I9XX(dev) ? 2 : 0;
1349 int ret = 0;
1350
1351 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
1352 0xff000000;
1353
Jesse Barnes79e53942008-11-07 14:24:08 -08001354 /* Basic memrange allocator for stolen space (aka vram) */
1355 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
Jesse Barnes80824002009-09-10 15:28:06 -07001356 DRM_INFO("set up %ldM of stolen space\n", prealloc_size / (1024*1024));
Jesse Barnes79e53942008-11-07 14:24:08 -08001357
Ben Gamari11ed50e2009-09-14 17:48:45 -04001358 /* We're off and running w/KMS */
1359 dev_priv->mm.suspended = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001360
Eric Anholt13f4c432009-05-12 15:27:36 -07001361 /* Let GEM Manage from end of prealloc space to end of aperture.
1362 *
1363 * However, leave one page at the end still bound to the scratch page.
1364 * There are a number of places where the hardware apparently
1365 * prefetches past the end of the object, and we've seen multiple
1366 * hangs with the GPU head pointer stuck in a batchbuffer bound
1367 * at the last page of the aperture. One page should be enough to
1368 * keep any prefetching inside of the aperture.
1369 */
1370 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001371
Ben Gamari11ed50e2009-09-14 17:48:45 -04001372 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001373 ret = i915_gem_init_ringbuffer(dev);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001374 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001375 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001376 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001377
Jesse Barnes80824002009-09-10 15:28:06 -07001378 /* Try to set up FBC with a reasonable compressed buffer size */
Shaohua Li9216d442009-10-10 15:20:55 +08001379 if (I915_HAS_FBC(dev) && i915_powersave) {
Jesse Barnes80824002009-09-10 15:28:06 -07001380 int cfb_size;
1381
1382 /* Try to get an 8M buffer... */
1383 if (prealloc_size > (9*1024*1024))
1384 cfb_size = 8*1024*1024;
1385 else /* fall back to 7/8 of the stolen space */
1386 cfb_size = prealloc_size * 7 / 8;
1387 i915_setup_compression(dev, cfb_size);
1388 }
1389
Jesse Barnes79e53942008-11-07 14:24:08 -08001390 /* Allow hardware batchbuffers unless told otherwise.
1391 */
1392 dev_priv->allow_batchbuffer = 1;
1393
1394 ret = intel_init_bios(dev);
1395 if (ret)
1396 DRM_INFO("failed to find VBIOS tables\n");
1397
Dave Airlie28d52042009-09-21 14:33:58 +10001398 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1399 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1400 if (ret)
1401 goto destroy_ringbuffer;
1402
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001403 ret = vga_switcheroo_register_client(dev->pdev,
1404 i915_switcheroo_set_state,
1405 i915_switcheroo_can_switch);
1406 if (ret)
1407 goto destroy_ringbuffer;
1408
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001409 intel_modeset_init(dev);
1410
Jesse Barnes79e53942008-11-07 14:24:08 -08001411 ret = drm_irq_install(dev);
1412 if (ret)
1413 goto destroy_ringbuffer;
1414
Jesse Barnes79e53942008-11-07 14:24:08 -08001415 /* Always safe in the mode setting case. */
1416 /* FIXME: do pre/post-mode set stuff in core KMS code */
1417 dev->vblank_disable_allowed = 1;
1418
1419 /*
1420 * Initialize the hardware status page IRQ location.
1421 */
1422
1423 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1424
Dave Airlie38651672010-03-30 05:34:13 +00001425 intel_fbdev_init(dev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001426 drm_kms_helper_poll_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001427 return 0;
1428
Jesse Barnes79e53942008-11-07 14:24:08 -08001429destroy_ringbuffer:
Eric Anholt21099532009-11-09 14:57:34 -08001430 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001431 i915_gem_cleanup_ringbuffer(dev);
Eric Anholt21099532009-11-09 14:57:34 -08001432 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001433out:
1434 return ret;
1435}
1436
Dave Airlie7c1c2872008-11-28 14:22:24 +10001437int i915_master_create(struct drm_device *dev, struct drm_master *master)
1438{
1439 struct drm_i915_master_private *master_priv;
1440
Eric Anholt9a298b22009-03-24 12:23:04 -07001441 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001442 if (!master_priv)
1443 return -ENOMEM;
1444
1445 master->driver_priv = master_priv;
1446 return 0;
1447}
1448
1449void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1450{
1451 struct drm_i915_master_private *master_priv = master->driver_priv;
1452
1453 if (!master_priv)
1454 return;
1455
Eric Anholt9a298b22009-03-24 12:23:04 -07001456 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001457
1458 master->driver_priv = NULL;
1459}
1460
Shaohua Li7662c8b2009-06-26 11:23:55 +08001461static void i915_get_mem_freq(struct drm_device *dev)
1462{
1463 drm_i915_private_t *dev_priv = dev->dev_private;
1464 u32 tmp;
1465
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001466 if (!IS_PINEVIEW(dev))
Shaohua Li7662c8b2009-06-26 11:23:55 +08001467 return;
1468
1469 tmp = I915_READ(CLKCFG);
1470
1471 switch (tmp & CLKCFG_FSB_MASK) {
1472 case CLKCFG_FSB_533:
1473 dev_priv->fsb_freq = 533; /* 133*4 */
1474 break;
1475 case CLKCFG_FSB_800:
1476 dev_priv->fsb_freq = 800; /* 200*4 */
1477 break;
1478 case CLKCFG_FSB_667:
1479 dev_priv->fsb_freq = 667; /* 167*4 */
1480 break;
1481 case CLKCFG_FSB_400:
1482 dev_priv->fsb_freq = 400; /* 100*4 */
1483 break;
1484 }
1485
1486 switch (tmp & CLKCFG_MEM_MASK) {
1487 case CLKCFG_MEM_533:
1488 dev_priv->mem_freq = 533;
1489 break;
1490 case CLKCFG_MEM_667:
1491 dev_priv->mem_freq = 667;
1492 break;
1493 case CLKCFG_MEM_800:
1494 dev_priv->mem_freq = 800;
1495 break;
1496 }
1497}
1498
Jesse Barnes79e53942008-11-07 14:24:08 -08001499/**
1500 * i915_driver_load - setup chip and create an initial config
1501 * @dev: DRM device
1502 * @flags: startup flags
1503 *
1504 * The driver load routine has to do several things:
1505 * - drive output discovery via intel_modeset_init()
1506 * - initialize the memory manager
1507 * - allocate initial config memory
1508 * - setup the DRM framebuffer with the allocated memory
1509 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001510int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001511{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001512 struct drm_i915_private *dev_priv;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001513 resource_size_t base, size;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001514 int ret = 0, mmio_bar;
Jesse Barnes80824002009-09-10 15:28:06 -07001515 uint32_t agp_size, prealloc_size, prealloc_start;
Dave Airlie22eae942005-11-10 22:16:34 +11001516 /* i915 has 4 more counters */
1517 dev->counters += 4;
1518 dev->types[6] = _DRM_STAT_IRQ;
1519 dev->types[7] = _DRM_STAT_PRIMARY;
1520 dev->types[8] = _DRM_STAT_SECONDARY;
1521 dev->types[9] = _DRM_STAT_DMA;
1522
Eric Anholt9a298b22009-03-24 12:23:04 -07001523 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001524 if (dev_priv == NULL)
1525 return -ENOMEM;
1526
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001527 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001528 dev_priv->dev = dev;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001529 dev_priv->info = (struct intel_device_info *) flags;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001530
1531 /* Add register map (needed for suspend/resume) */
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001532 mmio_bar = IS_I9XX(dev) ? 0 : 1;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001533 base = drm_get_resource_start(dev, mmio_bar);
1534 size = drm_get_resource_len(dev, mmio_bar);
1535
Dave Airlieec2a4c32009-08-04 11:43:41 +10001536 if (i915_get_bridge_dev(dev)) {
1537 ret = -EIO;
1538 goto free_priv;
1539 }
1540
Eric Anholt3043c602008-10-02 12:24:47 -07001541 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001542 if (!dev_priv->regs) {
1543 DRM_ERROR("failed to map registers\n");
1544 ret = -EIO;
Dave Airlieec2a4c32009-08-04 11:43:41 +10001545 goto put_bridge;
Jesse Barnes79e53942008-11-07 14:24:08 -08001546 }
Eric Anholted4cb412008-07-29 12:10:39 -07001547
Eric Anholtab657db12009-01-23 12:57:47 -08001548 dev_priv->mm.gtt_mapping =
1549 io_mapping_create_wc(dev->agp->base,
1550 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001551 if (dev_priv->mm.gtt_mapping == NULL) {
1552 ret = -EIO;
1553 goto out_rmmap;
1554 }
1555
Eric Anholtab657db12009-01-23 12:57:47 -08001556 /* Set up a WC MTRR for non-PAT systems. This is more common than
1557 * one would think, because the kernel disables PAT on first
1558 * generation Core chips because WC PAT gets overridden by a UC
1559 * MTRR if present. Even if a UC MTRR isn't present.
1560 */
1561 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1562 dev->agp->agp_info.aper_size *
1563 1024 * 1024,
1564 MTRR_TYPE_WRCOMB, 1);
1565 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001566 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001567 "performance may suffer.\n");
1568 }
1569
Jesse Barnes80824002009-09-10 15:28:06 -07001570 ret = i915_probe_agp(dev, &agp_size, &prealloc_size, &prealloc_start);
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001571 if (ret)
1572 goto out_iomapfree;
1573
Chris Wilsonaed5f1d2009-10-14 13:40:04 +01001574 dev_priv->wq = create_singlethread_workqueue("i915");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001575 if (dev_priv->wq == NULL) {
1576 DRM_ERROR("Failed to create our workqueue.\n");
1577 ret = -ENOMEM;
1578 goto out_iomapfree;
1579 }
1580
Dave Airlieac5c4e72008-12-19 15:38:34 +10001581 /* enable GEM by default */
1582 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10001583
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001584 if (prealloc_size > agp_size * 3 / 4) {
1585 DRM_ERROR("Detected broken video BIOS with %d/%dkB of video "
1586 "memory stolen.\n",
1587 prealloc_size / 1024, agp_size / 1024);
1588 DRM_ERROR("Disabling GEM. (try reducing stolen memory or "
1589 "updating the BIOS to fix).\n");
1590 dev_priv->has_gem = 0;
1591 }
1592
Chris Wilson79a78dd2010-05-17 09:23:54 +01001593 if (dev_priv->has_gem == 0 &&
1594 drm_core_check_feature(dev, DRIVER_MODESET)) {
1595 DRM_ERROR("kernel modesetting requires GEM, disabling driver.\n");
1596 ret = -ENODEV;
1597 goto out_iomapfree;
1598 }
1599
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001600 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001601 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eric Anholtbad720f2009-10-22 16:11:14 -07001602 if (IS_G4X(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001603 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001604 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001605 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001606
Zhenyu Wangc48044112009-12-17 14:48:43 +08001607 /* Try to make sure MCHBAR is enabled before poking at it */
1608 intel_setup_mchbar(dev);
1609
Eric Anholt673a3942008-07-30 12:06:12 -07001610 i915_gem_load(dev);
1611
Keith Packard398c9cb2008-07-30 13:03:43 -07001612 /* Init HWS */
1613 if (!I915_NEED_GFX_HWS(dev)) {
1614 ret = i915_init_phys_hws(dev);
1615 if (ret != 0)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001616 goto out_workqueue_free;
Keith Packard398c9cb2008-07-30 13:03:43 -07001617 }
Eric Anholted4cb412008-07-29 12:10:39 -07001618
Shaohua Li7662c8b2009-06-26 11:23:55 +08001619 i915_get_mem_freq(dev);
1620
Eric Anholted4cb412008-07-29 12:10:39 -07001621 /* On the 945G/GM, the chipset reports the MSI capability on the
1622 * integrated graphics even though the support isn't actually there
1623 * according to the published specs. It doesn't appear to function
1624 * correctly in testing on 945G.
1625 * This may be a side effect of MSI having been made available for PEG
1626 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001627 *
1628 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001629 * be lost or delayed, but we use them anyways to avoid
1630 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001631 */
Keith Packardb60678a2008-12-08 11:12:28 -08001632 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001633 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001634
1635 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001636 spin_lock_init(&dev_priv->error_lock);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001637 dev_priv->trace_irq_seqno = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001638
Keith Packard52440212008-11-18 09:30:25 -08001639 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1640
1641 if (ret) {
1642 (void) i915_driver_unload(dev);
1643 return ret;
1644 }
1645
Ben Gamari11ed50e2009-09-14 17:48:45 -04001646 /* Start out suspended */
1647 dev_priv->mm.suspended = 1;
1648
Zhenyu Wang3bad0782010-04-07 16:15:53 +08001649 intel_detect_pch(dev);
1650
Jesse Barnes79e53942008-11-07 14:24:08 -08001651 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001652 ret = i915_load_modeset_init(dev, prealloc_start,
1653 prealloc_size, agp_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001654 if (ret < 0) {
1655 DRM_ERROR("failed to init modeset\n");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001656 goto out_workqueue_free;
Jesse Barnes79e53942008-11-07 14:24:08 -08001657 }
1658 }
1659
Matthew Garrett74a365b2009-03-19 21:35:39 +00001660 /* Must be done after probing outputs */
Zhao Yakui01c66882009-10-28 05:10:00 +00001661 intel_opregion_init(dev, 0);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001662
Ben Gamarif65d9422009-09-14 17:48:44 -04001663 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
1664 (unsigned long) dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001665 return 0;
1666
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001667out_workqueue_free:
1668 destroy_workqueue(dev_priv->wq);
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001669out_iomapfree:
1670 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001671out_rmmap:
1672 iounmap(dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001673put_bridge:
1674 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001675free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001676 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001677 return ret;
1678}
1679
1680int i915_driver_unload(struct drm_device *dev)
1681{
1682 struct drm_i915_private *dev_priv = dev->dev_private;
1683
Chris Wilson9df30792010-02-18 10:24:56 +00001684 i915_destroy_error_state(dev);
1685
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001686 destroy_workqueue(dev_priv->wq);
Ben Gamarif65d9422009-09-14 17:48:44 -04001687 del_timer_sync(&dev_priv->hangcheck_timer);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001688
Eric Anholtab657db12009-01-23 12:57:47 -08001689 io_mapping_free(dev_priv->mm.gtt_mapping);
1690 if (dev_priv->mm.gtt_mtrr >= 0) {
1691 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1692 dev->agp->agp_info.aper_size * 1024 * 1024);
1693 dev_priv->mm.gtt_mtrr = -1;
1694 }
1695
Jesse Barnes79e53942008-11-07 14:24:08 -08001696 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001697 intel_modeset_cleanup(dev);
1698
Zhao Yakui6363ee62009-11-24 09:48:44 +08001699 /*
1700 * free the memory space allocated for the child device
1701 * config parsed from VBT
1702 */
1703 if (dev_priv->child_dev && dev_priv->child_dev_num) {
1704 kfree(dev_priv->child_dev);
1705 dev_priv->child_dev = NULL;
1706 dev_priv->child_dev_num = 0;
1707 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001708 drm_irq_uninstall(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001709 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001710 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001711 }
1712
Eric Anholted4cb412008-07-29 12:10:39 -07001713 if (dev->pdev->msi_enabled)
1714 pci_disable_msi(dev->pdev);
1715
Eric Anholt3043c602008-10-02 12:24:47 -07001716 if (dev_priv->regs != NULL)
1717 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001718
Zhao Yakui01c66882009-10-28 05:10:00 +00001719 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001720
Jesse Barnes79e53942008-11-07 14:24:08 -08001721 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie71acb5e2008-12-30 20:31:46 +10001722 i915_gem_free_all_phys_object(dev);
1723
Jesse Barnes79e53942008-11-07 14:24:08 -08001724 mutex_lock(&dev->struct_mutex);
1725 i915_gem_cleanup_ringbuffer(dev);
1726 mutex_unlock(&dev->struct_mutex);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001727 if (I915_HAS_FBC(dev) && i915_powersave)
1728 i915_cleanup_compression(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001729 drm_mm_takedown(&dev_priv->vram);
1730 i915_gem_lastclose(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001731
1732 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001733 }
1734
Zhenyu Wangc48044112009-12-17 14:48:43 +08001735 intel_teardown_mchbar(dev);
1736
Dave Airlieec2a4c32009-08-04 11:43:41 +10001737 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07001738 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001739
Dave Airlie22eae942005-11-10 22:16:34 +11001740 return 0;
1741}
1742
Eric Anholt673a3942008-07-30 12:06:12 -07001743int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1744{
1745 struct drm_i915_file_private *i915_file_priv;
1746
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001747 DRM_DEBUG_DRIVER("\n");
Eric Anholt673a3942008-07-30 12:06:12 -07001748 i915_file_priv = (struct drm_i915_file_private *)
Eric Anholt9a298b22009-03-24 12:23:04 -07001749 kmalloc(sizeof(*i915_file_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001750
1751 if (!i915_file_priv)
1752 return -ENOMEM;
1753
1754 file_priv->driver_priv = i915_file_priv;
1755
Eric Anholtb9624422009-06-03 07:27:35 +00001756 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001757
1758 return 0;
1759}
1760
Jesse Barnes79e53942008-11-07 14:24:08 -08001761/**
1762 * i915_driver_lastclose - clean up after all DRM clients have exited
1763 * @dev: DRM device
1764 *
1765 * Take care of cleaning up after all DRM clients have exited. In the
1766 * mode setting case, we want to restore the kernel's initial mode (just
1767 * in case the last client left us in a bad state).
1768 *
1769 * Additionally, in the non-mode setting case, we'll tear down the AGP
1770 * and DMA structures, since the kernel won't be using them, and clea
1771 * up any GEM state.
1772 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001773void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001775 drm_i915_private_t *dev_priv = dev->dev_private;
1776
Jesse Barnes79e53942008-11-07 14:24:08 -08001777 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001778 drm_fb_helper_restore();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001779 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001780 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001781 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001782
Eric Anholt673a3942008-07-30 12:06:12 -07001783 i915_gem_lastclose(dev);
1784
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001785 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001786 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001787
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001788 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Eric Anholt6c340ea2007-08-25 20:23:09 +10001791void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001793 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001794 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08001795 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1796 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Eric Anholt673a3942008-07-30 12:06:12 -07001799void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1800{
1801 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1802
Eric Anholt9a298b22009-03-24 12:23:04 -07001803 kfree(i915_file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001804}
1805
Eric Anholtc153f452007-09-03 12:06:45 +10001806struct drm_ioctl_desc i915_ioctls[] = {
1807 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1808 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1809 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1810 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1811 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1812 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1813 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1814 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1815 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1816 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1817 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1818 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1819 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1820 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1821 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1822 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001823 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 -08001824 DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1825 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
1826 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
1827 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1828 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1829 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
1830 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
1831 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1832 DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1833 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
1834 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
1835 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
1836 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
1837 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
1838 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
1839 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
1840 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
1841 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
1842 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
1843 DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
1844 DRM_IOCTL_DEF(DRM_I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
1845 DRM_IOCTL_DEF(DRM_I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1846 DRM_IOCTL_DEF(DRM_I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10001847};
1848
1849int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001850
1851/**
1852 * Determine if the device really is AGP or not.
1853 *
1854 * All Intel graphics chipsets are treated as AGP, even if they are really
1855 * PCI-e.
1856 *
1857 * \param dev The device to be tested.
1858 *
1859 * \returns
1860 * A value of 1 is always retured to indictate every i9x5 is AGP.
1861 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001862int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001863{
1864 return 1;
1865}