blob: 76f2df7b712d9cab835688d0f19b710afb448915 [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"
Eric Anholt63ee41d2010-12-20 18:40:06 -080037#include "../../../platform/x86/intel_ips.h"
Jordan Crousedcdb1672010-05-27 13:40:25 -060038#include <linux/pci.h>
Dave Airlie28d52042009-09-21 14:33:58 +100039#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080040#include <linux/acpi.h>
41#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100042#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Chris Wilson44834a62010-08-19 16:09:23 +010044#include <acpi/video.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Keith Packard398c9cb2008-07-30 13:03:43 -070046/**
47 * Sets up the hardware status page for devices that need a physical address
48 * in the register.
49 */
Eric Anholt3043c602008-10-02 12:24:47 -070050static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070051{
52 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +000053 struct intel_ring_buffer *ring = LP_RING(dev_priv);
54
Keith Packard398c9cb2008-07-30 13:03:43 -070055 /* Program Hardware Status Page */
56 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +080057 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -070058
59 if (!dev_priv->status_page_dmah) {
60 DRM_ERROR("Can not allocate hardware status page\n");
61 return -ENOMEM;
62 }
Chris Wilson311bd682011-01-13 19:06:50 +000063 ring->status_page.page_addr =
64 (void __force __iomem *)dev_priv->status_page_dmah->vaddr;
Keith Packard398c9cb2008-07-30 13:03:43 -070065 dev_priv->dma_status_page = dev_priv->status_page_dmah->busaddr;
66
Chris Wilson311bd682011-01-13 19:06:50 +000067 memset_io(ring->status_page.page_addr, 0, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -070068
Chris Wilsona6c45cf2010-09-17 00:32:17 +010069 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wang9b974cc2010-01-05 11:25:06 +080070 dev_priv->dma_status_page |= (dev_priv->dma_status_page >> 28) &
71 0xf0;
72
Keith Packard398c9cb2008-07-30 13:03:43 -070073 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +080074 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -070075 return 0;
76}
77
78/**
79 * Frees the hardware status page, whether it's a physical address or a virtual
80 * address set up by the X Server.
81 */
Eric Anholt3043c602008-10-02 12:24:47 -070082static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070083{
84 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +000085 struct intel_ring_buffer *ring = LP_RING(dev_priv);
86
Keith Packard398c9cb2008-07-30 13:03:43 -070087 if (dev_priv->status_page_dmah) {
88 drm_pci_free(dev, dev_priv->status_page_dmah);
89 dev_priv->status_page_dmah = NULL;
90 }
91
Chris Wilson1ec14ad2010-12-04 11:30:53 +000092 if (ring->status_page.gfx_addr) {
93 ring->status_page.gfx_addr = 0;
Keith Packard398c9cb2008-07-30 13:03:43 -070094 drm_core_ioremapfree(&dev_priv->hws_map, dev);
95 }
96
97 /* Need to rewrite hardware status page */
98 I915_WRITE(HWS_PGA, 0x1ffff000);
99}
100
Dave Airlie84b1fd12007-07-11 15:53:27 +1000101void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000104 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000105 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jesse Barnes79e53942008-11-07 14:24:08 -0800107 /*
108 * We should never lose context on the ring with modesetting
109 * as we don't expose it to userspace
110 */
111 if (drm_core_check_feature(dev, DRIVER_MODESET))
112 return;
113
Chris Wilson8168bd42010-11-11 17:54:52 +0000114 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
115 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ring->space = ring->head - (ring->tail + 8);
117 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800118 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Dave Airlie7c1c2872008-11-28 14:22:24 +1000120 if (!dev->primary->master)
121 return;
122
123 master_priv = dev->primary->master->driver_priv;
124 if (ring->head == ring->tail && master_priv->sarea_priv)
125 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Dave Airlie84b1fd12007-07-11 15:53:27 +1000128static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000130 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000131 int i;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* Make sure interrupts are disabled here because the uninstall ioctl
134 * may not have been called from userspace and after dev_private
135 * is freed, it's too late.
136 */
Eric Anholted4cb412008-07-29 12:10:39 -0700137 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200140 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000141 for (i = 0; i < I915_NUM_RINGS; i++)
142 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200143 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Keith Packard398c9cb2008-07-30 13:03:43 -0700145 /* Clear the HWS virtual address at teardown */
146 if (I915_NEED_GFX_HWS(dev))
147 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 return 0;
150}
151
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000152static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000154 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000155 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000156 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dave Airlie3a03ac12009-01-11 09:03:49 +1000158 master_priv->sarea = drm_getsarea(dev);
159 if (master_priv->sarea) {
160 master_priv->sarea_priv = (drm_i915_sarea_t *)
161 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
162 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800163 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000164 }
165
Eric Anholt673a3942008-07-30 12:06:12 -0700166 if (init->ring_size != 0) {
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000167 if (ring->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700168 i915_dma_cleanup(dev);
169 DRM_ERROR("Client tried to initialize ringbuffer in "
170 "GEM mode\n");
171 return -EINVAL;
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000174 ring->size = init->ring_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000176 ring->map.offset = init->ring_start;
177 ring->map.size = init->ring_size;
178 ring->map.type = 0;
179 ring->map.flags = 0;
180 ring->map.mtrr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000182 drm_core_ioremap_wc(&ring->map, dev);
Eric Anholt673a3942008-07-30 12:06:12 -0700183
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000184 if (ring->map.handle == NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700185 i915_dma_cleanup(dev);
186 DRM_ERROR("can not ioremap virtual address for"
187 " ring buffer\n");
188 return -ENOMEM;
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
Chris Wilson311bd682011-01-13 19:06:50 +0000192 ring->virtual_start = (void __force __iomem *)ring->map.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000194 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 dev_priv->back_offset = init->back_offset;
196 dev_priv->front_offset = init->front_offset;
197 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000198 if (master_priv->sarea_priv)
199 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* Allow hardware batchbuffers unless told otherwise.
202 */
203 dev_priv->allow_batchbuffer = 1;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 0;
206}
207
Dave Airlie84b1fd12007-07-11 15:53:27 +1000208static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000211 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800213 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800215 if (ring->map.handle == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 DRM_ERROR("can not ioremap virtual address for"
217 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000218 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
221 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800222 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800226 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800227 ring->status_page.page_addr);
228 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100229 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000230 else
Jesse Barnes585fb112008-07-29 11:54:06 -0700231 I915_WRITE(HWS_PGA, dev_priv->dma_status_page);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800232
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800233 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 return 0;
236}
237
Eric Anholtc153f452007-09-03 12:06:45 +1000238static int i915_dma_init(struct drm_device *dev, void *data,
239 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Eric Anholtc153f452007-09-03 12:06:45 +1000241 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 int retcode = 0;
243
Eric Anholtc153f452007-09-03 12:06:45 +1000244 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000246 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 break;
248 case I915_CLEANUP_DMA:
249 retcode = i915_dma_cleanup(dev);
250 break;
251 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100252 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 break;
254 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000255 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257 }
258
259 return retcode;
260}
261
262/* Implement basically the same security restrictions as hardware does
263 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
264 *
265 * Most of the calculations below involve calculating the size of a
266 * particular instruction. It's important to get the size right as
267 * that tells us where the next instruction to check is. Any illegal
268 * instruction detected will be given a size of zero, which is a
269 * signal to abort the rest of the buffer.
270 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100271static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 switch (((cmd >> 29) & 0x7)) {
274 case 0x0:
275 switch ((cmd >> 23) & 0x3f) {
276 case 0x0:
277 return 1; /* MI_NOOP */
278 case 0x4:
279 return 1; /* MI_FLUSH */
280 default:
281 return 0; /* disallow everything else */
282 }
283 break;
284 case 0x1:
285 return 0; /* reserved */
286 case 0x2:
287 return (cmd & 0xff) + 2; /* 2d commands */
288 case 0x3:
289 if (((cmd >> 24) & 0x1f) <= 0x18)
290 return 1;
291
292 switch ((cmd >> 24) & 0x1f) {
293 case 0x1c:
294 return 1;
295 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000296 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 case 0x3:
298 return (cmd & 0x1f) + 2;
299 case 0x4:
300 return (cmd & 0xf) + 2;
301 default:
302 return (cmd & 0xffff) + 2;
303 }
304 case 0x1e:
305 if (cmd & (1 << 23))
306 return (cmd & 0xffff) + 1;
307 else
308 return 1;
309 case 0x1f:
310 if ((cmd & (1 << 23)) == 0) /* inline vertices */
311 return (cmd & 0x1ffff) + 2;
312 else if (cmd & (1 << 17)) /* indirect random */
313 if ((cmd & 0xffff) == 0)
314 return 0; /* unknown length, too hard */
315 else
316 return (((cmd & 0xffff) + 1) / 2) + 1;
317 else
318 return 2; /* indirect sequential */
319 default:
320 return 0;
321 }
322 default:
323 return 0;
324 }
325
326 return 0;
327}
328
Eric Anholt201361a2009-03-11 12:30:04 -0700329static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100332 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000334 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000335 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100338 int sz = validate_cmd(buffer[i]);
339 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000340 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100341 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100344 ret = BEGIN_LP_RING((dwords+1)&~1);
345 if (ret)
346 return ret;
347
348 for (i = 0; i < dwords; i++)
349 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100350 if (dwords & 1)
351 OUT_RING(0);
352
353 ADVANCE_LP_RING();
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return 0;
356}
357
Eric Anholt673a3942008-07-30 12:06:12 -0700358int
359i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000360 struct drm_clip_rect *box,
361 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100363 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100364 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000366 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
367 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000369 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000370 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100373 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100374 ret = BEGIN_LP_RING(4);
375 if (ret)
376 return ret;
377
Alan Hourihanec29b6692006-08-12 16:29:24 +1000378 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000379 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
380 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000381 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000382 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100383 ret = BEGIN_LP_RING(6);
384 if (ret)
385 return ret;
386
Alan Hourihanec29b6692006-08-12 16:29:24 +1000387 OUT_RING(GFX_OP_DRAWRECT_INFO);
388 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000389 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
390 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000391 OUT_RING(DR4);
392 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000393 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100394 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 return 0;
397}
398
Alan Hourihanec29b6692006-08-12 16:29:24 +1000399/* XXX: Emitting the counter should really be moved to part of the IRQ
400 * emit. For now, do it in both places:
401 */
402
Dave Airlie84b1fd12007-07-11 15:53:27 +1000403static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100404{
405 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000406 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100407
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400408 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000409 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400410 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000411 if (master_priv->sarea_priv)
412 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100413
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100414 if (BEGIN_LP_RING(4) == 0) {
415 OUT_RING(MI_STORE_DWORD_INDEX);
416 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
417 OUT_RING(dev_priv->counter);
418 OUT_RING(0);
419 ADVANCE_LP_RING();
420 }
Dave Airliede227f52006-01-25 15:31:43 +1100421}
422
Dave Airlie84b1fd12007-07-11 15:53:27 +1000423static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700424 drm_i915_cmdbuffer_t *cmd,
425 struct drm_clip_rect *cliprects,
426 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 int nbox = cmd->num_cliprects;
429 int i = 0, count, ret;
430
431 if (cmd->sz & 0x3) {
432 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
436 i915_kernel_lost_context(dev);
437
438 count = nbox ? nbox : 1;
439
440 for (i = 0; i < count; i++) {
441 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000442 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 cmd->DR1, cmd->DR4);
444 if (ret)
445 return ret;
446 }
447
Eric Anholt201361a2009-03-11 12:30:04 -0700448 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (ret)
450 return ret;
451 }
452
Dave Airliede227f52006-01-25 15:31:43 +1100453 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
Dave Airlie84b1fd12007-07-11 15:53:27 +1000457static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700458 drm_i915_batchbuffer_t * batch,
459 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100461 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100463 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 if ((batch->start | batch->used) & 0x7) {
466 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 i915_kernel_lost_context(dev);
471
472 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 for (i = 0; i < count; i++) {
474 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000475 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100476 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (ret)
478 return ret;
479 }
480
Keith Packard0790d5e2008-07-30 12:28:47 -0700481 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100482 ret = BEGIN_LP_RING(2);
483 if (ret)
484 return ret;
485
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100486 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000487 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
488 OUT_RING(batch->start);
489 } else {
490 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
491 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100494 ret = BEGIN_LP_RING(4);
495 if (ret)
496 return ret;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 OUT_RING(MI_BATCH_BUFFER);
499 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
500 OUT_RING(batch->start + batch->used - 4);
501 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100503 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
Zou Nan hai1cafd342010-06-25 13:40:24 +0800506
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100507 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100508 if (BEGIN_LP_RING(2) == 0) {
509 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
510 OUT_RING(MI_NOOP);
511 ADVANCE_LP_RING();
512 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100515 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return 0;
517}
518
Dave Airlieaf6061a2008-05-07 12:15:39 +1000519static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000522 struct drm_i915_master_private *master_priv =
523 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100524 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Dave Airlie7c1c2872008-11-28 14:22:24 +1000526 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400527 return -EINVAL;
528
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800529 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800530 __func__,
531 dev_priv->current_page,
532 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Dave Airlieaf6061a2008-05-07 12:15:39 +1000534 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100536 ret = BEGIN_LP_RING(10);
537 if (ret)
538 return ret;
539
Jesse Barnes585fb112008-07-29 11:54:06 -0700540 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000541 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dave Airlieaf6061a2008-05-07 12:15:39 +1000543 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
544 OUT_RING(0);
545 if (dev_priv->current_page == 0) {
546 OUT_RING(dev_priv->back_offset);
547 dev_priv->current_page = 1;
548 } else {
549 OUT_RING(dev_priv->front_offset);
550 dev_priv->current_page = 0;
551 }
552 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000553
Dave Airlieaf6061a2008-05-07 12:15:39 +1000554 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
555 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100556
Dave Airlieaf6061a2008-05-07 12:15:39 +1000557 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000558
Dave Airlie7c1c2872008-11-28 14:22:24 +1000559 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000560
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100561 if (BEGIN_LP_RING(4) == 0) {
562 OUT_RING(MI_STORE_DWORD_INDEX);
563 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
564 OUT_RING(dev_priv->counter);
565 OUT_RING(0);
566 ADVANCE_LP_RING();
567 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000568
Dave Airlie7c1c2872008-11-28 14:22:24 +1000569 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000573static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000575 struct intel_ring_buffer *ring = LP_RING(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 i915_kernel_lost_context(dev);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000578 return intel_wait_ring_buffer(ring, ring->size - 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Eric Anholtc153f452007-09-03 12:06:45 +1000581static int i915_flush_ioctl(struct drm_device *dev, void *data,
582 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Eric Anholt546b0972008-09-01 16:45:29 -0700584 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Eric Anholt546b0972008-09-01 16:45:29 -0700586 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
587
588 mutex_lock(&dev->struct_mutex);
589 ret = i915_quiescent(dev);
590 mutex_unlock(&dev->struct_mutex);
591
592 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Eric Anholtc153f452007-09-03 12:06:45 +1000595static int i915_batchbuffer(struct drm_device *dev, void *data,
596 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000599 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000601 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000602 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700604 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (!dev_priv->allow_batchbuffer) {
607 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000608 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800611 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800612 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Eric Anholt546b0972008-09-01 16:45:29 -0700614 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Eric Anholt201361a2009-03-11 12:30:04 -0700616 if (batch->num_cliprects < 0)
617 return -EINVAL;
618
619 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700620 cliprects = kcalloc(batch->num_cliprects,
621 sizeof(struct drm_clip_rect),
622 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700623 if (cliprects == NULL)
624 return -ENOMEM;
625
626 ret = copy_from_user(cliprects, batch->cliprects,
627 batch->num_cliprects *
628 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200629 if (ret != 0) {
630 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700631 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200632 }
Eric Anholt201361a2009-03-11 12:30:04 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Eric Anholt546b0972008-09-01 16:45:29 -0700635 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700636 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700637 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400639 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000640 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700641
642fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700643 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return ret;
646}
647
Eric Anholtc153f452007-09-03 12:06:45 +1000648static int i915_cmdbuffer(struct drm_device *dev, void *data,
649 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000652 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000654 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000655 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700656 struct drm_clip_rect *cliprects = NULL;
657 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 int ret;
659
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800660 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800661 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Eric Anholt546b0972008-09-01 16:45:29 -0700663 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Eric Anholt201361a2009-03-11 12:30:04 -0700665 if (cmdbuf->num_cliprects < 0)
666 return -EINVAL;
667
Eric Anholt9a298b22009-03-24 12:23:04 -0700668 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700669 if (batch_data == NULL)
670 return -ENOMEM;
671
672 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200673 if (ret != 0) {
674 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700675 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200676 }
Eric Anholt201361a2009-03-11 12:30:04 -0700677
678 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700679 cliprects = kcalloc(cmdbuf->num_cliprects,
680 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000681 if (cliprects == NULL) {
682 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700683 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000684 }
Eric Anholt201361a2009-03-11 12:30:04 -0700685
686 ret = copy_from_user(cliprects, cmdbuf->cliprects,
687 cmdbuf->num_cliprects *
688 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200689 if (ret != 0) {
690 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700691 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
Eric Anholt546b0972008-09-01 16:45:29 -0700695 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700696 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700697 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (ret) {
699 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000700 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400703 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000704 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700705
Eric Anholt201361a2009-03-11 12:30:04 -0700706fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700707 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000708fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700709 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700710
711 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Eric Anholtc153f452007-09-03 12:06:45 +1000714static int i915_flip_bufs(struct drm_device *dev, void *data,
715 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Eric Anholt546b0972008-09-01 16:45:29 -0700717 int ret;
718
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800719 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Eric Anholt546b0972008-09-01 16:45:29 -0700721 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Eric Anholt546b0972008-09-01 16:45:29 -0700723 mutex_lock(&dev->struct_mutex);
724 ret = i915_dispatch_flip(dev);
725 mutex_unlock(&dev->struct_mutex);
726
727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Eric Anholtc153f452007-09-03 12:06:45 +1000730static int i915_getparam(struct drm_device *dev, void *data,
731 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000734 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int value;
736
737 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000738 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000739 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
Eric Anholtc153f452007-09-03 12:06:45 +1000742 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700744 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 case I915_PARAM_ALLOW_BATCHBUFFER:
747 value = dev_priv->allow_batchbuffer ? 1 : 0;
748 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100749 case I915_PARAM_LAST_DISPATCH:
750 value = READ_BREADCRUMB(dev_priv);
751 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400752 case I915_PARAM_CHIPSET_ID:
753 value = dev->pci_device;
754 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700755 case I915_PARAM_HAS_GEM:
Dave Airlieac5c4e72008-12-19 15:38:34 +1000756 value = dev_priv->has_gem;
Eric Anholt673a3942008-07-30 12:06:12 -0700757 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800758 case I915_PARAM_NUM_FENCES_AVAIL:
759 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
760 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200761 case I915_PARAM_HAS_OVERLAY:
762 value = dev_priv->overlay ? 1 : 0;
763 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800764 case I915_PARAM_HAS_PAGEFLIPPING:
765 value = 1;
766 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500767 case I915_PARAM_HAS_EXECBUF2:
768 /* depends on GEM */
769 value = dev_priv->has_gem;
770 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800771 case I915_PARAM_HAS_BSD:
772 value = HAS_BSD(dev);
773 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100774 case I915_PARAM_HAS_BLT:
775 value = HAS_BLT(dev);
776 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100777 case I915_PARAM_HAS_RELAXED_FENCING:
778 value = 1;
779 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100780 case I915_PARAM_HAS_COHERENT_RINGS:
781 value = 1;
782 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000783 case I915_PARAM_HAS_EXEC_CONSTANTS:
784 value = INTEL_INFO(dev)->gen >= 4;
785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800787 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -0500788 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000789 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Eric Anholtc153f452007-09-03 12:06:45 +1000792 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000794 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
797 return 0;
798}
799
Eric Anholtc153f452007-09-03 12:06:45 +1000800static int i915_setparam(struct drm_device *dev, void *data,
801 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000804 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000807 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000808 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
Eric Anholtc153f452007-09-03 12:06:45 +1000811 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 break;
814 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000815 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 break;
817 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000818 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800820 case I915_SETPARAM_NUM_USED_FENCES:
821 if (param->value > dev_priv->num_fence_regs ||
822 param->value < 0)
823 return -EINVAL;
824 /* Userspace can use first N regs */
825 dev_priv->fence_reg_start = param->value;
826 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800828 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800829 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000830 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 return 0;
834}
835
Eric Anholtc153f452007-09-03 12:06:45 +1000836static int i915_set_status_page(struct drm_device *dev, void *data,
837 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000838{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000839 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000840 drm_i915_hws_addr_t *hws = data;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000841 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000842
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000843 if (!I915_NEED_GFX_HWS(dev))
844 return -EINVAL;
845
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000846 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000847 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000848 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000849 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000850
Jesse Barnes79e53942008-11-07 14:24:08 -0800851 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
852 WARN(1, "tried to set status page when mode setting active\n");
853 return 0;
854 }
855
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800856 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000857
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800858 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +1000859
Eric Anholt8b409582007-11-22 16:40:37 +1000860 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000861 dev_priv->hws_map.size = 4*1024;
862 dev_priv->hws_map.type = 0;
863 dev_priv->hws_map.flags = 0;
864 dev_priv->hws_map.mtrr = 0;
865
Dave Airliedd0910b2009-02-25 14:49:21 +1000866 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000867 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000868 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -0700869 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000870 DRM_ERROR("can not ioremap virtual address for"
871 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000872 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000873 }
Chris Wilson311bd682011-01-13 19:06:50 +0000874 ring->status_page.page_addr =
875 (void __force __iomem *)dev_priv->hws_map.handle;
876 memset_io(ring->status_page.page_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800877 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000878
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800879 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -0700880 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800881 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -0700882 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000883 return 0;
884}
885
Dave Airlieec2a4c32009-08-04 11:43:41 +1000886static int i915_get_bridge_dev(struct drm_device *dev)
887{
888 struct drm_i915_private *dev_priv = dev->dev_private;
889
890 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
891 if (!dev_priv->bridge_dev) {
892 DRM_ERROR("bridge device not found\n");
893 return -1;
894 }
895 return 0;
896}
897
Zhenyu Wangc48044112009-12-17 14:48:43 +0800898#define MCHBAR_I915 0x44
899#define MCHBAR_I965 0x48
900#define MCHBAR_SIZE (4*4096)
901
902#define DEVEN_REG 0x54
903#define DEVEN_MCHBAR_EN (1 << 28)
904
905/* Allocate space for the MCH regs if needed, return nonzero on error */
906static int
907intel_alloc_mchbar_resource(struct drm_device *dev)
908{
909 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100910 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800911 u32 temp_lo, temp_hi = 0;
912 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +0100913 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800914
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100915 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +0800916 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
917 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
918 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
919
920 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
921#ifdef CONFIG_PNP
922 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +0100923 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
924 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800925#endif
926
927 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +0100928 dev_priv->mch_res.name = "i915 MCHBAR";
929 dev_priv->mch_res.flags = IORESOURCE_MEM;
930 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
931 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +0800932 MCHBAR_SIZE, MCHBAR_SIZE,
933 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +0100934 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +0800935 dev_priv->bridge_dev);
936 if (ret) {
937 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
938 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +0100939 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800940 }
941
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100942 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +0800943 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
944 upper_32_bits(dev_priv->mch_res.start));
945
946 pci_write_config_dword(dev_priv->bridge_dev, reg,
947 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +0100948 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800949}
950
951/* Setup MCHBAR if possible, return true if we should disable it again */
952static void
953intel_setup_mchbar(struct drm_device *dev)
954{
955 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100956 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800957 u32 temp;
958 bool enabled;
959
960 dev_priv->mchbar_need_disable = false;
961
962 if (IS_I915G(dev) || IS_I915GM(dev)) {
963 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
964 enabled = !!(temp & DEVEN_MCHBAR_EN);
965 } else {
966 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
967 enabled = temp & 1;
968 }
969
970 /* If it's already enabled, don't have to do anything */
971 if (enabled)
972 return;
973
974 if (intel_alloc_mchbar_resource(dev))
975 return;
976
977 dev_priv->mchbar_need_disable = true;
978
979 /* Space is allocated or reserved, so enable it. */
980 if (IS_I915G(dev) || IS_I915GM(dev)) {
981 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
982 temp | DEVEN_MCHBAR_EN);
983 } else {
984 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
985 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
986 }
987}
988
989static void
990intel_teardown_mchbar(struct drm_device *dev)
991{
992 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100993 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800994 u32 temp;
995
996 if (dev_priv->mchbar_need_disable) {
997 if (IS_I915G(dev) || IS_I915GM(dev)) {
998 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
999 temp &= ~DEVEN_MCHBAR_EN;
1000 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1001 } else {
1002 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1003 temp &= ~1;
1004 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1005 }
1006 }
1007
1008 if (dev_priv->mch_res.start)
1009 release_resource(&dev_priv->mch_res);
1010}
1011
Jesse Barnes80824002009-09-10 15:28:06 -07001012#define PTE_ADDRESS_MASK 0xfffff000
1013#define PTE_ADDRESS_MASK_HIGH 0x000000f0 /* i915+ */
1014#define PTE_MAPPING_TYPE_UNCACHED (0 << 1)
1015#define PTE_MAPPING_TYPE_DCACHE (1 << 1) /* i830 only */
1016#define PTE_MAPPING_TYPE_CACHED (3 << 1)
1017#define PTE_MAPPING_TYPE_MASK (3 << 1)
1018#define PTE_VALID (1 << 0)
1019
1020/**
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001021 * i915_stolen_to_phys - take an offset into stolen memory and turn it into
1022 * a physical one
Jesse Barnes80824002009-09-10 15:28:06 -07001023 * @dev: drm device
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001024 * @offset: address to translate
Jesse Barnes80824002009-09-10 15:28:06 -07001025 *
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001026 * Some chip functions require allocations from stolen space and need the
1027 * physical address of the memory in question.
Jesse Barnes80824002009-09-10 15:28:06 -07001028 */
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001029static unsigned long i915_stolen_to_phys(struct drm_device *dev, u32 offset)
Jesse Barnes80824002009-09-10 15:28:06 -07001030{
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001031 struct drm_i915_private *dev_priv = dev->dev_private;
1032 struct pci_dev *pdev = dev_priv->bridge_dev;
1033 u32 base;
Jesse Barnes80824002009-09-10 15:28:06 -07001034
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001035#if 0
1036 /* On the machines I have tested the Graphics Base of Stolen Memory
1037 * is unreliable, so compute the base by subtracting the stolen memory
1038 * from the Top of Low Usable DRAM which is where the BIOS places
1039 * the graphics stolen memory.
1040 */
1041 if (INTEL_INFO(dev)->gen > 3 || IS_G33(dev)) {
1042 /* top 32bits are reserved = 0 */
1043 pci_read_config_dword(pdev, 0xA4, &base);
Jesse Barnes80824002009-09-10 15:28:06 -07001044 } else {
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001045 /* XXX presume 8xx is the same as i915 */
1046 pci_bus_read_config_dword(pdev->bus, 2, 0x5C, &base);
Jesse Barnes80824002009-09-10 15:28:06 -07001047 }
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001048#else
1049 if (INTEL_INFO(dev)->gen > 3 || IS_G33(dev)) {
1050 u16 val;
1051 pci_read_config_word(pdev, 0xb0, &val);
1052 base = val >> 4 << 20;
1053 } else {
1054 u8 val;
1055 pci_read_config_byte(pdev, 0x9c, &val);
1056 base = val >> 3 << 27;
Jesse Barnes80824002009-09-10 15:28:06 -07001057 }
Chris Wilsonc64f7ba2010-11-23 14:24:24 +00001058 base -= dev_priv->mm.gtt->stolen_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001059#endif
Jesse Barnes80824002009-09-10 15:28:06 -07001060
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001061 return base + offset;
Jesse Barnes80824002009-09-10 15:28:06 -07001062}
1063
1064static void i915_warn_stolen(struct drm_device *dev)
1065{
1066 DRM_ERROR("not enough stolen space for compressed buffer, disabling\n");
1067 DRM_ERROR("hint: you may be able to increase stolen memory size in the BIOS to avoid this\n");
1068}
1069
1070static void i915_setup_compression(struct drm_device *dev, int size)
1071{
1072 struct drm_i915_private *dev_priv = dev->dev_private;
Prarit Bhargava132b6aa2010-05-27 13:37:56 -04001073 struct drm_mm_node *compressed_fb, *uninitialized_var(compressed_llb);
Andrew Morton29bd0ae2009-11-17 14:08:52 -08001074 unsigned long cfb_base;
1075 unsigned long ll_base = 0;
Jesse Barnes80824002009-09-10 15:28:06 -07001076
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001077 compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen, size, 4096, 0);
1078 if (compressed_fb)
1079 compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
1080 if (!compressed_fb)
1081 goto err;
Jesse Barnes80824002009-09-10 15:28:06 -07001082
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001083 cfb_base = i915_stolen_to_phys(dev, compressed_fb->start);
1084 if (!cfb_base)
1085 goto err_fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001086
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001087 if (!(IS_GM45(dev) || HAS_PCH_SPLIT(dev))) {
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001088 compressed_llb = drm_mm_search_free(&dev_priv->mm.stolen,
1089 4096, 4096, 0);
1090 if (compressed_llb)
1091 compressed_llb = drm_mm_get_block(compressed_llb,
1092 4096, 4096);
1093 if (!compressed_llb)
1094 goto err_fb;
Jesse Barnes74dff282009-09-14 15:39:40 -07001095
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001096 ll_base = i915_stolen_to_phys(dev, compressed_llb->start);
1097 if (!ll_base)
1098 goto err_llb;
Jesse Barnes80824002009-09-10 15:28:06 -07001099 }
1100
1101 dev_priv->cfb_size = size;
1102
Adam Jacksonee5382a2010-04-23 11:17:39 -04001103 intel_disable_fbc(dev);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001104 dev_priv->compressed_fb = compressed_fb;
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001105 if (HAS_PCH_SPLIT(dev))
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001106 I915_WRITE(ILK_DPFC_CB_BASE, compressed_fb->start);
1107 else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07001108 I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
1109 } else {
Jesse Barnes74dff282009-09-14 15:39:40 -07001110 I915_WRITE(FBC_CFB_BASE, cfb_base);
1111 I915_WRITE(FBC_LL_BASE, ll_base);
Jesse Barnes20bf3772010-04-21 11:39:22 -07001112 dev_priv->compressed_llb = compressed_llb;
Jesse Barnes80824002009-09-10 15:28:06 -07001113 }
1114
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001115 DRM_DEBUG_KMS("FBC base 0x%08lx, ll base 0x%08lx, size %dM\n",
1116 cfb_base, ll_base, size >> 20);
1117 return;
1118
1119err_llb:
1120 drm_mm_put_block(compressed_llb);
1121err_fb:
1122 drm_mm_put_block(compressed_fb);
1123err:
1124 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
1125 i915_warn_stolen(dev);
Jesse Barnes80824002009-09-10 15:28:06 -07001126}
1127
Jesse Barnes20bf3772010-04-21 11:39:22 -07001128static void i915_cleanup_compression(struct drm_device *dev)
1129{
1130 struct drm_i915_private *dev_priv = dev->dev_private;
1131
1132 drm_mm_put_block(dev_priv->compressed_fb);
Jesse Barnesaebf0da2010-07-22 08:12:20 -07001133 if (dev_priv->compressed_llb)
Jesse Barnes20bf3772010-04-21 11:39:22 -07001134 drm_mm_put_block(dev_priv->compressed_llb);
1135}
1136
Dave Airlie28d52042009-09-21 14:33:58 +10001137/* true = enable decode, false = disable decoder */
1138static unsigned int i915_vga_set_decode(void *cookie, bool state)
1139{
1140 struct drm_device *dev = cookie;
1141
1142 intel_modeset_vga_set_state(dev, state);
1143 if (state)
1144 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1145 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1146 else
1147 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1148}
1149
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001150static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1151{
1152 struct drm_device *dev = pci_get_drvdata(pdev);
1153 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1154 if (state == VGA_SWITCHEROO_ON) {
Dave Airliefbf81762010-06-01 09:09:06 +10001155 printk(KERN_INFO "i915: switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001156 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001157 /* i915 resume handler doesn't set to D0 */
1158 pci_set_power_state(dev->pdev, PCI_D0);
1159 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001160 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001161 } else {
1162 printk(KERN_ERR "i915: switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001163 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001164 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001165 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001166 }
1167}
1168
1169static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1170{
1171 struct drm_device *dev = pci_get_drvdata(pdev);
1172 bool can_switch;
1173
1174 spin_lock(&dev->count_lock);
1175 can_switch = (dev->open_count == 0);
1176 spin_unlock(&dev->count_lock);
1177 return can_switch;
1178}
1179
Daniel Vetter53984632010-09-22 23:44:24 +02001180static int i915_load_modeset_init(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08001181{
1182 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter53984632010-09-22 23:44:24 +02001183 unsigned long prealloc_size, gtt_size, mappable_size;
Jesse Barnes79e53942008-11-07 14:24:08 -08001184 int ret = 0;
1185
Chris Wilsonc64f7ba2010-11-23 14:24:24 +00001186 prealloc_size = dev_priv->mm.gtt->stolen_size;
Daniel Vetter53984632010-09-22 23:44:24 +02001187 gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
1188 mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
Daniel Vetter53984632010-09-22 23:44:24 +02001189
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001190 /* Basic memrange allocator for stolen space */
1191 drm_mm_init(&dev_priv->mm.stolen, 0, prealloc_size);
Jesse Barnes79e53942008-11-07 14:24:08 -08001192
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001193 /* Let GEM Manage all of the aperture.
Eric Anholt13f4c432009-05-12 15:27:36 -07001194 *
1195 * However, leave one page at the end still bound to the scratch page.
1196 * There are a number of places where the hardware apparently
1197 * prefetches past the end of the object, and we've seen multiple
1198 * hangs with the GPU head pointer stuck in a batchbuffer bound
1199 * at the last page of the aperture. One page should be enough to
1200 * keep any prefetching inside of the aperture.
1201 */
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001202 i915_gem_do_init(dev, 0, mappable_size, gtt_size - PAGE_SIZE);
Jesse Barnes79e53942008-11-07 14:24:08 -08001203
Ben Gamari11ed50e2009-09-14 17:48:45 -04001204 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001205 ret = i915_gem_init_ringbuffer(dev);
Ben Gamari11ed50e2009-09-14 17:48:45 -04001206 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001207 if (ret)
Dave Airlieb8da7de2009-06-02 16:50:35 +10001208 goto out;
Jesse Barnes79e53942008-11-07 14:24:08 -08001209
Jesse Barnes80824002009-09-10 15:28:06 -07001210 /* Try to set up FBC with a reasonable compressed buffer size */
Shaohua Li9216d442009-10-10 15:20:55 +08001211 if (I915_HAS_FBC(dev) && i915_powersave) {
Jesse Barnes80824002009-09-10 15:28:06 -07001212 int cfb_size;
1213
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001214 /* Leave 1M for line length buffer & misc. */
1215
1216 /* Try to get a 32M buffer... */
1217 if (prealloc_size > (36*1024*1024))
1218 cfb_size = 32*1024*1024;
Jesse Barnes80824002009-09-10 15:28:06 -07001219 else /* fall back to 7/8 of the stolen space */
1220 cfb_size = prealloc_size * 7 / 8;
1221 i915_setup_compression(dev, cfb_size);
1222 }
1223
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001224 /* Allow hardware batchbuffers unless told otherwise. */
Jesse Barnes79e53942008-11-07 14:24:08 -08001225 dev_priv->allow_batchbuffer = 1;
1226
Bryan Freed6d139a82010-10-14 09:14:51 +01001227 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001228 if (ret)
1229 DRM_INFO("failed to find VBIOS tables\n");
1230
Dave Airlie28d52042009-09-21 14:33:58 +10001231 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1232 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1233 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001234 goto cleanup_ringbuffer;
Dave Airlie28d52042009-09-21 14:33:58 +10001235
Jesse Barnes723bfd72010-10-07 16:01:13 -07001236 intel_register_dsm_handler();
1237
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001238 ret = vga_switcheroo_register_client(dev->pdev,
1239 i915_switcheroo_set_state,
Dave Airlie8d608aa2010-12-07 08:57:57 +10001240 NULL,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001241 i915_switcheroo_can_switch);
1242 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001243 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001244
Jesse Barnes1afe3e92010-03-26 10:35:20 -07001245 /* IIR "flip pending" bit means done if this bit is set */
1246 if (IS_GEN3(dev) && (I915_READ(ECOSKPD) & ECO_FLIP_DONE))
1247 dev_priv->flip_pending_is_done = true;
1248
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001249 intel_modeset_init(dev);
1250
Jesse Barnes79e53942008-11-07 14:24:08 -08001251 ret = drm_irq_install(dev);
1252 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001253 goto cleanup_vga_switcheroo;
Jesse Barnes79e53942008-11-07 14:24:08 -08001254
Jesse Barnes79e53942008-11-07 14:24:08 -08001255 /* Always safe in the mode setting case. */
1256 /* FIXME: do pre/post-mode set stuff in core KMS code */
1257 dev->vblank_disable_allowed = 1;
1258
Chris Wilson5a793952010-06-06 10:50:03 +01001259 ret = intel_fbdev_init(dev);
1260 if (ret)
1261 goto cleanup_irq;
1262
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001263 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001264
1265 /* We're off and running w/KMS */
1266 dev_priv->mm.suspended = 0;
1267
Jesse Barnes79e53942008-11-07 14:24:08 -08001268 return 0;
1269
Chris Wilson5a793952010-06-06 10:50:03 +01001270cleanup_irq:
1271 drm_irq_uninstall(dev);
1272cleanup_vga_switcheroo:
1273 vga_switcheroo_unregister_client(dev->pdev);
1274cleanup_vga_client:
1275 vga_client_register(dev->pdev, NULL, NULL, NULL);
1276cleanup_ringbuffer:
Eric Anholt21099532009-11-09 14:57:34 -08001277 mutex_lock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001278 i915_gem_cleanup_ringbuffer(dev);
Eric Anholt21099532009-11-09 14:57:34 -08001279 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08001280out:
1281 return ret;
1282}
1283
Dave Airlie7c1c2872008-11-28 14:22:24 +10001284int i915_master_create(struct drm_device *dev, struct drm_master *master)
1285{
1286 struct drm_i915_master_private *master_priv;
1287
Eric Anholt9a298b22009-03-24 12:23:04 -07001288 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001289 if (!master_priv)
1290 return -ENOMEM;
1291
1292 master->driver_priv = master_priv;
1293 return 0;
1294}
1295
1296void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1297{
1298 struct drm_i915_master_private *master_priv = master->driver_priv;
1299
1300 if (!master_priv)
1301 return;
1302
Eric Anholt9a298b22009-03-24 12:23:04 -07001303 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001304
1305 master->driver_priv = NULL;
1306}
1307
Jesse Barnes7648fa92010-05-20 14:28:11 -07001308static void i915_pineview_get_mem_freq(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08001309{
1310 drm_i915_private_t *dev_priv = dev->dev_private;
1311 u32 tmp;
1312
Shaohua Li7662c8b2009-06-26 11:23:55 +08001313 tmp = I915_READ(CLKCFG);
1314
1315 switch (tmp & CLKCFG_FSB_MASK) {
1316 case CLKCFG_FSB_533:
1317 dev_priv->fsb_freq = 533; /* 133*4 */
1318 break;
1319 case CLKCFG_FSB_800:
1320 dev_priv->fsb_freq = 800; /* 200*4 */
1321 break;
1322 case CLKCFG_FSB_667:
1323 dev_priv->fsb_freq = 667; /* 167*4 */
1324 break;
1325 case CLKCFG_FSB_400:
1326 dev_priv->fsb_freq = 400; /* 100*4 */
1327 break;
1328 }
1329
1330 switch (tmp & CLKCFG_MEM_MASK) {
1331 case CLKCFG_MEM_533:
1332 dev_priv->mem_freq = 533;
1333 break;
1334 case CLKCFG_MEM_667:
1335 dev_priv->mem_freq = 667;
1336 break;
1337 case CLKCFG_MEM_800:
1338 dev_priv->mem_freq = 800;
1339 break;
1340 }
Li Peng95534262010-05-18 18:58:44 +08001341
1342 /* detect pineview DDR3 setting */
1343 tmp = I915_READ(CSHRDDR3CTL);
1344 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
Shaohua Li7662c8b2009-06-26 11:23:55 +08001345}
1346
Jesse Barnes7648fa92010-05-20 14:28:11 -07001347static void i915_ironlake_get_mem_freq(struct drm_device *dev)
1348{
1349 drm_i915_private_t *dev_priv = dev->dev_private;
1350 u16 ddrpll, csipll;
1351
1352 ddrpll = I915_READ16(DDRMPLL1);
1353 csipll = I915_READ16(CSIPLL0);
1354
1355 switch (ddrpll & 0xff) {
1356 case 0xc:
1357 dev_priv->mem_freq = 800;
1358 break;
1359 case 0x10:
1360 dev_priv->mem_freq = 1066;
1361 break;
1362 case 0x14:
1363 dev_priv->mem_freq = 1333;
1364 break;
1365 case 0x18:
1366 dev_priv->mem_freq = 1600;
1367 break;
1368 default:
1369 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
1370 ddrpll & 0xff);
1371 dev_priv->mem_freq = 0;
1372 break;
1373 }
1374
1375 dev_priv->r_t = dev_priv->mem_freq;
1376
1377 switch (csipll & 0x3ff) {
1378 case 0x00c:
1379 dev_priv->fsb_freq = 3200;
1380 break;
1381 case 0x00e:
1382 dev_priv->fsb_freq = 3733;
1383 break;
1384 case 0x010:
1385 dev_priv->fsb_freq = 4266;
1386 break;
1387 case 0x012:
1388 dev_priv->fsb_freq = 4800;
1389 break;
1390 case 0x014:
1391 dev_priv->fsb_freq = 5333;
1392 break;
1393 case 0x016:
1394 dev_priv->fsb_freq = 5866;
1395 break;
1396 case 0x018:
1397 dev_priv->fsb_freq = 6400;
1398 break;
1399 default:
1400 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
1401 csipll & 0x3ff);
1402 dev_priv->fsb_freq = 0;
1403 break;
1404 }
1405
1406 if (dev_priv->fsb_freq == 3200) {
1407 dev_priv->c_m = 0;
1408 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
1409 dev_priv->c_m = 1;
1410 } else {
1411 dev_priv->c_m = 2;
1412 }
1413}
1414
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001415static const struct cparams {
1416 u16 i;
1417 u16 t;
1418 u16 m;
1419 u16 c;
1420} cparams[] = {
Jesse Barnes7648fa92010-05-20 14:28:11 -07001421 { 1, 1333, 301, 28664 },
1422 { 1, 1066, 294, 24460 },
1423 { 1, 800, 294, 25192 },
1424 { 0, 1333, 276, 27605 },
1425 { 0, 1066, 276, 27605 },
1426 { 0, 800, 231, 23784 },
1427};
1428
1429unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
1430{
1431 u64 total_count, diff, ret;
1432 u32 count1, count2, count3, m = 0, c = 0;
1433 unsigned long now = jiffies_to_msecs(jiffies), diff1;
1434 int i;
1435
1436 diff1 = now - dev_priv->last_time1;
1437
1438 count1 = I915_READ(DMIEC);
1439 count2 = I915_READ(DDREC);
1440 count3 = I915_READ(CSIEC);
1441
1442 total_count = count1 + count2 + count3;
1443
1444 /* FIXME: handle per-counter overflow */
1445 if (total_count < dev_priv->last_count1) {
1446 diff = ~0UL - dev_priv->last_count1;
1447 diff += total_count;
1448 } else {
1449 diff = total_count - dev_priv->last_count1;
1450 }
1451
1452 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
1453 if (cparams[i].i == dev_priv->c_m &&
1454 cparams[i].t == dev_priv->r_t) {
1455 m = cparams[i].m;
1456 c = cparams[i].c;
1457 break;
1458 }
1459 }
1460
Jesse Barnesd270ae32010-09-27 10:35:44 -07001461 diff = div_u64(diff, diff1);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001462 ret = ((m * diff) + c);
Jesse Barnesd270ae32010-09-27 10:35:44 -07001463 ret = div_u64(ret, 10);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001464
1465 dev_priv->last_count1 = total_count;
1466 dev_priv->last_time1 = now;
1467
1468 return ret;
1469}
1470
1471unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
1472{
1473 unsigned long m, x, b;
1474 u32 tsfs;
1475
1476 tsfs = I915_READ(TSFS);
1477
1478 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
1479 x = I915_READ8(TR1);
1480
1481 b = tsfs & TSFS_INTR_MASK;
1482
1483 return ((m * x) / 127) - b;
1484}
1485
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001486static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Jesse Barnes7648fa92010-05-20 14:28:11 -07001487{
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001488 static const struct v_table {
1489 u16 vd; /* in .1 mil */
1490 u16 vm; /* in .1 mil */
1491 } v_table[] = {
1492 { 0, 0, },
1493 { 375, 0, },
1494 { 500, 0, },
1495 { 625, 0, },
1496 { 750, 0, },
1497 { 875, 0, },
1498 { 1000, 0, },
1499 { 1125, 0, },
1500 { 4125, 3000, },
1501 { 4125, 3000, },
1502 { 4125, 3000, },
1503 { 4125, 3000, },
1504 { 4125, 3000, },
1505 { 4125, 3000, },
1506 { 4125, 3000, },
1507 { 4125, 3000, },
1508 { 4125, 3000, },
1509 { 4125, 3000, },
1510 { 4125, 3000, },
1511 { 4125, 3000, },
1512 { 4125, 3000, },
1513 { 4125, 3000, },
1514 { 4125, 3000, },
1515 { 4125, 3000, },
1516 { 4125, 3000, },
1517 { 4125, 3000, },
1518 { 4125, 3000, },
1519 { 4125, 3000, },
1520 { 4125, 3000, },
1521 { 4125, 3000, },
1522 { 4125, 3000, },
1523 { 4125, 3000, },
1524 { 4250, 3125, },
1525 { 4375, 3250, },
1526 { 4500, 3375, },
1527 { 4625, 3500, },
1528 { 4750, 3625, },
1529 { 4875, 3750, },
1530 { 5000, 3875, },
1531 { 5125, 4000, },
1532 { 5250, 4125, },
1533 { 5375, 4250, },
1534 { 5500, 4375, },
1535 { 5625, 4500, },
1536 { 5750, 4625, },
1537 { 5875, 4750, },
1538 { 6000, 4875, },
1539 { 6125, 5000, },
1540 { 6250, 5125, },
1541 { 6375, 5250, },
1542 { 6500, 5375, },
1543 { 6625, 5500, },
1544 { 6750, 5625, },
1545 { 6875, 5750, },
1546 { 7000, 5875, },
1547 { 7125, 6000, },
1548 { 7250, 6125, },
1549 { 7375, 6250, },
1550 { 7500, 6375, },
1551 { 7625, 6500, },
1552 { 7750, 6625, },
1553 { 7875, 6750, },
1554 { 8000, 6875, },
1555 { 8125, 7000, },
1556 { 8250, 7125, },
1557 { 8375, 7250, },
1558 { 8500, 7375, },
1559 { 8625, 7500, },
1560 { 8750, 7625, },
1561 { 8875, 7750, },
1562 { 9000, 7875, },
1563 { 9125, 8000, },
1564 { 9250, 8125, },
1565 { 9375, 8250, },
1566 { 9500, 8375, },
1567 { 9625, 8500, },
1568 { 9750, 8625, },
1569 { 9875, 8750, },
1570 { 10000, 8875, },
1571 { 10125, 9000, },
1572 { 10250, 9125, },
1573 { 10375, 9250, },
1574 { 10500, 9375, },
1575 { 10625, 9500, },
1576 { 10750, 9625, },
1577 { 10875, 9750, },
1578 { 11000, 9875, },
1579 { 11125, 10000, },
1580 { 11250, 10125, },
1581 { 11375, 10250, },
1582 { 11500, 10375, },
1583 { 11625, 10500, },
1584 { 11750, 10625, },
1585 { 11875, 10750, },
1586 { 12000, 10875, },
1587 { 12125, 11000, },
1588 { 12250, 11125, },
1589 { 12375, 11250, },
1590 { 12500, 11375, },
1591 { 12625, 11500, },
1592 { 12750, 11625, },
1593 { 12875, 11750, },
1594 { 13000, 11875, },
1595 { 13125, 12000, },
1596 { 13250, 12125, },
1597 { 13375, 12250, },
1598 { 13500, 12375, },
1599 { 13625, 12500, },
1600 { 13750, 12625, },
1601 { 13875, 12750, },
1602 { 14000, 12875, },
1603 { 14125, 13000, },
1604 { 14250, 13125, },
1605 { 14375, 13250, },
1606 { 14500, 13375, },
1607 { 14625, 13500, },
1608 { 14750, 13625, },
1609 { 14875, 13750, },
1610 { 15000, 13875, },
1611 { 15125, 14000, },
1612 { 15250, 14125, },
1613 { 15375, 14250, },
1614 { 15500, 14375, },
1615 { 15625, 14500, },
1616 { 15750, 14625, },
1617 { 15875, 14750, },
1618 { 16000, 14875, },
1619 { 16125, 15000, },
1620 };
1621 if (dev_priv->info->is_mobile)
1622 return v_table[pxvid].vm;
1623 else
1624 return v_table[pxvid].vd;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001625}
1626
1627void i915_update_gfx_val(struct drm_i915_private *dev_priv)
1628{
1629 struct timespec now, diff1;
1630 u64 diff;
1631 unsigned long diffms;
1632 u32 count;
1633
1634 getrawmonotonic(&now);
1635 diff1 = timespec_sub(now, dev_priv->last_time2);
1636
1637 /* Don't divide by 0 */
1638 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
1639 if (!diffms)
1640 return;
1641
1642 count = I915_READ(GFXEC);
1643
1644 if (count < dev_priv->last_count2) {
1645 diff = ~0UL - dev_priv->last_count2;
1646 diff += count;
1647 } else {
1648 diff = count - dev_priv->last_count2;
1649 }
1650
1651 dev_priv->last_count2 = count;
1652 dev_priv->last_time2 = now;
1653
1654 /* More magic constants... */
1655 diff = diff * 1181;
Jesse Barnesd270ae32010-09-27 10:35:44 -07001656 diff = div_u64(diff, diffms * 10);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001657 dev_priv->gfx_power = diff;
1658}
1659
1660unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
1661{
1662 unsigned long t, corr, state1, corr2, state2;
1663 u32 pxvid, ext_v;
1664
1665 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->cur_delay * 4));
1666 pxvid = (pxvid >> 24) & 0x7f;
1667 ext_v = pvid_to_extvid(dev_priv, pxvid);
1668
1669 state1 = ext_v;
1670
1671 t = i915_mch_val(dev_priv);
1672
1673 /* Revel in the empirically derived constants */
1674
1675 /* Correction factor in 1/100000 units */
1676 if (t > 80)
1677 corr = ((t * 2349) + 135940);
1678 else if (t >= 50)
1679 corr = ((t * 964) + 29317);
1680 else /* < 50 */
1681 corr = ((t * 301) + 1004);
1682
1683 corr = corr * ((150142 * state1) / 10000 - 78642);
1684 corr /= 100000;
1685 corr2 = (corr * dev_priv->corr);
1686
1687 state2 = (corr2 * state1) / 10000;
1688 state2 /= 100; /* convert to mW */
1689
1690 i915_update_gfx_val(dev_priv);
1691
1692 return dev_priv->gfx_power + state2;
1693}
1694
1695/* Global for IPS driver to get at the current i915 device */
1696static struct drm_i915_private *i915_mch_dev;
1697/*
1698 * Lock protecting IPS related data structures
1699 * - i915_mch_dev
1700 * - dev_priv->max_delay
1701 * - dev_priv->min_delay
1702 * - dev_priv->fmax
1703 * - dev_priv->gpu_busy
1704 */
Chris Wilson995b6762010-08-20 13:23:26 +01001705static DEFINE_SPINLOCK(mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001706
1707/**
1708 * i915_read_mch_val - return value for IPS use
1709 *
1710 * Calculate and return a value for the IPS driver to use when deciding whether
1711 * we have thermal and power headroom to increase CPU or GPU power budget.
1712 */
1713unsigned long i915_read_mch_val(void)
1714{
1715 struct drm_i915_private *dev_priv;
1716 unsigned long chipset_val, graphics_val, ret = 0;
1717
1718 spin_lock(&mchdev_lock);
1719 if (!i915_mch_dev)
1720 goto out_unlock;
1721 dev_priv = i915_mch_dev;
1722
1723 chipset_val = i915_chipset_val(dev_priv);
1724 graphics_val = i915_gfx_val(dev_priv);
1725
1726 ret = chipset_val + graphics_val;
1727
1728out_unlock:
1729 spin_unlock(&mchdev_lock);
1730
1731 return ret;
1732}
1733EXPORT_SYMBOL_GPL(i915_read_mch_val);
1734
1735/**
1736 * i915_gpu_raise - raise GPU frequency limit
1737 *
1738 * Raise the limit; IPS indicates we have thermal headroom.
1739 */
1740bool i915_gpu_raise(void)
1741{
1742 struct drm_i915_private *dev_priv;
1743 bool ret = true;
1744
1745 spin_lock(&mchdev_lock);
1746 if (!i915_mch_dev) {
1747 ret = false;
1748 goto out_unlock;
1749 }
1750 dev_priv = i915_mch_dev;
1751
1752 if (dev_priv->max_delay > dev_priv->fmax)
1753 dev_priv->max_delay--;
1754
1755out_unlock:
1756 spin_unlock(&mchdev_lock);
1757
1758 return ret;
1759}
1760EXPORT_SYMBOL_GPL(i915_gpu_raise);
1761
1762/**
1763 * i915_gpu_lower - lower GPU frequency limit
1764 *
1765 * IPS indicates we're close to a thermal limit, so throttle back the GPU
1766 * frequency maximum.
1767 */
1768bool i915_gpu_lower(void)
1769{
1770 struct drm_i915_private *dev_priv;
1771 bool ret = true;
1772
1773 spin_lock(&mchdev_lock);
1774 if (!i915_mch_dev) {
1775 ret = false;
1776 goto out_unlock;
1777 }
1778 dev_priv = i915_mch_dev;
1779
1780 if (dev_priv->max_delay < dev_priv->min_delay)
1781 dev_priv->max_delay++;
1782
1783out_unlock:
1784 spin_unlock(&mchdev_lock);
1785
1786 return ret;
1787}
1788EXPORT_SYMBOL_GPL(i915_gpu_lower);
1789
1790/**
1791 * i915_gpu_busy - indicate GPU business to IPS
1792 *
1793 * Tell the IPS driver whether or not the GPU is busy.
1794 */
1795bool i915_gpu_busy(void)
1796{
1797 struct drm_i915_private *dev_priv;
1798 bool ret = false;
1799
1800 spin_lock(&mchdev_lock);
1801 if (!i915_mch_dev)
1802 goto out_unlock;
1803 dev_priv = i915_mch_dev;
1804
1805 ret = dev_priv->busy;
1806
1807out_unlock:
1808 spin_unlock(&mchdev_lock);
1809
1810 return ret;
1811}
1812EXPORT_SYMBOL_GPL(i915_gpu_busy);
1813
1814/**
1815 * i915_gpu_turbo_disable - disable graphics turbo
1816 *
1817 * Disable graphics turbo by resetting the max frequency and setting the
1818 * current frequency to the default.
1819 */
1820bool i915_gpu_turbo_disable(void)
1821{
1822 struct drm_i915_private *dev_priv;
1823 bool ret = true;
1824
1825 spin_lock(&mchdev_lock);
1826 if (!i915_mch_dev) {
1827 ret = false;
1828 goto out_unlock;
1829 }
1830 dev_priv = i915_mch_dev;
1831
1832 dev_priv->max_delay = dev_priv->fstart;
1833
1834 if (!ironlake_set_drps(dev_priv->dev, dev_priv->fstart))
1835 ret = false;
1836
1837out_unlock:
1838 spin_unlock(&mchdev_lock);
1839
1840 return ret;
1841}
1842EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
1843
Jesse Barnes79e53942008-11-07 14:24:08 -08001844/**
Eric Anholt63ee41d2010-12-20 18:40:06 -08001845 * Tells the intel_ips driver that the i915 driver is now loaded, if
1846 * IPS got loaded first.
1847 *
1848 * This awkward dance is so that neither module has to depend on the
1849 * other in order for IPS to do the appropriate communication of
1850 * GPU turbo limits to i915.
1851 */
1852static void
1853ips_ping_for_i915_load(void)
1854{
1855 void (*link)(void);
1856
1857 link = symbol_get(ips_link_to_i915_driver);
1858 if (link) {
1859 link();
1860 symbol_put(ips_link_to_i915_driver);
1861 }
1862}
1863
1864/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001865 * i915_driver_load - setup chip and create an initial config
1866 * @dev: DRM device
1867 * @flags: startup flags
1868 *
1869 * The driver load routine has to do several things:
1870 * - drive output discovery via intel_modeset_init()
1871 * - initialize the memory manager
1872 * - allocate initial config memory
1873 * - setup the DRM framebuffer with the allocated memory
1874 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001875int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001876{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001877 struct drm_i915_private *dev_priv;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001878 int ret = 0, mmio_bar;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001879 uint32_t agp_size;
1880
Dave Airlie22eae942005-11-10 22:16:34 +11001881 /* i915 has 4 more counters */
1882 dev->counters += 4;
1883 dev->types[6] = _DRM_STAT_IRQ;
1884 dev->types[7] = _DRM_STAT_PRIMARY;
1885 dev->types[8] = _DRM_STAT_SECONDARY;
1886 dev->types[9] = _DRM_STAT_DMA;
1887
Eric Anholt9a298b22009-03-24 12:23:04 -07001888 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001889 if (dev_priv == NULL)
1890 return -ENOMEM;
1891
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001892 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001893 dev_priv->dev = dev;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001894 dev_priv->info = (struct intel_device_info *) flags;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001895
Dave Airlieec2a4c32009-08-04 11:43:41 +10001896 if (i915_get_bridge_dev(dev)) {
1897 ret = -EIO;
1898 goto free_priv;
1899 }
1900
Daniel Vetter9f82d232010-08-30 21:25:23 +02001901 /* overlay on gen2 is broken and can't address above 1G */
1902 if (IS_GEN2(dev))
1903 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1904
Chris Wilsonb4ce0f82010-10-28 11:26:06 +01001905 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1906 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, 0);
1907 if (!dev_priv->regs) {
1908 DRM_ERROR("failed to map registers\n");
1909 ret = -EIO;
1910 goto put_bridge;
1911 }
1912
Chris Wilson71e93392010-10-27 18:46:52 +01001913 dev_priv->mm.gtt = intel_gtt_get();
1914 if (!dev_priv->mm.gtt) {
1915 DRM_ERROR("Failed to initialize GTT\n");
1916 ret = -ENODEV;
1917 goto out_iomapfree;
1918 }
1919
Chris Wilson71e93392010-10-27 18:46:52 +01001920 agp_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
1921
Eric Anholtab657db12009-01-23 12:57:47 -08001922 dev_priv->mm.gtt_mapping =
Chris Wilson71e93392010-10-27 18:46:52 +01001923 io_mapping_create_wc(dev->agp->base, agp_size);
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001924 if (dev_priv->mm.gtt_mapping == NULL) {
1925 ret = -EIO;
1926 goto out_rmmap;
1927 }
1928
Eric Anholtab657db12009-01-23 12:57:47 -08001929 /* Set up a WC MTRR for non-PAT systems. This is more common than
1930 * one would think, because the kernel disables PAT on first
1931 * generation Core chips because WC PAT gets overridden by a UC
1932 * MTRR if present. Even if a UC MTRR isn't present.
1933 */
1934 dev_priv->mm.gtt_mtrr = mtrr_add(dev->agp->base,
Chris Wilson71e93392010-10-27 18:46:52 +01001935 agp_size,
Eric Anholtab657db12009-01-23 12:57:47 -08001936 MTRR_TYPE_WRCOMB, 1);
1937 if (dev_priv->mm.gtt_mtrr < 0) {
Eric Anholt040aefa2009-03-10 12:31:12 -07001938 DRM_INFO("MTRR allocation failed. Graphics "
Eric Anholtab657db12009-01-23 12:57:47 -08001939 "performance may suffer.\n");
1940 }
1941
Chris Wilsone642abb2010-09-09 12:46:34 +01001942 /* The i915 workqueue is primarily used for batched retirement of
1943 * requests (and thus managing bo) once the task has been completed
1944 * by the GPU. i915_gem_retire_requests() is called directly when we
1945 * need high-priority retirement, such as waiting for an explicit
1946 * bo.
1947 *
1948 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001949 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001950 *
1951 * All tasks on the workqueue are expected to acquire the dev mutex
1952 * so there is no point in running more than one instance of the
1953 * workqueue at any time: max_active = 1 and NON_REENTRANT.
1954 */
1955 dev_priv->wq = alloc_workqueue("i915",
1956 WQ_UNBOUND | WQ_NON_REENTRANT,
1957 1);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001958 if (dev_priv->wq == NULL) {
1959 DRM_ERROR("Failed to create our workqueue.\n");
1960 ret = -ENOMEM;
1961 goto out_iomapfree;
1962 }
1963
Dave Airlieac5c4e72008-12-19 15:38:34 +10001964 /* enable GEM by default */
1965 dev_priv->has_gem = 1;
Dave Airlieac5c4e72008-12-19 15:38:34 +10001966
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001967 dev->driver->get_vblank_counter = i915_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001968 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01001969 if (IS_G4X(dev) || IS_GEN5(dev) || IS_GEN6(dev)) {
Jesse Barnes42c27982009-05-05 13:13:16 -07001970 dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001971 dev->driver->get_vblank_counter = gm45_get_vblank_counter;
Jesse Barnes42c27982009-05-05 13:13:16 -07001972 }
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001973
Zhenyu Wangc48044112009-12-17 14:48:43 +08001974 /* Try to make sure MCHBAR is enabled before poking at it */
1975 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001976 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001977 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001978
Bryan Freed6d139a82010-10-14 09:14:51 +01001979 /* Make sure the bios did its job and set up vital registers */
1980 intel_setup_bios(dev);
1981
Eric Anholt673a3942008-07-30 12:06:12 -07001982 i915_gem_load(dev);
1983
Keith Packard398c9cb2008-07-30 13:03:43 -07001984 /* Init HWS */
1985 if (!I915_NEED_GFX_HWS(dev)) {
1986 ret = i915_init_phys_hws(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001987 if (ret)
1988 goto out_gem_unload;
Keith Packard398c9cb2008-07-30 13:03:43 -07001989 }
Eric Anholted4cb412008-07-29 12:10:39 -07001990
Jesse Barnes7648fa92010-05-20 14:28:11 -07001991 if (IS_PINEVIEW(dev))
1992 i915_pineview_get_mem_freq(dev);
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01001993 else if (IS_GEN5(dev))
Jesse Barnes7648fa92010-05-20 14:28:11 -07001994 i915_ironlake_get_mem_freq(dev);
Shaohua Li7662c8b2009-06-26 11:23:55 +08001995
Eric Anholted4cb412008-07-29 12:10:39 -07001996 /* On the 945G/GM, the chipset reports the MSI capability on the
1997 * integrated graphics even though the support isn't actually there
1998 * according to the published specs. It doesn't appear to function
1999 * correctly in testing on 945G.
2000 * This may be a side effect of MSI having been made available for PEG
2001 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07002002 *
2003 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08002004 * be lost or delayed, but we use them anyways to avoid
2005 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07002006 */
Keith Packardb60678a2008-12-08 11:12:28 -08002007 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08002008 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07002009
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002010 spin_lock_init(&dev_priv->irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07002011 spin_lock_init(&dev_priv->error_lock);
Chris Wilson9d34e5d2009-09-24 05:26:06 +01002012 dev_priv->trace_irq_seqno = 0;
Eric Anholted4cb412008-07-29 12:10:39 -07002013
Keith Packard52440212008-11-18 09:30:25 -08002014 ret = drm_vblank_init(dev, I915_NUM_PIPE);
Chris Wilson56e2ea32010-11-08 17:10:29 +00002015 if (ret)
2016 goto out_gem_unload;
Keith Packard52440212008-11-18 09:30:25 -08002017
Ben Gamari11ed50e2009-09-14 17:48:45 -04002018 /* Start out suspended */
2019 dev_priv->mm.suspended = 1;
2020
Zhenyu Wang3bad0782010-04-07 16:15:53 +08002021 intel_detect_pch(dev);
2022
Jesse Barnes79e53942008-11-07 14:24:08 -08002023 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02002024 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002025 if (ret < 0) {
2026 DRM_ERROR("failed to init modeset\n");
Chris Wilson56e2ea32010-11-08 17:10:29 +00002027 goto out_gem_unload;
Jesse Barnes79e53942008-11-07 14:24:08 -08002028 }
2029 }
2030
Matthew Garrett74a365b2009-03-19 21:35:39 +00002031 /* Must be done after probing outputs */
Chris Wilson44834a62010-08-19 16:09:23 +01002032 intel_opregion_init(dev);
2033 acpi_video_register();
Matthew Garrett74a365b2009-03-19 21:35:39 +00002034
Ben Gamarif65d9422009-09-14 17:48:44 -04002035 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
2036 (unsigned long) dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07002037
2038 spin_lock(&mchdev_lock);
2039 i915_mch_dev = dev_priv;
2040 dev_priv->mchdev_lock = &mchdev_lock;
2041 spin_unlock(&mchdev_lock);
2042
Eric Anholt63ee41d2010-12-20 18:40:06 -08002043 ips_ping_for_i915_load();
2044
Jesse Barnes79e53942008-11-07 14:24:08 -08002045 return 0;
2046
Chris Wilson56e2ea32010-11-08 17:10:29 +00002047out_gem_unload:
2048 if (dev->pdev->msi_enabled)
2049 pci_disable_msi(dev->pdev);
2050
2051 intel_teardown_gmbus(dev);
2052 intel_teardown_mchbar(dev);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002053 destroy_workqueue(dev_priv->wq);
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08002054out_iomapfree:
2055 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08002056out_rmmap:
Chris Wilson6dda5692010-10-29 21:02:18 +01002057 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10002058put_bridge:
2059 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002060free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07002061 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002062 return ret;
2063}
2064
2065int i915_driver_unload(struct drm_device *dev)
2066{
2067 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02002068 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002069
Jesse Barnes7648fa92010-05-20 14:28:11 -07002070 spin_lock(&mchdev_lock);
2071 i915_mch_dev = NULL;
2072 spin_unlock(&mchdev_lock);
2073
Chris Wilson17250b72010-10-28 12:51:39 +01002074 if (dev_priv->mm.inactive_shrinker.shrink)
2075 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
2076
Daniel Vetterc911fc12010-08-20 21:23:20 +02002077 mutex_lock(&dev->struct_mutex);
2078 ret = i915_gpu_idle(dev);
2079 if (ret)
2080 DRM_ERROR("failed to idle hardware: %d\n", ret);
2081 mutex_unlock(&dev->struct_mutex);
2082
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002083 /* Cancel the retire work handler, which should be idle now. */
2084 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2085
Eric Anholtab657db12009-01-23 12:57:47 -08002086 io_mapping_free(dev_priv->mm.gtt_mapping);
2087 if (dev_priv->mm.gtt_mtrr >= 0) {
2088 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
2089 dev->agp->agp_info.aper_size * 1024 * 1024);
2090 dev_priv->mm.gtt_mtrr = -1;
2091 }
2092
Chris Wilson44834a62010-08-19 16:09:23 +01002093 acpi_video_unregister();
2094
Jesse Barnes79e53942008-11-07 14:24:08 -08002095 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01002096 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07002097 intel_modeset_cleanup(dev);
2098
Zhao Yakui6363ee62009-11-24 09:48:44 +08002099 /*
2100 * free the memory space allocated for the child device
2101 * config parsed from VBT
2102 */
2103 if (dev_priv->child_dev && dev_priv->child_dev_num) {
2104 kfree(dev_priv->child_dev);
2105 dev_priv->child_dev = NULL;
2106 dev_priv->child_dev_num = 0;
2107 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02002108
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10002109 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10002110 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08002111 }
2112
Daniel Vettera8b48992010-08-20 21:25:11 +02002113 /* Free error state after interrupts are fully disabled. */
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002114 del_timer_sync(&dev_priv->hangcheck_timer);
2115 cancel_work_sync(&dev_priv->error_work);
Daniel Vettera8b48992010-08-20 21:25:11 +02002116 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002117
Eric Anholted4cb412008-07-29 12:10:39 -07002118 if (dev->pdev->msi_enabled)
2119 pci_disable_msi(dev->pdev);
2120
Chris Wilson44834a62010-08-19 16:09:23 +01002121 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01002122
Jesse Barnes79e53942008-11-07 14:24:08 -08002123 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02002124 /* Flush any outstanding unpin_work. */
2125 flush_workqueue(dev_priv->wq);
2126
Dave Airlie71acb5e2008-12-30 20:31:46 +10002127 i915_gem_free_all_phys_object(dev);
2128
Jesse Barnes79e53942008-11-07 14:24:08 -08002129 mutex_lock(&dev->struct_mutex);
2130 i915_gem_cleanup_ringbuffer(dev);
2131 mutex_unlock(&dev->struct_mutex);
Jesse Barnes20bf3772010-04-21 11:39:22 -07002132 if (I915_HAS_FBC(dev) && i915_powersave)
2133 i915_cleanup_compression(dev);
Chris Wilsonfe669bf2010-11-23 12:09:30 +00002134 drm_mm_takedown(&dev_priv->mm.stolen);
Daniel Vetter02e792f2009-09-15 22:57:34 +02002135
2136 intel_cleanup_overlay(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01002137
2138 if (!I915_NEED_GFX_HWS(dev))
2139 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002140 }
2141
Daniel Vetter701394c2010-10-10 18:54:08 +01002142 if (dev_priv->regs != NULL)
Chris Wilson6dda5692010-10-29 21:02:18 +01002143 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vetter701394c2010-10-10 18:54:08 +01002144
Chris Wilsonf899fc62010-07-20 15:44:45 -07002145 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08002146 intel_teardown_mchbar(dev);
2147
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002148 destroy_workqueue(dev_priv->wq);
2149
Dave Airlieec2a4c32009-08-04 11:43:41 +10002150 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07002151 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002152
Dave Airlie22eae942005-11-10 22:16:34 +11002153 return 0;
2154}
2155
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002156int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07002157{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002158 struct drm_i915_file_private *file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002159
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002160 DRM_DEBUG_DRIVER("\n");
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002161 file_priv = kmalloc(sizeof(*file_priv), GFP_KERNEL);
2162 if (!file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07002163 return -ENOMEM;
2164
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002165 file->driver_priv = file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002166
Chris Wilson1c255952010-09-26 11:03:27 +01002167 spin_lock_init(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002168 INIT_LIST_HEAD(&file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002169
2170 return 0;
2171}
2172
Jesse Barnes79e53942008-11-07 14:24:08 -08002173/**
2174 * i915_driver_lastclose - clean up after all DRM clients have exited
2175 * @dev: DRM device
2176 *
2177 * Take care of cleaning up after all DRM clients have exited. In the
2178 * mode setting case, we want to restore the kernel's initial mode (just
2179 * in case the last client left us in a bad state).
2180 *
2181 * Additionally, in the non-mode setting case, we'll tear down the AGP
2182 * and DMA structures, since the kernel won't be using them, and clea
2183 * up any GEM state.
2184 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002185void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002187 drm_i915_private_t *dev_priv = dev->dev_private;
2188
Jesse Barnes79e53942008-11-07 14:24:08 -08002189 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airlie785b93e2009-08-28 15:46:53 +10002190 drm_fb_helper_restore();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10002191 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10002192 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08002193 }
Dave Airlie144a75f2008-03-30 07:53:58 +10002194
Eric Anholt673a3942008-07-30 12:06:12 -07002195 i915_gem_lastclose(dev);
2196
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002197 if (dev_priv->agp_heap)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002198 i915_mem_takedown(&(dev_priv->agp_heap));
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002199
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002200 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Eric Anholt6c340ea2007-08-25 20:23:09 +10002203void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002205 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtb9624422009-06-03 07:27:35 +00002206 i915_gem_release(dev, file_priv);
Jesse Barnes79e53942008-11-07 14:24:08 -08002207 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2208 i915_mem_release(dev, file_priv, dev_priv->agp_heap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002211void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07002212{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002213 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002214
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002215 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07002216}
2217
Eric Anholtc153f452007-09-03 12:06:45 +10002218struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10002219 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2220 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
2221 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
2222 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
2223 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
2224 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
2225 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH),
2226 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2227 DRM_IOCTL_DEF_DRV(I915_ALLOC, i915_mem_alloc, DRM_AUTH),
2228 DRM_IOCTL_DEF_DRV(I915_FREE, i915_mem_free, DRM_AUTH),
2229 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, i915_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2230 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
2231 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, i915_mem_destroy_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2232 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, i915_vblank_pipe_set, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2233 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
2234 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
2235 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2236 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2237 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
2238 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
2239 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2240 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2241 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
2242 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
2243 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2244 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2245 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
2246 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
2247 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
2248 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
2249 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
2250 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
2251 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
2252 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
2253 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
2254 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
2255 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
2256 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
2257 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2258 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10002259};
2260
2261int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10002262
2263/**
2264 * Determine if the device really is AGP or not.
2265 *
2266 * All Intel graphics chipsets are treated as AGP, even if they are really
2267 * PCI-e.
2268 *
2269 * \param dev The device to be tested.
2270 *
2271 * \returns
2272 * A value of 1 is always retured to indictate every i9x5 is AGP.
2273 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002274int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10002275{
2276 return 1;
2277}