blob: 2dae3be9ebef04284f3e594bbb528f2b9d05640e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
2 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "drmP.h"
30#include "drm.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080031#include "drm_crtc_helper.h"
Dave Airlie785b93e2009-08-28 15:46:53 +100032#include "drm_fb_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "i915_drm.h"
35#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010036#include "i915_trace.h"
Jordan Crousedcdb1672010-05-27 13:40:25 -060037#include <linux/pci.h>
Dave Airlie28d52042009-09-21 14:33:58 +100038#include <linux/vgaarb.h>
Zhenyu Wangc4804412009-12-17 14:48:43 +080039#include <linux/acpi.h>
40#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100041#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Chris Wilson44834a62010-08-19 16:09:23 +010043#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jesse Barnesd1d6ca72010-07-08 09:22:46 -070045extern int intel_max_stolen; /* from AGP driver */
46
Keith Packard398c9cb2008-07-30 13:03:43 -070047/**
48 * Sets up the hardware status page for devices that need a physical address
49 * in the register.
50 */
Eric Anholt3043c602008-10-02 12:24:47 -070051static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070052{
53 drm_i915_private_t *dev_priv = dev->dev_private;
54 /* Program Hardware Status Page */
55 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +080056 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -070057
58 if (!dev_priv->status_page_dmah) {
59 DRM_ERROR("Can not allocate hardware status page\n");
60 return -ENOMEM;
61 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +080062 dev_priv->render_ring.status_page.page_addr
63 = dev_priv->status_page_dmah->vaddr;
Keith Packard398c9cb2008-07-30 13:03:43 -070064 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
65
Zou Nan hai8187a2b2010-05-21 09:08:55 +080066 memset(dev_priv->render_ring.status_page.page_addr, 0, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -070067
Zhenyu Wang9b974cc2010-01-05 11:25:06 +080068 if (IS_I965G(dev))
69 dev_priv->dma_status_page |= (dev_priv->dma_status_page >> 28) &
70 0xf0;
71
Keith Packard398c9cb2008-07-30 13:03:43 -070072 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +080073 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -070074 return 0;
75}
76
77/**
78 * Frees the hardware status page, whether it's a physical address or a virtual
79 * address set up by the X Server.
80 */
Eric Anholt3043c602008-10-02 12:24:47 -070081static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070082{
83 drm_i915_private_t *dev_priv = dev->dev_private;
84 if (dev_priv->status_page_dmah) {
85 drm_pci_free(dev, dev_priv->status_page_dmah);
86 dev_priv->status_page_dmah = NULL;
87 }
88
Zou Nan hai852835f2010-05-21 09:08:56 +080089 if (dev_priv->render_ring.status_page.gfx_addr) {
90 dev_priv->render_ring.status_page.gfx_addr = 0;
Keith Packard398c9cb2008-07-30 13:03:43 -070091 drm_core_ioremapfree(&dev_priv->hws_map, dev);
92 }
93
94 /* Need to rewrite hardware status page */
95 I915_WRITE(HWS_PGA, 0x1ffff000);
96}
97
Dave Airlie84b1fd12007-07-11 15:53:27 +100098void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000101 struct drm_i915_master_private *master_priv;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800102 struct intel_ring_buffer *ring = &dev_priv->render_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jesse Barnes79e53942008-11-07 14:24:08 -0800104 /*
105 * We should never lose context on the ring with modesetting
106 * as we don't expose it to userspace
107 */
108 if (drm_core_check_feature(dev, DRIVER_MODESET))
109 return;
110
Jesse Barnes585fb112008-07-29 11:54:06 -0700111 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
112 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 ring->space = ring->head - (ring->tail + 8);
114 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800115 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Dave Airlie7c1c2872008-11-28 14:22:24 +1000117 if (!dev->primary->master)
118 return;
119
120 master_priv = dev->primary->master->driver_priv;
121 if (ring->head == ring->tail && master_priv->sarea_priv)
122 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Dave Airlie84b1fd12007-07-11 15:53:27 +1000125static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000127 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* Make sure interrupts are disabled here because the uninstall ioctl
129 * may not have been called from userspace and after dev_private
130 * is freed, it's too late.
131 */
Eric Anholted4cb412008-07-29 12:10:39 -0700132 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200135 mutex_lock(&dev->struct_mutex);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800136 intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
Zou Nan haid1b851f2010-05-21 09:08:57 +0800137 if (HAS_BSD(dev))
138 intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200139 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Keith Packard398c9cb2008-07-30 13:03:43 -0700141 /* Clear the HWS virtual address at teardown */
142 if (I915_NEED_GFX_HWS(dev))
143 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 return 0;
146}
147
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000148static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000150 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000151 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Dave Airlie3a03ac12009-01-11 09:03:49 +1000153 master_priv->sarea = drm_getsarea(dev);
154 if (master_priv->sarea) {
155 master_priv->sarea_priv = (drm_i915_sarea_t *)
156 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
157 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800158 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000159 }
160
Eric Anholt673a3942008-07-30 12:06:12 -0700161 if (init->ring_size != 0) {
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800162 if (dev_priv->render_ring.gem_object != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700163 i915_dma_cleanup(dev);
164 DRM_ERROR("Client tried to initialize ringbuffer in "
165 "GEM mode\n");
166 return -EINVAL;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800169 dev_priv->render_ring.size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Eric Anholtd3301d82010-05-21 13:55:54 -0700171 dev_priv->render_ring.map.offset = init->ring_start;
172 dev_priv->render_ring.map.size = init->ring_size;
173 dev_priv->render_ring.map.type = 0;
174 dev_priv->render_ring.map.flags = 0;
175 dev_priv->render_ring.map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Eric Anholtd3301d82010-05-21 13:55:54 -0700177 drm_core_ioremap_wc(&dev_priv->render_ring.map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700178
Eric Anholtd3301d82010-05-21 13:55:54 -0700179 if (dev_priv->render_ring.map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700180 i915_dma_cleanup(dev);
181 DRM_ERROR("can not ioremap virtual address for"
182 " ring buffer\n");
183 return -ENOMEM;
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
Eric Anholtd3301d82010-05-21 13:55:54 -0700187 dev_priv->render_ring.virtual_start = dev_priv->render_ring.map.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000189 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 dev_priv->back_offset = init->back_offset;
191 dev_priv->front_offset = init->front_offset;
192 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000193 if (master_priv->sarea_priv)
194 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Allow hardware batchbuffers unless told otherwise.
197 */
198 dev_priv->allow_batchbuffer = 1;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201}
202
Dave Airlie84b1fd12007-07-11 15:53:27 +1000203static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
206
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800207 struct intel_ring_buffer *ring;
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800208 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800210 ring = &dev_priv->render_ring;
211
212 if (ring->map.handle == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 DRM_ERROR("can not ioremap virtual address for"
214 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000215 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800219 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800223 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800224 ring->status_page.page_addr);
225 if (ring->status_page.gfx_addr != 0)
226 ring->setup_status_page(dev, ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000227 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700228 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800229
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800230 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 return 0;
233}
234
Eric Anholtc153f452007-09-03 12:06:45 +1000235static int i915_dma_init(struct drm_device *dev, void *data,
236 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Eric Anholtc153f452007-09-03 12:06:45 +1000238 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int retcode = 0;
240
Eric Anholtc153f452007-09-03 12:06:45 +1000241 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000243 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245 case I915_CLEANUP_DMA:
246 retcode = i915_dma_cleanup(dev);
247 break;
248 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100249 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000252 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 break;
254 }
255
256 return retcode;
257}
258
259/* Implement basically the same security restrictions as hardware does
260 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
261 *
262 * Most of the calculations below involve calculating the size of a
263 * particular instruction. It's important to get the size right as
264 * that tells us where the next instruction to check is. Any illegal
265 * instruction detected will be given a size of zero, which is a
266 * signal to abort the rest of the buffer.
267 */
268static int do_validate_cmd(int cmd)
269{
270 switch (((cmd >> 29) & 0x7)) {
271 case 0x0:
272 switch ((cmd >> 23) & 0x3f) {
273 case 0x0:
274 return 1; /* MI_NOOP */
275 case 0x4:
276 return 1; /* MI_FLUSH */
277 default:
278 return 0; /* disallow everything else */
279 }
280 break;
281 case 0x1:
282 return 0; /* reserved */
283 case 0x2:
284 return (cmd & 0xff) + 2; /* 2d commands */
285 case 0x3:
286 if (((cmd >> 24) & 0x1f) <= 0x18)
287 return 1;
288
289 switch ((cmd >> 24) & 0x1f) {
290 case 0x1c:
291 return 1;
292 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 case 0x3:
295 return (cmd & 0x1f) + 2;
296 case 0x4:
297 return (cmd & 0xf) + 2;
298 default:
299 return (cmd & 0xffff) + 2;
300 }
301 case 0x1e:
302 if (cmd & (1 << 23))
303 return (cmd & 0xffff) + 1;
304 else
305 return 1;
306 case 0x1f:
307 if ((cmd & (1 << 23)) == 0) /* inline vertices */
308 return (cmd & 0x1ffff) + 2;
309 else if (cmd & (1 << 17)) /* indirect random */
310 if ((cmd & 0xffff) == 0)
311 return 0; /* unknown length, too hard */
312 else
313 return (((cmd & 0xffff) + 1) / 2) + 1;
314 else
315 return 2; /* indirect sequential */
316 default:
317 return 0;
318 }
319 default:
320 return 0;
321 }
322
323 return 0;
324}
325
326static int validate_cmd(int cmd)
327{
328 int ret = do_validate_cmd(cmd);
329
Dave Airliebc5f4522007-11-05 12:50:58 +1000330/* printk("validate_cmd( %x ): %d\n", cmd, ret); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 return ret;
333}
334
Eric Anholt201361a2009-03-11 12:30:04 -0700335static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 drm_i915_private_t *dev_priv = dev->dev_private;
338 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800340 if ((dwords+1) * sizeof(int) >= dev_priv->render_ring.size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000341 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100342
Alan Hourihanec29b6692006-08-12 16:29:24 +1000343 BEGIN_LP_RING((dwords+1)&~1);
Dave Airliede227f52006-01-25 15:31:43 +1100344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 for (i = 0; i < dwords;) {
346 int cmd, sz;
347
Eric Anholt201361a2009-03-11 12:30:04 -0700348 cmd = buffer[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if ((sz = validate_cmd(cmd)) == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 OUT_RING(cmd);
354
355 while (++i, --sz) {
Eric Anholt201361a2009-03-11 12:30:04 -0700356 OUT_RING(buffer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
Dave Airliede227f52006-01-25 15:31:43 +1100360 if (dwords & 1)
361 OUT_RING(0);
362
363 ADVANCE_LP_RING();
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
367
Eric Anholt673a3942008-07-30 12:06:12 -0700368int
369i915_emit_box(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700370 struct drm_clip_rect *boxes,
Eric Anholt673a3942008-07-30 12:06:12 -0700371 int i, int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Eric Anholt201361a2009-03-11 12:30:04 -0700373 struct drm_clip_rect box = boxes[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (box.y2 <= box.y1 || box.x2 <= box.x1 || box.y2 <= 0 || box.x2 <= 0) {
376 DRM_ERROR("Bad box %d,%d..%d,%d\n",
377 box.x1, box.y1, box.x2, box.y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Alan Hourihanec29b6692006-08-12 16:29:24 +1000381 if (IS_I965G(dev)) {
382 BEGIN_LP_RING(4);
383 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
384 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
Andrew Morton78eca432006-08-16 09:15:51 +1000385 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000386 OUT_RING(DR4);
387 ADVANCE_LP_RING();
388 } else {
389 BEGIN_LP_RING(6);
390 OUT_RING(GFX_OP_DRAWRECT_INFO);
391 OUT_RING(DR1);
392 OUT_RING((box.x1 & 0xffff) | (box.y1 << 16));
393 OUT_RING(((box.x2 - 1) & 0xffff) | ((box.y2 - 1) << 16));
394 OUT_RING(DR4);
395 OUT_RING(0);
396 ADVANCE_LP_RING();
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400}
401
Alan Hourihanec29b6692006-08-12 16:29:24 +1000402/* XXX: Emitting the counter should really be moved to part of the IRQ
403 * emit. For now, do it in both places:
404 */
405
Dave Airlie84b1fd12007-07-11 15:53:27 +1000406static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100407{
408 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000409 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100410
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400411 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000412 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400413 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000414 if (master_priv->sarea_priv)
415 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100416
417 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700418 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000419 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airliede227f52006-01-25 15:31:43 +1100420 OUT_RING(dev_priv->counter);
421 OUT_RING(0);
422 ADVANCE_LP_RING();
423}
424
Dave Airlie84b1fd12007-07-11 15:53:27 +1000425static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700426 drm_i915_cmdbuffer_t *cmd,
427 struct drm_clip_rect *cliprects,
428 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 int nbox = cmd->num_cliprects;
431 int i = 0, count, ret;
432
433 if (cmd->sz & 0x3) {
434 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000435 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
438 i915_kernel_lost_context(dev);
439
440 count = nbox ? nbox : 1;
441
442 for (i = 0; i < count; i++) {
443 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700444 ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 cmd->DR1, cmd->DR4);
446 if (ret)
447 return ret;
448 }
449
Eric Anholt201361a2009-03-11 12:30:04 -0700450 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (ret)
452 return ret;
453 }
454
Dave Airliede227f52006-01-25 15:31:43 +1100455 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457}
458
Dave Airlie84b1fd12007-07-11 15:53:27 +1000459static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700460 drm_i915_batchbuffer_t * batch,
461 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int nbox = batch->num_cliprects;
464 int i = 0, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if ((batch->start | batch->used) & 0x7) {
467 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
471 i915_kernel_lost_context(dev);
472
473 count = nbox ? nbox : 1;
474
475 for (i = 0; i < count; i++) {
476 if (i < nbox) {
Eric Anholt201361a2009-03-11 12:30:04 -0700477 int ret = i915_emit_box(dev, cliprects, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 batch->DR1, batch->DR4);
479 if (ret)
480 return ret;
481 }
482
Keith Packard0790d5e2008-07-30 12:28:47 -0700483 if (!IS_I830(dev) && !IS_845G(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 BEGIN_LP_RING(2);
Dave Airlie21f16282007-08-07 09:09:51 +1000485 if (IS_I965G(dev)) {
486 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
487 OUT_RING(batch->start);
488 } else {
489 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
490 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 ADVANCE_LP_RING();
493 } else {
494 BEGIN_LP_RING(4);
495 OUT_RING(MI_BATCH_BUFFER);
496 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
497 OUT_RING(batch->start + batch->used - 4);
498 OUT_RING(0);
499 ADVANCE_LP_RING();
500 }
501 }
502
Zou Nan hai1cafd342010-06-25 13:40:24 +0800503
504 if (IS_G4X(dev) || IS_IRONLAKE(dev)) {
505 BEGIN_LP_RING(2);
506 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
507 OUT_RING(MI_NOOP);
508 ADVANCE_LP_RING();
509 }
Dave Airliede227f52006-01-25 15:31:43 +1100510 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 return 0;
513}
514
Dave Airlieaf6061a2008-05-07 12:15:39 +1000515static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000518 struct drm_i915_master_private *master_priv =
519 dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dave Airlie7c1c2872008-11-28 14:22:24 +1000521 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400522 return -EINVAL;
523
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800524 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800525 __func__,
526 dev_priv->current_page,
527 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Dave Airlieaf6061a2008-05-07 12:15:39 +1000529 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Dave Airlieaf6061a2008-05-07 12:15:39 +1000531 BEGIN_LP_RING(2);
Jesse Barnes585fb112008-07-29 11:54:06 -0700532 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000533 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ADVANCE_LP_RING();
535
Dave Airlieaf6061a2008-05-07 12:15:39 +1000536 BEGIN_LP_RING(6);
537 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
538 OUT_RING(0);
539 if (dev_priv->current_page == 0) {
540 OUT_RING(dev_priv->back_offset);
541 dev_priv->current_page = 1;
542 } else {
543 OUT_RING(dev_priv->front_offset);
544 dev_priv->current_page = 0;
545 }
546 OUT_RING(0);
547 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000548
Dave Airlieaf6061a2008-05-07 12:15:39 +1000549 BEGIN_LP_RING(2);
550 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
551 OUT_RING(0);
552 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000553
Dave Airlie7c1c2872008-11-28 14:22:24 +1000554 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000555
Dave Airlieaf6061a2008-05-07 12:15:39 +1000556 BEGIN_LP_RING(4);
Jesse Barnes585fb112008-07-29 11:54:06 -0700557 OUT_RING(MI_STORE_DWORD_INDEX);
Keith Packard0baf8232008-11-08 11:44:14 +1000558 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000559 OUT_RING(dev_priv->counter);
560 OUT_RING(0);
561 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000562
Dave Airlie7c1c2872008-11-28 14:22:24 +1000563 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Dave Airlie84b1fd12007-07-11 15:53:27 +1000567static int i915_quiescent(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 drm_i915_private_t *dev_priv = dev->dev_private;
570
571 i915_kernel_lost_context(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800572 return intel_wait_ring_buffer(dev, &dev_priv->render_ring,
573 dev_priv->render_ring.size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Eric Anholtc153f452007-09-03 12:06:45 +1000576static int i915_flush_ioctl(struct drm_device *dev, void *data,
577 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Eric Anholt546b0972008-09-01 16:45:29 -0700579 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Eric Anholt546b0972008-09-01 16:45:29 -0700581 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
582
583 mutex_lock(&dev->struct_mutex);
584 ret = i915_quiescent(dev);
585 mutex_unlock(&dev->struct_mutex);
586
587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Eric Anholtc153f452007-09-03 12:06:45 +1000590static int i915_batchbuffer(struct drm_device *dev, void *data,
591 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000594 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000596 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000597 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700599 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (!dev_priv->allow_batchbuffer) {
602 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000603 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800606 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800607 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Eric Anholt546b0972008-09-01 16:45:29 -0700609 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Eric Anholt201361a2009-03-11 12:30:04 -0700611 if (batch->num_cliprects < 0)
612 return -EINVAL;
613
614 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700615 cliprects = kcalloc(batch->num_cliprects,
616 sizeof(struct drm_clip_rect),
617 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700618 if (cliprects == NULL)
619 return -ENOMEM;
620
621 ret = copy_from_user(cliprects, batch->cliprects,
622 batch->num_cliprects *
623 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200624 if (ret != 0) {
625 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700626 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200627 }
Eric Anholt201361a2009-03-11 12:30:04 -0700628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Eric Anholt546b0972008-09-01 16:45:29 -0700630 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700631 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700632 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400634 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000635 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700636
637fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700638 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return ret;
641}
642
Eric Anholtc153f452007-09-03 12:06:45 +1000643static int i915_cmdbuffer(struct drm_device *dev, void *data,
644 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000647 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000649 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000650 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700651 struct drm_clip_rect *cliprects = NULL;
652 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int ret;
654
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800655 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800656 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Eric Anholt546b0972008-09-01 16:45:29 -0700658 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Eric Anholt201361a2009-03-11 12:30:04 -0700660 if (cmdbuf->num_cliprects < 0)
661 return -EINVAL;
662
Eric Anholt9a298b22009-03-24 12:23:04 -0700663 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700664 if (batch_data == NULL)
665 return -ENOMEM;
666
667 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200668 if (ret != 0) {
669 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700670 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200671 }
Eric Anholt201361a2009-03-11 12:30:04 -0700672
673 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700674 cliprects = kcalloc(cmdbuf->num_cliprects,
675 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000676 if (cliprects == NULL) {
677 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700678 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000679 }
Eric Anholt201361a2009-03-11 12:30:04 -0700680
681 ret = copy_from_user(cliprects, cmdbuf->cliprects,
682 cmdbuf->num_cliprects *
683 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200684 if (ret != 0) {
685 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700686 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Eric Anholt546b0972008-09-01 16:45:29 -0700690 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700691 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700692 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (ret) {
694 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000695 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400698 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000699 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700700
Eric Anholt201361a2009-03-11 12:30:04 -0700701fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700702 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000703fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700704 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700705
706 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Eric Anholtc153f452007-09-03 12:06:45 +1000709static int i915_flip_bufs(struct drm_device *dev, void *data,
710 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Eric Anholt546b0972008-09-01 16:45:29 -0700712 int ret;
713
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800714 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Eric Anholt546b0972008-09-01 16:45:29 -0700716 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Eric Anholt546b0972008-09-01 16:45:29 -0700718 mutex_lock(&dev->struct_mutex);
719 ret = i915_dispatch_flip(dev);
720 mutex_unlock(&dev->struct_mutex);
721
722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Eric Anholtc153f452007-09-03 12:06:45 +1000725static int i915_getparam(struct drm_device *dev, void *data,
726 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000729 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int value;
731
732 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000733 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000734 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
Eric Anholtc153f452007-09-03 12:06:45 +1000737 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700739 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741 case I915_PARAM_ALLOW_BATCHBUFFER:
742 value = dev_priv->allow_batchbuffer ? 1 : 0;
743 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100744 case I915_PARAM_LAST_DISPATCH:
745 value = READ_BREADCRUMB(dev_priv);
746 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400747 case I915_PARAM_CHIPSET_ID:
748 value = dev->pci_device;
749 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700750 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000751 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700752 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800753 case I915_PARAM_NUM_FENCES_AVAIL:
754 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
755 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200756 case I915_PARAM_HAS_OVERLAY:
757 value = dev_priv->overlay ? 1 : 0;
758 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800759 case I915_PARAM_HAS_PAGEFLIPPING:
760 value = 1;
761 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500762 case I915_PARAM_HAS_EXECBUF2:
763 /* depends on GEM */
764 value = dev_priv->has_gem;
765 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800766 case I915_PARAM_HAS_BSD:
767 value = HAS_BSD(dev);
768 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800770 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -0500771 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Eric Anholtc153f452007-09-03 12:06:45 +1000775 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000777 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
779
780 return 0;
781}
782
Eric Anholtc153f452007-09-03 12:06:45 +1000783static int i915_setparam(struct drm_device *dev, void *data,
784 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000787 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000790 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000791 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Eric Anholtc153f452007-09-03 12:06:45 +1000794 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 break;
797 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000798 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
800 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000801 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800803 case I915_SETPARAM_NUM_USED_FENCES:
804 if (param->value > dev_priv->num_fence_regs ||
805 param->value < 0)
806 return -EINVAL;
807 /* Userspace can use first N regs */
808 dev_priv->fence_reg_start = param->value;
809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800811 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800812 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 return 0;
817}
818
Eric Anholtc153f452007-09-03 12:06:45 +1000819static int i915_set_status_page(struct drm_device *dev, void *data,
820 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000821{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000822 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000823 drm_i915_hws_addr_t *hws = data;
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800824 struct intel_ring_buffer *ring = &dev_priv->render_ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000825
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000826 if (!I915_NEED_GFX_HWS(dev))
827 return -EINVAL;
828
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000829 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000830 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000831 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000832 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000833
Jesse Barnes79e53942008-11-07 14:24:08 -0800834 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
835 WARN(1, "tried to set status page when mode setting active\n");
836 return 0;
837 }
838
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800839 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000840
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800841 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +1000842
Eric Anholt8b409582007-11-22 16:40:37 +1000843 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000844 dev_priv->hws_map.size = 4*1024;
845 dev_priv->hws_map.type = 0;
846 dev_priv->hws_map.flags = 0;
847 dev_priv->hws_map.mtrr = 0;
848
Dave Airliedd0910b2009-02-25 14:49:21 +1000849 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000850 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000851 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -0700852 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000853 DRM_ERROR("can not ioremap virtual address for"
854 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000855 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000856 }
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800857 ring->status_page.page_addr = dev_priv->hws_map.handle;
858 memset(ring->status_page.page_addr, 0, PAGE_SIZE);
859 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000860
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800861 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -0700862 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800863 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -0700864 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000865 return 0;
866}
867
Dave Airlieec2a4c32009-08-04 11:43:41 +1000868static int i915_get_bridge_dev(struct drm_device *dev)
869{
870 struct drm_i915_private *dev_priv = dev->dev_private;
871
872 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
873 if (!dev_priv->bridge_dev) {
874 DRM_ERROR("bridge device not found\n");
875 return -1;
876 }
877 return 0;
878}
879
Zhenyu Wangc4804412009-12-17 14:48:43 +0800880#define MCHBAR_I915 0x44
881#define MCHBAR_I965 0x48
882#define MCHBAR_SIZE (4*4096)
883
884#define DEVEN_REG 0x54
885#define DEVEN_MCHBAR_EN (1 << 28)
886
887/* Allocate space for the MCH regs if needed, return nonzero on error */
888static int
889intel_alloc_mchbar_resource(struct drm_device *dev)
890{
891 drm_i915_private_t *dev_priv = dev->dev_private;
892 int reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
893 u32 temp_lo, temp_hi = 0;
894 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +0100895 int ret;
Zhenyu Wangc4804412009-12-17 14:48:43 +0800896
897 if (IS_I965G(dev))
898 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
899 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
900 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
901
902 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
903#ifdef CONFIG_PNP
904 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +0100905 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
906 return 0;
Zhenyu Wangc4804412009-12-17 14:48:43 +0800907#endif
908
909 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +0100910 dev_priv->mch_res.name = "i915 MCHBAR";
911 dev_priv->mch_res.flags = IORESOURCE_MEM;
912 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
913 &dev_priv->mch_res,
Zhenyu Wangc4804412009-12-17 14:48:43 +0800914 MCHBAR_SIZE, MCHBAR_SIZE,
915 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +0100916 0, pcibios_align_resource,
Zhenyu Wangc4804412009-12-17 14:48:43 +0800917 dev_priv->bridge_dev);
918 if (ret) {
919 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
920 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +0100921 return ret;
Zhenyu Wangc4804412009-12-17 14:48:43 +0800922 }
923
924 if (IS_I965G(dev))
925 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
926 upper_32_bits(dev_priv->mch_res.start));
927
928 pci_write_config_dword(dev_priv->bridge_dev, reg,
929 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +0100930 return 0;
Zhenyu Wangc4804412009-12-17 14:48:43 +0800931}
932
933/* Setup MCHBAR if possible, return true if we should disable it again */
934static void
935intel_setup_mchbar(struct drm_device *dev)
936{
937 drm_i915_private_t *dev_priv = dev->dev_private;
938 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
939 u32 temp;
940 bool enabled;
941
942 dev_priv->mchbar_need_disable = false;
943
944 if (IS_I915G(dev) || IS_I915GM(dev)) {
945 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
946 enabled = !!(temp & DEVEN_MCHBAR_EN);
947 } else {
948 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
949 enabled = temp & 1;
950 }
951
952 /* If it's already enabled, don't have to do anything */
953 if (enabled)
954 return;
955
956 if (intel_alloc_mchbar_resource(dev))
957 return;
958
959 dev_priv->mchbar_need_disable = true;
960
961 /* Space is allocated or reserved, so enable it. */
962 if (IS_I915G(dev) || IS_I915GM(dev)) {
963 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
964 temp | DEVEN_MCHBAR_EN);
965 } else {
966 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
967 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
968 }
969}
970
971static void
972intel_teardown_mchbar(struct drm_device *dev)
973{
974 drm_i915_private_t *dev_priv = dev->dev_private;
975 int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
976 u32 temp;
977
978 if (dev_priv->mchbar_need_disable) {
979 if (IS_I915G(dev) || IS_I915GM(dev)) {
980 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
981 temp &= ~DEVEN_MCHBAR_EN;
982 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
983 } else {
984 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
985 temp &= ~1;
986 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
987 }
988 }
989
990 if (dev_priv->mch_res.start)
991 release_resource(&dev_priv->mch_res);
992}
993
Jesse Barnes79e53942008-11-07 14:24:08 -0800994/**
995 * i915_probe_agp - get AGP bootup configuration
996 * @pdev: PCI device
997 * @aperture_size: returns AGP aperture configured size
998 * @preallocated_size: returns size of BIOS preallocated AGP space
999 *
1000 * Since Intel integrated graphics are UMA, the BIOS has to set aside
1001 * some RAM for the framebuffer at early boot. This code figures out
1002 * how much was set aside so we can use it for our own purposes.
1003 */
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001004static int i915_probe_agp(struct drm_device *dev, uint32_t *aperture_size,
Jesse Barnes80824002009-09-10 15:28:06 -07001005 uint32_t *preallocated_size,
1006 uint32_t *start)
Jesse Barnes79e53942008-11-07 14:24:08 -08001007{
Dave Airlieec2a4c32009-08-04 11:43:41 +10001008 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001009 u16 tmp = 0;
1010 unsigned long overhead;
Eric Anholt241fa852009-01-02 18:05:51 -08001011 unsigned long stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -08001012
Jesse Barnes79e53942008-11-07 14:24:08 -08001013 /* Get the fb aperture size and "stolen" memory amount. */
Dave Airlieec2a4c32009-08-04 11:43:41 +10001014 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &tmp);
Jesse Barnes79e53942008-11-07 14:24:08 -08001015
1016 *aperture_size = 1024 * 1024;
1017 *preallocated_size = 1024 * 1024;
1018
Eric Anholt60fd99e2008-12-03 22:50:02 -08001019 switch (dev->pdev->device) {
Jesse Barnes79e53942008-11-07 14:24:08 -08001020 case PCI_DEVICE_ID_INTEL_82830_CGC:
1021 case PCI_DEVICE_ID_INTEL_82845G_IG:
1022 case PCI_DEVICE_ID_INTEL_82855GM_IG:
1023 case PCI_DEVICE_ID_INTEL_82865_IG:
1024 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
1025 *aperture_size *= 64;
1026 else
1027 *aperture_size *= 128;
1028 break;
1029 default:
1030 /* 9xx supports large sizes, just look at the length */
Eric Anholt60fd99e2008-12-03 22:50:02 -08001031 *aperture_size = pci_resource_len(dev->pdev, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001032 break;
1033 }
1034
1035 /*
1036 * Some of the preallocated space is taken by the GTT
1037 * and popup. GTT is 1K per MB of aperture size, and popup is 4K.
1038 */
Eric Anholtbad720f2009-10-22 16:11:14 -07001039 if (IS_G4X(dev) || IS_PINEVIEW(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev))
Eric Anholt60fd99e2008-12-03 22:50:02 -08001040 overhead = 4096;
1041 else
1042 overhead = (*aperture_size / 1024) + 4096;
1043
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001044 if (IS_GEN6(dev)) {
1045 /* SNB has memory control reg at 0x50.w */
1046 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &tmp);
1047
1048 switch (tmp & SNB_GMCH_GMS_STOLEN_MASK) {
1049 case INTEL_855_GMCH_GMS_DISABLED:
Eric Anholtbad720f2009-10-22 16:11:14 -07001050 DRM_ERROR("video memory is disabled\n");
1051 return -1;
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001052 case SNB_GMCH_GMS_STOLEN_32M:
1053 stolen = 32 * 1024 * 1024;
1054 break;
1055 case SNB_GMCH_GMS_STOLEN_64M:
1056 stolen = 64 * 1024 * 1024;
1057 break;
1058 case SNB_GMCH_GMS_STOLEN_96M:
1059 stolen = 96 * 1024 * 1024;
1060 break;
1061 case SNB_GMCH_GMS_STOLEN_128M:
1062 stolen = 128 * 1024 * 1024;
1063 break;
1064 case SNB_GMCH_GMS_STOLEN_160M:
1065 stolen = 160 * 1024 * 1024;
1066 break;
1067 case SNB_GMCH_GMS_STOLEN_192M:
1068 stolen = 192 * 1024 * 1024;
1069 break;
1070 case SNB_GMCH_GMS_STOLEN_224M:
1071 stolen = 224 * 1024 * 1024;
1072 break;
1073 case SNB_GMCH_GMS_STOLEN_256M:
1074 stolen = 256 * 1024 * 1024;
1075 break;
1076 case SNB_GMCH_GMS_STOLEN_288M:
1077 stolen = 288 * 1024 * 1024;
1078 break;
1079 case SNB_GMCH_GMS_STOLEN_320M:
1080 stolen = 320 * 1024 * 1024;
1081 break;
1082 case SNB_GMCH_GMS_STOLEN_352M:
1083 stolen = 352 * 1024 * 1024;
1084 break;
1085 case SNB_GMCH_GMS_STOLEN_384M:
1086 stolen = 384 * 1024 * 1024;
1087 break;
1088 case SNB_GMCH_GMS_STOLEN_416M:
1089 stolen = 416 * 1024 * 1024;
1090 break;
1091 case SNB_GMCH_GMS_STOLEN_448M:
1092 stolen = 448 * 1024 * 1024;
1093 break;
1094 case SNB_GMCH_GMS_STOLEN_480M:
1095 stolen = 480 * 1024 * 1024;
1096 break;
1097 case SNB_GMCH_GMS_STOLEN_512M:
1098 stolen = 512 * 1024 * 1024;
1099 break;
1100 default:
1101 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
1102 tmp & SNB_GMCH_GMS_STOLEN_MASK);
1103 return -1;
Eric Anholtbad720f2009-10-22 16:11:14 -07001104 }
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001105 } else {
1106 switch (tmp & INTEL_GMCH_GMS_MASK) {
1107 case INTEL_855_GMCH_GMS_DISABLED:
1108 DRM_ERROR("video memory is disabled\n");
1109 return -1;
1110 case INTEL_855_GMCH_GMS_STOLEN_1M:
1111 stolen = 1 * 1024 * 1024;
1112 break;
1113 case INTEL_855_GMCH_GMS_STOLEN_4M:
1114 stolen = 4 * 1024 * 1024;
1115 break;
1116 case INTEL_855_GMCH_GMS_STOLEN_8M:
1117 stolen = 8 * 1024 * 1024;
1118 break;
1119 case INTEL_855_GMCH_GMS_STOLEN_16M:
1120 stolen = 16 * 1024 * 1024;
1121 break;
1122 case INTEL_855_GMCH_GMS_STOLEN_32M:
1123 stolen = 32 * 1024 * 1024;
1124 break;
1125 case INTEL_915G_GMCH_GMS_STOLEN_48M:
1126 stolen = 48 * 1024 * 1024;
1127 break;
1128 case INTEL_915G_GMCH_GMS_STOLEN_64M:
1129 stolen = 64 * 1024 * 1024;
1130 break;
1131 case INTEL_GMCH_GMS_STOLEN_128M:
1132 stolen = 128 * 1024 * 1024;
1133 break;
1134 case INTEL_GMCH_GMS_STOLEN_256M:
1135 stolen = 256 * 1024 * 1024;
1136 break;
1137 case INTEL_GMCH_GMS_STOLEN_96M:
1138 stolen = 96 * 1024 * 1024;
1139 break;
1140 case INTEL_GMCH_GMS_STOLEN_160M:
1141 stolen = 160 * 1024 * 1024;
1142 break;
1143 case INTEL_GMCH_GMS_STOLEN_224M:
1144 stolen = 224 * 1024 * 1024;
1145 break;
1146 case INTEL_GMCH_GMS_STOLEN_352M:
1147 stolen = 352 * 1024 * 1024;
1148 break;
1149 default:
1150 DRM_ERROR("unexpected GMCH_GMS value: 0x%02x\n",
1151 tmp & INTEL_GMCH_GMS_MASK);
1152 return -1;
1153 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001154 }
Zhenyu Wang14bc4902009-11-11 01:25:25 +08001155
Eric Anholt241fa852009-01-02 18:05:51 -08001156 *preallocated_size = stolen - overhead;
Jesse Barnes80824002009-09-10 15:28:06 -07001157 *start = overhead;
Jesse Barnes79e53942008-11-07 14:24:08 -08001158
1159 return 0;
1160}
1161
Jesse Barnes80824002009-09-10 15:28:06 -07001162#define PTE_ADDRESS_MASK 0xfffff000
1163#define PTE_ADDRESS_MASK_HIGH 0x000000f0 /* i915+ */
1164#define PTE_MAPPING_TYPE_UNCACHED (0 << 1)
1165#define PTE_MAPPING_TYPE_DCACHE (1 << 1) /* i830 only */
1166#define PTE_MAPPING_TYPE_CACHED (3 << 1)
1167#define PTE_MAPPING_TYPE_MASK (3 << 1)
1168#define PTE_VALID (1 << 0)
1169
1170/**
1171 * i915_gtt_to_phys - take a GTT address and turn it into a physical one
1172 * @dev: drm device
1173 * @gtt_addr: address to translate
1174 *
1175 * Some chip functions require allocations from stolen space but need the
1176 * physical address of the memory in question. We use this routine
1177 * to get a physical address suitable for register programming from a given
1178 * GTT address.
1179 */
1180static unsigned long i915_gtt_to_phys(struct drm_device *dev,
1181 unsigned long gtt_addr)
1182{
1183 unsigned long *gtt;
1184 unsigned long entry, phys;
1185 int gtt_bar = IS_I9XX(dev) ? 0 : 1;
1186 int gtt_offset, gtt_size;
1187
1188 if (IS_I965G(dev)) {
Eric Anholtbad720f2009-10-22 16:11:14 -07001189 if (IS_G4X(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev)) {
Jesse Barnes80824002009-09-10 15:28:06 -07001190 gtt_offset = 2*1024*1024;
1191 gtt_size = 2*1024*1024;
1192 } else {
1193 gtt_offset = 512*1024;
1194 gtt_size = 512*1024;
1195 }
1196 } else {
1197 gtt_bar = 3;
1198 gtt_offset = 0;
1199 gtt_size = pci_resource_len(dev->pdev, gtt_bar);
1200 }
1201
1202 gtt = ioremap_wc(pci_resource_start(dev->pdev, gtt_bar) + gtt_offset,
1203 gtt_size);
1204 if (!gtt) {
1205 DRM_ERROR("ioremap of GTT failed\n");
1206 return 0;
1207 }
1208
1209 entry = *(volatile u32 *)(gtt + (gtt_addr / 1024));
1210
Zhao Yakui44d98a62009-10-09 11:39:40 +08001211 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, PTE: 0x%08lx\n", gtt_addr, entry);
Jesse Barnes80824002009-09-10 15:28:06 -07001212
1213 /* Mask out these reserved bits on this hardware. */
1214 if (!IS_I9XX(dev) || IS_I915G(dev) || IS_I915GM(dev) ||
1215 IS_I945G(dev) || IS_I945GM(dev)) {
1216 entry &= ~PTE_ADDRESS_MASK_HIGH;
1217 }
1218
1219 /* If it's not a mapping type we know, then bail. */
1220 if ((entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_UNCACHED &&
1221 (entry & PTE_MAPPING_TYPE_MASK) != PTE_MAPPING_TYPE_CACHED) {
1222 iounmap(gtt);
1223 return 0;
1224 }
1225
1226 if (!(entry & PTE_VALID)) {
1227 DRM_ERROR("bad GTT entry in stolen space\n");
1228 iounmap(gtt);
1229 return 0;
1230 }
1231
1232 iounmap(gtt);
1233
1234 phys =(entry & PTE_ADDRESS_MASK) |
1235 ((uint64_t)(entry & PTE_ADDRESS_MASK_HIGH) << (32 - 4));
1236
Zhao Yakui44d98a62009-10-09 11:39:40 +08001237 DRM_DEBUG_DRIVER("GTT addr: 0x%08lx, phys addr: 0x%08lx\n", gtt_addr, phys);
Jesse Barnes80824002009-09-10 15:28:06 -07001238
1239 return phys;
1240}
1241
1242static void i915_warn_stolen(struct drm_device *dev)
1243{
1244 DRM_ERROR("not enough stolen space for compressed buffer, disabling\n");
1245 DRM_ERROR("hint: you may be able to increase stolen memory size in the BIOS to avoid this\n");
1246}
1247
1248static void i915_setup_compression(struct drm_device *dev, int size)
1249{
1250 struct drm_i915_private *dev_priv = dev->dev_private;
Prarit Bhargava132b6aa2010-05-27 13:37:56 -04001251 struct drm_mm_node *compressed_fb, *uninitialized_var(compressed_llb);
Andrew Morton29bd0ae2009-11-17 14:08:52 -08001252 unsigned long cfb_base;
1253 unsigned long ll_base = 0;
Jesse Barnes80824002009-09-10 15:28:06 -07001254
1255 /* Leave 1M for line length buffer & misc. */
1256 compressed_fb = drm_mm_search_free(&dev_priv->vram, size, 4096, 0);
1257 if (!compressed_fb) {
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001258 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001259 i915_warn_stolen(dev);
1260 return;
1261 }
1262
1263 compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
1264 if (!compressed_fb) {
1265 i915_warn_stolen(dev);
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001266 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001267 return;
1268 }
1269
Jesse Barnes74dff282009-09-14 15:39:40 -07001270 cfb_base = i915_gtt_to_phys(dev, compressed_fb->start);
1271 if (!cfb_base) {
1272 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1273 drm_mm_put_block(compressed_fb);
Jesse Barnes80824002009-09-10 15:28:06 -07001274 }
1275
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001276 if (!(IS_GM45(dev) || IS_IRONLAKE_M(dev))) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001277 compressed_llb = drm_mm_search_free(&dev_priv->vram, 4096,
1278 4096, 0);
1279 if (!compressed_llb) {
1280 i915_warn_stolen(dev);
1281 return;
1282 }
1283
1284 compressed_llb = drm_mm_get_block(compressed_llb, 4096, 4096);
1285 if (!compressed_llb) {
1286 i915_warn_stolen(dev);
1287 return;
1288 }
1289
1290 ll_base = i915_gtt_to_phys(dev, compressed_llb->start);
1291 if (!ll_base) {
1292 DRM_ERROR("failed to get stolen phys addr, disabling FBC\n");
1293 drm_mm_put_block(compressed_fb);
1294 drm_mm_put_block(compressed_llb);
1295 }
Jesse Barnes80824002009-09-10 15:28:06 -07001296 }
1297
1298 dev_priv->cfb_size = size;
1299
Adam Jacksonee5382a2010-04-23 11:17:39 -04001300 intel_disable_fbc(dev);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001301 dev_priv->compressed_fb = compressed_fb;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001302 if (IS_IRONLAKE_M(dev))
1303 I915_WRITE(ILK_DPFC_CB_BASE, compressed_fb->start);
1304 else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001305 I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
1306 } else {
Jesse Barnes74dff282009-09-14 15:39:40 -07001307 I915_WRITE(FBC_CFB_BASE, cfb_base);
1308 I915_WRITE(FBC_LL_BASE, ll_base);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001309 dev_priv->compressed_llb = compressed_llb;
Jesse Barnes80824002009-09-10 15:28:06 -07001310 }
1311
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001312 DRM_DEBUG_KMS("FBC base 0x%08lx, ll base 0x%08lx, size %dM\n", cfb_base,
Jesse Barnes80824002009-09-10 15:28:06 -07001313 ll_base, size >> 20);
Jesse Barnes80824002009-09-10 15:28:06 -07001314}
1315
Jesse Barnes20bf3772010-04-21 11:39:22 -07001316static void i915_cleanup_compression(struct drm_device *dev)
1317{
1318 struct drm_i915_private *dev_priv = dev->dev_private;
1319
1320 drm_mm_put_block(dev_priv->compressed_fb);
Jesse Barnesaebf0da2010-07-22 08:12:20 -07001321 if (dev_priv->compressed_llb)
Jesse Barnes20bf3772010-04-21 11:39:22 -07001322 drm_mm_put_block(dev_priv->compressed_llb);
1323}
1324
Dave Airlie28d52042009-09-21 14:33:58 +10001325/* true = enable decode, false = disable decoder */
1326static unsigned int i915_vga_set_decode(void *cookie, bool state)
1327{
1328 struct drm_device *dev = cookie;
1329
1330 intel_modeset_vga_set_state(dev, state);
1331 if (state)
1332 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1333 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1334 else
1335 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1336}
1337
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001338static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1339{
1340 struct drm_device *dev = pci_get_drvdata(pdev);
1341 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1342 if (state == VGA_SWITCHEROO_ON) {
Dave Airliefbf81762010-06-01 09:09:06 +10001343 printk(KERN_INFO "i915: switched on\n");
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001344 /* i915 resume handler doesn't set to D0 */
1345 pci_set_power_state(dev->pdev, PCI_D0);
1346 i915_resume(dev);
Dave Airliefbf81762010-06-01 09:09:06 +10001347 drm_kms_helper_poll_enable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001348 } else {
1349 printk(KERN_ERR "i915: switched off\n");
Dave Airliefbf81762010-06-01 09:09:06 +10001350 drm_kms_helper_poll_disable(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001351 i915_suspend(dev, pmm);
1352 }
1353}
1354
1355static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1356{
1357 struct drm_device *dev = pci_get_drvdata(pdev);
1358 bool can_switch;
1359
1360 spin_lock(&dev->count_lock);
1361 can_switch = (dev->open_count == 0);
1362 spin_unlock(&dev->count_lock);
1363 return can_switch;
1364}
1365
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001366static int i915_load_modeset_init(struct drm_device *dev,
Jesse Barnes80824002009-09-10 15:28:06 -07001367 unsigned long prealloc_start,
Eric Anholt2a34f5e62009-07-02 09:30:50 -07001368 unsigned long prealloc_size,
1369 unsigned long agp_size)
Jesse Barnes79e53942008-11-07 14:24:08 -08001370{
1371 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001372 int fb_bar = IS_I9XX(dev) ? 2 : 0;
1373 int ret = 0;
1374
Jordan Crouse01d73a62010-05-27 13:40:24 -06001375 dev->mode_config.fb_base = pci_resource_start(dev->pdev, fb_bar) &
Jesse Barnes79e53942008-11-07 14:24:08 -08001376 0xff000000;
1377
Jesse Barnes79e53942008-11-07 14:24:08 -08001378 /* Basic memrange allocator for stolen space (aka vram) */
1379 drm_mm_init(&dev_priv->vram, 0, prealloc_size);
Jesse Barnes80824002009-09-10 15:28:06 -07001380 DRM_INFO("set up %ldM of stolen space\n", prealloc_size / (1024*1024));
Jesse Barnes79e53942008-11-07 14:24:08 -08001381
Ben Gamari11ed50e2009-09-14 17:48:45 -04001382 /* We're off and running w/KMS */
1383 dev_priv->mm.suspended = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001384
Eric Anholt13f4c432009-05-12 15:27:36 -07001385 /* Let GEM Manage from end of prealloc space to end of aperture.
1386 *
1387 * However, leave one page at the end still bound to the scratch page.
1388 * There are a number of places where the hardware apparently
1389 * prefetches past the end of the object, and we've seen multiple
1390 * hangs with the GPU head pointer stuck in a batchbuffer bound
1391 * at the last page of the aperture. One page should be enough to
1392 * keep any prefetching inside of the aperture.
1393 */
1394 i915_gem_do_init(dev, prealloc_size, agp_size - 4096);
Jesse Barnes79e53942008-11-07 14:24:08 -08001395
Ben Gamari11ed50e2009-09-14 17:48:45 -04001396 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001397 ret = i915_gem_init_ringbuffer(dev);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001398 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001399 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001400 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001401
Jesse Barnes80824002009-09-10 15:28:06 -07001402 /* Try to set up FBC with a reasonable compressed buffer size */
Shaohua Li9216d442009-10-10 15:20:55 +08001403 if (I915_HAS_FBC(dev) && i915_powersave) {
Jesse Barnes80824002009-09-10 15:28:06 -07001404 int cfb_size;
1405
1406 /* Try to get an 8M buffer... */
1407 if (prealloc_size > (9*1024*1024))
1408 cfb_size = 8*1024*1024;
1409 else /* fall back to 7/8 of the stolen space */
1410 cfb_size = prealloc_size * 7 / 8;
1411 i915_setup_compression(dev, cfb_size);
1412 }
1413
Jesse Barnes79e53942008-11-07 14:24:08 -08001414 /* Allow hardware batchbuffers unless told otherwise.
1415 */
1416 dev_priv->allow_batchbuffer = 1;
1417
1418 ret = intel_init_bios(dev);
1419 if (ret)
1420 DRM_INFO("failed to find VBIOS tables\n");
1421
Dave Airlie28d52042009-09-21 14:33:58 +10001422 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1423 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1424 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001425 goto cleanup_ringbuffer;
Dave Airlie28d52042009-09-21 14:33:58 +10001426
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001427 ret = vga_switcheroo_register_client(dev->pdev,
1428 i915_switcheroo_set_state,
1429 i915_switcheroo_can_switch);
1430 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001431 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001432
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001433 /* IIR "flip pending" bit means done if this bit is set */
1434 if (IS_GEN3(dev) && (I915_READ(ECOSKPD) & ECO_FLIP_DONE))
1435 dev_priv->flip_pending_is_done = true;
1436
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001437 intel_modeset_init(dev);
1438
Jesse Barnes79e53942008-11-07 14:24:08 -08001439 ret = drm_irq_install(dev);
1440 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001441 goto cleanup_vga_switcheroo;
Jesse Barnes79e53942008-11-07 14:24:08 -08001442
Jesse Barnes79e53942008-11-07 14:24:08 -08001443 /* Always safe in the mode setting case. */
1444 /* FIXME: do pre/post-mode set stuff in core KMS code */
1445 dev->vblank_disable_allowed = 1;
1446
1447 /*
1448 * Initialize the hardware status page IRQ location.
1449 */
1450
1451 I915_WRITE(INSTPM, (1 << 5) | (1 << 21));
1452
Chris Wilson5a793952010-06-06 10:50:03 +01001453 ret = intel_fbdev_init(dev);
1454 if (ret)
1455 goto cleanup_irq;
1456
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001457 drm_kms_helper_poll_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001458 return 0;
1459
Chris Wilson5a793952010-06-06 10:50:03 +01001460cleanup_irq:
1461 drm_irq_uninstall(dev);
1462cleanup_vga_switcheroo:
1463 vga_switcheroo_unregister_client(dev->pdev);
1464cleanup_vga_client:
1465 vga_client_register(dev->pdev, NULL, NULL, NULL);
1466cleanup_ringbuffer:
Eric Anholt21099532009-11-09 14:57:34 -08001467 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001468 i915_gem_cleanup_ringbuffer(dev);
Eric Anholt21099532009-11-09 14:57:34 -08001469 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001470out:
1471 return ret;
1472}
1473
Dave Airlie7c1c2872008-11-28 14:22:24 +10001474int i915_master_create(struct drm_device *dev, struct drm_master *master)
1475{
1476 struct drm_i915_master_private *master_priv;
1477
Eric Anholt9a298b22009-03-24 12:23:04 -07001478 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001479 if (!master_priv)
1480 return -ENOMEM;
1481
1482 master->driver_priv = master_priv;
1483 return 0;
1484}
1485
1486void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1487{
1488 struct drm_i915_master_private *master_priv = master->driver_priv;
1489
1490 if (!master_priv)
1491 return;
1492
Eric Anholt9a298b22009-03-24 12:23:04 -07001493 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001494
1495 master->driver_priv = NULL;
1496}
1497
Jesse Barnes7648fa92010-05-20 14:28:11 -07001498static void i915_pineview_get_mem_freq(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08001499{
1500 drm_i915_private_t *dev_priv = dev->dev_private;
1501 u32 tmp;
1502
Shaohua Li7662c8b2009-06-26 11:23:55 +08001503 tmp = I915_READ(CLKCFG);
1504
1505 switch (tmp & CLKCFG_FSB_MASK) {
1506 case CLKCFG_FSB_533:
1507 dev_priv->fsb_freq = 533; /* 133*4 */
1508 break;
1509 case CLKCFG_FSB_800:
1510 dev_priv->fsb_freq = 800; /* 200*4 */
1511 break;
1512 case CLKCFG_FSB_667:
1513 dev_priv->fsb_freq = 667; /* 167*4 */
1514 break;
1515 case CLKCFG_FSB_400:
1516 dev_priv->fsb_freq = 400; /* 100*4 */
1517 break;
1518 }
1519
1520 switch (tmp & CLKCFG_MEM_MASK) {
1521 case CLKCFG_MEM_533:
1522 dev_priv->mem_freq = 533;
1523 break;
1524 case CLKCFG_MEM_667:
1525 dev_priv->mem_freq = 667;
1526 break;
1527 case CLKCFG_MEM_800:
1528 dev_priv->mem_freq = 800;
1529 break;
1530 }
Li Peng95534262010-05-18 18:58:44 +08001531
1532 /* detect pineview DDR3 setting */
1533 tmp = I915_READ(CSHRDDR3CTL);
1534 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
Shaohua Li7662c8b2009-06-26 11:23:55 +08001535}
1536
Jesse Barnes7648fa92010-05-20 14:28:11 -07001537static void i915_ironlake_get_mem_freq(struct drm_device *dev)
1538{
1539 drm_i915_private_t *dev_priv = dev->dev_private;
1540 u16 ddrpll, csipll;
1541
1542 ddrpll = I915_READ16(DDRMPLL1);
1543 csipll = I915_READ16(CSIPLL0);
1544
1545 switch (ddrpll & 0xff) {
1546 case 0xc:
1547 dev_priv->mem_freq = 800;
1548 break;
1549 case 0x10:
1550 dev_priv->mem_freq = 1066;
1551 break;
1552 case 0x14:
1553 dev_priv->mem_freq = 1333;
1554 break;
1555 case 0x18:
1556 dev_priv->mem_freq = 1600;
1557 break;
1558 default:
1559 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
1560 ddrpll & 0xff);
1561 dev_priv->mem_freq = 0;
1562 break;
1563 }
1564
1565 dev_priv->r_t = dev_priv->mem_freq;
1566
1567 switch (csipll & 0x3ff) {
1568 case 0x00c:
1569 dev_priv->fsb_freq = 3200;
1570 break;
1571 case 0x00e:
1572 dev_priv->fsb_freq = 3733;
1573 break;
1574 case 0x010:
1575 dev_priv->fsb_freq = 4266;
1576 break;
1577 case 0x012:
1578 dev_priv->fsb_freq = 4800;
1579 break;
1580 case 0x014:
1581 dev_priv->fsb_freq = 5333;
1582 break;
1583 case 0x016:
1584 dev_priv->fsb_freq = 5866;
1585 break;
1586 case 0x018:
1587 dev_priv->fsb_freq = 6400;
1588 break;
1589 default:
1590 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
1591 csipll & 0x3ff);
1592 dev_priv->fsb_freq = 0;
1593 break;
1594 }
1595
1596 if (dev_priv->fsb_freq == 3200) {
1597 dev_priv->c_m = 0;
1598 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
1599 dev_priv->c_m = 1;
1600 } else {
1601 dev_priv->c_m = 2;
1602 }
1603}
1604
1605struct v_table {
1606 u8 vid;
1607 unsigned long vd; /* in .1 mil */
1608 unsigned long vm; /* in .1 mil */
1609 u8 pvid;
1610};
1611
1612static struct v_table v_table[] = {
1613 { 0, 16125, 15000, 0x7f, },
1614 { 1, 16000, 14875, 0x7e, },
1615 { 2, 15875, 14750, 0x7d, },
1616 { 3, 15750, 14625, 0x7c, },
1617 { 4, 15625, 14500, 0x7b, },
1618 { 5, 15500, 14375, 0x7a, },
1619 { 6, 15375, 14250, 0x79, },
1620 { 7, 15250, 14125, 0x78, },
1621 { 8, 15125, 14000, 0x77, },
1622 { 9, 15000, 13875, 0x76, },
1623 { 10, 14875, 13750, 0x75, },
1624 { 11, 14750, 13625, 0x74, },
1625 { 12, 14625, 13500, 0x73, },
1626 { 13, 14500, 13375, 0x72, },
1627 { 14, 14375, 13250, 0x71, },
1628 { 15, 14250, 13125, 0x70, },
1629 { 16, 14125, 13000, 0x6f, },
1630 { 17, 14000, 12875, 0x6e, },
1631 { 18, 13875, 12750, 0x6d, },
1632 { 19, 13750, 12625, 0x6c, },
1633 { 20, 13625, 12500, 0x6b, },
1634 { 21, 13500, 12375, 0x6a, },
1635 { 22, 13375, 12250, 0x69, },
1636 { 23, 13250, 12125, 0x68, },
1637 { 24, 13125, 12000, 0x67, },
1638 { 25, 13000, 11875, 0x66, },
1639 { 26, 12875, 11750, 0x65, },
1640 { 27, 12750, 11625, 0x64, },
1641 { 28, 12625, 11500, 0x63, },
1642 { 29, 12500, 11375, 0x62, },
1643 { 30, 12375, 11250, 0x61, },
1644 { 31, 12250, 11125, 0x60, },
1645 { 32, 12125, 11000, 0x5f, },
1646 { 33, 12000, 10875, 0x5e, },
1647 { 34, 11875, 10750, 0x5d, },
1648 { 35, 11750, 10625, 0x5c, },
1649 { 36, 11625, 10500, 0x5b, },
1650 { 37, 11500, 10375, 0x5a, },
1651 { 38, 11375, 10250, 0x59, },
1652 { 39, 11250, 10125, 0x58, },
1653 { 40, 11125, 10000, 0x57, },
1654 { 41, 11000, 9875, 0x56, },
1655 { 42, 10875, 9750, 0x55, },
1656 { 43, 10750, 9625, 0x54, },
1657 { 44, 10625, 9500, 0x53, },
1658 { 45, 10500, 9375, 0x52, },
1659 { 46, 10375, 9250, 0x51, },
1660 { 47, 10250, 9125, 0x50, },
1661 { 48, 10125, 9000, 0x4f, },
1662 { 49, 10000, 8875, 0x4e, },
1663 { 50, 9875, 8750, 0x4d, },
1664 { 51, 9750, 8625, 0x4c, },
1665 { 52, 9625, 8500, 0x4b, },
1666 { 53, 9500, 8375, 0x4a, },
1667 { 54, 9375, 8250, 0x49, },
1668 { 55, 9250, 8125, 0x48, },
1669 { 56, 9125, 8000, 0x47, },
1670 { 57, 9000, 7875, 0x46, },
1671 { 58, 8875, 7750, 0x45, },
1672 { 59, 8750, 7625, 0x44, },
1673 { 60, 8625, 7500, 0x43, },
1674 { 61, 8500, 7375, 0x42, },
1675 { 62, 8375, 7250, 0x41, },
1676 { 63, 8250, 7125, 0x40, },
1677 { 64, 8125, 7000, 0x3f, },
1678 { 65, 8000, 6875, 0x3e, },
1679 { 66, 7875, 6750, 0x3d, },
1680 { 67, 7750, 6625, 0x3c, },
1681 { 68, 7625, 6500, 0x3b, },
1682 { 69, 7500, 6375, 0x3a, },
1683 { 70, 7375, 6250, 0x39, },
1684 { 71, 7250, 6125, 0x38, },
1685 { 72, 7125, 6000, 0x37, },
1686 { 73, 7000, 5875, 0x36, },
1687 { 74, 6875, 5750, 0x35, },
1688 { 75, 6750, 5625, 0x34, },
1689 { 76, 6625, 5500, 0x33, },
1690 { 77, 6500, 5375, 0x32, },
1691 { 78, 6375, 5250, 0x31, },
1692 { 79, 6250, 5125, 0x30, },
1693 { 80, 6125, 5000, 0x2f, },
1694 { 81, 6000, 4875, 0x2e, },
1695 { 82, 5875, 4750, 0x2d, },
1696 { 83, 5750, 4625, 0x2c, },
1697 { 84, 5625, 4500, 0x2b, },
1698 { 85, 5500, 4375, 0x2a, },
1699 { 86, 5375, 4250, 0x29, },
1700 { 87, 5250, 4125, 0x28, },
1701 { 88, 5125, 4000, 0x27, },
1702 { 89, 5000, 3875, 0x26, },
1703 { 90, 4875, 3750, 0x25, },
1704 { 91, 4750, 3625, 0x24, },
1705 { 92, 4625, 3500, 0x23, },
1706 { 93, 4500, 3375, 0x22, },
1707 { 94, 4375, 3250, 0x21, },
1708 { 95, 4250, 3125, 0x20, },
1709 { 96, 4125, 3000, 0x1f, },
1710 { 97, 4125, 3000, 0x1e, },
1711 { 98, 4125, 3000, 0x1d, },
1712 { 99, 4125, 3000, 0x1c, },
1713 { 100, 4125, 3000, 0x1b, },
1714 { 101, 4125, 3000, 0x1a, },
1715 { 102, 4125, 3000, 0x19, },
1716 { 103, 4125, 3000, 0x18, },
1717 { 104, 4125, 3000, 0x17, },
1718 { 105, 4125, 3000, 0x16, },
1719 { 106, 4125, 3000, 0x15, },
1720 { 107, 4125, 3000, 0x14, },
1721 { 108, 4125, 3000, 0x13, },
1722 { 109, 4125, 3000, 0x12, },
1723 { 110, 4125, 3000, 0x11, },
1724 { 111, 4125, 3000, 0x10, },
1725 { 112, 4125, 3000, 0x0f, },
1726 { 113, 4125, 3000, 0x0e, },
1727 { 114, 4125, 3000, 0x0d, },
1728 { 115, 4125, 3000, 0x0c, },
1729 { 116, 4125, 3000, 0x0b, },
1730 { 117, 4125, 3000, 0x0a, },
1731 { 118, 4125, 3000, 0x09, },
1732 { 119, 4125, 3000, 0x08, },
1733 { 120, 1125, 0, 0x07, },
1734 { 121, 1000, 0, 0x06, },
1735 { 122, 875, 0, 0x05, },
1736 { 123, 750, 0, 0x04, },
1737 { 124, 625, 0, 0x03, },
1738 { 125, 500, 0, 0x02, },
1739 { 126, 375, 0, 0x01, },
1740 { 127, 0, 0, 0x00, },
1741};
1742
1743struct cparams {
1744 int i;
1745 int t;
1746 int m;
1747 int c;
1748};
1749
1750static struct cparams cparams[] = {
1751 { 1, 1333, 301, 28664 },
1752 { 1, 1066, 294, 24460 },
1753 { 1, 800, 294, 25192 },
1754 { 0, 1333, 276, 27605 },
1755 { 0, 1066, 276, 27605 },
1756 { 0, 800, 231, 23784 },
1757};
1758
1759unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
1760{
1761 u64 total_count, diff, ret;
1762 u32 count1, count2, count3, m = 0, c = 0;
1763 unsigned long now = jiffies_to_msecs(jiffies), diff1;
1764 int i;
1765
1766 diff1 = now - dev_priv->last_time1;
1767
1768 count1 = I915_READ(DMIEC);
1769 count2 = I915_READ(DDREC);
1770 count3 = I915_READ(CSIEC);
1771
1772 total_count = count1 + count2 + count3;
1773
1774 /* FIXME: handle per-counter overflow */
1775 if (total_count < dev_priv->last_count1) {
1776 diff = ~0UL - dev_priv->last_count1;
1777 diff += total_count;
1778 } else {
1779 diff = total_count - dev_priv->last_count1;
1780 }
1781
1782 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
1783 if (cparams[i].i == dev_priv->c_m &&
1784 cparams[i].t == dev_priv->r_t) {
1785 m = cparams[i].m;
1786 c = cparams[i].c;
1787 break;
1788 }
1789 }
1790
1791 div_u64(diff, diff1);
1792 ret = ((m * diff) + c);
1793 div_u64(ret, 10);
1794
1795 dev_priv->last_count1 = total_count;
1796 dev_priv->last_time1 = now;
1797
1798 return ret;
1799}
1800
1801unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
1802{
1803 unsigned long m, x, b;
1804 u32 tsfs;
1805
1806 tsfs = I915_READ(TSFS);
1807
1808 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
1809 x = I915_READ8(TR1);
1810
1811 b = tsfs & TSFS_INTR_MASK;
1812
1813 return ((m * x) / 127) - b;
1814}
1815
1816static unsigned long pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
1817{
1818 unsigned long val = 0;
1819 int i;
1820
1821 for (i = 0; i < ARRAY_SIZE(v_table); i++) {
1822 if (v_table[i].pvid == pxvid) {
1823 if (IS_MOBILE(dev_priv->dev))
1824 val = v_table[i].vm;
1825 else
1826 val = v_table[i].vd;
1827 }
1828 }
1829
1830 return val;
1831}
1832
1833void i915_update_gfx_val(struct drm_i915_private *dev_priv)
1834{
1835 struct timespec now, diff1;
1836 u64 diff;
1837 unsigned long diffms;
1838 u32 count;
1839
1840 getrawmonotonic(&now);
1841 diff1 = timespec_sub(now, dev_priv->last_time2);
1842
1843 /* Don't divide by 0 */
1844 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
1845 if (!diffms)
1846 return;
1847
1848 count = I915_READ(GFXEC);
1849
1850 if (count < dev_priv->last_count2) {
1851 diff = ~0UL - dev_priv->last_count2;
1852 diff += count;
1853 } else {
1854 diff = count - dev_priv->last_count2;
1855 }
1856
1857 dev_priv->last_count2 = count;
1858 dev_priv->last_time2 = now;
1859
1860 /* More magic constants... */
1861 diff = diff * 1181;
1862 div_u64(diff, diffms * 10);
1863 dev_priv->gfx_power = diff;
1864}
1865
1866unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
1867{
1868 unsigned long t, corr, state1, corr2, state2;
1869 u32 pxvid, ext_v;
1870
1871 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->cur_delay * 4));
1872 pxvid = (pxvid >> 24) & 0x7f;
1873 ext_v = pvid_to_extvid(dev_priv, pxvid);
1874
1875 state1 = ext_v;
1876
1877 t = i915_mch_val(dev_priv);
1878
1879 /* Revel in the empirically derived constants */
1880
1881 /* Correction factor in 1/100000 units */
1882 if (t > 80)
1883 corr = ((t * 2349) + 135940);
1884 else if (t >= 50)
1885 corr = ((t * 964) + 29317);
1886 else /* < 50 */
1887 corr = ((t * 301) + 1004);
1888
1889 corr = corr * ((150142 * state1) / 10000 - 78642);
1890 corr /= 100000;
1891 corr2 = (corr * dev_priv->corr);
1892
1893 state2 = (corr2 * state1) / 10000;
1894 state2 /= 100; /* convert to mW */
1895
1896 i915_update_gfx_val(dev_priv);
1897
1898 return dev_priv->gfx_power + state2;
1899}
1900
1901/* Global for IPS driver to get at the current i915 device */
1902static struct drm_i915_private *i915_mch_dev;
1903/*
1904 * Lock protecting IPS related data structures
1905 * - i915_mch_dev
1906 * - dev_priv->max_delay
1907 * - dev_priv->min_delay
1908 * - dev_priv->fmax
1909 * - dev_priv->gpu_busy
1910 */
1911DEFINE_SPINLOCK(mchdev_lock);
1912
1913/**
1914 * i915_read_mch_val - return value for IPS use
1915 *
1916 * Calculate and return a value for the IPS driver to use when deciding whether
1917 * we have thermal and power headroom to increase CPU or GPU power budget.
1918 */
1919unsigned long i915_read_mch_val(void)
1920{
1921 struct drm_i915_private *dev_priv;
1922 unsigned long chipset_val, graphics_val, ret = 0;
1923
1924 spin_lock(&mchdev_lock);
1925 if (!i915_mch_dev)
1926 goto out_unlock;
1927 dev_priv = i915_mch_dev;
1928
1929 chipset_val = i915_chipset_val(dev_priv);
1930 graphics_val = i915_gfx_val(dev_priv);
1931
1932 ret = chipset_val + graphics_val;
1933
1934out_unlock:
1935 spin_unlock(&mchdev_lock);
1936
1937 return ret;
1938}
1939EXPORT_SYMBOL_GPL(i915_read_mch_val);
1940
1941/**
1942 * i915_gpu_raise - raise GPU frequency limit
1943 *
1944 * Raise the limit; IPS indicates we have thermal headroom.
1945 */
1946bool i915_gpu_raise(void)
1947{
1948 struct drm_i915_private *dev_priv;
1949 bool ret = true;
1950
1951 spin_lock(&mchdev_lock);
1952 if (!i915_mch_dev) {
1953 ret = false;
1954 goto out_unlock;
1955 }
1956 dev_priv = i915_mch_dev;
1957
1958 if (dev_priv->max_delay > dev_priv->fmax)
1959 dev_priv->max_delay--;
1960
1961out_unlock:
1962 spin_unlock(&mchdev_lock);
1963
1964 return ret;
1965}
1966EXPORT_SYMBOL_GPL(i915_gpu_raise);
1967
1968/**
1969 * i915_gpu_lower - lower GPU frequency limit
1970 *
1971 * IPS indicates we're close to a thermal limit, so throttle back the GPU
1972 * frequency maximum.
1973 */
1974bool i915_gpu_lower(void)
1975{
1976 struct drm_i915_private *dev_priv;
1977 bool ret = true;
1978
1979 spin_lock(&mchdev_lock);
1980 if (!i915_mch_dev) {
1981 ret = false;
1982 goto out_unlock;
1983 }
1984 dev_priv = i915_mch_dev;
1985
1986 if (dev_priv->max_delay < dev_priv->min_delay)
1987 dev_priv->max_delay++;
1988
1989out_unlock:
1990 spin_unlock(&mchdev_lock);
1991
1992 return ret;
1993}
1994EXPORT_SYMBOL_GPL(i915_gpu_lower);
1995
1996/**
1997 * i915_gpu_busy - indicate GPU business to IPS
1998 *
1999 * Tell the IPS driver whether or not the GPU is busy.
2000 */
2001bool i915_gpu_busy(void)
2002{
2003 struct drm_i915_private *dev_priv;
2004 bool ret = false;
2005
2006 spin_lock(&mchdev_lock);
2007 if (!i915_mch_dev)
2008 goto out_unlock;
2009 dev_priv = i915_mch_dev;
2010
2011 ret = dev_priv->busy;
2012
2013out_unlock:
2014 spin_unlock(&mchdev_lock);
2015
2016 return ret;
2017}
2018EXPORT_SYMBOL_GPL(i915_gpu_busy);
2019
2020/**
2021 * i915_gpu_turbo_disable - disable graphics turbo
2022 *
2023 * Disable graphics turbo by resetting the max frequency and setting the
2024 * current frequency to the default.
2025 */
2026bool i915_gpu_turbo_disable(void)
2027{
2028 struct drm_i915_private *dev_priv;
2029 bool ret = true;
2030
2031 spin_lock(&mchdev_lock);
2032 if (!i915_mch_dev) {
2033 ret = false;
2034 goto out_unlock;
2035 }
2036 dev_priv = i915_mch_dev;
2037
2038 dev_priv->max_delay = dev_priv->fstart;
2039
2040 if (!ironlake_set_drps(dev_priv->dev, dev_priv->fstart))
2041 ret = false;
2042
2043out_unlock:
2044 spin_unlock(&mchdev_lock);
2045
2046 return ret;
2047}
2048EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
2049
Jesse Barnes79e53942008-11-07 14:24:08 -08002050/**
2051 * i915_driver_load - setup chip and create an initial config
2052 * @dev: DRM device
2053 * @flags: startup flags
2054 *
2055 * The driver load routine has to do several things:
2056 * - drive output discovery via intel_modeset_init()
2057 * - initialize the memory manager
2058 * - allocate initial config memory
2059 * - setup the DRM framebuffer with the allocated memory
2060 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002061int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11002062{
Luca Tettamantiea059a12010-04-08 21:41:59 +02002063 struct drm_i915_private *dev_priv;
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +11002064 resource_size_t base, size;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05002065 int ret = 0, mmio_bar;
Jesse Barnes80824002009-09-10 15:28:06 -07002066 uint32_t agp_size, prealloc_size, prealloc_start;
Dave Airlie22eae942005-11-10 22:16:34 +11002067 /* i915 has 4 more counters */
2068 dev->counters += 4;
2069 dev->types[6] = _DRM_STAT_IRQ;
2070 dev->types[7] = _DRM_STAT_PRIMARY;
2071 dev->types[8] = _DRM_STAT_SECONDARY;
2072 dev->types[9] = _DRM_STAT_DMA;
2073
Eric Anholt9a298b22009-03-24 12:23:04 -07002074 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002075 if (dev_priv == NULL)
2076 return -ENOMEM;
2077
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002078 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002079 dev_priv->dev = dev;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05002080 dev_priv->info = (struct intel_device_info *) flags;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002081
2082 /* Add register map (needed for suspend/resume) */
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05002083 mmio_bar = IS_I9XX(dev) ? 0 : 1;
Jordan Crouse01d73a62010-05-27 13:40:24 -06002084 base = pci_resource_start(dev->pdev, mmio_bar);
2085 size = pci_resource_len(dev->pdev, mmio_bar);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002086
Dave Airlieec2a4c32009-08-04 11:43:41 +10002087 if (i915_get_bridge_dev(dev)) {
2088 ret = -EIO;
2089 goto free_priv;
2090 }
2091
Daniel Vetter9f82d232010-08-30 21:25:23 +02002092 /* overlay on gen2 is broken and can't address above 1G */
2093 if (IS_GEN2(dev))
2094 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
2095
Eric Anholt3043c602008-10-02 12:24:47 -07002096 dev_priv->regs = ioremap(base, size);
Jesse Barnes79e53942008-11-07 14:24:08 -08002097 if (!dev_priv->regs) {
2098 DRM_ERROR("failed to map registers\n");
2099 ret = -EIO;
Dave Airlieec2a4c32009-08-04 11:43:41 +10002100 goto put_bridge;
Jesse Barnes79e53942008-11-07 14:24:08 -08002101 }
Eric Anholted4cb412008-07-29 12:10:39 -07002102
Eric Anholtab657db12009-01-23 12:57:47 -08002103 dev_priv->mm.gtt_mapping =
2104 io_mapping_create_wc(dev->agp->base,
2105 dev->agp->agp_info.aper_size * 1024*1024);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08002106 if (dev_priv->mm.gtt_mapping == NULL) {
2107 ret = -EIO;
2108 goto out_rmmap;
2109 }
2110
Eric Anholtab657db12009-01-23 12:57:47 -08002111 /* Set up a WC MTRR for non-PAT systems. This is more common than
2112 * one would think, because the kernel disables PAT on first
2113 * generation Core chips because WC PAT gets overridden by a UC
2114 * MTRR if present. Even if a UC MTRR isn't present.
2115 */
2116 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
2117 dev->agp->agp_info.aper_size *
2118 1024 * 1024,
2119 MTRR_TYPE_WRCOMB, 1);
2120 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07002121 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08002122 "performance may suffer.\n");
2123 }
2124
Jesse Barnes80824002009-09-10 15:28:06 -07002125 ret = i915_probe_agp(dev, &agp_size, &prealloc_size, &prealloc_start);
Eric Anholt2a34f5e62009-07-02 09:30:50 -07002126 if (ret)
2127 goto out_iomapfree;
2128
Jesse Barnesd1d6ca72010-07-08 09:22:46 -07002129 if (prealloc_size > intel_max_stolen) {
2130 DRM_INFO("detected %dM stolen memory, trimming to %dM\n",
2131 prealloc_size >> 20, intel_max_stolen >> 20);
2132 prealloc_size = intel_max_stolen;
2133 }
2134
Chris Wilsonaed5f1d2009-10-14 13:40:04 +01002135 dev_priv->wq = create_singlethread_workqueue("i915");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002136 if (dev_priv->wq == NULL) {
2137 DRM_ERROR("Failed to create our workqueue.\n");
2138 ret = -ENOMEM;
2139 goto out_iomapfree;
2140 }
2141
Dave Airlieac5c4e72008-12-19 15:38:34 +10002142 /* enable GEM by default */
2143 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10002144
Eric Anholt2a34f5e62009-07-02 09:30:50 -07002145 if (prealloc_size > agp_size * 3 / 4) {
2146 DRM_ERROR("Detected broken video BIOS with %d/%dkB of video "
2147 "memory stolen.\n",
2148 prealloc_size / 1024, agp_size / 1024);
2149 DRM_ERROR("Disabling GEM. (try reducing stolen memory or "
2150 "updating the BIOS to fix).\n");
2151 dev_priv->has_gem = 0;
2152 }
2153
Chris Wilson79a78dd2010-05-17 09:23:54 +01002154 if (dev_priv->has_gem == 0 &&
2155 drm_core_check_feature(dev, DRIVER_MODESET)) {
2156 DRM_ERROR("kernel modesetting requires GEM, disabling driver.\n");
2157 ret = -ENODEV;
2158 goto out_iomapfree;
2159 }
2160
Jesse Barnes9880b7a2009-02-06 10:22:41 -08002161 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07002162 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Eric Anholtbad720f2009-10-22 16:11:14 -07002163 if (IS_G4X(dev) || IS_IRONLAKE(dev) || IS_GEN6(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07002164 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08002165 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07002166 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08002167
Zhenyu Wangc4804412009-12-17 14:48:43 +08002168 /* Try to make sure MCHBAR is enabled before poking at it */
2169 intel_setup_mchbar(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01002170 intel_opregion_setup(dev);
Zhenyu Wangc4804412009-12-17 14:48:43 +08002171
Eric Anholt673a3942008-07-30 12:06:12 -07002172 i915_gem_load(dev);
2173
Keith Packard398c9cb2008-07-30 13:03:43 -07002174 /* Init HWS */
2175 if (!I915_NEED_GFX_HWS(dev)) {
2176 ret = i915_init_phys_hws(dev);
2177 if (ret != 0)
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002178 goto out_workqueue_free;
Keith Packard398c9cb2008-07-30 13:03:43 -07002179 }
Eric Anholted4cb412008-07-29 12:10:39 -07002180
Jesse Barnes7648fa92010-05-20 14:28:11 -07002181 if (IS_PINEVIEW(dev))
2182 i915_pineview_get_mem_freq(dev);
2183 else if (IS_IRONLAKE(dev))
2184 i915_ironlake_get_mem_freq(dev);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002185
Eric Anholted4cb412008-07-29 12:10:39 -07002186 /* On the 945G/GM, the chipset reports the MSI capability on the
2187 * integrated graphics even though the support isn't actually there
2188 * according to the published specs. It doesn't appear to function
2189 * correctly in testing on 945G.
2190 * This may be a side effect of MSI having been made available for PEG
2191 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07002192 *
2193 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08002194 * be lost or delayed, but we use them anyways to avoid
2195 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07002196 */
Keith Packardb60678a2008-12-08 11:12:28 -08002197 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08002198 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07002199
2200 spin_lock_init(&dev_priv->user_irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07002201 spin_lock_init(&dev_priv->error_lock);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002202 dev_priv->trace_irq_seqno = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07002203
Keith Packard52440212008-11-18 09:30:25 -08002204 ret = drm_vblank_init(dev, I915_NUM_PIPE);
2205
2206 if (ret) {
2207 (void) i915_driver_unload(dev);
2208 return ret;
2209 }
2210
Ben Gamari11ed50e2009-09-14 17:48:45 -04002211 /* Start out suspended */
2212 dev_priv->mm.suspended = 1;
2213
Zhenyu Wang3bad0782010-04-07 16:15:53 +08002214 intel_detect_pch(dev);
2215
Jesse Barnes79e53942008-11-07 14:24:08 -08002216 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes80824002009-09-10 15:28:06 -07002217 ret = i915_load_modeset_init(dev, prealloc_start,
2218 prealloc_size, agp_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08002219 if (ret < 0) {
2220 DRM_ERROR("failed to init modeset\n");
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002221 goto out_workqueue_free;
Jesse Barnes79e53942008-11-07 14:24:08 -08002222 }
2223 }
2224
Matthew Garrett74a365b2009-03-19 21:35:39 +00002225 /* Must be done after probing outputs */
Chris Wilson44834a62010-08-19 16:09:23 +01002226 intel_opregion_init(dev);
2227 acpi_video_register();
Matthew Garrett74a365b2009-03-19 21:35:39 +00002228
Ben Gamarif65d9422009-09-14 17:48:44 -04002229 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
2230 (unsigned long) dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07002231
2232 spin_lock(&mchdev_lock);
2233 i915_mch_dev = dev_priv;
2234 dev_priv->mchdev_lock = &mchdev_lock;
2235 spin_unlock(&mchdev_lock);
2236
Jesse Barnes79e53942008-11-07 14:24:08 -08002237 return 0;
2238
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002239out_workqueue_free:
2240 destroy_workqueue(dev_priv->wq);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08002241out_iomapfree:
2242 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08002243out_rmmap:
2244 iounmap(dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10002245put_bridge:
2246 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002247free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07002248 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002249 return ret;
2250}
2251
2252int i915_driver_unload(struct drm_device *dev)
2253{
2254 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02002255 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002256
Jesse Barnes7648fa92010-05-20 14:28:11 -07002257 spin_lock(&mchdev_lock);
2258 i915_mch_dev = NULL;
2259 spin_unlock(&mchdev_lock);
2260
Daniel Vetterc911fc12010-08-20 21:23:20 +02002261 mutex_lock(&dev->struct_mutex);
2262 ret = i915_gpu_idle(dev);
2263 if (ret)
2264 DRM_ERROR("failed to idle hardware: %d\n", ret);
2265 mutex_unlock(&dev->struct_mutex);
2266
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002267 /* Cancel the retire work handler, which should be idle now. */
2268 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2269
Eric Anholtab657db12009-01-23 12:57:47 -08002270 io_mapping_free(dev_priv->mm.gtt_mapping);
2271 if (dev_priv->mm.gtt_mtrr >= 0) {
2272 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
2273 dev->agp->agp_info.aper_size * 1024 * 1024);
2274 dev_priv->mm.gtt_mtrr = -1;
2275 }
2276
Chris Wilson44834a62010-08-19 16:09:23 +01002277 acpi_video_unregister();
2278
Jesse Barnes79e53942008-11-07 14:24:08 -08002279 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes3d8620c2010-03-26 11:07:21 -07002280 intel_modeset_cleanup(dev);
2281
Zhao Yakui6363ee62009-11-24 09:48:44 +08002282 /*
2283 * free the memory space allocated for the child device
2284 * config parsed from VBT
2285 */
2286 if (dev_priv->child_dev && dev_priv->child_dev_num) {
2287 kfree(dev_priv->child_dev);
2288 dev_priv->child_dev = NULL;
2289 dev_priv->child_dev_num = 0;
2290 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02002291
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10002292 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10002293 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08002294 }
2295
Daniel Vettera8b48992010-08-20 21:25:11 +02002296 /* Free error state after interrupts are fully disabled. */
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002297 del_timer_sync(&dev_priv->hangcheck_timer);
2298 cancel_work_sync(&dev_priv->error_work);
Daniel Vettera8b48992010-08-20 21:25:11 +02002299 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002300
Eric Anholted4cb412008-07-29 12:10:39 -07002301 if (dev->pdev->msi_enabled)
2302 pci_disable_msi(dev->pdev);
2303
Eric Anholt3043c602008-10-02 12:24:47 -07002304 if (dev_priv->regs != NULL)
2305 iounmap(dev_priv->regs);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002306
Chris Wilson44834a62010-08-19 16:09:23 +01002307 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01002308
Jesse Barnes79e53942008-11-07 14:24:08 -08002309 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02002310 /* Flush any outstanding unpin_work. */
2311 flush_workqueue(dev_priv->wq);
2312
Dave Airlie71acb5e2008-12-30 20:31:46 +10002313 i915_gem_free_all_phys_object(dev);
2314
Jesse Barnes79e53942008-11-07 14:24:08 -08002315 mutex_lock(&dev->struct_mutex);
2316 i915_gem_cleanup_ringbuffer(dev);
2317 mutex_unlock(&dev->struct_mutex);
Jesse Barnes20bf3772010-04-21 11:39:22 -07002318 if (I915_HAS_FBC(dev) && i915_powersave)
2319 i915_cleanup_compression(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002320 drm_mm_takedown(&dev_priv->vram);
Daniel Vetter02e792f2009-09-15 22:57:34 +02002321
2322 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002323 }
2324
Zhenyu Wangc4804412009-12-17 14:48:43 +08002325 intel_teardown_mchbar(dev);
2326
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002327 destroy_workqueue(dev_priv->wq);
2328
Dave Airlieec2a4c32009-08-04 11:43:41 +10002329 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07002330 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002331
Dave Airlie22eae942005-11-10 22:16:34 +11002332 return 0;
2333}
2334
Eric Anholt673a3942008-07-30 12:06:12 -07002335int i915_driver_open(struct drm_device *dev, struct drm_file *file_priv)
2336{
2337 struct drm_i915_file_private *i915_file_priv;
2338
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002339 DRM_DEBUG_DRIVER("\n");
Eric Anholt673a3942008-07-30 12:06:12 -07002340 i915_file_priv = (struct drm_i915_file_private *)
Eric Anholt9a298b22009-03-24 12:23:04 -07002341 kmalloc(sizeof(*i915_file_priv), GFP_KERNEL);
Eric Anholt673a3942008-07-30 12:06:12 -07002342
2343 if (!i915_file_priv)
2344 return -ENOMEM;
2345
2346 file_priv->driver_priv = i915_file_priv;
2347
Eric Anholtb9624422009-06-03 07:27:35 +00002348 INIT_LIST_HEAD(&i915_file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002349
2350 return 0;
2351}
2352
Jesse Barnes79e53942008-11-07 14:24:08 -08002353/**
2354 * i915_driver_lastclose - clean up after all DRM clients have exited
2355 * @dev: DRM device
2356 *
2357 * Take care of cleaning up after all DRM clients have exited. In the
2358 * mode setting case, we want to restore the kernel's initial mode (just
2359 * in case the last client left us in a bad state).
2360 *
2361 * Additionally, in the non-mode setting case, we'll tear down the AGP
2362 * and DMA structures, since the kernel won't be using them, and clea
2363 * up any GEM state.
2364 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002365void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002367 drm_i915_private_t *dev_priv = dev->dev_private;
2368
Jesse Barnes79e53942008-11-07 14:24:08 -08002369 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10002370 drm_fb_helper_restore();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10002371 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10002372 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08002373 }
Dave Airlie144a75f2008-03-30 07:53:58 +10002374
Eric Anholt673a3942008-07-30 12:06:12 -07002375 i915_gem_lastclose(dev);
2376
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002377 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002378 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002379
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002380 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
Eric Anholt6c340ea2007-08-25 20:23:09 +10002383void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002385 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00002386 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08002387 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2388 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
Eric Anholt673a3942008-07-30 12:06:12 -07002391void i915_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
2392{
2393 struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
2394
Eric Anholt9a298b22009-03-24 12:23:04 -07002395 kfree(i915_file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07002396}
2397
Eric Anholtc153f452007-09-03 12:06:45 +10002398struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10002399 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2400 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
2401 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
2402 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
2403 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
2404 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
2405 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH),
2406 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2407 DRM_IOCTL_DEF_DRV(I915_ALLOC, i915_mem_alloc, DRM_AUTH),
2408 DRM_IOCTL_DEF_DRV(I915_FREE, i915_mem_free, DRM_AUTH),
2409 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2410 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
2411 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2412 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2413 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
2414 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
2415 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2416 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2417 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
2418 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
2419 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2420 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2421 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
2422 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
2423 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2424 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2425 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
2426 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
2427 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
2428 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
2429 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
2430 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
2431 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
2432 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
2433 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
2434 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
2435 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
2436 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
2437 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2438 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10002439};
2440
2441int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10002442
2443/**
2444 * Determine if the device really is AGP or not.
2445 *
2446 * All Intel graphics chipsets are treated as AGP, even if they are really
2447 * PCI-e.
2448 *
2449 * \param dev The device to be tested.
2450 *
2451 * \returns
2452 * A value of 1 is always retured to indictate every i9x5 is AGP.
2453 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002454int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10002455{
2456 return 1;
2457}