blob: f485880300ce89b0542db18106b9944d5bfda143 [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 Wangc4804412009-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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Keith Packard398c9cb2008-07-30 13:03:43 -0700134 /* Clear the HWS virtual address at teardown */
135 if (I915_NEED_GFX_HWS(dev))
136 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 return 0;
139}
140
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000141static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000143 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000144 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Dave Airlie3a03ac12009-01-11 09:03:49 +1000146 master_priv->sarea = drm_getsarea(dev);
147 if (master_priv->sarea) {
148 master_priv->sarea_priv = (drm_i915_sarea_t *)
149 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
150 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800151 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000152 }
153
Eric Anholt673a3942008-07-30 12:06:12 -0700154 if (init->ring_size != 0) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800155 if (dev_priv->render_ring.gem_object != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700156 i915_dma_cleanup(dev);
157 DRM_ERROR("Client tried to initialize ringbuffer in "
158 "GEM mode\n");
159 return -EINVAL;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800162 dev_priv->render_ring.size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Eric Anholtd3301d82010-05-21 13:55:54 -0700164 dev_priv->render_ring.map.offset = init->ring_start;
165 dev_priv->render_ring.map.size = init->ring_size;
166 dev_priv->render_ring.map.type = 0;
167 dev_priv->render_ring.map.flags = 0;
168 dev_priv->render_ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Eric Anholtd3301d82010-05-21 13:55:54 -0700170 drm_core_ioremap_wc(&dev_priv->render_ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700171
Eric Anholtd3301d82010-05-21 13:55:54 -0700172 if (dev_priv->render_ring.map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700173 i915_dma_cleanup(dev);
174 DRM_ERROR("can not ioremap virtual address for"
175 " ring buffer\n");
176 return -ENOMEM;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
Eric Anholtd3301d82010-05-21 13:55:54 -0700180 dev_priv->render_ring.virtual_start = dev_priv->render_ring.map.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000182 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 dev_priv->back_offset = init->back_offset;
184 dev_priv->front_offset = init->front_offset;
185 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000186 if (master_priv->sarea_priv)
187 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Allow hardware batchbuffers unless told otherwise.
190 */
191 dev_priv->allow_batchbuffer = 1;
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return 0;
194}
195
Dave Airlie84b1fd12007-07-11 15:53:27 +1000196static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
199
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800200 struct intel_ring_buffer *ring;
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800201 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800203 ring = &dev_priv->render_ring;
204
205 if (ring->map.handle == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 DRM_ERROR("can not ioremap virtual address for"
207 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000208 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800212 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000214 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800216 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800217 ring->status_page.page_addr);
218 if (ring->status_page.gfx_addr != 0)
219 ring->setup_status_page(dev, ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000220 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700221 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800222
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800223 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 return 0;
226}
227
Eric Anholtc153f452007-09-03 12:06:45 +1000228static int i915_dma_init(struct drm_device *dev, void *data,
229 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Eric Anholtc153f452007-09-03 12:06:45 +1000231 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int retcode = 0;
233
Eric Anholtc153f452007-09-03 12:06:45 +1000234 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000236 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
238 case I915_CLEANUP_DMA:
239 retcode = i915_dma_cleanup(dev);
240 break;
241 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100242 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 break;
244 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000245 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
247 }
248
249 return retcode;
250}
251
252/* Implement basically the same security restrictions as hardware does
253 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
254 *
255 * Most of the calculations below involve calculating the size of a
256 * particular instruction. It's important to get the size right as
257 * that tells us where the next instruction to check is. Any illegal
258 * instruction detected will be given a size of zero, which is a
259 * signal to abort the rest of the buffer.
260 */
261static int do_validate_cmd(int cmd)
262{
263 switch (((cmd >> 29) & 0x7)) {
264 case 0x0:
265 switch ((cmd >> 23) & 0x3f) {
266 case 0x0:
267 return 1; /* MI_NOOP */
268 case 0x4:
269 return 1; /* MI_FLUSH */
270 default:
271 return 0; /* disallow everything else */
272 }
273 break;
274 case 0x1:
275 return 0; /* reserved */
276 case 0x2:
277 return (cmd & 0xff) + 2; /* 2d commands */
278 case 0x3:
279 if (((cmd >> 24) & 0x1f) <= 0x18)
280 return 1;
281
282 switch ((cmd >> 24) & 0x1f) {
283 case 0x1c:
284 return 1;
285 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 case 0x3:
288 return (cmd & 0x1f) + 2;
289 case 0x4:
290 return (cmd & 0xf) + 2;
291 default:
292 return (cmd & 0xffff) + 2;
293 }
294 case 0x1e:
295 if (cmd & (1 << 23))
296 return (cmd & 0xffff) + 1;
297 else
298 return 1;
299 case 0x1f:
300 if ((cmd & (1 << 23)) == 0) /* inline vertices */
301 return (cmd & 0x1ffff) + 2;
302 else if (cmd & (1 << 17)) /* indirect random */
303 if ((cmd & 0xffff) == 0)
304 return 0; /* unknown length, too hard */
305 else
306 return (((cmd & 0xffff) + 1) / 2) + 1;
307 else
308 return 2; /* indirect sequential */
309 default:
310 return 0;
311 }
312 default:
313 return 0;
314 }
315
316 return 0;
317}
318
319static int validate_cmd(int cmd)
320{
321 int ret = do_validate_cmd(cmd);
322
Dave Airliebc5f4522007-11-05 12:50:58 +1000323/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 return ret;
326}
327
Eric Anholt201361a2009-03-11 12:30:04 -0700328static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 drm_i915_private_t *dev_priv = dev->dev_private;
331 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800333 if ((dwords+1) * sizeof(int) >= dev_priv->render_ring.size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000334 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100335
Alan Hourihanec29b6692006-08-12 16:29:24 +1000336 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 for (i = 0; i < dwords;) {
339 int cmd, sz;
340
Eric Anholt201361a2009-03-11 12:30:04 -0700341 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 OUT_RING(cmd);
347
348 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700349 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
Dave Airliede227f52006-01-25 15:31:43 +1100353 if (dwords & 1)
354 OUT_RING(0);
355
356 ADVANCE_LP_RING();
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359}
360
Eric Anholt673a3942008-07-30 12:06:12 -0700361int
362i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700363 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700364 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Eric Anholt201361a2009-03-11 12:30:04 -0700366 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
369 DRM_ERROR("Bad box %d,%d..%d,%d\n",
370 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000371 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Alan Hourihanec29b6692006-08-12 16:29:24 +1000374 if (IS_I965G(dev)) {
375 BEGIN_LP_RING(4);
376 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
377 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000378 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000379 OUT_RING(DR4);
380 ADVANCE_LP_RING();
381 } else {
382 BEGIN_LP_RING(6);
383 OUT_RING(GFX_OP_DRAWRECT_INFO);
384 OUT_RING(DR1);
385 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
386 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
387 OUT_RING(DR4);
388 OUT_RING(0);
389 ADVANCE_LP_RING();
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 return 0;
393}
394
Alan Hourihanec29b6692006-08-12 16:29:24 +1000395/* XXX: Emitting the counter should really be moved to part of the IRQ
396 * emit. For now, do it in both places:
397 */
398
Dave Airlie84b1fd12007-07-11 15:53:27 +1000399static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100400{
401 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000402 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100403
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400404 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000405 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400406 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000407 if (master_priv->sarea_priv)
408 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100409
410 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700411 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000412 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100413 OUT_RING(dev_priv->counter);
414 OUT_RING(0);
415 ADVANCE_LP_RING();
416}
417
Dave Airlie84b1fd12007-07-11 15:53:27 +1000418static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700419 drm_i915_cmdbuffer_t *cmd,
420 struct drm_clip_rect *cliprects,
421 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 int nbox = cmd->num_cliprects;
424 int i = 0, count, ret;
425
426 if (cmd->sz & 0x3) {
427 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000428 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 i915_kernel_lost_context(dev);
432
433 count = nbox ? nbox : 1;
434
435 for (i = 0; i < count; i++) {
436 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700437 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 cmd->DR1, cmd->DR4);
439 if (ret)
440 return ret;
441 }
442
Eric Anholt201361a2009-03-11 12:30:04 -0700443 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (ret)
445 return ret;
446 }
447
Dave Airliede227f52006-01-25 15:31:43 +1100448 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
450}
451
Dave Airlie84b1fd12007-07-11 15:53:27 +1000452static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700453 drm_i915_batchbuffer_t * batch,
454 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int nbox = batch->num_cliprects;
457 int i = 0, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if ((batch->start | batch->used) & 0x7) {
460 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 i915_kernel_lost_context(dev);
465
466 count = nbox ? nbox : 1;
467
468 for (i = 0; i < count; i++) {
469 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700470 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 batch->DR1, batch->DR4);
472 if (ret)
473 return ret;
474 }
475
Keith Packard0790d5e2008-07-30 12:28:47 -0700476 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000478 if (IS_I965G(dev)) {
479 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
480 OUT_RING(batch->start);
481 } else {
482 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
483 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 ADVANCE_LP_RING();
486 } else {
487 BEGIN_LP_RING(4);
488 OUT_RING(MI_BATCH_BUFFER);
489 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
490 OUT_RING(batch->start + batch->used - 4);
491 OUT_RING(0);
492 ADVANCE_LP_RING();
493 }
494 }
495
Dave Airliede227f52006-01-25 15:31:43 +1100496 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 return 0;
499}
500
Dave Airlieaf6061a2008-05-07 12:15:39 +1000501static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000504 struct drm_i915_master_private *master_priv =
505 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dave Airlie7c1c2872008-11-28 14:22:24 +1000507 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400508 return -EINVAL;
509
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800510 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800511 __func__,
512 dev_priv->current_page,
513 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Dave Airlieaf6061a2008-05-07 12:15:39 +1000515 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Dave Airlieaf6061a2008-05-07 12:15:39 +1000517 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700518 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000519 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ADVANCE_LP_RING();
521
Dave Airlieaf6061a2008-05-07 12:15:39 +1000522 BEGIN_LP_RING(6);
523 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
524 OUT_RING(0);
525 if (dev_priv->current_page == 0) {
526 OUT_RING(dev_priv->back_offset);
527 dev_priv->current_page = 1;
528 } else {
529 OUT_RING(dev_priv->front_offset);
530 dev_priv->current_page = 0;
531 }
532 OUT_RING(0);
533 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000534
Dave Airlieaf6061a2008-05-07 12:15:39 +1000535 BEGIN_LP_RING(2);
536 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
537 OUT_RING(0);
538 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000539
Dave Airlie7c1c2872008-11-28 14:22:24 +1000540 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000541
Dave Airlieaf6061a2008-05-07 12:15:39 +1000542 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700543 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000544 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000545 OUT_RING(dev_priv->counter);
546 OUT_RING(0);
547 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000548
Dave Airlie7c1c2872008-11-28 14:22:24 +1000549 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Dave Airlie84b1fd12007-07-11 15:53:27 +1000553static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 drm_i915_private_t *dev_priv = dev->dev_private;
556
557 i915_kernel_lost_context(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800558 return intel_wait_ring_buffer(dev, &dev_priv->render_ring,
559 dev_priv->render_ring.size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Eric Anholtc153f452007-09-03 12:06:45 +1000562static int i915_flush_ioctl(struct drm_device *dev, void *data,
563 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Eric Anholt546b0972008-09-01 16:45:29 -0700565 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Eric Anholt546b0972008-09-01 16:45:29 -0700567 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
568
569 mutex_lock(&dev->struct_mutex);
570 ret = i915_quiescent(dev);
571 mutex_unlock(&dev->struct_mutex);
572
573 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Eric Anholtc153f452007-09-03 12:06:45 +1000576static int i915_batchbuffer(struct drm_device *dev, void *data,
577 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000580 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000582 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000583 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700585 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (!dev_priv->allow_batchbuffer) {
588 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000589 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800592 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800593 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Eric Anholt546b0972008-09-01 16:45:29 -0700595 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Eric Anholt201361a2009-03-11 12:30:04 -0700597 if (batch->num_cliprects < 0)
598 return -EINVAL;
599
600 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700601 cliprects = kcalloc(batch->num_cliprects,
602 sizeof(struct drm_clip_rect),
603 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700604 if (cliprects == NULL)
605 return -ENOMEM;
606
607 ret = copy_from_user(cliprects, batch->cliprects,
608 batch->num_cliprects *
609 sizeof(struct drm_clip_rect));
610 if (ret != 0)
611 goto fail_free;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Eric Anholt546b0972008-09-01 16:45:29 -0700614 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700615 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700616 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400618 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000619 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700620
621fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700622 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ret;
625}
626
Eric Anholtc153f452007-09-03 12:06:45 +1000627static int i915_cmdbuffer(struct drm_device *dev, void *data,
628 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000631 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000633 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000634 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700635 struct drm_clip_rect *cliprects = NULL;
636 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int ret;
638
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800639 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800640 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric Anholt546b0972008-09-01 16:45:29 -0700642 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Eric Anholt201361a2009-03-11 12:30:04 -0700644 if (cmdbuf->num_cliprects < 0)
645 return -EINVAL;
646
Eric Anholt9a298b22009-03-24 12:23:04 -0700647 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700648 if (batch_data == NULL)
649 return -ENOMEM;
650
651 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
652 if (ret != 0)
653 goto fail_batch_free;
654
655 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700656 cliprects = kcalloc(cmdbuf->num_cliprects,
657 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000658 if (cliprects == NULL) {
659 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700660 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000661 }
Eric Anholt201361a2009-03-11 12:30:04 -0700662
663 ret = copy_from_user(cliprects, cmdbuf->cliprects,
664 cmdbuf->num_cliprects *
665 sizeof(struct drm_clip_rect));
666 if (ret != 0)
667 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
Eric Anholt546b0972008-09-01 16:45:29 -0700670 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700671 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700672 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (ret) {
674 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000675 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400678 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000679 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700680
Eric Anholt201361a2009-03-11 12:30:04 -0700681fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700682 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000683fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700684 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700685
686 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Eric Anholtc153f452007-09-03 12:06:45 +1000689static int i915_flip_bufs(struct drm_device *dev, void *data,
690 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Eric Anholt546b0972008-09-01 16:45:29 -0700692 int ret;
693
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800694 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric Anholt546b0972008-09-01 16:45:29 -0700696 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Eric Anholt546b0972008-09-01 16:45:29 -0700698 mutex_lock(&dev->struct_mutex);
699 ret = i915_dispatch_flip(dev);
700 mutex_unlock(&dev->struct_mutex);
701
702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Eric Anholtc153f452007-09-03 12:06:45 +1000705static int i915_getparam(struct drm_device *dev, void *data,
706 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000709 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int value;
711
712 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000713 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000714 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
Eric Anholtc153f452007-09-03 12:06:45 +1000717 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700719 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721 case I915_PARAM_ALLOW_BATCHBUFFER:
722 value = dev_priv->allow_batchbuffer ? 1 : 0;
723 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100724 case I915_PARAM_LAST_DISPATCH:
725 value = READ_BREADCRUMB(dev_priv);
726 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400727 case I915_PARAM_CHIPSET_ID:
728 value = dev->pci_device;
729 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700730 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000731 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700732 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800733 case I915_PARAM_NUM_FENCES_AVAIL:
734 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
735 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200736 case I915_PARAM_HAS_OVERLAY:
737 value = dev_priv->overlay ? 1 : 0;
738 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800739 case I915_PARAM_HAS_PAGEFLIPPING:
740 value = 1;
741 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500742 case I915_PARAM_HAS_EXECBUF2:
743 /* depends on GEM */
744 value = dev_priv->has_gem;
745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800747 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -0500748 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000749 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Eric Anholtc153f452007-09-03 12:06:45 +1000752 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000754 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
757 return 0;
758}
759
Eric Anholtc153f452007-09-03 12:06:45 +1000760static int i915_setparam(struct drm_device *dev, void *data,
761 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000764 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000767 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000768 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
Eric Anholtc153f452007-09-03 12:06:45 +1000771 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 break;
774 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000775 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000778 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800780 case I915_SETPARAM_NUM_USED_FENCES:
781 if (param->value > dev_priv->num_fence_regs ||
782 param->value < 0)
783 return -EINVAL;
784 /* Userspace can use first N regs */
785 dev_priv->fence_reg_start = param->value;
786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800788 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800789 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000790 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 return 0;
794}
795
Eric Anholtc153f452007-09-03 12:06:45 +1000796static int i915_set_status_page(struct drm_device *dev, void *data,
797 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000798{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000799 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000800 drm_i915_hws_addr_t *hws = data;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800801 struct intel_ring_buffer *ring = &dev_priv->render_ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000802
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000803 if (!I915_NEED_GFX_HWS(dev))
804 return -EINVAL;
805
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000806 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000807 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000808 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000809 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000810
Jesse Barnes79e53942008-11-07 14:24:08 -0800811 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
812 WARN(1, "tried to set status page when mode setting active\n");
813 return 0;
814 }
815
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800816 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000817
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800818 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +1000819
Eric Anholt8b409582007-11-22 16:40:37 +1000820 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000821 dev_priv->hws_map.size = 4*1024;
822 dev_priv->hws_map.type = 0;
823 dev_priv->hws_map.flags = 0;
824 dev_priv->hws_map.mtrr = 0;
825
Dave Airliedd0910b2009-02-25 14:49:21 +1000826 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000827 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000828 i915_dma_cleanup(dev);
829 dev_priv->status_gfx_addr = 0;
830 DRM_ERROR("can not ioremap virtual address for"
831 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000832 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000833 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800834 ring->status_page.page_addr = dev_priv->hws_map.handle;
835 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
836 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000837
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800838 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Zou Nan hai852835f2010-05-21 09:08:56 +0800839 dev_priv->status_gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800840 DRM_DEBUG_DRIVER("load hws at %p\n",
Zou Nan hai852835f2010-05-21 09:08:56 +0800841 dev_priv->hw_status_page);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000842 return 0;
843}
844
Dave Airlieec2a4c32009-08-04 11:43:41 +1000845static int i915_get_bridge_dev(struct drm_device *dev)
846{
847 struct drm_i915_private *dev_priv = dev->dev_private;
848
849 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
850 if (!dev_priv->bridge_dev) {
851 DRM_ERROR("bridge device not found\n");
852 return -1;
853 }
854 return 0;
855}
856
Zhenyu Wangc4804412009-12-17 14:48:43 +0800857#define MCHBAR_I915 0x44
858#define MCHBAR_I965 0x48
859#define MCHBAR_SIZE (4*4096)
860
861#define DEVEN_REG 0x54
862#define DEVEN_MCHBAR_EN (1 << 28)
863
864/* Allocate space for the MCH regs if needed, return nonzero on error */
865static int
866intel_alloc_mchbar_resource(struct drm_device *dev)
867{
868 drm_i915_private_t *dev_priv = dev->dev_private;
869 int reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
870 u32 temp_lo, temp_hi = 0;
871 u64 mchbar_addr;
872 int ret = 0;
873
874 if (IS_I965G(dev))
875 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
876 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
877 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
878
879 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
880#ifdef CONFIG_PNP
881 if (mchbar_addr &&
882 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE)) {
883 ret = 0;
884 goto out;
885 }
886#endif
887
888 /* Get some space for it */
889 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus, &dev_priv->mch_res,
890 MCHBAR_SIZE, MCHBAR_SIZE,
891 PCIBIOS_MIN_MEM,
892 0, pcibios_align_resource,
893 dev_priv->bridge_dev);
894 if (ret) {
895 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
896 dev_priv->mch_res.start = 0;
897 goto out;
898 }
899
900 if (IS_I965G(dev))
901 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
902 upper_32_bits(dev_priv->mch_res.start));
903
904 pci_write_config_dword(dev_priv->bridge_dev, reg,
905 lower_32_bits(dev_priv->mch_res.start));
906out:
907 return ret;
908}
909
910/* Setup MCHBAR if possible, return true if we should disable it again */
911static void
912intel_setup_mchbar(struct drm_device *dev)
913{
914 drm_i915_private_t *dev_priv = dev->dev_private;
915 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
916 u32 temp;
917 bool enabled;
918
919 dev_priv->mchbar_need_disable = false;
920
921 if (IS_I915G(dev) || IS_I915GM(dev)) {
922 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
923 enabled = !!(temp & DEVEN_MCHBAR_EN);
924 } else {
925 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
926 enabled = temp & 1;
927 }
928
929 /* If it's already enabled, don't have to do anything */
930 if (enabled)
931 return;
932
933 if (intel_alloc_mchbar_resource(dev))
934 return;
935
936 dev_priv->mchbar_need_disable = true;
937
938 /* Space is allocated or reserved, so enable it. */
939 if (IS_I915G(dev) || IS_I915GM(dev)) {
940 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
941 temp | DEVEN_MCHBAR_EN);
942 } else {
943 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
944 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
945 }
946}
947
948static void
949intel_teardown_mchbar(struct drm_device *dev)
950{
951 drm_i915_private_t *dev_priv = dev->dev_private;
952 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
953 u32 temp;
954
955 if (dev_priv->mchbar_need_disable) {
956 if (IS_I915G(dev) || IS_I915GM(dev)) {
957 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
958 temp &= ~DEVEN_MCHBAR_EN;
959 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
960 } else {
961 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
962 temp &= ~1;
963 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
964 }
965 }
966
967 if (dev_priv->mch_res.start)
968 release_resource(&dev_priv->mch_res);
969}
970
Jesse Barnes79e53942008-11-07 14:24:08 -0800971/**
972 * i915_probe_agp - get AGP bootup configuration
973 * @pdev: PCI device
974 * @aperture_size: returns AGP aperture configured size
975 * @preallocated_size: returns size of BIOS preallocated AGP space
976 *
977 * Since Intel integrated graphics are UMA, the BIOS has to set aside
978 * some RAM for the framebuffer at early boot. This code figures out
979 * how much was set aside so we can use it for our own purposes.
980 */
Eric Anholt2a34f5e62009-07-02 09:30:50 -0700981static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,
Jesse Barnes80824002009-09-10 15:28:06 -0700982 uint32_t *preallocated_size,
983 uint32_t *start)
Jesse Barnes79e53942008-11-07 14:24:08 -0800984{
Dave Airlieec2a4c32009-08-04 11:43:41 +1000985 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800986 u16 tmp = 0;
987 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -0800988 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -0800989
Jesse Barnes79e53942008-11-07 14:24:08 -0800990 /* Get the fb aperture size and "stolen" memory amount. */
Dave Airlieec2a4c32009-08-04 11:43:41 +1000991 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &tmp);
Jesse Barnes79e53942008-11-07 14:24:08 -0800992
993 *aperture_size = 1024 * 1024;
994 *preallocated_size = 1024 * 1024;
995
Eric Anholt60fd99e2008-12-03 22:50:02 -0800996 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800997 case PCI_DEVICE_ID_INTEL_82830_CGC:
998 case PCI_DEVICE_ID_INTEL_82845G_IG:
999 case PCI_DEVICE_ID_INTEL_82855GM_IG:
1000 case PCI_DEVICE_ID_INTEL_82865_IG:
1001 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
1002 *aperture_size *= 64;
1003 else
1004 *aperture_size *= 128;
1005 break;
1006 default:
1007 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -08001008 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001009 break;
1010 }
1011
1012 /*
1013 * Some of the preallocated space is taken by the GTT
1014 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
1015 */
Eric Anholtbad720f2009-10-22 16:11:14 -07001016 if (IS_G4X(dev) || IS_PINEVIEW(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -08001017 overhead = 4096;
1018 else
1019 overhead = (*aperture_size / 1024) + 4096;
1020
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001021 if (IS_GEN6(dev)) {
1022 /* SNB has memory control reg at 0x50.w */
1023 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &tmp);
1024
1025 switch (tmp & SNB_GMCH_GMS_STOLEN_MASK) {
1026 case INTEL_855_GMCH_GMS_DISABLED:
Eric Anholtbad720f2009-10-22 16:11:14 -07001027 DRM_ERROR("video memory is disabled\n");
1028 return -1;
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001029 case SNB_GMCH_GMS_STOLEN_32M:
1030 stolen = 32 * 1024 * 1024;
1031 break;
1032 case SNB_GMCH_GMS_STOLEN_64M:
1033 stolen = 64 * 1024 * 1024;
1034 break;
1035 case SNB_GMCH_GMS_STOLEN_96M:
1036 stolen = 96 * 1024 * 1024;
1037 break;
1038 case SNB_GMCH_GMS_STOLEN_128M:
1039 stolen = 128 * 1024 * 1024;
1040 break;
1041 case SNB_GMCH_GMS_STOLEN_160M:
1042 stolen = 160 * 1024 * 1024;
1043 break;
1044 case SNB_GMCH_GMS_STOLEN_192M:
1045 stolen = 192 * 1024 * 1024;
1046 break;
1047 case SNB_GMCH_GMS_STOLEN_224M:
1048 stolen = 224 * 1024 * 1024;
1049 break;
1050 case SNB_GMCH_GMS_STOLEN_256M:
1051 stolen = 256 * 1024 * 1024;
1052 break;
1053 case SNB_GMCH_GMS_STOLEN_288M:
1054 stolen = 288 * 1024 * 1024;
1055 break;
1056 case SNB_GMCH_GMS_STOLEN_320M:
1057 stolen = 320 * 1024 * 1024;
1058 break;
1059 case SNB_GMCH_GMS_STOLEN_352M:
1060 stolen = 352 * 1024 * 1024;
1061 break;
1062 case SNB_GMCH_GMS_STOLEN_384M:
1063 stolen = 384 * 1024 * 1024;
1064 break;
1065 case SNB_GMCH_GMS_STOLEN_416M:
1066 stolen = 416 * 1024 * 1024;
1067 break;
1068 case SNB_GMCH_GMS_STOLEN_448M:
1069 stolen = 448 * 1024 * 1024;
1070 break;
1071 case SNB_GMCH_GMS_STOLEN_480M:
1072 stolen = 480 * 1024 * 1024;
1073 break;
1074 case SNB_GMCH_GMS_STOLEN_512M:
1075 stolen = 512 * 1024 * 1024;
1076 break;
1077 default:
1078 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
1079 tmp & SNB_GMCH_GMS_STOLEN_MASK);
1080 return -1;
Eric Anholtbad720f2009-10-22 16:11:14 -07001081 }
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001082 } else {
1083 switch (tmp & INTEL_GMCH_GMS_MASK) {
1084 case INTEL_855_GMCH_GMS_DISABLED:
1085 DRM_ERROR("video memory is disabled\n");
1086 return -1;
1087 case INTEL_855_GMCH_GMS_STOLEN_1M:
1088 stolen = 1 * 1024 * 1024;
1089 break;
1090 case INTEL_855_GMCH_GMS_STOLEN_4M:
1091 stolen = 4 * 1024 * 1024;
1092 break;
1093 case INTEL_855_GMCH_GMS_STOLEN_8M:
1094 stolen = 8 * 1024 * 1024;
1095 break;
1096 case INTEL_855_GMCH_GMS_STOLEN_16M:
1097 stolen = 16 * 1024 * 1024;
1098 break;
1099 case INTEL_855_GMCH_GMS_STOLEN_32M:
1100 stolen = 32 * 1024 * 1024;
1101 break;
1102 case INTEL_915G_GMCH_GMS_STOLEN_48M:
1103 stolen = 48 * 1024 * 1024;
1104 break;
1105 case INTEL_915G_GMCH_GMS_STOLEN_64M:
1106 stolen = 64 * 1024 * 1024;
1107 break;
1108 case INTEL_GMCH_GMS_STOLEN_128M:
1109 stolen = 128 * 1024 * 1024;
1110 break;
1111 case INTEL_GMCH_GMS_STOLEN_256M:
1112 stolen = 256 * 1024 * 1024;
1113 break;
1114 case INTEL_GMCH_GMS_STOLEN_96M:
1115 stolen = 96 * 1024 * 1024;
1116 break;
1117 case INTEL_GMCH_GMS_STOLEN_160M:
1118 stolen = 160 * 1024 * 1024;
1119 break;
1120 case INTEL_GMCH_GMS_STOLEN_224M:
1121 stolen = 224 * 1024 * 1024;
1122 break;
1123 case INTEL_GMCH_GMS_STOLEN_352M:
1124 stolen = 352 * 1024 * 1024;
1125 break;
1126 default:
1127 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
1128 tmp & INTEL_GMCH_GMS_MASK);
1129 return -1;
1130 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001131 }
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001132
Eric Anholt241fa852009-01-02 18:05:51 -08001133 *preallocated_size = stolen - overhead;
Jesse Barnes80824002009-09-10 15:28:06 -07001134 *start = overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -08001135
1136 return 0;
1137}
1138
Jesse Barnes80824002009-09-10 15:28:06 -07001139#define PTE_ADDRESS_MASK 0xfffff000
1140#define PTE_ADDRESS_MASK_HIGH 0x000000f0 /* i915+ */
1141#define PTE_MAPPING_TYPE_UNCACHED (0 << 1)
1142#define PTE_MAPPING_TYPE_DCACHE (1 << 1) /* i830 only */
1143#define PTE_MAPPING_TYPE_CACHED (3 << 1)
1144#define PTE_MAPPING_TYPE_MASK (3 << 1)
1145#define PTE_VALID (1 << 0)
1146
1147/**
1148 * i915_gtt_to_phys - take a GTT address and turn it into a physical one
1149 * @dev: drm device
1150 * @gtt_addr: address to translate
1151 *
1152 * Some chip functions require allocations from stolen space but need the
1153 * physical address of the memory in question. We use this routine
1154 * to get a physical address suitable for register programming from a given
1155 * GTT address.
1156 */
1157static unsigned long i915_gtt_to_phys(struct drm_device *dev,
1158 unsigned long gtt_addr)
1159{
1160 unsigned long *gtt;
1161 unsigned long entry, phys;
1162 int gtt_bar = IS_I9XX(dev) ? 0 : 1;
1163 int gtt_offset, gtt_size;
1164
1165 if (IS_I965G(dev)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001166 if (IS_G4X(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001167 gtt_offset = 2*1024*1024;
1168 gtt_size = 2*1024*1024;
1169 } else {
1170 gtt_offset = 512*1024;
1171 gtt_size = 512*1024;
1172 }
1173 } else {
1174 gtt_bar = 3;
1175 gtt_offset = 0;
1176 gtt_size = pci_resource_len(dev->pdev, gtt_bar);
1177 }
1178
1179 gtt = ioremap_wc(pci_resource_start(dev->pdev, gtt_bar) + gtt_offset,
1180 gtt_size);
1181 if (!gtt) {
1182 DRM_ERROR("ioremap of GTT failed\n");
1183 return 0;
1184 }
1185
1186 entry = *(volatile u32 *)(gtt + (gtt_addr / 1024));
1187
Zhao Yakui44d98a62009-10-09 11:39:40 +08001188 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, PTE: 0x%08lx\n", gtt_addr, entry);
Jesse Barnes80824002009-09-10 15:28:06 -07001189
1190 /* Mask out these reserved bits on this hardware. */
1191 if (!IS_I9XX(dev) || IS_I915G(dev) || IS_I915GM(dev) ||
1192 IS_I945G(dev) || IS_I945GM(dev)) {
1193 entry &= ~PTE_ADDRESS_MASK_HIGH;
1194 }
1195
1196 /* If it's not a mapping type we know, then bail. */
1197 if ((entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_UNCACHED &&
1198 (entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_CACHED) {
1199 iounmap(gtt);
1200 return 0;
1201 }
1202
1203 if (!(entry & PTE_VALID)) {
1204 DRM_ERROR("bad GTT entry in stolen space\n");
1205 iounmap(gtt);
1206 return 0;
1207 }
1208
1209 iounmap(gtt);
1210
1211 phys =(entry & PTE_ADDRESS_MASK) |
1212 ((uint64_t)(entry & PTE_ADDRESS_MASK_HIGH) << (32 - 4));
1213
Zhao Yakui44d98a62009-10-09 11:39:40 +08001214 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, phys addr: 0x%08lx\n", gtt_addr, phys);
Jesse Barnes80824002009-09-10 15:28:06 -07001215
1216 return phys;
1217}
1218
1219static void i915_warn_stolen(struct drm_device *dev)
1220{
1221 DRM_ERROR("not enough stolen space for compressed buffer, disabling\n");
1222 DRM_ERROR("hint: you may be able to increase stolen memory size in the BIOS to avoid this\n");
1223}
1224
1225static void i915_setup_compression(struct drm_device *dev, int size)
1226{
1227 struct drm_i915_private *dev_priv = dev->dev_private;
1228 struct drm_mm_node *compressed_fb, *compressed_llb;
Andrew Morton29bd0ae2009-11-17 14:08:52 -08001229 unsigned long cfb_base;
1230 unsigned long ll_base = 0;
Jesse Barnes80824002009-09-10 15:28:06 -07001231
1232 /* Leave 1M for line length buffer & misc. */
1233 compressed_fb = drm_mm_search_free(&dev_priv->vram, size, 4096, 0);
1234 if (!compressed_fb) {
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001235 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001236 i915_warn_stolen(dev);
1237 return;
1238 }
1239
1240 compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
1241 if (!compressed_fb) {
1242 i915_warn_stolen(dev);
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001243 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001244 return;
1245 }
1246
Jesse Barnes74dff282009-09-14 15:39:40 -07001247 cfb_base = i915_gtt_to_phys(dev, compressed_fb->start);
1248 if (!cfb_base) {
1249 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1250 drm_mm_put_block(compressed_fb);
Jesse Barnes80824002009-09-10 15:28:06 -07001251 }
1252
Jesse Barnes74dff282009-09-14 15:39:40 -07001253 if (!IS_GM45(dev)) {
1254 compressed_llb = drm_mm_search_free(&dev_priv->vram, 4096,
1255 4096, 0);
1256 if (!compressed_llb) {
1257 i915_warn_stolen(dev);
1258 return;
1259 }
1260
1261 compressed_llb = drm_mm_get_block(compressed_llb, 4096, 4096);
1262 if (!compressed_llb) {
1263 i915_warn_stolen(dev);
1264 return;
1265 }
1266
1267 ll_base = i915_gtt_to_phys(dev, compressed_llb->start);
1268 if (!ll_base) {
1269 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1270 drm_mm_put_block(compressed_fb);
1271 drm_mm_put_block(compressed_llb);
1272 }
Jesse Barnes80824002009-09-10 15:28:06 -07001273 }
1274
1275 dev_priv->cfb_size = size;
1276
Adam Jacksonee5382a2010-04-23 11:17:39 -04001277 intel_disable_fbc(dev);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001278 dev_priv->compressed_fb = compressed_fb;
1279
Jesse Barnes74dff282009-09-14 15:39:40 -07001280 if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001281 I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
1282 } else {
Jesse Barnes74dff282009-09-14 15:39:40 -07001283 I915_WRITE(FBC_CFB_BASE, cfb_base);
1284 I915_WRITE(FBC_LL_BASE, ll_base);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001285 dev_priv->compressed_llb = compressed_llb;
Jesse Barnes80824002009-09-10 15:28:06 -07001286 }
1287
Jesse Barnes80824002009-09-10 15:28:06 -07001288 DRM_DEBUG("FBC base 0x%08lx, ll base 0x%08lx, size %dM\n", cfb_base,
1289 ll_base, size >> 20);
Jesse Barnes80824002009-09-10 15:28:06 -07001290}
1291
Jesse Barnes20bf3772010-04-21 11:39:22 -07001292static void i915_cleanup_compression(struct drm_device *dev)
1293{
1294 struct drm_i915_private *dev_priv = dev->dev_private;
1295
1296 drm_mm_put_block(dev_priv->compressed_fb);
1297 if (!IS_GM45(dev))
1298 drm_mm_put_block(dev_priv->compressed_llb);
1299}
1300
Dave Airlie28d52042009-09-21 14:33:58 +10001301/* true = enable decode, false = disable decoder */
1302static unsigned int i915_vga_set_decode(void *cookie, bool state)
1303{
1304 struct drm_device *dev = cookie;
1305
1306 intel_modeset_vga_set_state(dev, state);
1307 if (state)
1308 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1309 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1310 else
1311 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1312}
1313
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001314static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1315{
1316 struct drm_device *dev = pci_get_drvdata(pdev);
1317 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1318 if (state == VGA_SWITCHEROO_ON) {
1319 printk(KERN_INFO "i915: switched off\n");
1320 /* i915 resume handler doesn't set to D0 */
1321 pci_set_power_state(dev->pdev, PCI_D0);
1322 i915_resume(dev);
1323 } else {
1324 printk(KERN_ERR "i915: switched off\n");
1325 i915_suspend(dev, pmm);
1326 }
1327}
1328
1329static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1330{
1331 struct drm_device *dev = pci_get_drvdata(pdev);
1332 bool can_switch;
1333
1334 spin_lock(&dev->count_lock);
1335 can_switch = (dev->open_count == 0);
1336 spin_unlock(&dev->count_lock);
1337 return can_switch;
1338}
1339
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001340static int i915_load_modeset_init(struct drm_device *dev,
Jesse Barnes80824002009-09-10 15:28:06 -07001341 unsigned long prealloc_start,
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001342 unsigned long prealloc_size,
1343 unsigned long agp_size)
Jesse Barnes79e53942008-11-07 14:24:08 -08001344{
1345 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001346 int fb_bar = IS_I9XX(dev) ? 2 : 0;
1347 int ret = 0;
1348
1349 dev->mode_config.fb_base = drm_get_resource_start(dev, fb_bar) &
1350 0xff000000;
1351
Jesse Barnes79e53942008-11-07 14:24:08 -08001352 /* Basic memrange allocator for stolen space (aka vram) */
1353 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
Jesse Barnes80824002009-09-10 15:28:06 -07001354 DRM_INFO("set up %ldM of stolen space\n", prealloc_size / (1024*1024));
Jesse Barnes79e53942008-11-07 14:24:08 -08001355
Ben Gamari11ed50e2009-09-14 17:48:45 -04001356 /* We're off and running w/KMS */
1357 dev_priv->mm.suspended = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001358
Eric Anholt13f4c432009-05-12 15:27:36 -07001359 /* Let GEM Manage from end of prealloc space to end of aperture.
1360 *
1361 * However, leave one page at the end still bound to the scratch page.
1362 * There are a number of places where the hardware apparently
1363 * prefetches past the end of the object, and we've seen multiple
1364 * hangs with the GPU head pointer stuck in a batchbuffer bound
1365 * at the last page of the aperture. One page should be enough to
1366 * keep any prefetching inside of the aperture.
1367 */
1368 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001369
Ben Gamari11ed50e2009-09-14 17:48:45 -04001370 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001371 ret = i915_gem_init_ringbuffer(dev);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001372 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001373 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001374 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001375
Jesse Barnes80824002009-09-10 15:28:06 -07001376 /* Try to set up FBC with a reasonable compressed buffer size */
Shaohua Li9216d442009-10-10 15:20:55 +08001377 if (I915_HAS_FBC(dev) && i915_powersave) {
Jesse Barnes80824002009-09-10 15:28:06 -07001378 int cfb_size;
1379
1380 /* Try to get an 8M buffer... */
1381 if (prealloc_size > (9*1024*1024))
1382 cfb_size = 8*1024*1024;
1383 else /* fall back to 7/8 of the stolen space */
1384 cfb_size = prealloc_size * 7 / 8;
1385 i915_setup_compression(dev, cfb_size);
1386 }
1387
Jesse Barnes79e53942008-11-07 14:24:08 -08001388 /* Allow hardware batchbuffers unless told otherwise.
1389 */
1390 dev_priv->allow_batchbuffer = 1;
1391
1392 ret = intel_init_bios(dev);
1393 if (ret)
1394 DRM_INFO("failed to find VBIOS tables\n");
1395
Dave Airlie28d52042009-09-21 14:33:58 +10001396 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1397 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1398 if (ret)
1399 goto destroy_ringbuffer;
1400
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001401 ret = vga_switcheroo_register_client(dev->pdev,
1402 i915_switcheroo_set_state,
1403 i915_switcheroo_can_switch);
1404 if (ret)
1405 goto destroy_ringbuffer;
1406
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001407 intel_modeset_init(dev);
1408
Jesse Barnes79e53942008-11-07 14:24:08 -08001409 ret = drm_irq_install(dev);
1410 if (ret)
1411 goto destroy_ringbuffer;
1412
Jesse Barnes79e53942008-11-07 14:24:08 -08001413 /* Always safe in the mode setting case. */
1414 /* FIXME: do pre/post-mode set stuff in core KMS code */
1415 dev->vblank_disable_allowed = 1;
1416
1417 /*
1418 * Initialize the hardware status page IRQ location.
1419 */
1420
1421 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1422
Dave Airlie38651672010-03-30 05:34:13 +00001423 intel_fbdev_init(dev);
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001424 drm_kms_helper_poll_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001425 return 0;
1426
Jesse Barnes79e53942008-11-07 14:24:08 -08001427destroy_ringbuffer:
Eric Anholt21099532009-11-09 14:57:34 -08001428 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001429 i915_gem_cleanup_ringbuffer(dev);
Eric Anholt21099532009-11-09 14:57:34 -08001430 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001431out:
1432 return ret;
1433}
1434
Dave Airlie7c1c2872008-11-28 14:22:24 +10001435int i915_master_create(struct drm_device *dev, struct drm_master *master)
1436{
1437 struct drm_i915_master_private *master_priv;
1438
Eric Anholt9a298b22009-03-24 12:23:04 -07001439 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001440 if (!master_priv)
1441 return -ENOMEM;
1442
1443 master->driver_priv = master_priv;
1444 return 0;
1445}
1446
1447void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1448{
1449 struct drm_i915_master_private *master_priv = master->driver_priv;
1450
1451 if (!master_priv)
1452 return;
1453
Eric Anholt9a298b22009-03-24 12:23:04 -07001454 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001455
1456 master->driver_priv = NULL;
1457}
1458
Shaohua Li7662c8b2009-06-26 11:23:55 +08001459static void i915_get_mem_freq(struct drm_device *dev)
1460{
1461 drm_i915_private_t *dev_priv = dev->dev_private;
1462 u32 tmp;
1463
Adam Jacksonf2b115e2009-12-03 17:14:42 -05001464 if (!IS_PINEVIEW(dev))
Shaohua Li7662c8b2009-06-26 11:23:55 +08001465 return;
1466
1467 tmp = I915_READ(CLKCFG);
1468
1469 switch (tmp & CLKCFG_FSB_MASK) {
1470 case CLKCFG_FSB_533:
1471 dev_priv->fsb_freq = 533; /* 133*4 */
1472 break;
1473 case CLKCFG_FSB_800:
1474 dev_priv->fsb_freq = 800; /* 200*4 */
1475 break;
1476 case CLKCFG_FSB_667:
1477 dev_priv->fsb_freq = 667; /* 167*4 */
1478 break;
1479 case CLKCFG_FSB_400:
1480 dev_priv->fsb_freq = 400; /* 100*4 */
1481 break;
1482 }
1483
1484 switch (tmp & CLKCFG_MEM_MASK) {
1485 case CLKCFG_MEM_533:
1486 dev_priv->mem_freq = 533;
1487 break;
1488 case CLKCFG_MEM_667:
1489 dev_priv->mem_freq = 667;
1490 break;
1491 case CLKCFG_MEM_800:
1492 dev_priv->mem_freq = 800;
1493 break;
1494 }
1495}
1496
Jesse Barnes79e53942008-11-07 14:24:08 -08001497/**
1498 * i915_driver_load - setup chip and create an initial config
1499 * @dev: DRM device
1500 * @flags: startup flags
1501 *
1502 * The driver load routine has to do several things:
1503 * - drive output discovery via intel_modeset_init()
1504 * - initialize the memory manager
1505 * - allocate initial config memory
1506 * - setup the DRM framebuffer with the allocated memory
1507 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001508int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001509{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001510 struct drm_i915_private *dev_priv;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11001511 resource_size_t base, size;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001512 int ret = 0, mmio_bar;
Jesse Barnes80824002009-09-10 15:28:06 -07001513 uint32_t agp_size, prealloc_size, prealloc_start;
Dave Airlie22eae942005-11-10 22:16:34 +11001514 /* i915 has 4 more counters */
1515 dev->counters += 4;
1516 dev->types[6] = _DRM_STAT_IRQ;
1517 dev->types[7] = _DRM_STAT_PRIMARY;
1518 dev->types[8] = _DRM_STAT_SECONDARY;
1519 dev->types[9] = _DRM_STAT_DMA;
1520
Eric Anholt9a298b22009-03-24 12:23:04 -07001521 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001522 if (dev_priv == NULL)
1523 return -ENOMEM;
1524
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001525 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001526 dev_priv->dev = dev;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001527 dev_priv->info = (struct intel_device_info *) flags;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001528
1529 /* Add register map (needed for suspend/resume) */
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001530 mmio_bar = IS_I9XX(dev) ? 0 : 1;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001531 base = drm_get_resource_start(dev, mmio_bar);
1532 size = drm_get_resource_len(dev, mmio_bar);
1533
Dave Airlieec2a4c32009-08-04 11:43:41 +10001534 if (i915_get_bridge_dev(dev)) {
1535 ret = -EIO;
1536 goto free_priv;
1537 }
1538
Eric Anholt3043c602008-10-02 12:24:47 -07001539 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001540 if (!dev_priv->regs) {
1541 DRM_ERROR("failed to map registers\n");
1542 ret = -EIO;
Dave Airlieec2a4c32009-08-04 11:43:41 +10001543 goto put_bridge;
Jesse Barnes79e53942008-11-07 14:24:08 -08001544 }
Eric Anholted4cb412008-07-29 12:10:39 -07001545
Eric Anholtab657db12009-01-23 12:57:47 -08001546 dev_priv->mm.gtt_mapping =
1547 io_mapping_create_wc(dev->agp->base,
1548 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001549 if (dev_priv->mm.gtt_mapping == NULL) {
1550 ret = -EIO;
1551 goto out_rmmap;
1552 }
1553
Eric Anholtab657db12009-01-23 12:57:47 -08001554 /* Set up a WC MTRR for non-PAT systems. This is more common than
1555 * one would think, because the kernel disables PAT on first
1556 * generation Core chips because WC PAT gets overridden by a UC
1557 * MTRR if present. Even if a UC MTRR isn't present.
1558 */
1559 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
1560 dev->agp->agp_info.aper_size *
1561 1024 * 1024,
1562 MTRR_TYPE_WRCOMB, 1);
1563 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001564 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001565 "performance may suffer.\n");
1566 }
1567
Jesse Barnes80824002009-09-10 15:28:06 -07001568 ret = i915_probe_agp(dev, &agp_size, &prealloc_size, &prealloc_start);
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001569 if (ret)
1570 goto out_iomapfree;
1571
Chris Wilsonaed5f1d2009-10-14 13:40:04 +01001572 dev_priv->wq = create_singlethread_workqueue("i915");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001573 if (dev_priv->wq == NULL) {
1574 DRM_ERROR("Failed to create our workqueue.\n");
1575 ret = -ENOMEM;
1576 goto out_iomapfree;
1577 }
1578
Dave Airlieac5c4e72008-12-19 15:38:34 +10001579 /* enable GEM by default */
1580 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10001581
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001582 if (prealloc_size > agp_size * 3 / 4) {
1583 DRM_ERROR("Detected broken video BIOS with %d/%dkB of video "
1584 "memory stolen.\n",
1585 prealloc_size / 1024, agp_size / 1024);
1586 DRM_ERROR("Disabling GEM. (try reducing stolen memory or "
1587 "updating the BIOS to fix).\n");
1588 dev_priv->has_gem = 0;
1589 }
1590
Chris Wilson79a78dd2010-05-17 09:23:54 +01001591 if (dev_priv->has_gem == 0 &&
1592 drm_core_check_feature(dev, DRIVER_MODESET)) {
1593 DRM_ERROR("kernel modesetting requires GEM, disabling driver.\n");
1594 ret = -ENODEV;
1595 goto out_iomapfree;
1596 }
1597
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001598 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001599 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eric Anholtbad720f2009-10-22 16:11:14 -07001600 if (IS_G4X(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001601 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001602 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001603 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001604
Zhenyu Wangc4804412009-12-17 14:48:43 +08001605 /* Try to make sure MCHBAR is enabled before poking at it */
1606 intel_setup_mchbar(dev);
1607
Eric Anholt673a3942008-07-30 12:06:12 -07001608 i915_gem_load(dev);
1609
Keith Packard398c9cb2008-07-30 13:03:43 -07001610 /* Init HWS */
1611 if (!I915_NEED_GFX_HWS(dev)) {
1612 ret = i915_init_phys_hws(dev);
1613 if (ret != 0)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001614 goto out_workqueue_free;
Keith Packard398c9cb2008-07-30 13:03:43 -07001615 }
Eric Anholted4cb412008-07-29 12:10:39 -07001616
Shaohua Li7662c8b2009-06-26 11:23:55 +08001617 i915_get_mem_freq(dev);
1618
Eric Anholted4cb412008-07-29 12:10:39 -07001619 /* On the 945G/GM, the chipset reports the MSI capability on the
1620 * integrated graphics even though the support isn't actually there
1621 * according to the published specs. It doesn't appear to function
1622 * correctly in testing on 945G.
1623 * This may be a side effect of MSI having been made available for PEG
1624 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001625 *
1626 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001627 * be lost or delayed, but we use them anyways to avoid
1628 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001629 */
Keith Packardb60678a2008-12-08 11:12:28 -08001630 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001631 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001632
1633 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001634 spin_lock_init(&dev_priv->error_lock);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01001635 dev_priv->trace_irq_seqno = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07001636
Keith Packard52440212008-11-18 09:30:25 -08001637 ret = drm_vblank_init(dev, I915_NUM_PIPE);
1638
1639 if (ret) {
1640 (void) i915_driver_unload(dev);
1641 return ret;
1642 }
1643
Ben Gamari11ed50e2009-09-14 17:48:45 -04001644 /* Start out suspended */
1645 dev_priv->mm.suspended = 1;
1646
Zhenyu Wang3bad0782010-04-07 16:15:53 +08001647 intel_detect_pch(dev);
1648
Jesse Barnes79e53942008-11-07 14:24:08 -08001649 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001650 ret = i915_load_modeset_init(dev, prealloc_start,
1651 prealloc_size, agp_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001652 if (ret < 0) {
1653 DRM_ERROR("failed to init modeset\n");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001654 goto out_workqueue_free;
Jesse Barnes79e53942008-11-07 14:24:08 -08001655 }
1656 }
1657
Matthew Garrett74a365b2009-03-19 21:35:39 +00001658 /* Must be done after probing outputs */
Zhao Yakui01c66882009-10-28 05:10:00 +00001659 intel_opregion_init(dev, 0);
Matthew Garrett74a365b2009-03-19 21:35:39 +00001660
Ben Gamarif65d9422009-09-14 17:48:44 -04001661 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
1662 (unsigned long) dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001663 return 0;
1664
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001665out_workqueue_free:
1666 destroy_workqueue(dev_priv->wq);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001667out_iomapfree:
1668 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08001669out_rmmap:
1670 iounmap(dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001671put_bridge:
1672 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001673free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07001674 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001675 return ret;
1676}
1677
1678int i915_driver_unload(struct drm_device *dev)
1679{
1680 struct drm_i915_private *dev_priv = dev->dev_private;
1681
Chris Wilson9df30792010-02-18 10:24:56 +00001682 i915_destroy_error_state(dev);
1683
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001684 destroy_workqueue(dev_priv->wq);
Ben Gamarif65d9422009-09-14 17:48:44 -04001685 del_timer_sync(&dev_priv->hangcheck_timer);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001686
Eric Anholtab657db12009-01-23 12:57:47 -08001687 io_mapping_free(dev_priv->mm.gtt_mapping);
1688 if (dev_priv->mm.gtt_mtrr >= 0) {
1689 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
1690 dev->agp->agp_info.aper_size * 1024 * 1024);
1691 dev_priv->mm.gtt_mtrr = -1;
1692 }
1693
Jesse Barnes79e53942008-11-07 14:24:08 -08001694 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001695 intel_modeset_cleanup(dev);
1696
Zhao Yakui6363ee62009-11-24 09:48:44 +08001697 /*
1698 * free the memory space allocated for the child device
1699 * config parsed from VBT
1700 */
1701 if (dev_priv->child_dev && dev_priv->child_dev_num) {
1702 kfree(dev_priv->child_dev);
1703 dev_priv->child_dev = NULL;
1704 dev_priv->child_dev_num = 0;
1705 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001706 drm_irq_uninstall(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001707 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001708 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001709 }
1710
Eric Anholted4cb412008-07-29 12:10:39 -07001711 if (dev->pdev->msi_enabled)
1712 pci_disable_msi(dev->pdev);
1713
Eric Anholt3043c602008-10-02 12:24:47 -07001714 if (dev_priv->regs != NULL)
1715 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001716
Zhao Yakui01c66882009-10-28 05:10:00 +00001717 intel_opregion_free(dev, 0);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001718
Jesse Barnes79e53942008-11-07 14:24:08 -08001719 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie71acb5e2008-12-30 20:31:46 +10001720 i915_gem_free_all_phys_object(dev);
1721
Jesse Barnes79e53942008-11-07 14:24:08 -08001722 mutex_lock(&dev->struct_mutex);
1723 i915_gem_cleanup_ringbuffer(dev);
1724 mutex_unlock(&dev->struct_mutex);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001725 if (I915_HAS_FBC(dev) && i915_powersave)
1726 i915_cleanup_compression(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001727 drm_mm_takedown(&dev_priv->vram);
1728 i915_gem_lastclose(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001729
1730 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001731 }
1732
Zhenyu Wangc4804412009-12-17 14:48:43 +08001733 intel_teardown_mchbar(dev);
1734
Dave Airlieec2a4c32009-08-04 11:43:41 +10001735 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07001736 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001737
Dave Airlie22eae942005-11-10 22:16:34 +11001738 return 0;
1739}
1740
Eric Anholt673a3942008-07-30 12:06:12 -07001741int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1742{
1743 struct drm_i915_file_private *i915_file_priv;
1744
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001745 DRM_DEBUG_DRIVER("\n");
Eric Anholt673a3942008-07-30 12:06:12 -07001746 i915_file_priv = (struct drm_i915_file_private *)
Eric Anholt9a298b22009-03-24 12:23:04 -07001747 kmalloc(sizeof(*i915_file_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07001748
1749 if (!i915_file_priv)
1750 return -ENOMEM;
1751
1752 file_priv->driver_priv = i915_file_priv;
1753
Eric Anholtb9624422009-06-03 07:27:35 +00001754 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07001755
1756 return 0;
1757}
1758
Jesse Barnes79e53942008-11-07 14:24:08 -08001759/**
1760 * i915_driver_lastclose - clean up after all DRM clients have exited
1761 * @dev: DRM device
1762 *
1763 * Take care of cleaning up after all DRM clients have exited. In the
1764 * mode setting case, we want to restore the kernel's initial mode (just
1765 * in case the last client left us in a bad state).
1766 *
1767 * Additionally, in the non-mode setting case, we'll tear down the AGP
1768 * and DMA structures, since the kernel won't be using them, and clea
1769 * up any GEM state.
1770 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001771void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001773 drm_i915_private_t *dev_priv = dev->dev_private;
1774
Jesse Barnes79e53942008-11-07 14:24:08 -08001775 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10001776 drm_fb_helper_restore();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001777 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001778 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001779 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001780
Eric Anholt673a3942008-07-30 12:06:12 -07001781 i915_gem_lastclose(dev);
1782
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001783 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001784 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001785
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001786 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
Eric Anholt6c340ea2007-08-25 20:23:09 +10001789void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001791 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00001792 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08001793 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1794 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Eric Anholt673a3942008-07-30 12:06:12 -07001797void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
1798{
1799 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
1800
Eric Anholt9a298b22009-03-24 12:23:04 -07001801 kfree(i915_file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001802}
1803
Eric Anholtc153f452007-09-03 12:06:45 +10001804struct drm_ioctl_desc i915_ioctls[] = {
1805 DRM_IOCTL_DEF(DRM_I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1806 DRM_IOCTL_DEF(DRM_I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1807 DRM_IOCTL_DEF(DRM_I915_FLIP, i915_flip_bufs, DRM_AUTH),
1808 DRM_IOCTL_DEF(DRM_I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1809 DRM_IOCTL_DEF(DRM_I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1810 DRM_IOCTL_DEF(DRM_I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
1811 DRM_IOCTL_DEF(DRM_I915_GETPARAM, i915_getparam, DRM_AUTH),
1812 DRM_IOCTL_DEF(DRM_I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1813 DRM_IOCTL_DEF(DRM_I915_ALLOC, i915_mem_alloc, DRM_AUTH),
1814 DRM_IOCTL_DEF(DRM_I915_FREE, i915_mem_free, DRM_AUTH),
1815 DRM_IOCTL_DEF(DRM_I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1816 DRM_IOCTL_DEF(DRM_I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
1817 DRM_IOCTL_DEF(DRM_I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1818 DRM_IOCTL_DEF(DRM_I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY ),
1819 DRM_IOCTL_DEF(DRM_I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH ),
1820 DRM_IOCTL_DEF(DRM_I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
Matthias Hopf4b408932008-10-18 07:18:05 +10001821 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 -08001822 DRM_IOCTL_DEF(DRM_I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1823 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
1824 DRM_IOCTL_DEF(DRM_I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
1825 DRM_IOCTL_DEF(DRM_I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1826 DRM_IOCTL_DEF(DRM_I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1827 DRM_IOCTL_DEF(DRM_I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
1828 DRM_IOCTL_DEF(DRM_I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
1829 DRM_IOCTL_DEF(DRM_I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1830 DRM_IOCTL_DEF(DRM_I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1831 DRM_IOCTL_DEF(DRM_I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
1832 DRM_IOCTL_DEF(DRM_I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
1833 DRM_IOCTL_DEF(DRM_I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
1834 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
1835 DRM_IOCTL_DEF(DRM_I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
1836 DRM_IOCTL_DEF(DRM_I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
1837 DRM_IOCTL_DEF(DRM_I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
1838 DRM_IOCTL_DEF(DRM_I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
1839 DRM_IOCTL_DEF(DRM_I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
1840 DRM_IOCTL_DEF(DRM_I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
1841 DRM_IOCTL_DEF(DRM_I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
1842 DRM_IOCTL_DEF(DRM_I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
1843 DRM_IOCTL_DEF(DRM_I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1844 DRM_IOCTL_DEF(DRM_I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10001845};
1846
1847int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10001848
1849/**
1850 * Determine if the device really is AGP or not.
1851 *
1852 * All Intel graphics chipsets are treated as AGP, even if they are really
1853 * PCI-e.
1854 *
1855 * \param dev The device to be tested.
1856 *
1857 * \returns
1858 * A value of 1 is always retured to indictate every i9x5 is AGP.
1859 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001860int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10001861{
1862 return 1;
1863}