blob: 5fde7ed86b1736dd7683bd7bddd5d3908fcf2993 [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
Joe Perchesa70491c2012-03-18 13:00:11 -070029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "drmP.h"
32#include "drm.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include "drm_crtc_helper.h"
Dave Airlie785b93e2009-08-28 15:46:53 +100034#include "drm_fb_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "intel_drv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "i915_drm.h"
37#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010038#include "i915_trace.h"
Eric Anholt63ee41d2010-12-20 18:40:06 -080039#include "../../../platform/x86/intel_ips.h"
Jordan Crousedcdb1672010-05-27 13:40:25 -060040#include <linux/pci.h>
Dave Airlie28d52042009-09-21 14:33:58 +100041#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080042#include <linux/acpi.h>
43#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100044#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040046#include <linux/module.h>
Chris Wilson44834a62010-08-19 16:09:23 +010047#include <acpi/video.h>
Adam Jackson9e984bc12012-03-14 11:22:11 -040048#include <asm/pat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Chris Wilson4cbf74c2011-02-25 22:26:23 +000050static void i915_write_hws_pga(struct drm_device *dev)
51{
52 drm_i915_private_t *dev_priv = dev->dev_private;
53 u32 addr;
54
55 addr = dev_priv->status_page_dmah->busaddr;
56 if (INTEL_INFO(dev)->gen >= 4)
57 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
58 I915_WRITE(HWS_PGA, addr);
59}
60
Keith Packard398c9cb2008-07-30 13:03:43 -070061/**
62 * Sets up the hardware status page for devices that need a physical address
63 * in the register.
64 */
Eric Anholt3043c602008-10-02 12:24:47 -070065static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070066{
67 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +000068
Keith Packard398c9cb2008-07-30 13:03:43 -070069 /* Program Hardware Status Page */
70 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +080071 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -070072
73 if (!dev_priv->status_page_dmah) {
74 DRM_ERROR("Can not allocate hardware status page\n");
75 return -ENOMEM;
76 }
Keith Packard398c9cb2008-07-30 13:03:43 -070077
Keith Packardf3234702011-07-22 10:44:39 -070078 memset_io((void __force __iomem *)dev_priv->status_page_dmah->vaddr,
79 0, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -070080
Chris Wilson4cbf74c2011-02-25 22:26:23 +000081 i915_write_hws_pga(dev);
Zhenyu Wang9b974cc2010-01-05 11:25:06 +080082
Zhao Yakui8a4c47f2009-07-20 13:48:04 +080083 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -070084 return 0;
85}
86
87/**
88 * Frees the hardware status page, whether it's a physical address or a virtual
89 * address set up by the X Server.
90 */
Eric Anholt3043c602008-10-02 12:24:47 -070091static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -070092{
93 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +000094 struct intel_ring_buffer *ring = LP_RING(dev_priv);
95
Keith Packard398c9cb2008-07-30 13:03:43 -070096 if (dev_priv->status_page_dmah) {
97 drm_pci_free(dev, dev_priv->status_page_dmah);
98 dev_priv->status_page_dmah = NULL;
99 }
100
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000101 if (ring->status_page.gfx_addr) {
102 ring->status_page.gfx_addr = 0;
Keith Packard398c9cb2008-07-30 13:03:43 -0700103 drm_core_ioremapfree(&dev_priv->hws_map, dev);
104 }
105
106 /* Need to rewrite hardware status page */
107 I915_WRITE(HWS_PGA, 0x1ffff000);
108}
109
Dave Airlie84b1fd12007-07-11 15:53:27 +1000110void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000113 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000114 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Jesse Barnes79e53942008-11-07 14:24:08 -0800116 /*
117 * We should never lose context on the ring with modesetting
118 * as we don't expose it to userspace
119 */
120 if (drm_core_check_feature(dev, DRIVER_MODESET))
121 return;
122
Chris Wilson8168bd42010-11-11 17:54:52 +0000123 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
124 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 ring->space = ring->head - (ring->tail + 8);
126 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800127 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Dave Airlie7c1c2872008-11-28 14:22:24 +1000129 if (!dev->primary->master)
130 return;
131
132 master_priv = dev->primary->master->driver_priv;
133 if (ring->head == ring->tail && master_priv->sarea_priv)
134 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Dave Airlie84b1fd12007-07-11 15:53:27 +1000137static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000139 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000140 int i;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* Make sure interrupts are disabled here because the uninstall ioctl
143 * may not have been called from userspace and after dev_private
144 * is freed, it's too late.
145 */
Eric Anholted4cb412008-07-29 12:10:39 -0700146 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000147 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200149 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000150 for (i = 0; i < I915_NUM_RINGS; i++)
151 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200152 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Keith Packard398c9cb2008-07-30 13:03:43 -0700154 /* Clear the HWS virtual address at teardown */
155 if (I915_NEED_GFX_HWS(dev))
156 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return 0;
159}
160
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000161static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000163 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000164 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000165 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Dave Airlie3a03ac12009-01-11 09:03:49 +1000167 master_priv->sarea = drm_getsarea(dev);
168 if (master_priv->sarea) {
169 master_priv->sarea_priv = (drm_i915_sarea_t *)
170 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
171 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800172 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000173 }
174
Eric Anholt673a3942008-07-30 12:06:12 -0700175 if (init->ring_size != 0) {
Chris Wilsone8616b62011-01-20 09:57:11 +0000176 if (LP_RING(dev_priv)->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700177 i915_dma_cleanup(dev);
178 DRM_ERROR("Client tried to initialize ringbuffer in "
179 "GEM mode\n");
180 return -EINVAL;
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Chris Wilsone8616b62011-01-20 09:57:11 +0000183 ret = intel_render_ring_init_dri(dev,
184 init->ring_start,
185 init->ring_size);
186 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700187 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000188 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000192 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 dev_priv->back_offset = init->back_offset;
194 dev_priv->front_offset = init->front_offset;
195 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000196 if (master_priv->sarea_priv)
197 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Allow hardware batchbuffers unless told otherwise.
200 */
201 dev_priv->allow_batchbuffer = 1;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return 0;
204}
205
Dave Airlie84b1fd12007-07-11 15:53:27 +1000206static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000209 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800211 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800213 if (ring->map.handle == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 DRM_ERROR("can not ioremap virtual address for"
215 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000216 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
219 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800220 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000222 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800224 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800225 ring->status_page.page_addr);
226 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100227 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000228 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000229 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800230
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800231 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 return 0;
234}
235
Eric Anholtc153f452007-09-03 12:06:45 +1000236static int i915_dma_init(struct drm_device *dev, void *data,
237 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Eric Anholtc153f452007-09-03 12:06:45 +1000239 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 int retcode = 0;
241
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200242 if (drm_core_check_feature(dev, DRIVER_MODESET))
243 return -ENODEV;
244
Eric Anholtc153f452007-09-03 12:06:45 +1000245 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000247 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249 case I915_CLEANUP_DMA:
250 retcode = i915_dma_cleanup(dev);
251 break;
252 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100253 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
255 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000256 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
258 }
259
260 return retcode;
261}
262
263/* Implement basically the same security restrictions as hardware does
264 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
265 *
266 * Most of the calculations below involve calculating the size of a
267 * particular instruction. It's important to get the size right as
268 * that tells us where the next instruction to check is. Any illegal
269 * instruction detected will be given a size of zero, which is a
270 * signal to abort the rest of the buffer.
271 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100272static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 switch (((cmd >> 29) & 0x7)) {
275 case 0x0:
276 switch ((cmd >> 23) & 0x3f) {
277 case 0x0:
278 return 1; /* MI_NOOP */
279 case 0x4:
280 return 1; /* MI_FLUSH */
281 default:
282 return 0; /* disallow everything else */
283 }
284 break;
285 case 0x1:
286 return 0; /* reserved */
287 case 0x2:
288 return (cmd & 0xff) + 2; /* 2d commands */
289 case 0x3:
290 if (((cmd >> 24) & 0x1f) <= 0x18)
291 return 1;
292
293 switch ((cmd >> 24) & 0x1f) {
294 case 0x1c:
295 return 1;
296 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case 0x3:
299 return (cmd & 0x1f) + 2;
300 case 0x4:
301 return (cmd & 0xf) + 2;
302 default:
303 return (cmd & 0xffff) + 2;
304 }
305 case 0x1e:
306 if (cmd & (1 << 23))
307 return (cmd & 0xffff) + 1;
308 else
309 return 1;
310 case 0x1f:
311 if ((cmd & (1 << 23)) == 0) /* inline vertices */
312 return (cmd & 0x1ffff) + 2;
313 else if (cmd & (1 << 17)) /* indirect random */
314 if ((cmd & 0xffff) == 0)
315 return 0; /* unknown length, too hard */
316 else
317 return (((cmd & 0xffff) + 1) / 2) + 1;
318 else
319 return 2; /* indirect sequential */
320 default:
321 return 0;
322 }
323 default:
324 return 0;
325 }
326
327 return 0;
328}
329
Eric Anholt201361a2009-03-11 12:30:04 -0700330static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100333 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000335 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000336 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100339 int sz = validate_cmd(buffer[i]);
340 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000341 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100342 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100345 ret = BEGIN_LP_RING((dwords+1)&~1);
346 if (ret)
347 return ret;
348
349 for (i = 0; i < dwords; i++)
350 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100351 if (dwords & 1)
352 OUT_RING(0);
353
354 ADVANCE_LP_RING();
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357}
358
Eric Anholt673a3942008-07-30 12:06:12 -0700359int
360i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000361 struct drm_clip_rect *box,
362 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100364 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100365 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000367 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
368 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000370 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000371 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100374 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100375 ret = BEGIN_LP_RING(4);
376 if (ret)
377 return ret;
378
Alan Hourihanec29b6692006-08-12 16:29:24 +1000379 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000380 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
381 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000382 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000383 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100384 ret = BEGIN_LP_RING(6);
385 if (ret)
386 return ret;
387
Alan Hourihanec29b6692006-08-12 16:29:24 +1000388 OUT_RING(GFX_OP_DRAWRECT_INFO);
389 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000390 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
391 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000392 OUT_RING(DR4);
393 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000394 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100395 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 return 0;
398}
399
Alan Hourihanec29b6692006-08-12 16:29:24 +1000400/* XXX: Emitting the counter should really be moved to part of the IRQ
401 * emit. For now, do it in both places:
402 */
403
Dave Airlie84b1fd12007-07-11 15:53:27 +1000404static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100405{
406 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000407 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100408
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400409 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000410 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400411 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000412 if (master_priv->sarea_priv)
413 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100414
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100415 if (BEGIN_LP_RING(4) == 0) {
416 OUT_RING(MI_STORE_DWORD_INDEX);
417 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
418 OUT_RING(dev_priv->counter);
419 OUT_RING(0);
420 ADVANCE_LP_RING();
421 }
Dave Airliede227f52006-01-25 15:31:43 +1100422}
423
Dave Airlie84b1fd12007-07-11 15:53:27 +1000424static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700425 drm_i915_cmdbuffer_t *cmd,
426 struct drm_clip_rect *cliprects,
427 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 int nbox = cmd->num_cliprects;
430 int i = 0, count, ret;
431
432 if (cmd->sz & 0x3) {
433 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
437 i915_kernel_lost_context(dev);
438
439 count = nbox ? nbox : 1;
440
441 for (i = 0; i < count; i++) {
442 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000443 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 cmd->DR1, cmd->DR4);
445 if (ret)
446 return ret;
447 }
448
Eric Anholt201361a2009-03-11 12:30:04 -0700449 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (ret)
451 return ret;
452 }
453
Dave Airliede227f52006-01-25 15:31:43 +1100454 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
456}
457
Dave Airlie84b1fd12007-07-11 15:53:27 +1000458static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700459 drm_i915_batchbuffer_t * batch,
460 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100462 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100464 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if ((batch->start | batch->used) & 0x7) {
467 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
471 i915_kernel_lost_context(dev);
472
473 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 for (i = 0; i < count; i++) {
475 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000476 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100477 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (ret)
479 return ret;
480 }
481
Keith Packard0790d5e2008-07-30 12:28:47 -0700482 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100483 ret = BEGIN_LP_RING(2);
484 if (ret)
485 return ret;
486
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100487 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000488 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
489 OUT_RING(batch->start);
490 } else {
491 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
492 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100495 ret = BEGIN_LP_RING(4);
496 if (ret)
497 return ret;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 OUT_RING(MI_BATCH_BUFFER);
500 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
501 OUT_RING(batch->start + batch->used - 4);
502 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100504 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506
Zou Nan hai1cafd342010-06-25 13:40:24 +0800507
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100508 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100509 if (BEGIN_LP_RING(2) == 0) {
510 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
511 OUT_RING(MI_NOOP);
512 ADVANCE_LP_RING();
513 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100516 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return 0;
518}
519
Dave Airlieaf6061a2008-05-07 12:15:39 +1000520static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000523 struct drm_i915_master_private *master_priv =
524 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100525 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Dave Airlie7c1c2872008-11-28 14:22:24 +1000527 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400528 return -EINVAL;
529
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800530 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800531 __func__,
532 dev_priv->current_page,
533 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Dave Airlieaf6061a2008-05-07 12:15:39 +1000535 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100537 ret = BEGIN_LP_RING(10);
538 if (ret)
539 return ret;
540
Jesse Barnes585fb112008-07-29 11:54:06 -0700541 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000542 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Dave Airlieaf6061a2008-05-07 12:15:39 +1000544 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
545 OUT_RING(0);
546 if (dev_priv->current_page == 0) {
547 OUT_RING(dev_priv->back_offset);
548 dev_priv->current_page = 1;
549 } else {
550 OUT_RING(dev_priv->front_offset);
551 dev_priv->current_page = 0;
552 }
553 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000554
Dave Airlieaf6061a2008-05-07 12:15:39 +1000555 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
556 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100557
Dave Airlieaf6061a2008-05-07 12:15:39 +1000558 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000559
Dave Airlie7c1c2872008-11-28 14:22:24 +1000560 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000561
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100562 if (BEGIN_LP_RING(4) == 0) {
563 OUT_RING(MI_STORE_DWORD_INDEX);
564 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
565 OUT_RING(dev_priv->counter);
566 OUT_RING(0);
567 ADVANCE_LP_RING();
568 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000569
Dave Airlie7c1c2872008-11-28 14:22:24 +1000570 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000574static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000576 struct intel_ring_buffer *ring = LP_RING(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 i915_kernel_lost_context(dev);
Ben Widawsky96f298a2011-03-19 18:14:27 -0700579 return intel_wait_ring_idle(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Eric Anholtc153f452007-09-03 12:06:45 +1000582static int i915_flush_ioctl(struct drm_device *dev, void *data,
583 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Eric Anholt546b0972008-09-01 16:45:29 -0700585 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200587 if (drm_core_check_feature(dev, DRIVER_MODESET))
588 return -ENODEV;
589
Eric Anholt546b0972008-09-01 16:45:29 -0700590 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
591
592 mutex_lock(&dev->struct_mutex);
593 ret = i915_quiescent(dev);
594 mutex_unlock(&dev->struct_mutex);
595
596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Eric Anholtc153f452007-09-03 12:06:45 +1000599static int i915_batchbuffer(struct drm_device *dev, void *data,
600 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000603 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000605 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000606 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700608 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200610 if (drm_core_check_feature(dev, DRIVER_MODESET))
611 return -ENODEV;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!dev_priv->allow_batchbuffer) {
614 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000615 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800618 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800619 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Eric Anholt546b0972008-09-01 16:45:29 -0700621 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Eric Anholt201361a2009-03-11 12:30:04 -0700623 if (batch->num_cliprects < 0)
624 return -EINVAL;
625
626 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700627 cliprects = kcalloc(batch->num_cliprects,
628 sizeof(struct drm_clip_rect),
629 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700630 if (cliprects == NULL)
631 return -ENOMEM;
632
633 ret = copy_from_user(cliprects, batch->cliprects,
634 batch->num_cliprects *
635 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200636 if (ret != 0) {
637 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700638 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200639 }
Eric Anholt201361a2009-03-11 12:30:04 -0700640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Eric Anholt546b0972008-09-01 16:45:29 -0700642 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700643 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700644 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400646 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000647 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700648
649fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700650 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return ret;
653}
654
Eric Anholtc153f452007-09-03 12:06:45 +1000655static int i915_cmdbuffer(struct drm_device *dev, void *data,
656 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000659 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000661 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000662 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700663 struct drm_clip_rect *cliprects = NULL;
664 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int ret;
666
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800667 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800668 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200670 if (drm_core_check_feature(dev, DRIVER_MODESET))
671 return -ENODEV;
672
Eric Anholt546b0972008-09-01 16:45:29 -0700673 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Eric Anholt201361a2009-03-11 12:30:04 -0700675 if (cmdbuf->num_cliprects < 0)
676 return -EINVAL;
677
Eric Anholt9a298b22009-03-24 12:23:04 -0700678 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700679 if (batch_data == NULL)
680 return -ENOMEM;
681
682 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200683 if (ret != 0) {
684 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700685 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200686 }
Eric Anholt201361a2009-03-11 12:30:04 -0700687
688 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700689 cliprects = kcalloc(cmdbuf->num_cliprects,
690 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000691 if (cliprects == NULL) {
692 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700693 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000694 }
Eric Anholt201361a2009-03-11 12:30:04 -0700695
696 ret = copy_from_user(cliprects, cmdbuf->cliprects,
697 cmdbuf->num_cliprects *
698 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200699 if (ret != 0) {
700 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700701 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Eric Anholt546b0972008-09-01 16:45:29 -0700705 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700706 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700707 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (ret) {
709 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000710 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400713 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000714 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700715
Eric Anholt201361a2009-03-11 12:30:04 -0700716fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700717 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000718fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700719 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700720
721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200724static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
725 struct drm_file *file_priv)
726{
727 drm_i915_private_t *dev_priv = dev->dev_private;
728 drm_i915_vblank_pipe_t *pipe = data;
729
730 if (drm_core_check_feature(dev, DRIVER_MODESET))
731 return -ENODEV;
732
733 if (!dev_priv) {
734 DRM_ERROR("called with no initialization\n");
735 return -EINVAL;
736 }
737
738 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
739
740 return 0;
741}
742
743/**
744 * Schedule buffer swap at given vertical blank.
745 */
746static int i915_vblank_swap(struct drm_device *dev, void *data,
747 struct drm_file *file_priv)
748{
749 /* The delayed swap mechanism was fundamentally racy, and has been
750 * removed. The model was that the client requested a delayed flip/swap
751 * from the kernel, then waited for vblank before continuing to perform
752 * rendering. The problem was that the kernel might wake the client
753 * up before it dispatched the vblank swap (since the lock has to be
754 * held while touching the ringbuffer), in which case the client would
755 * clear and start the next frame before the swap occurred, and
756 * flicker would occur in addition to likely missing the vblank.
757 *
758 * In the absence of this ioctl, userland falls back to a correct path
759 * of waiting for a vblank, then dispatching the swap on its own.
760 * Context switching to userland and back is plenty fast enough for
761 * meeting the requirements of vblank swapping.
762 */
763 return -EINVAL;
764}
765
Eric Anholtc153f452007-09-03 12:06:45 +1000766static int i915_flip_bufs(struct drm_device *dev, void *data,
767 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Eric Anholt546b0972008-09-01 16:45:29 -0700769 int ret;
770
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200771 if (drm_core_check_feature(dev, DRIVER_MODESET))
772 return -ENODEV;
773
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800774 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Eric Anholt546b0972008-09-01 16:45:29 -0700776 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Eric Anholt546b0972008-09-01 16:45:29 -0700778 mutex_lock(&dev->struct_mutex);
779 ret = i915_dispatch_flip(dev);
780 mutex_unlock(&dev->struct_mutex);
781
782 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Eric Anholtc153f452007-09-03 12:06:45 +1000785static int i915_getparam(struct drm_device *dev, void *data,
786 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000789 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int value;
791
792 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000793 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000794 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Eric Anholtc153f452007-09-03 12:06:45 +1000797 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700799 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 break;
801 case I915_PARAM_ALLOW_BATCHBUFFER:
802 value = dev_priv->allow_batchbuffer ? 1 : 0;
803 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100804 case I915_PARAM_LAST_DISPATCH:
805 value = READ_BREADCRUMB(dev_priv);
806 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400807 case I915_PARAM_CHIPSET_ID:
808 value = dev->pci_device;
809 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700810 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200811 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700812 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800813 case I915_PARAM_NUM_FENCES_AVAIL:
814 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
815 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200816 case I915_PARAM_HAS_OVERLAY:
817 value = dev_priv->overlay ? 1 : 0;
818 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800819 case I915_PARAM_HAS_PAGEFLIPPING:
820 value = 1;
821 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500822 case I915_PARAM_HAS_EXECBUF2:
823 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200824 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500825 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800826 case I915_PARAM_HAS_BSD:
827 value = HAS_BSD(dev);
828 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100829 case I915_PARAM_HAS_BLT:
830 value = HAS_BLT(dev);
831 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100832 case I915_PARAM_HAS_RELAXED_FENCING:
833 value = 1;
834 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100835 case I915_PARAM_HAS_COHERENT_RINGS:
836 value = 1;
837 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000838 case I915_PARAM_HAS_EXEC_CONSTANTS:
839 value = INTEL_INFO(dev)->gen >= 4;
840 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000841 case I915_PARAM_HAS_RELAXED_DELTA:
842 value = 1;
843 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800844 case I915_PARAM_HAS_GEN7_SOL_RESET:
845 value = 1;
846 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200847 case I915_PARAM_HAS_LLC:
848 value = HAS_LLC(dev);
849 break;
Daniel Vetter777ee962012-02-15 23:50:25 +0100850 case I915_PARAM_HAS_ALIASING_PPGTT:
851 value = dev_priv->mm.aliasing_ppgtt ? 1 : 0;
852 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800854 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -0500855 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000856 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
Eric Anholtc153f452007-09-03 12:06:45 +1000859 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000861 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
864 return 0;
865}
866
Eric Anholtc153f452007-09-03 12:06:45 +1000867static int i915_setparam(struct drm_device *dev, void *data,
868 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000871 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000874 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Eric Anholtc153f452007-09-03 12:06:45 +1000878 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
881 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Eric Anholtc153f452007-09-03 12:06:45 +1000882 dev_priv->tex_lru_log_granularity = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Eric Anholtc153f452007-09-03 12:06:45 +1000885 dev_priv->allow_batchbuffer = param->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800887 case I915_SETPARAM_NUM_USED_FENCES:
888 if (param->value > dev_priv->num_fence_regs ||
889 param->value < 0)
890 return -EINVAL;
891 /* Userspace can use first N regs */
892 dev_priv->fence_reg_start = param->value;
893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800895 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800896 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +1000897 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
900 return 0;
901}
902
Eric Anholtc153f452007-09-03 12:06:45 +1000903static int i915_set_status_page(struct drm_device *dev, void *data,
904 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000905{
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000906 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000907 drm_i915_hws_addr_t *hws = data;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000908 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000909
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200910 if (drm_core_check_feature(dev, DRIVER_MODESET))
911 return -ENODEV;
912
Zhenyu Wangb39d50e2008-02-19 20:59:09 +1000913 if (!I915_NEED_GFX_HWS(dev))
914 return -EINVAL;
915
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000916 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000917 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000918 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000919 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000920
Jesse Barnes79e53942008-11-07 14:24:08 -0800921 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
922 WARN(1, "tried to set status page when mode setting active\n");
923 return 0;
924 }
925
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800926 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000927
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800928 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +1000929
Eric Anholt8b409582007-11-22 16:40:37 +1000930 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000931 dev_priv->hws_map.size = 4*1024;
932 dev_priv->hws_map.type = 0;
933 dev_priv->hws_map.flags = 0;
934 dev_priv->hws_map.mtrr = 0;
935
Dave Airliedd0910b2009-02-25 14:49:21 +1000936 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000937 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000938 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -0700939 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000940 DRM_ERROR("can not ioremap virtual address for"
941 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000942 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000943 }
Chris Wilson311bd682011-01-13 19:06:50 +0000944 ring->status_page.page_addr =
945 (void __force __iomem *)dev_priv->hws_map.handle;
946 memset_io(ring->status_page.page_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800947 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000948
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800949 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -0700950 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800951 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -0700952 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000953 return 0;
954}
955
Dave Airlieec2a4c32009-08-04 11:43:41 +1000956static int i915_get_bridge_dev(struct drm_device *dev)
957{
958 struct drm_i915_private *dev_priv = dev->dev_private;
959
Akshay Joshi0206e352011-08-16 15:34:10 -0400960 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +1000961 if (!dev_priv->bridge_dev) {
962 DRM_ERROR("bridge device not found\n");
963 return -1;
964 }
965 return 0;
966}
967
Zhenyu Wangc48044112009-12-17 14:48:43 +0800968#define MCHBAR_I915 0x44
969#define MCHBAR_I965 0x48
970#define MCHBAR_SIZE (4*4096)
971
972#define DEVEN_REG 0x54
973#define DEVEN_MCHBAR_EN (1 << 28)
974
975/* Allocate space for the MCH regs if needed, return nonzero on error */
976static int
977intel_alloc_mchbar_resource(struct drm_device *dev)
978{
979 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100980 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800981 u32 temp_lo, temp_hi = 0;
982 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +0100983 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800984
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100985 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +0800986 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
987 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
988 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
989
990 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
991#ifdef CONFIG_PNP
992 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +0100993 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
994 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +0800995#endif
996
997 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +0100998 dev_priv->mch_res.name = "i915 MCHBAR";
999 dev_priv->mch_res.flags = IORESOURCE_MEM;
1000 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1001 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001002 MCHBAR_SIZE, MCHBAR_SIZE,
1003 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001004 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001005 dev_priv->bridge_dev);
1006 if (ret) {
1007 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1008 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001009 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001010 }
1011
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001012 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001013 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1014 upper_32_bits(dev_priv->mch_res.start));
1015
1016 pci_write_config_dword(dev_priv->bridge_dev, reg,
1017 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001018 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001019}
1020
1021/* Setup MCHBAR if possible, return true if we should disable it again */
1022static void
1023intel_setup_mchbar(struct drm_device *dev)
1024{
1025 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001026 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001027 u32 temp;
1028 bool enabled;
1029
1030 dev_priv->mchbar_need_disable = false;
1031
1032 if (IS_I915G(dev) || IS_I915GM(dev)) {
1033 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1034 enabled = !!(temp & DEVEN_MCHBAR_EN);
1035 } else {
1036 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1037 enabled = temp & 1;
1038 }
1039
1040 /* If it's already enabled, don't have to do anything */
1041 if (enabled)
1042 return;
1043
1044 if (intel_alloc_mchbar_resource(dev))
1045 return;
1046
1047 dev_priv->mchbar_need_disable = true;
1048
1049 /* Space is allocated or reserved, so enable it. */
1050 if (IS_I915G(dev) || IS_I915GM(dev)) {
1051 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1052 temp | DEVEN_MCHBAR_EN);
1053 } else {
1054 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1055 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1056 }
1057}
1058
1059static void
1060intel_teardown_mchbar(struct drm_device *dev)
1061{
1062 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001063 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001064 u32 temp;
1065
1066 if (dev_priv->mchbar_need_disable) {
1067 if (IS_I915G(dev) || IS_I915GM(dev)) {
1068 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1069 temp &= ~DEVEN_MCHBAR_EN;
1070 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1071 } else {
1072 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1073 temp &= ~1;
1074 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1075 }
1076 }
1077
1078 if (dev_priv->mch_res.start)
1079 release_resource(&dev_priv->mch_res);
1080}
1081
Dave Airlie28d52042009-09-21 14:33:58 +10001082/* true = enable decode, false = disable decoder */
1083static unsigned int i915_vga_set_decode(void *cookie, bool state)
1084{
1085 struct drm_device *dev = cookie;
1086
1087 intel_modeset_vga_set_state(dev, state);
1088 if (state)
1089 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1090 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1091 else
1092 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1093}
1094
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001095static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1096{
1097 struct drm_device *dev = pci_get_drvdata(pdev);
1098 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1099 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001100 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001101 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001102 /* i915 resume handler doesn't set to D0 */
1103 pci_set_power_state(dev->pdev, PCI_D0);
1104 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001105 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001106 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001107 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001108 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001109 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001110 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001111 }
1112}
1113
1114static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1115{
1116 struct drm_device *dev = pci_get_drvdata(pdev);
1117 bool can_switch;
1118
1119 spin_lock(&dev->count_lock);
1120 can_switch = (dev->open_count == 0);
1121 spin_unlock(&dev->count_lock);
1122 return can_switch;
1123}
1124
Chris Wilson2c7111d2011-03-29 10:40:27 +01001125static int i915_load_modeset_init(struct drm_device *dev)
1126{
1127 struct drm_i915_private *dev_priv = dev->dev_private;
1128 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001129
Bryan Freed6d139a82010-10-14 09:14:51 +01001130 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001131 if (ret)
1132 DRM_INFO("failed to find VBIOS tables\n");
1133
Chris Wilson934f9922011-01-20 13:09:12 +00001134 /* If we have > 1 VGA cards, then we need to arbitrate access
1135 * to the common VGA resources.
1136 *
1137 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1138 * then we do not take part in VGA arbitration and the
1139 * vga_client_register() fails with -ENODEV.
1140 */
Dave Airlie28d52042009-09-21 14:33:58 +10001141 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
Chris Wilson934f9922011-01-20 13:09:12 +00001142 if (ret && ret != -ENODEV)
Chris Wilson2c7111d2011-03-29 10:40:27 +01001143 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001144
Jesse Barnes723bfd72010-10-07 16:01:13 -07001145 intel_register_dsm_handler();
1146
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001147 ret = vga_switcheroo_register_client(dev->pdev,
1148 i915_switcheroo_set_state,
Dave Airlie8d608aa2010-12-07 08:57:57 +10001149 NULL,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001150 i915_switcheroo_can_switch);
1151 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001152 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001153
Chris Wilson9797fbf2012-04-24 15:47:39 +01001154 /* Initialise stolen first so that we may reserve preallocated
1155 * objects for the BIOS to KMS transition.
1156 */
1157 ret = i915_gem_init_stolen(dev);
1158 if (ret)
1159 goto cleanup_vga_switcheroo;
1160
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001161 intel_modeset_init(dev);
1162
Chris Wilson1070a422012-04-24 15:47:41 +01001163 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001164 if (ret)
Chris Wilson9797fbf2012-04-24 15:47:39 +01001165 goto cleanup_gem_stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -08001166
Chris Wilson2c7111d2011-03-29 10:40:27 +01001167 intel_modeset_gem_init(dev);
1168
1169 ret = drm_irq_install(dev);
1170 if (ret)
1171 goto cleanup_gem;
1172
Jesse Barnes79e53942008-11-07 14:24:08 -08001173 /* Always safe in the mode setting case. */
1174 /* FIXME: do pre/post-mode set stuff in core KMS code */
1175 dev->vblank_disable_allowed = 1;
1176
Chris Wilson5a793952010-06-06 10:50:03 +01001177 ret = intel_fbdev_init(dev);
1178 if (ret)
1179 goto cleanup_irq;
1180
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001181 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001182
1183 /* We're off and running w/KMS */
1184 dev_priv->mm.suspended = 0;
1185
Jesse Barnes79e53942008-11-07 14:24:08 -08001186 return 0;
1187
Chris Wilson5a793952010-06-06 10:50:03 +01001188cleanup_irq:
1189 drm_irq_uninstall(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001190cleanup_gem:
1191 mutex_lock(&dev->struct_mutex);
1192 i915_gem_cleanup_ringbuffer(dev);
1193 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001194 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001195cleanup_gem_stolen:
1196 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001197cleanup_vga_switcheroo:
1198 vga_switcheroo_unregister_client(dev->pdev);
1199cleanup_vga_client:
1200 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001201out:
1202 return ret;
1203}
1204
Dave Airlie7c1c2872008-11-28 14:22:24 +10001205int i915_master_create(struct drm_device *dev, struct drm_master *master)
1206{
1207 struct drm_i915_master_private *master_priv;
1208
Eric Anholt9a298b22009-03-24 12:23:04 -07001209 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001210 if (!master_priv)
1211 return -ENOMEM;
1212
1213 master->driver_priv = master_priv;
1214 return 0;
1215}
1216
1217void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1218{
1219 struct drm_i915_master_private *master_priv = master->driver_priv;
1220
1221 if (!master_priv)
1222 return;
1223
Eric Anholt9a298b22009-03-24 12:23:04 -07001224 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001225
1226 master->driver_priv = NULL;
1227}
1228
Jesse Barnes7648fa92010-05-20 14:28:11 -07001229static void i915_pineview_get_mem_freq(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08001230{
1231 drm_i915_private_t *dev_priv = dev->dev_private;
1232 u32 tmp;
1233
Shaohua Li7662c8b2009-06-26 11:23:55 +08001234 tmp = I915_READ(CLKCFG);
1235
1236 switch (tmp & CLKCFG_FSB_MASK) {
1237 case CLKCFG_FSB_533:
1238 dev_priv->fsb_freq = 533; /* 133*4 */
1239 break;
1240 case CLKCFG_FSB_800:
1241 dev_priv->fsb_freq = 800; /* 200*4 */
1242 break;
1243 case CLKCFG_FSB_667:
1244 dev_priv->fsb_freq = 667; /* 167*4 */
1245 break;
1246 case CLKCFG_FSB_400:
1247 dev_priv->fsb_freq = 400; /* 100*4 */
1248 break;
1249 }
1250
1251 switch (tmp & CLKCFG_MEM_MASK) {
1252 case CLKCFG_MEM_533:
1253 dev_priv->mem_freq = 533;
1254 break;
1255 case CLKCFG_MEM_667:
1256 dev_priv->mem_freq = 667;
1257 break;
1258 case CLKCFG_MEM_800:
1259 dev_priv->mem_freq = 800;
1260 break;
1261 }
Li Peng95534262010-05-18 18:58:44 +08001262
1263 /* detect pineview DDR3 setting */
1264 tmp = I915_READ(CSHRDDR3CTL);
1265 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
Shaohua Li7662c8b2009-06-26 11:23:55 +08001266}
1267
Jesse Barnes7648fa92010-05-20 14:28:11 -07001268static void i915_ironlake_get_mem_freq(struct drm_device *dev)
1269{
1270 drm_i915_private_t *dev_priv = dev->dev_private;
1271 u16 ddrpll, csipll;
1272
1273 ddrpll = I915_READ16(DDRMPLL1);
1274 csipll = I915_READ16(CSIPLL0);
1275
1276 switch (ddrpll & 0xff) {
1277 case 0xc:
1278 dev_priv->mem_freq = 800;
1279 break;
1280 case 0x10:
1281 dev_priv->mem_freq = 1066;
1282 break;
1283 case 0x14:
1284 dev_priv->mem_freq = 1333;
1285 break;
1286 case 0x18:
1287 dev_priv->mem_freq = 1600;
1288 break;
1289 default:
1290 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
1291 ddrpll & 0xff);
1292 dev_priv->mem_freq = 0;
1293 break;
1294 }
1295
1296 dev_priv->r_t = dev_priv->mem_freq;
1297
1298 switch (csipll & 0x3ff) {
1299 case 0x00c:
1300 dev_priv->fsb_freq = 3200;
1301 break;
1302 case 0x00e:
1303 dev_priv->fsb_freq = 3733;
1304 break;
1305 case 0x010:
1306 dev_priv->fsb_freq = 4266;
1307 break;
1308 case 0x012:
1309 dev_priv->fsb_freq = 4800;
1310 break;
1311 case 0x014:
1312 dev_priv->fsb_freq = 5333;
1313 break;
1314 case 0x016:
1315 dev_priv->fsb_freq = 5866;
1316 break;
1317 case 0x018:
1318 dev_priv->fsb_freq = 6400;
1319 break;
1320 default:
1321 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
1322 csipll & 0x3ff);
1323 dev_priv->fsb_freq = 0;
1324 break;
1325 }
1326
1327 if (dev_priv->fsb_freq == 3200) {
1328 dev_priv->c_m = 0;
1329 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
1330 dev_priv->c_m = 1;
1331 } else {
1332 dev_priv->c_m = 2;
1333 }
1334}
1335
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001336static const struct cparams {
1337 u16 i;
1338 u16 t;
1339 u16 m;
1340 u16 c;
1341} cparams[] = {
Jesse Barnes7648fa92010-05-20 14:28:11 -07001342 { 1, 1333, 301, 28664 },
1343 { 1, 1066, 294, 24460 },
1344 { 1, 800, 294, 25192 },
1345 { 0, 1333, 276, 27605 },
1346 { 0, 1066, 276, 27605 },
1347 { 0, 800, 231, 23784 },
1348};
1349
1350unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
1351{
1352 u64 total_count, diff, ret;
1353 u32 count1, count2, count3, m = 0, c = 0;
1354 unsigned long now = jiffies_to_msecs(jiffies), diff1;
1355 int i;
1356
1357 diff1 = now - dev_priv->last_time1;
1358
Eugeni Dodonov4ed0b572011-11-10 13:55:15 -02001359 /* Prevent division-by-zero if we are asking too fast.
1360 * Also, we don't get interesting results if we are polling
1361 * faster than once in 10ms, so just return the saved value
1362 * in such cases.
1363 */
1364 if (diff1 <= 10)
1365 return dev_priv->chipset_power;
1366
Jesse Barnes7648fa92010-05-20 14:28:11 -07001367 count1 = I915_READ(DMIEC);
1368 count2 = I915_READ(DDREC);
1369 count3 = I915_READ(CSIEC);
1370
1371 total_count = count1 + count2 + count3;
1372
1373 /* FIXME: handle per-counter overflow */
1374 if (total_count < dev_priv->last_count1) {
1375 diff = ~0UL - dev_priv->last_count1;
1376 diff += total_count;
1377 } else {
1378 diff = total_count - dev_priv->last_count1;
1379 }
1380
1381 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
1382 if (cparams[i].i == dev_priv->c_m &&
1383 cparams[i].t == dev_priv->r_t) {
1384 m = cparams[i].m;
1385 c = cparams[i].c;
1386 break;
1387 }
1388 }
1389
Jesse Barnesd270ae32010-09-27 10:35:44 -07001390 diff = div_u64(diff, diff1);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001391 ret = ((m * diff) + c);
Jesse Barnesd270ae32010-09-27 10:35:44 -07001392 ret = div_u64(ret, 10);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001393
1394 dev_priv->last_count1 = total_count;
1395 dev_priv->last_time1 = now;
1396
Eugeni Dodonov4ed0b572011-11-10 13:55:15 -02001397 dev_priv->chipset_power = ret;
1398
Jesse Barnes7648fa92010-05-20 14:28:11 -07001399 return ret;
1400}
1401
1402unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
1403{
1404 unsigned long m, x, b;
1405 u32 tsfs;
1406
1407 tsfs = I915_READ(TSFS);
1408
1409 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
1410 x = I915_READ8(TR1);
1411
1412 b = tsfs & TSFS_INTR_MASK;
1413
1414 return ((m * x) / 127) - b;
1415}
1416
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001417static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Jesse Barnes7648fa92010-05-20 14:28:11 -07001418{
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001419 static const struct v_table {
1420 u16 vd; /* in .1 mil */
1421 u16 vm; /* in .1 mil */
1422 } v_table[] = {
1423 { 0, 0, },
1424 { 375, 0, },
1425 { 500, 0, },
1426 { 625, 0, },
1427 { 750, 0, },
1428 { 875, 0, },
1429 { 1000, 0, },
1430 { 1125, 0, },
1431 { 4125, 3000, },
1432 { 4125, 3000, },
1433 { 4125, 3000, },
1434 { 4125, 3000, },
1435 { 4125, 3000, },
1436 { 4125, 3000, },
1437 { 4125, 3000, },
1438 { 4125, 3000, },
1439 { 4125, 3000, },
1440 { 4125, 3000, },
1441 { 4125, 3000, },
1442 { 4125, 3000, },
1443 { 4125, 3000, },
1444 { 4125, 3000, },
1445 { 4125, 3000, },
1446 { 4125, 3000, },
1447 { 4125, 3000, },
1448 { 4125, 3000, },
1449 { 4125, 3000, },
1450 { 4125, 3000, },
1451 { 4125, 3000, },
1452 { 4125, 3000, },
1453 { 4125, 3000, },
1454 { 4125, 3000, },
1455 { 4250, 3125, },
1456 { 4375, 3250, },
1457 { 4500, 3375, },
1458 { 4625, 3500, },
1459 { 4750, 3625, },
1460 { 4875, 3750, },
1461 { 5000, 3875, },
1462 { 5125, 4000, },
1463 { 5250, 4125, },
1464 { 5375, 4250, },
1465 { 5500, 4375, },
1466 { 5625, 4500, },
1467 { 5750, 4625, },
1468 { 5875, 4750, },
1469 { 6000, 4875, },
1470 { 6125, 5000, },
1471 { 6250, 5125, },
1472 { 6375, 5250, },
1473 { 6500, 5375, },
1474 { 6625, 5500, },
1475 { 6750, 5625, },
1476 { 6875, 5750, },
1477 { 7000, 5875, },
1478 { 7125, 6000, },
1479 { 7250, 6125, },
1480 { 7375, 6250, },
1481 { 7500, 6375, },
1482 { 7625, 6500, },
1483 { 7750, 6625, },
1484 { 7875, 6750, },
1485 { 8000, 6875, },
1486 { 8125, 7000, },
1487 { 8250, 7125, },
1488 { 8375, 7250, },
1489 { 8500, 7375, },
1490 { 8625, 7500, },
1491 { 8750, 7625, },
1492 { 8875, 7750, },
1493 { 9000, 7875, },
1494 { 9125, 8000, },
1495 { 9250, 8125, },
1496 { 9375, 8250, },
1497 { 9500, 8375, },
1498 { 9625, 8500, },
1499 { 9750, 8625, },
1500 { 9875, 8750, },
1501 { 10000, 8875, },
1502 { 10125, 9000, },
1503 { 10250, 9125, },
1504 { 10375, 9250, },
1505 { 10500, 9375, },
1506 { 10625, 9500, },
1507 { 10750, 9625, },
1508 { 10875, 9750, },
1509 { 11000, 9875, },
1510 { 11125, 10000, },
1511 { 11250, 10125, },
1512 { 11375, 10250, },
1513 { 11500, 10375, },
1514 { 11625, 10500, },
1515 { 11750, 10625, },
1516 { 11875, 10750, },
1517 { 12000, 10875, },
1518 { 12125, 11000, },
1519 { 12250, 11125, },
1520 { 12375, 11250, },
1521 { 12500, 11375, },
1522 { 12625, 11500, },
1523 { 12750, 11625, },
1524 { 12875, 11750, },
1525 { 13000, 11875, },
1526 { 13125, 12000, },
1527 { 13250, 12125, },
1528 { 13375, 12250, },
1529 { 13500, 12375, },
1530 { 13625, 12500, },
1531 { 13750, 12625, },
1532 { 13875, 12750, },
1533 { 14000, 12875, },
1534 { 14125, 13000, },
1535 { 14250, 13125, },
1536 { 14375, 13250, },
1537 { 14500, 13375, },
1538 { 14625, 13500, },
1539 { 14750, 13625, },
1540 { 14875, 13750, },
1541 { 15000, 13875, },
1542 { 15125, 14000, },
1543 { 15250, 14125, },
1544 { 15375, 14250, },
1545 { 15500, 14375, },
1546 { 15625, 14500, },
1547 { 15750, 14625, },
1548 { 15875, 14750, },
1549 { 16000, 14875, },
1550 { 16125, 15000, },
1551 };
1552 if (dev_priv->info->is_mobile)
1553 return v_table[pxvid].vm;
1554 else
1555 return v_table[pxvid].vd;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001556}
1557
1558void i915_update_gfx_val(struct drm_i915_private *dev_priv)
1559{
1560 struct timespec now, diff1;
1561 u64 diff;
1562 unsigned long diffms;
1563 u32 count;
1564
Chris Wilson582be6b2012-04-30 19:35:02 +01001565 if (dev_priv->info->gen != 5)
1566 return;
1567
Jesse Barnes7648fa92010-05-20 14:28:11 -07001568 getrawmonotonic(&now);
1569 diff1 = timespec_sub(now, dev_priv->last_time2);
1570
1571 /* Don't divide by 0 */
1572 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
1573 if (!diffms)
1574 return;
1575
1576 count = I915_READ(GFXEC);
1577
1578 if (count < dev_priv->last_count2) {
1579 diff = ~0UL - dev_priv->last_count2;
1580 diff += count;
1581 } else {
1582 diff = count - dev_priv->last_count2;
1583 }
1584
1585 dev_priv->last_count2 = count;
1586 dev_priv->last_time2 = now;
1587
1588 /* More magic constants... */
1589 diff = diff * 1181;
Jesse Barnesd270ae32010-09-27 10:35:44 -07001590 diff = div_u64(diff, diffms * 10);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001591 dev_priv->gfx_power = diff;
1592}
1593
1594unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
1595{
1596 unsigned long t, corr, state1, corr2, state2;
1597 u32 pxvid, ext_v;
1598
1599 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->cur_delay * 4));
1600 pxvid = (pxvid >> 24) & 0x7f;
1601 ext_v = pvid_to_extvid(dev_priv, pxvid);
1602
1603 state1 = ext_v;
1604
1605 t = i915_mch_val(dev_priv);
1606
1607 /* Revel in the empirically derived constants */
1608
1609 /* Correction factor in 1/100000 units */
1610 if (t > 80)
1611 corr = ((t * 2349) + 135940);
1612 else if (t >= 50)
1613 corr = ((t * 964) + 29317);
1614 else /* < 50 */
1615 corr = ((t * 301) + 1004);
1616
1617 corr = corr * ((150142 * state1) / 10000 - 78642);
1618 corr /= 100000;
1619 corr2 = (corr * dev_priv->corr);
1620
1621 state2 = (corr2 * state1) / 10000;
1622 state2 /= 100; /* convert to mW */
1623
1624 i915_update_gfx_val(dev_priv);
1625
1626 return dev_priv->gfx_power + state2;
1627}
1628
1629/* Global for IPS driver to get at the current i915 device */
1630static struct drm_i915_private *i915_mch_dev;
1631/*
1632 * Lock protecting IPS related data structures
1633 * - i915_mch_dev
1634 * - dev_priv->max_delay
1635 * - dev_priv->min_delay
1636 * - dev_priv->fmax
1637 * - dev_priv->gpu_busy
1638 */
Chris Wilson995b6762010-08-20 13:23:26 +01001639static DEFINE_SPINLOCK(mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001640
1641/**
1642 * i915_read_mch_val - return value for IPS use
1643 *
1644 * Calculate and return a value for the IPS driver to use when deciding whether
1645 * we have thermal and power headroom to increase CPU or GPU power budget.
1646 */
1647unsigned long i915_read_mch_val(void)
1648{
Akshay Joshi0206e352011-08-16 15:34:10 -04001649 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001650 unsigned long chipset_val, graphics_val, ret = 0;
1651
Akshay Joshi0206e352011-08-16 15:34:10 -04001652 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001653 if (!i915_mch_dev)
1654 goto out_unlock;
1655 dev_priv = i915_mch_dev;
1656
1657 chipset_val = i915_chipset_val(dev_priv);
1658 graphics_val = i915_gfx_val(dev_priv);
1659
1660 ret = chipset_val + graphics_val;
1661
1662out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001663 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001664
Akshay Joshi0206e352011-08-16 15:34:10 -04001665 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001666}
1667EXPORT_SYMBOL_GPL(i915_read_mch_val);
1668
1669/**
1670 * i915_gpu_raise - raise GPU frequency limit
1671 *
1672 * Raise the limit; IPS indicates we have thermal headroom.
1673 */
1674bool i915_gpu_raise(void)
1675{
Akshay Joshi0206e352011-08-16 15:34:10 -04001676 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001677 bool ret = true;
1678
Akshay Joshi0206e352011-08-16 15:34:10 -04001679 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001680 if (!i915_mch_dev) {
1681 ret = false;
1682 goto out_unlock;
1683 }
1684 dev_priv = i915_mch_dev;
1685
1686 if (dev_priv->max_delay > dev_priv->fmax)
1687 dev_priv->max_delay--;
1688
1689out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001690 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001691
Akshay Joshi0206e352011-08-16 15:34:10 -04001692 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001693}
1694EXPORT_SYMBOL_GPL(i915_gpu_raise);
1695
1696/**
1697 * i915_gpu_lower - lower GPU frequency limit
1698 *
1699 * IPS indicates we're close to a thermal limit, so throttle back the GPU
1700 * frequency maximum.
1701 */
1702bool i915_gpu_lower(void)
1703{
Akshay Joshi0206e352011-08-16 15:34:10 -04001704 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001705 bool ret = true;
1706
Akshay Joshi0206e352011-08-16 15:34:10 -04001707 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001708 if (!i915_mch_dev) {
1709 ret = false;
1710 goto out_unlock;
1711 }
1712 dev_priv = i915_mch_dev;
1713
1714 if (dev_priv->max_delay < dev_priv->min_delay)
1715 dev_priv->max_delay++;
1716
1717out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001718 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001719
Akshay Joshi0206e352011-08-16 15:34:10 -04001720 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001721}
1722EXPORT_SYMBOL_GPL(i915_gpu_lower);
1723
1724/**
1725 * i915_gpu_busy - indicate GPU business to IPS
1726 *
1727 * Tell the IPS driver whether or not the GPU is busy.
1728 */
1729bool i915_gpu_busy(void)
1730{
Akshay Joshi0206e352011-08-16 15:34:10 -04001731 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001732 bool ret = false;
1733
Akshay Joshi0206e352011-08-16 15:34:10 -04001734 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001735 if (!i915_mch_dev)
1736 goto out_unlock;
1737 dev_priv = i915_mch_dev;
1738
1739 ret = dev_priv->busy;
1740
1741out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001742 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001743
Akshay Joshi0206e352011-08-16 15:34:10 -04001744 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001745}
1746EXPORT_SYMBOL_GPL(i915_gpu_busy);
1747
1748/**
1749 * i915_gpu_turbo_disable - disable graphics turbo
1750 *
1751 * Disable graphics turbo by resetting the max frequency and setting the
1752 * current frequency to the default.
1753 */
1754bool i915_gpu_turbo_disable(void)
1755{
Akshay Joshi0206e352011-08-16 15:34:10 -04001756 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001757 bool ret = true;
1758
Akshay Joshi0206e352011-08-16 15:34:10 -04001759 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001760 if (!i915_mch_dev) {
1761 ret = false;
1762 goto out_unlock;
1763 }
1764 dev_priv = i915_mch_dev;
1765
1766 dev_priv->max_delay = dev_priv->fstart;
1767
1768 if (!ironlake_set_drps(dev_priv->dev, dev_priv->fstart))
1769 ret = false;
1770
1771out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001772 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001773
Akshay Joshi0206e352011-08-16 15:34:10 -04001774 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001775}
1776EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
1777
Jesse Barnes79e53942008-11-07 14:24:08 -08001778/**
Eric Anholt63ee41d2010-12-20 18:40:06 -08001779 * Tells the intel_ips driver that the i915 driver is now loaded, if
1780 * IPS got loaded first.
1781 *
1782 * This awkward dance is so that neither module has to depend on the
1783 * other in order for IPS to do the appropriate communication of
1784 * GPU turbo limits to i915.
1785 */
1786static void
1787ips_ping_for_i915_load(void)
1788{
1789 void (*link)(void);
1790
1791 link = symbol_get(ips_link_to_i915_driver);
1792 if (link) {
1793 link();
1794 symbol_put(ips_link_to_i915_driver);
1795 }
1796}
1797
Adam Jacksone2b665c2012-03-14 11:22:10 -04001798static void
1799i915_mtrr_setup(struct drm_i915_private *dev_priv, unsigned long base,
1800 unsigned long size)
1801{
Chris Wilson23f54be2012-03-23 17:38:49 +00001802 dev_priv->mm.gtt_mtrr = -1;
1803
Adam Jackson9e984bc12012-03-14 11:22:11 -04001804#if defined(CONFIG_X86_PAT)
1805 if (cpu_has_pat)
1806 return;
1807#endif
1808
Adam Jacksone2b665c2012-03-14 11:22:10 -04001809 /* Set up a WC MTRR for non-PAT systems. This is more common than
1810 * one would think, because the kernel disables PAT on first
1811 * generation Core chips because WC PAT gets overridden by a UC
1812 * MTRR if present. Even if a UC MTRR isn't present.
1813 */
1814 dev_priv->mm.gtt_mtrr = mtrr_add(base, size, MTRR_TYPE_WRCOMB, 1);
1815 if (dev_priv->mm.gtt_mtrr < 0) {
1816 DRM_INFO("MTRR allocation failed. Graphics "
1817 "performance may suffer.\n");
1818 }
1819}
1820
Eric Anholt63ee41d2010-12-20 18:40:06 -08001821/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001822 * i915_driver_load - setup chip and create an initial config
1823 * @dev: DRM device
1824 * @flags: startup flags
1825 *
1826 * The driver load routine has to do several things:
1827 * - drive output discovery via intel_modeset_init()
1828 * - initialize the memory manager
1829 * - allocate initial config memory
1830 * - setup the DRM framebuffer with the allocated memory
1831 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001832int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001833{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001834 struct drm_i915_private *dev_priv;
Daniel Vetter26394d92012-03-26 21:33:18 +02001835 struct intel_device_info *info;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001836 int ret = 0, mmio_bar;
Daniel Vetter9021f282012-03-26 09:45:41 +02001837 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001838
Daniel Vetter26394d92012-03-26 21:33:18 +02001839 info = (struct intel_device_info *) flags;
1840
1841 /* Refuse to load on gen6+ without kms enabled. */
1842 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET))
1843 return -ENODEV;
1844
Daniel Vetterac622a92010-09-08 21:26:07 +02001845
Dave Airlie22eae942005-11-10 22:16:34 +11001846 /* i915 has 4 more counters */
1847 dev->counters += 4;
1848 dev->types[6] = _DRM_STAT_IRQ;
1849 dev->types[7] = _DRM_STAT_PRIMARY;
1850 dev->types[8] = _DRM_STAT_SECONDARY;
1851 dev->types[9] = _DRM_STAT_DMA;
1852
Eric Anholt9a298b22009-03-24 12:23:04 -07001853 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001854 if (dev_priv == NULL)
1855 return -ENOMEM;
1856
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001857 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001858 dev_priv->dev = dev;
Daniel Vetter26394d92012-03-26 21:33:18 +02001859 dev_priv->info = info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001860
Dave Airlieec2a4c32009-08-04 11:43:41 +10001861 if (i915_get_bridge_dev(dev)) {
1862 ret = -EIO;
1863 goto free_priv;
1864 }
1865
Dave Airlie466e69b2011-12-19 11:15:29 +00001866 pci_set_master(dev->pdev);
1867
Daniel Vetter9f82d232010-08-30 21:25:23 +02001868 /* overlay on gen2 is broken and can't address above 1G */
1869 if (IS_GEN2(dev))
1870 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1871
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001872 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1873 * using 32bit addressing, overwriting memory if HWS is located
1874 * above 4GB.
1875 *
1876 * The documentation also mentions an issue with undefined
1877 * behaviour if any general state is accessed within a page above 4GB,
1878 * which also needs to be handled carefully.
1879 */
1880 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1881 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1882
Chris Wilsonb4ce0f82010-10-28 11:26:06 +01001883 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1884 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, 0);
1885 if (!dev_priv->regs) {
1886 DRM_ERROR("failed to map registers\n");
1887 ret = -EIO;
1888 goto put_bridge;
1889 }
1890
Chris Wilson71e93392010-10-27 18:46:52 +01001891 dev_priv->mm.gtt = intel_gtt_get();
1892 if (!dev_priv->mm.gtt) {
1893 DRM_ERROR("Failed to initialize GTT\n");
1894 ret = -ENODEV;
Keith Packarda7b85d22011-07-10 13:12:17 -07001895 goto out_rmmap;
Chris Wilson71e93392010-10-27 18:46:52 +01001896 }
1897
Daniel Vetter9021f282012-03-26 09:45:41 +02001898 aperture_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
Chris Wilson71e93392010-10-27 18:46:52 +01001899
Akshay Joshi0206e352011-08-16 15:34:10 -04001900 dev_priv->mm.gtt_mapping =
Daniel Vetter9021f282012-03-26 09:45:41 +02001901 io_mapping_create_wc(dev->agp->base, aperture_size);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08001902 if (dev_priv->mm.gtt_mapping == NULL) {
1903 ret = -EIO;
1904 goto out_rmmap;
1905 }
1906
Daniel Vetter9021f282012-03-26 09:45:41 +02001907 i915_mtrr_setup(dev_priv, dev->agp->base, aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001908
Chris Wilsone642abb2010-09-09 12:46:34 +01001909 /* The i915 workqueue is primarily used for batched retirement of
1910 * requests (and thus managing bo) once the task has been completed
1911 * by the GPU. i915_gem_retire_requests() is called directly when we
1912 * need high-priority retirement, such as waiting for an explicit
1913 * bo.
1914 *
1915 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001916 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001917 *
1918 * All tasks on the workqueue are expected to acquire the dev mutex
1919 * so there is no point in running more than one instance of the
1920 * workqueue at any time: max_active = 1 and NON_REENTRANT.
1921 */
1922 dev_priv->wq = alloc_workqueue("i915",
1923 WQ_UNBOUND | WQ_NON_REENTRANT,
1924 1);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001925 if (dev_priv->wq == NULL) {
1926 DRM_ERROR("Failed to create our workqueue.\n");
1927 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001928 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001929 }
1930
Jesse Barnesf71d4af2011-06-28 13:00:41 -07001931 intel_irq_init(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001932
Zhenyu Wangc48044112009-12-17 14:48:43 +08001933 /* Try to make sure MCHBAR is enabled before poking at it */
1934 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001935 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001936 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001937
Bryan Freed6d139a82010-10-14 09:14:51 +01001938 /* Make sure the bios did its job and set up vital registers */
1939 intel_setup_bios(dev);
1940
Eric Anholt673a3942008-07-30 12:06:12 -07001941 i915_gem_load(dev);
1942
Keith Packard398c9cb2008-07-30 13:03:43 -07001943 /* Init HWS */
1944 if (!I915_NEED_GFX_HWS(dev)) {
1945 ret = i915_init_phys_hws(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001946 if (ret)
1947 goto out_gem_unload;
Keith Packard398c9cb2008-07-30 13:03:43 -07001948 }
Eric Anholted4cb412008-07-29 12:10:39 -07001949
Jesse Barnes7648fa92010-05-20 14:28:11 -07001950 if (IS_PINEVIEW(dev))
1951 i915_pineview_get_mem_freq(dev);
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01001952 else if (IS_GEN5(dev))
Jesse Barnes7648fa92010-05-20 14:28:11 -07001953 i915_ironlake_get_mem_freq(dev);
Shaohua Li7662c8b2009-06-26 11:23:55 +08001954
Eric Anholted4cb412008-07-29 12:10:39 -07001955 /* On the 945G/GM, the chipset reports the MSI capability on the
1956 * integrated graphics even though the support isn't actually there
1957 * according to the published specs. It doesn't appear to function
1958 * correctly in testing on 945G.
1959 * This may be a side effect of MSI having been made available for PEG
1960 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001961 *
1962 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001963 * be lost or delayed, but we use them anyways to avoid
1964 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001965 */
Keith Packardb60678a2008-12-08 11:12:28 -08001966 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001967 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001968
Daniel Vetter9f1f46a2011-12-14 13:57:03 +01001969 spin_lock_init(&dev_priv->gt_lock);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001970 spin_lock_init(&dev_priv->irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07001971 spin_lock_init(&dev_priv->error_lock);
Ben Widawsky4912d042011-04-25 11:25:20 -07001972 spin_lock_init(&dev_priv->rps_lock);
Eric Anholted4cb412008-07-29 12:10:39 -07001973
Eugeni Dodonovc51ed782012-04-13 17:08:45 -03001974 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
Jesse Barnes27f82272011-09-02 12:54:37 -07001975 dev_priv->num_pipe = 3;
1976 else if (IS_MOBILE(dev) || !IS_GEN2(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001977 dev_priv->num_pipe = 2;
1978 else
1979 dev_priv->num_pipe = 1;
1980
1981 ret = drm_vblank_init(dev, dev_priv->num_pipe);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001982 if (ret)
1983 goto out_gem_unload;
Keith Packard52440212008-11-18 09:30:25 -08001984
Ben Gamari11ed50e2009-09-14 17:48:45 -04001985 /* Start out suspended */
1986 dev_priv->mm.suspended = 1;
1987
Zhenyu Wang3bad0782010-04-07 16:15:53 +08001988 intel_detect_pch(dev);
1989
Jesse Barnes79e53942008-11-07 14:24:08 -08001990 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001991 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001992 if (ret < 0) {
1993 DRM_ERROR("failed to init modeset\n");
Chris Wilson56e2ea32010-11-08 17:10:29 +00001994 goto out_gem_unload;
Jesse Barnes79e53942008-11-07 14:24:08 -08001995 }
1996 }
1997
Ben Widawsky0136db582012-04-10 21:17:01 -07001998 i915_setup_sysfs(dev);
1999
Matthew Garrett74a365b2009-03-19 21:35:39 +00002000 /* Must be done after probing outputs */
Chris Wilson44834a62010-08-19 16:09:23 +01002001 intel_opregion_init(dev);
2002 acpi_video_register();
Matthew Garrett74a365b2009-03-19 21:35:39 +00002003
Ben Gamarif65d9422009-09-14 17:48:44 -04002004 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
2005 (unsigned long) dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07002006
Chris Wilson582be6b2012-04-30 19:35:02 +01002007 if (IS_GEN5(dev)) {
2008 spin_lock(&mchdev_lock);
2009 i915_mch_dev = dev_priv;
2010 dev_priv->mchdev_lock = &mchdev_lock;
2011 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07002012
Chris Wilson582be6b2012-04-30 19:35:02 +01002013 ips_ping_for_i915_load();
2014 }
Eric Anholt63ee41d2010-12-20 18:40:06 -08002015
Jesse Barnes79e53942008-11-07 14:24:08 -08002016 return 0;
2017
Chris Wilson56e2ea32010-11-08 17:10:29 +00002018out_gem_unload:
Keith Packarda7b85d22011-07-10 13:12:17 -07002019 if (dev_priv->mm.inactive_shrinker.shrink)
2020 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
2021
Chris Wilson56e2ea32010-11-08 17:10:29 +00002022 if (dev->pdev->msi_enabled)
2023 pci_disable_msi(dev->pdev);
2024
2025 intel_teardown_gmbus(dev);
2026 intel_teardown_mchbar(dev);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002027 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07002028out_mtrrfree:
2029 if (dev_priv->mm.gtt_mtrr >= 0) {
2030 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
2031 dev->agp->agp_info.aper_size * 1024 * 1024);
2032 dev_priv->mm.gtt_mtrr = -1;
2033 }
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08002034 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08002035out_rmmap:
Chris Wilson6dda5692010-10-29 21:02:18 +01002036 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10002037put_bridge:
2038 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002039free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07002040 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002041 return ret;
2042}
2043
2044int i915_driver_unload(struct drm_device *dev)
2045{
2046 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02002047 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002048
Jesse Barnes7648fa92010-05-20 14:28:11 -07002049 spin_lock(&mchdev_lock);
2050 i915_mch_dev = NULL;
2051 spin_unlock(&mchdev_lock);
2052
Ben Widawsky0136db582012-04-10 21:17:01 -07002053 i915_teardown_sysfs(dev);
2054
Chris Wilson17250b72010-10-28 12:51:39 +01002055 if (dev_priv->mm.inactive_shrinker.shrink)
2056 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
2057
Daniel Vetterc911fc12010-08-20 21:23:20 +02002058 mutex_lock(&dev->struct_mutex);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002059 ret = i915_gpu_idle(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02002060 if (ret)
2061 DRM_ERROR("failed to idle hardware: %d\n", ret);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002062 i915_gem_retire_requests(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02002063 mutex_unlock(&dev->struct_mutex);
2064
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002065 /* Cancel the retire work handler, which should be idle now. */
2066 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2067
Eric Anholtab657db12009-01-23 12:57:47 -08002068 io_mapping_free(dev_priv->mm.gtt_mapping);
2069 if (dev_priv->mm.gtt_mtrr >= 0) {
2070 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
2071 dev->agp->agp_info.aper_size * 1024 * 1024);
2072 dev_priv->mm.gtt_mtrr = -1;
2073 }
2074
Chris Wilson44834a62010-08-19 16:09:23 +01002075 acpi_video_unregister();
2076
Jesse Barnes79e53942008-11-07 14:24:08 -08002077 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01002078 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07002079 intel_modeset_cleanup(dev);
2080
Zhao Yakui6363ee62009-11-24 09:48:44 +08002081 /*
2082 * free the memory space allocated for the child device
2083 * config parsed from VBT
2084 */
2085 if (dev_priv->child_dev && dev_priv->child_dev_num) {
2086 kfree(dev_priv->child_dev);
2087 dev_priv->child_dev = NULL;
2088 dev_priv->child_dev_num = 0;
2089 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02002090
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10002091 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10002092 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08002093 }
2094
Daniel Vettera8b48992010-08-20 21:25:11 +02002095 /* Free error state after interrupts are fully disabled. */
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002096 del_timer_sync(&dev_priv->hangcheck_timer);
2097 cancel_work_sync(&dev_priv->error_work);
Daniel Vettera8b48992010-08-20 21:25:11 +02002098 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002099
Eric Anholted4cb412008-07-29 12:10:39 -07002100 if (dev->pdev->msi_enabled)
2101 pci_disable_msi(dev->pdev);
2102
Chris Wilson44834a62010-08-19 16:09:23 +01002103 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01002104
Jesse Barnes79e53942008-11-07 14:24:08 -08002105 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02002106 /* Flush any outstanding unpin_work. */
2107 flush_workqueue(dev_priv->wq);
2108
Jesse Barnes79e53942008-11-07 14:24:08 -08002109 mutex_lock(&dev->struct_mutex);
Hugh Dickinsecbec532011-06-27 16:18:20 -07002110 i915_gem_free_all_phys_object(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002111 i915_gem_cleanup_ringbuffer(dev);
2112 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002113 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01002114 i915_gem_cleanup_stolen(dev);
Chris Wilsonfe669bf2010-11-23 12:09:30 +00002115 drm_mm_takedown(&dev_priv->mm.stolen);
Daniel Vetter02e792f2009-09-15 22:57:34 +02002116
2117 intel_cleanup_overlay(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01002118
2119 if (!I915_NEED_GFX_HWS(dev))
2120 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002121 }
2122
Daniel Vetter701394c2010-10-10 18:54:08 +01002123 if (dev_priv->regs != NULL)
Chris Wilson6dda5692010-10-29 21:02:18 +01002124 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vetter701394c2010-10-10 18:54:08 +01002125
Chris Wilsonf899fc62010-07-20 15:44:45 -07002126 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08002127 intel_teardown_mchbar(dev);
2128
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002129 destroy_workqueue(dev_priv->wq);
2130
Dave Airlieec2a4c32009-08-04 11:43:41 +10002131 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07002132 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002133
Dave Airlie22eae942005-11-10 22:16:34 +11002134 return 0;
2135}
2136
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002137int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07002138{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002139 struct drm_i915_file_private *file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002140
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002141 DRM_DEBUG_DRIVER("\n");
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002142 file_priv = kmalloc(sizeof(*file_priv), GFP_KERNEL);
2143 if (!file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07002144 return -ENOMEM;
2145
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002146 file->driver_priv = file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002147
Chris Wilson1c255952010-09-26 11:03:27 +01002148 spin_lock_init(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002149 INIT_LIST_HEAD(&file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002150
2151 return 0;
2152}
2153
Jesse Barnes79e53942008-11-07 14:24:08 -08002154/**
2155 * i915_driver_lastclose - clean up after all DRM clients have exited
2156 * @dev: DRM device
2157 *
2158 * Take care of cleaning up after all DRM clients have exited. In the
2159 * mode setting case, we want to restore the kernel's initial mode (just
2160 * in case the last client left us in a bad state).
2161 *
Daniel Vetter9021f282012-03-26 09:45:41 +02002162 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08002163 * and DMA structures, since the kernel won't be using them, and clea
2164 * up any GEM state.
2165 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002166void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002168 drm_i915_private_t *dev_priv = dev->dev_private;
2169
Jesse Barnes79e53942008-11-07 14:24:08 -08002170 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +01002171 intel_fb_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10002172 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10002173 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08002174 }
Dave Airlie144a75f2008-03-30 07:53:58 +10002175
Eric Anholt673a3942008-07-30 12:06:12 -07002176 i915_gem_lastclose(dev);
2177
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002178 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
Eric Anholt6c340ea2007-08-25 20:23:09 +10002181void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Eric Anholtb9624422009-06-03 07:27:35 +00002183 i915_gem_release(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002186void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07002187{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002188 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002189
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002190 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07002191}
2192
Eric Anholtc153f452007-09-03 12:06:45 +10002193struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10002194 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2195 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
2196 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
2197 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
2198 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
2199 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
2200 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH),
2201 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01002202 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
2203 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
2204 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002205 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01002206 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02002207 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002208 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
2209 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
2210 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2211 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2212 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
2213 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
2214 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2215 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2216 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
2217 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
2218 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2219 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2220 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
2221 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
2222 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
2223 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
2224 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
2225 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
2226 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
2227 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
2228 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
2229 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
2230 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
2231 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
2232 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2233 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08002234 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2235 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, intel_sprite_get_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Dave Airliec94f7022005-07-07 21:03:38 +10002236};
2237
2238int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10002239
Daniel Vetter9021f282012-03-26 09:45:41 +02002240/*
2241 * This is really ugly: Because old userspace abused the linux agp interface to
2242 * manage the gtt, we need to claim that all intel devices are agp. For
2243 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10002244 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002245int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10002246{
2247 return 1;
2248}