blob: 833ac8a0cf43e234a07b51f57d31d87f41d162b8 [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
Daniel Vetter09422b22012-04-26 23:28:10 +020050#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
51
52#define BEGIN_LP_RING(n) \
53 intel_ring_begin(LP_RING(dev_priv), (n))
54
55#define OUT_RING(x) \
56 intel_ring_emit(LP_RING(dev_priv), x)
57
58#define ADVANCE_LP_RING() \
59 intel_ring_advance(LP_RING(dev_priv))
60
61/**
62 * Lock test for when it's just for synchronization of ring access.
63 *
64 * In that case, we don't need to do it when GEM is initialized as nobody else
65 * has access to the ring.
66 */
67#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
68 if (LP_RING(dev->dev_private)->obj == NULL) \
69 LOCK_TEST_WITH_RETURN(dev, file); \
70} while (0)
71
72#define READ_HWSP(dev_priv, reg) intel_read_status_page(LP_RING(dev_priv), reg)
73#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
74#define I915_BREADCRUMB_INDEX 0x21
75
Daniel Vetterd05c6172012-04-26 23:28:09 +020076void i915_update_dri1_breadcrumb(struct drm_device *dev)
77{
78 drm_i915_private_t *dev_priv = dev->dev_private;
79 struct drm_i915_master_private *master_priv;
80
81 if (dev->primary->master) {
82 master_priv = dev->primary->master->driver_priv;
83 if (master_priv->sarea_priv)
84 master_priv->sarea_priv->last_dispatch =
85 READ_BREADCRUMB(dev_priv);
86 }
87}
88
Chris Wilson4cbf74c2011-02-25 22:26:23 +000089static void i915_write_hws_pga(struct drm_device *dev)
90{
91 drm_i915_private_t *dev_priv = dev->dev_private;
92 u32 addr;
93
94 addr = dev_priv->status_page_dmah->busaddr;
95 if (INTEL_INFO(dev)->gen >= 4)
96 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
97 I915_WRITE(HWS_PGA, addr);
98}
99
Keith Packard398c9cb2008-07-30 13:03:43 -0700100/**
101 * Sets up the hardware status page for devices that need a physical address
102 * in the register.
103 */
Eric Anholt3043c602008-10-02 12:24:47 -0700104static int i915_init_phys_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700105{
106 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000107
Keith Packard398c9cb2008-07-30 13:03:43 -0700108 /* Program Hardware Status Page */
109 dev_priv->status_page_dmah =
Zhenyu Wange6be8d92010-01-05 11:25:05 +0800110 drm_pci_alloc(dev, PAGE_SIZE, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -0700111
112 if (!dev_priv->status_page_dmah) {
113 DRM_ERROR("Can not allocate hardware status page\n");
114 return -ENOMEM;
115 }
Keith Packard398c9cb2008-07-30 13:03:43 -0700116
Keith Packardf3234702011-07-22 10:44:39 -0700117 memset_io((void __force __iomem *)dev_priv->status_page_dmah->vaddr,
118 0, PAGE_SIZE);
Keith Packard398c9cb2008-07-30 13:03:43 -0700119
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000120 i915_write_hws_pga(dev);
Zhenyu Wang9b974cc2010-01-05 11:25:06 +0800121
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800122 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Keith Packard398c9cb2008-07-30 13:03:43 -0700123 return 0;
124}
125
126/**
127 * Frees the hardware status page, whether it's a physical address or a virtual
128 * address set up by the X Server.
129 */
Eric Anholt3043c602008-10-02 12:24:47 -0700130static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700131{
132 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000133 struct intel_ring_buffer *ring = LP_RING(dev_priv);
134
Keith Packard398c9cb2008-07-30 13:03:43 -0700135 if (dev_priv->status_page_dmah) {
136 drm_pci_free(dev, dev_priv->status_page_dmah);
137 dev_priv->status_page_dmah = NULL;
138 }
139
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000140 if (ring->status_page.gfx_addr) {
141 ring->status_page.gfx_addr = 0;
Keith Packard398c9cb2008-07-30 13:03:43 -0700142 drm_core_ioremapfree(&dev_priv->hws_map, dev);
143 }
144
145 /* Need to rewrite hardware status page */
146 I915_WRITE(HWS_PGA, 0x1ffff000);
147}
148
Dave Airlie84b1fd12007-07-11 15:53:27 +1000149void i915_kernel_lost_context(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000152 struct drm_i915_master_private *master_priv;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000153 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jesse Barnes79e53942008-11-07 14:24:08 -0800155 /*
156 * We should never lose context on the ring with modesetting
157 * as we don't expose it to userspace
158 */
159 if (drm_core_check_feature(dev, DRIVER_MODESET))
160 return;
161
Chris Wilson8168bd42010-11-11 17:54:52 +0000162 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
163 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 ring->space = ring->head - (ring->tail + 8);
165 if (ring->space < 0)
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800166 ring->space += ring->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Dave Airlie7c1c2872008-11-28 14:22:24 +1000168 if (!dev->primary->master)
169 return;
170
171 master_priv = dev->primary->master->driver_priv;
172 if (ring->head == ring->tail && master_priv->sarea_priv)
173 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Dave Airlie84b1fd12007-07-11 15:53:27 +1000176static int i915_dma_cleanup(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000178 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000179 int i;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* Make sure interrupts are disabled here because the uninstall ioctl
182 * may not have been called from userspace and after dev_private
183 * is freed, it's too late.
184 */
Eric Anholted4cb412008-07-29 12:10:39 -0700185 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000186 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200188 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000189 for (i = 0; i < I915_NUM_RINGS; i++)
190 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200191 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Keith Packard398c9cb2008-07-30 13:03:43 -0700193 /* Clear the HWS virtual address at teardown */
194 if (I915_NEED_GFX_HWS(dev))
195 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 return 0;
198}
199
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000200static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000202 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000203 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000204 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Dave Airlie3a03ac12009-01-11 09:03:49 +1000206 master_priv->sarea = drm_getsarea(dev);
207 if (master_priv->sarea) {
208 master_priv->sarea_priv = (drm_i915_sarea_t *)
209 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
210 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800211 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000212 }
213
Eric Anholt673a3942008-07-30 12:06:12 -0700214 if (init->ring_size != 0) {
Chris Wilsone8616b62011-01-20 09:57:11 +0000215 if (LP_RING(dev_priv)->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700216 i915_dma_cleanup(dev);
217 DRM_ERROR("Client tried to initialize ringbuffer in "
218 "GEM mode\n");
219 return -EINVAL;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Chris Wilsone8616b62011-01-20 09:57:11 +0000222 ret = intel_render_ring_init_dri(dev,
223 init->ring_start,
224 init->ring_size);
225 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700226 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000227 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
=?utf-8?q?Michel_D=C3=A4nzer?=a6b54f32006-10-24 23:37:43 +1000231 dev_priv->cpp = init->cpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 dev_priv->back_offset = init->back_offset;
233 dev_priv->front_offset = init->front_offset;
234 dev_priv->current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000235 if (master_priv->sarea_priv)
236 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* Allow hardware batchbuffers unless told otherwise.
239 */
Daniel Vetter87813422012-05-02 11:49:32 +0200240 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return 0;
243}
244
Dave Airlie84b1fd12007-07-11 15:53:27 +1000245static int i915_dma_resume(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000248 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800250 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800252 if (ring->map.handle == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 DRM_ERROR("can not ioremap virtual address for"
254 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000255 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
258 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800259 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000261 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800263 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800264 ring->status_page.page_addr);
265 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100266 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000267 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000268 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800269
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800270 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return 0;
273}
274
Eric Anholtc153f452007-09-03 12:06:45 +1000275static int i915_dma_init(struct drm_device *dev, void *data,
276 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Eric Anholtc153f452007-09-03 12:06:45 +1000278 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int retcode = 0;
280
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200281 if (drm_core_check_feature(dev, DRIVER_MODESET))
282 return -ENODEV;
283
Eric Anholtc153f452007-09-03 12:06:45 +1000284 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000286 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
288 case I915_CLEANUP_DMA:
289 retcode = i915_dma_cleanup(dev);
290 break;
291 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100292 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000295 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 }
298
299 return retcode;
300}
301
302/* Implement basically the same security restrictions as hardware does
303 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
304 *
305 * Most of the calculations below involve calculating the size of a
306 * particular instruction. It's important to get the size right as
307 * that tells us where the next instruction to check is. Any illegal
308 * instruction detected will be given a size of zero, which is a
309 * signal to abort the rest of the buffer.
310 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100311static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 switch (((cmd >> 29) & 0x7)) {
314 case 0x0:
315 switch ((cmd >> 23) & 0x3f) {
316 case 0x0:
317 return 1; /* MI_NOOP */
318 case 0x4:
319 return 1; /* MI_FLUSH */
320 default:
321 return 0; /* disallow everything else */
322 }
323 break;
324 case 0x1:
325 return 0; /* reserved */
326 case 0x2:
327 return (cmd & 0xff) + 2; /* 2d commands */
328 case 0x3:
329 if (((cmd >> 24) & 0x1f) <= 0x18)
330 return 1;
331
332 switch ((cmd >> 24) & 0x1f) {
333 case 0x1c:
334 return 1;
335 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 case 0x3:
338 return (cmd & 0x1f) + 2;
339 case 0x4:
340 return (cmd & 0xf) + 2;
341 default:
342 return (cmd & 0xffff) + 2;
343 }
344 case 0x1e:
345 if (cmd & (1 << 23))
346 return (cmd & 0xffff) + 1;
347 else
348 return 1;
349 case 0x1f:
350 if ((cmd & (1 << 23)) == 0) /* inline vertices */
351 return (cmd & 0x1ffff) + 2;
352 else if (cmd & (1 << 17)) /* indirect random */
353 if ((cmd & 0xffff) == 0)
354 return 0; /* unknown length, too hard */
355 else
356 return (((cmd & 0xffff) + 1) / 2) + 1;
357 else
358 return 2; /* indirect sequential */
359 default:
360 return 0;
361 }
362 default:
363 return 0;
364 }
365
366 return 0;
367}
368
Eric Anholt201361a2009-03-11 12:30:04 -0700369static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100372 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000374 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000375 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100378 int sz = validate_cmd(buffer[i]);
379 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000380 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100381 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100384 ret = BEGIN_LP_RING((dwords+1)&~1);
385 if (ret)
386 return ret;
387
388 for (i = 0; i < dwords; i++)
389 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100390 if (dwords & 1)
391 OUT_RING(0);
392
393 ADVANCE_LP_RING();
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396}
397
Eric Anholt673a3942008-07-30 12:06:12 -0700398int
399i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000400 struct drm_clip_rect *box,
401 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100403 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100404 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000406 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
407 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000409 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100413 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100414 ret = BEGIN_LP_RING(4);
415 if (ret)
416 return ret;
417
Alan Hourihanec29b6692006-08-12 16:29:24 +1000418 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000419 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
420 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000421 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000422 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100423 ret = BEGIN_LP_RING(6);
424 if (ret)
425 return ret;
426
Alan Hourihanec29b6692006-08-12 16:29:24 +1000427 OUT_RING(GFX_OP_DRAWRECT_INFO);
428 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000429 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
430 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000431 OUT_RING(DR4);
432 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000433 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100434 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return 0;
437}
438
Alan Hourihanec29b6692006-08-12 16:29:24 +1000439/* XXX: Emitting the counter should really be moved to part of the IRQ
440 * emit. For now, do it in both places:
441 */
442
Dave Airlie84b1fd12007-07-11 15:53:27 +1000443static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100444{
445 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000446 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100447
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400448 dev_priv->counter++;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000449 if (dev_priv->counter > 0x7FFFFFFFUL)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400450 dev_priv->counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000451 if (master_priv->sarea_priv)
452 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
Dave Airliede227f52006-01-25 15:31:43 +1100453
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100454 if (BEGIN_LP_RING(4) == 0) {
455 OUT_RING(MI_STORE_DWORD_INDEX);
456 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
457 OUT_RING(dev_priv->counter);
458 OUT_RING(0);
459 ADVANCE_LP_RING();
460 }
Dave Airliede227f52006-01-25 15:31:43 +1100461}
462
Dave Airlie84b1fd12007-07-11 15:53:27 +1000463static int i915_dispatch_cmdbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700464 drm_i915_cmdbuffer_t *cmd,
465 struct drm_clip_rect *cliprects,
466 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 int nbox = cmd->num_cliprects;
469 int i = 0, count, ret;
470
471 if (cmd->sz & 0x3) {
472 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
476 i915_kernel_lost_context(dev);
477
478 count = nbox ? nbox : 1;
479
480 for (i = 0; i < count; i++) {
481 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000482 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 cmd->DR1, cmd->DR4);
484 if (ret)
485 return ret;
486 }
487
Eric Anholt201361a2009-03-11 12:30:04 -0700488 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (ret)
490 return ret;
491 }
492
Dave Airliede227f52006-01-25 15:31:43 +1100493 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495}
496
Dave Airlie84b1fd12007-07-11 15:53:27 +1000497static int i915_dispatch_batchbuffer(struct drm_device * dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700498 drm_i915_batchbuffer_t * batch,
499 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100501 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100503 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if ((batch->start | batch->used) & 0x7) {
506 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000507 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 i915_kernel_lost_context(dev);
511
512 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 for (i = 0; i < count; i++) {
514 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000515 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100516 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (ret)
518 return ret;
519 }
520
Keith Packard0790d5e2008-07-30 12:28:47 -0700521 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100522 ret = BEGIN_LP_RING(2);
523 if (ret)
524 return ret;
525
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100526 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000527 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
528 OUT_RING(batch->start);
529 } else {
530 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
531 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100534 ret = BEGIN_LP_RING(4);
535 if (ret)
536 return ret;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 OUT_RING(MI_BATCH_BUFFER);
539 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
540 OUT_RING(batch->start + batch->used - 4);
541 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100543 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
Zou Nan hai1cafd342010-06-25 13:40:24 +0800546
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100547 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100548 if (BEGIN_LP_RING(2) == 0) {
549 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
550 OUT_RING(MI_NOOP);
551 ADVANCE_LP_RING();
552 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100555 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 0;
557}
558
Dave Airlieaf6061a2008-05-07 12:15:39 +1000559static int i915_dispatch_flip(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 drm_i915_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000562 struct drm_i915_master_private *master_priv =
563 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100564 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Dave Airlie7c1c2872008-11-28 14:22:24 +1000566 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400567 return -EINVAL;
568
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800569 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800570 __func__,
571 dev_priv->current_page,
572 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dave Airlieaf6061a2008-05-07 12:15:39 +1000574 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100576 ret = BEGIN_LP_RING(10);
577 if (ret)
578 return ret;
579
Jesse Barnes585fb112008-07-29 11:54:06 -0700580 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000581 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dave Airlieaf6061a2008-05-07 12:15:39 +1000583 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
584 OUT_RING(0);
585 if (dev_priv->current_page == 0) {
586 OUT_RING(dev_priv->back_offset);
587 dev_priv->current_page = 1;
588 } else {
589 OUT_RING(dev_priv->front_offset);
590 dev_priv->current_page = 0;
591 }
592 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000593
Dave Airlieaf6061a2008-05-07 12:15:39 +1000594 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
595 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100596
Dave Airlieaf6061a2008-05-07 12:15:39 +1000597 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000598
Dave Airlie7c1c2872008-11-28 14:22:24 +1000599 master_priv->sarea_priv->last_enqueue = dev_priv->counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000600
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100601 if (BEGIN_LP_RING(4) == 0) {
602 OUT_RING(MI_STORE_DWORD_INDEX);
603 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
604 OUT_RING(dev_priv->counter);
605 OUT_RING(0);
606 ADVANCE_LP_RING();
607 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000608
Dave Airlie7c1c2872008-11-28 14:22:24 +1000609 master_priv->sarea_priv->pf_current_page = dev_priv->current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000613static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000615 struct intel_ring_buffer *ring = LP_RING(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 i915_kernel_lost_context(dev);
Ben Widawsky96f298a2011-03-19 18:14:27 -0700618 return intel_wait_ring_idle(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Eric Anholtc153f452007-09-03 12:06:45 +1000621static int i915_flush_ioctl(struct drm_device *dev, void *data,
622 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Eric Anholt546b0972008-09-01 16:45:29 -0700624 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200626 if (drm_core_check_feature(dev, DRIVER_MODESET))
627 return -ENODEV;
628
Eric Anholt546b0972008-09-01 16:45:29 -0700629 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
630
631 mutex_lock(&dev->struct_mutex);
632 ret = i915_quiescent(dev);
633 mutex_unlock(&dev->struct_mutex);
634
635 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Eric Anholtc153f452007-09-03 12:06:45 +1000638static int i915_batchbuffer(struct drm_device *dev, void *data,
639 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000642 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000644 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000645 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700647 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200649 if (drm_core_check_feature(dev, DRIVER_MODESET))
650 return -ENODEV;
651
Daniel Vetter87813422012-05-02 11:49:32 +0200652 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000654 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800657 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800658 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Eric Anholt546b0972008-09-01 16:45:29 -0700660 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Eric Anholt201361a2009-03-11 12:30:04 -0700662 if (batch->num_cliprects < 0)
663 return -EINVAL;
664
665 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700666 cliprects = kcalloc(batch->num_cliprects,
667 sizeof(struct drm_clip_rect),
668 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700669 if (cliprects == NULL)
670 return -ENOMEM;
671
672 ret = copy_from_user(cliprects, batch->cliprects,
673 batch->num_cliprects *
674 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200675 if (ret != 0) {
676 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700677 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200678 }
Eric Anholt201361a2009-03-11 12:30:04 -0700679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Eric Anholt546b0972008-09-01 16:45:29 -0700681 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700682 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700683 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400685 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000686 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700687
688fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700689 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return ret;
692}
693
Eric Anholtc153f452007-09-03 12:06:45 +1000694static int i915_cmdbuffer(struct drm_device *dev, void *data,
695 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000698 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000700 master_priv->sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000701 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700702 struct drm_clip_rect *cliprects = NULL;
703 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int ret;
705
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800706 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800707 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200709 if (drm_core_check_feature(dev, DRIVER_MODESET))
710 return -ENODEV;
711
Eric Anholt546b0972008-09-01 16:45:29 -0700712 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Eric Anholt201361a2009-03-11 12:30:04 -0700714 if (cmdbuf->num_cliprects < 0)
715 return -EINVAL;
716
Eric Anholt9a298b22009-03-24 12:23:04 -0700717 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700718 if (batch_data == NULL)
719 return -ENOMEM;
720
721 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200722 if (ret != 0) {
723 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700724 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200725 }
Eric Anholt201361a2009-03-11 12:30:04 -0700726
727 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700728 cliprects = kcalloc(cmdbuf->num_cliprects,
729 sizeof(struct drm_clip_rect), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000730 if (cliprects == NULL) {
731 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700732 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000733 }
Eric Anholt201361a2009-03-11 12:30:04 -0700734
735 ret = copy_from_user(cliprects, cmdbuf->cliprects,
736 cmdbuf->num_cliprects *
737 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200738 if (ret != 0) {
739 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700740 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Eric Anholt546b0972008-09-01 16:45:29 -0700744 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700745 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700746 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (ret) {
748 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000749 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400752 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000753 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700754
Eric Anholt201361a2009-03-11 12:30:04 -0700755fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700756 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000757fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700758 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700759
760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Daniel Vetter94888672012-04-26 23:28:08 +0200763static int i915_emit_irq(struct drm_device * dev)
764{
765 drm_i915_private_t *dev_priv = dev->dev_private;
766 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
767
768 i915_kernel_lost_context(dev);
769
770 DRM_DEBUG_DRIVER("\n");
771
772 dev_priv->counter++;
773 if (dev_priv->counter > 0x7FFFFFFFUL)
774 dev_priv->counter = 1;
775 if (master_priv->sarea_priv)
776 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
777
778 if (BEGIN_LP_RING(4) == 0) {
779 OUT_RING(MI_STORE_DWORD_INDEX);
780 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
781 OUT_RING(dev_priv->counter);
782 OUT_RING(MI_USER_INTERRUPT);
783 ADVANCE_LP_RING();
784 }
785
786 return dev_priv->counter;
787}
788
789static int i915_wait_irq(struct drm_device * dev, int irq_nr)
790{
791 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
792 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
793 int ret = 0;
794 struct intel_ring_buffer *ring = LP_RING(dev_priv);
795
796 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
797 READ_BREADCRUMB(dev_priv));
798
799 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
800 if (master_priv->sarea_priv)
801 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
802 return 0;
803 }
804
805 if (master_priv->sarea_priv)
806 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
807
808 if (ring->irq_get(ring)) {
809 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
810 READ_BREADCRUMB(dev_priv) >= irq_nr);
811 ring->irq_put(ring);
812 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
813 ret = -EBUSY;
814
815 if (ret == -EBUSY) {
816 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
817 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
818 }
819
820 return ret;
821}
822
823/* Needs the lock as it touches the ring.
824 */
825static int i915_irq_emit(struct drm_device *dev, void *data,
826 struct drm_file *file_priv)
827{
828 drm_i915_private_t *dev_priv = dev->dev_private;
829 drm_i915_irq_emit_t *emit = data;
830 int result;
831
832 if (drm_core_check_feature(dev, DRIVER_MODESET))
833 return -ENODEV;
834
835 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
836 DRM_ERROR("called with no initialization\n");
837 return -EINVAL;
838 }
839
840 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
841
842 mutex_lock(&dev->struct_mutex);
843 result = i915_emit_irq(dev);
844 mutex_unlock(&dev->struct_mutex);
845
846 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
847 DRM_ERROR("copy_to_user\n");
848 return -EFAULT;
849 }
850
851 return 0;
852}
853
854/* Doesn't need the hardware lock.
855 */
856static int i915_irq_wait(struct drm_device *dev, void *data,
857 struct drm_file *file_priv)
858{
859 drm_i915_private_t *dev_priv = dev->dev_private;
860 drm_i915_irq_wait_t *irqwait = data;
861
862 if (drm_core_check_feature(dev, DRIVER_MODESET))
863 return -ENODEV;
864
865 if (!dev_priv) {
866 DRM_ERROR("called with no initialization\n");
867 return -EINVAL;
868 }
869
870 return i915_wait_irq(dev, irqwait->irq_seq);
871}
872
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200873static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
874 struct drm_file *file_priv)
875{
876 drm_i915_private_t *dev_priv = dev->dev_private;
877 drm_i915_vblank_pipe_t *pipe = data;
878
879 if (drm_core_check_feature(dev, DRIVER_MODESET))
880 return -ENODEV;
881
882 if (!dev_priv) {
883 DRM_ERROR("called with no initialization\n");
884 return -EINVAL;
885 }
886
887 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
888
889 return 0;
890}
891
892/**
893 * Schedule buffer swap at given vertical blank.
894 */
895static int i915_vblank_swap(struct drm_device *dev, void *data,
896 struct drm_file *file_priv)
897{
898 /* The delayed swap mechanism was fundamentally racy, and has been
899 * removed. The model was that the client requested a delayed flip/swap
900 * from the kernel, then waited for vblank before continuing to perform
901 * rendering. The problem was that the kernel might wake the client
902 * up before it dispatched the vblank swap (since the lock has to be
903 * held while touching the ringbuffer), in which case the client would
904 * clear and start the next frame before the swap occurred, and
905 * flicker would occur in addition to likely missing the vblank.
906 *
907 * In the absence of this ioctl, userland falls back to a correct path
908 * of waiting for a vblank, then dispatching the swap on its own.
909 * Context switching to userland and back is plenty fast enough for
910 * meeting the requirements of vblank swapping.
911 */
912 return -EINVAL;
913}
914
Eric Anholtc153f452007-09-03 12:06:45 +1000915static int i915_flip_bufs(struct drm_device *dev, void *data,
916 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Eric Anholt546b0972008-09-01 16:45:29 -0700918 int ret;
919
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200920 if (drm_core_check_feature(dev, DRIVER_MODESET))
921 return -ENODEV;
922
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800923 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Eric Anholt546b0972008-09-01 16:45:29 -0700925 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Eric Anholt546b0972008-09-01 16:45:29 -0700927 mutex_lock(&dev->struct_mutex);
928 ret = i915_dispatch_flip(dev);
929 mutex_unlock(&dev->struct_mutex);
930
931 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Eric Anholtc153f452007-09-03 12:06:45 +1000934static int i915_getparam(struct drm_device *dev, void *data,
935 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000938 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int value;
940
941 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000942 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000943 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
Eric Anholtc153f452007-09-03 12:06:45 +1000946 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700948 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200951 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100953 case I915_PARAM_LAST_DISPATCH:
954 value = READ_BREADCRUMB(dev_priv);
955 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400956 case I915_PARAM_CHIPSET_ID:
957 value = dev->pci_device;
958 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700959 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200960 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700961 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800962 case I915_PARAM_NUM_FENCES_AVAIL:
963 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
964 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200965 case I915_PARAM_HAS_OVERLAY:
966 value = dev_priv->overlay ? 1 : 0;
967 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800968 case I915_PARAM_HAS_PAGEFLIPPING:
969 value = 1;
970 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500971 case I915_PARAM_HAS_EXECBUF2:
972 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200973 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500974 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800975 case I915_PARAM_HAS_BSD:
976 value = HAS_BSD(dev);
977 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100978 case I915_PARAM_HAS_BLT:
979 value = HAS_BLT(dev);
980 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100981 case I915_PARAM_HAS_RELAXED_FENCING:
982 value = 1;
983 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100984 case I915_PARAM_HAS_COHERENT_RINGS:
985 value = 1;
986 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000987 case I915_PARAM_HAS_EXEC_CONSTANTS:
988 value = INTEL_INFO(dev)->gen >= 4;
989 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000990 case I915_PARAM_HAS_RELAXED_DELTA:
991 value = 1;
992 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800993 case I915_PARAM_HAS_GEN7_SOL_RESET:
994 value = 1;
995 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200996 case I915_PARAM_HAS_LLC:
997 value = HAS_LLC(dev);
998 break;
Daniel Vetter777ee962012-02-15 23:50:25 +0100999 case I915_PARAM_HAS_ALIASING_PPGTT:
1000 value = dev_priv->mm.aliasing_ppgtt ? 1 : 0;
1001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001003 DRM_DEBUG_DRIVER("Unknown parameter %d\n",
Jesse Barnes76446ca2009-12-17 22:05:42 -05001004 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001005 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Eric Anholtc153f452007-09-03 12:06:45 +10001008 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 DRM_ERROR("DRM_COPY_TO_USER failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001010 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
1013 return 0;
1014}
1015
Eric Anholtc153f452007-09-03 12:06:45 +10001016static int i915_setparam(struct drm_device *dev, void *data,
1017 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001020 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001023 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001024 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Eric Anholtc153f452007-09-03 12:06:45 +10001027 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 break;
1030 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001033 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001035 case I915_SETPARAM_NUM_USED_FENCES:
1036 if (param->value > dev_priv->num_fence_regs ||
1037 param->value < 0)
1038 return -EINVAL;
1039 /* Userspace can use first N regs */
1040 dev_priv->fence_reg_start = param->value;
1041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001043 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001044 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001045 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
1048 return 0;
1049}
1050
Eric Anholtc153f452007-09-03 12:06:45 +10001051static int i915_set_status_page(struct drm_device *dev, void *data,
1052 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001053{
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001054 drm_i915_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001055 drm_i915_hws_addr_t *hws = data;
Chris Wilson1ec14ad2010-12-04 11:30:53 +00001056 struct intel_ring_buffer *ring = LP_RING(dev_priv);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001057
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001058 if (drm_core_check_feature(dev, DRIVER_MODESET))
1059 return -ENODEV;
1060
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001061 if (!I915_NEED_GFX_HWS(dev))
1062 return -EINVAL;
1063
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001064 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001065 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001066 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001067 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001068
Jesse Barnes79e53942008-11-07 14:24:08 -08001069 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1070 WARN(1, "tried to set status page when mode setting active\n");
1071 return 0;
1072 }
1073
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001074 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001075
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001076 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001077
Eric Anholt8b409582007-11-22 16:40:37 +10001078 dev_priv->hws_map.offset = dev->agp->base + hws->addr;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001079 dev_priv->hws_map.size = 4*1024;
1080 dev_priv->hws_map.type = 0;
1081 dev_priv->hws_map.flags = 0;
1082 dev_priv->hws_map.mtrr = 0;
1083
Dave Airliedd0910b2009-02-25 14:49:21 +10001084 drm_core_ioremap_wc(&dev_priv->hws_map, dev);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001085 if (dev_priv->hws_map.handle == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001086 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001087 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001088 DRM_ERROR("can not ioremap virtual address for"
1089 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001090 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001091 }
Chris Wilson311bd682011-01-13 19:06:50 +00001092 ring->status_page.page_addr =
1093 (void __force __iomem *)dev_priv->hws_map.handle;
1094 memset_io(ring->status_page.page_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001095 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001096
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001097 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001098 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001099 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001100 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001101 return 0;
1102}
1103
Dave Airlieec2a4c32009-08-04 11:43:41 +10001104static int i915_get_bridge_dev(struct drm_device *dev)
1105{
1106 struct drm_i915_private *dev_priv = dev->dev_private;
1107
Akshay Joshi0206e352011-08-16 15:34:10 -04001108 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001109 if (!dev_priv->bridge_dev) {
1110 DRM_ERROR("bridge device not found\n");
1111 return -1;
1112 }
1113 return 0;
1114}
1115
Zhenyu Wangc48044112009-12-17 14:48:43 +08001116#define MCHBAR_I915 0x44
1117#define MCHBAR_I965 0x48
1118#define MCHBAR_SIZE (4*4096)
1119
1120#define DEVEN_REG 0x54
1121#define DEVEN_MCHBAR_EN (1 << 28)
1122
1123/* Allocate space for the MCH regs if needed, return nonzero on error */
1124static int
1125intel_alloc_mchbar_resource(struct drm_device *dev)
1126{
1127 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001128 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001129 u32 temp_lo, temp_hi = 0;
1130 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001131 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001132
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001133 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001134 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1135 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1136 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1137
1138 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1139#ifdef CONFIG_PNP
1140 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001141 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1142 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001143#endif
1144
1145 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001146 dev_priv->mch_res.name = "i915 MCHBAR";
1147 dev_priv->mch_res.flags = IORESOURCE_MEM;
1148 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1149 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001150 MCHBAR_SIZE, MCHBAR_SIZE,
1151 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001152 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001153 dev_priv->bridge_dev);
1154 if (ret) {
1155 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1156 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001157 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001158 }
1159
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001160 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001161 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1162 upper_32_bits(dev_priv->mch_res.start));
1163
1164 pci_write_config_dword(dev_priv->bridge_dev, reg,
1165 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001166 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001167}
1168
1169/* Setup MCHBAR if possible, return true if we should disable it again */
1170static void
1171intel_setup_mchbar(struct drm_device *dev)
1172{
1173 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001174 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001175 u32 temp;
1176 bool enabled;
1177
1178 dev_priv->mchbar_need_disable = false;
1179
1180 if (IS_I915G(dev) || IS_I915GM(dev)) {
1181 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1182 enabled = !!(temp & DEVEN_MCHBAR_EN);
1183 } else {
1184 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1185 enabled = temp & 1;
1186 }
1187
1188 /* If it's already enabled, don't have to do anything */
1189 if (enabled)
1190 return;
1191
1192 if (intel_alloc_mchbar_resource(dev))
1193 return;
1194
1195 dev_priv->mchbar_need_disable = true;
1196
1197 /* Space is allocated or reserved, so enable it. */
1198 if (IS_I915G(dev) || IS_I915GM(dev)) {
1199 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1200 temp | DEVEN_MCHBAR_EN);
1201 } else {
1202 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1203 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1204 }
1205}
1206
1207static void
1208intel_teardown_mchbar(struct drm_device *dev)
1209{
1210 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001211 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001212 u32 temp;
1213
1214 if (dev_priv->mchbar_need_disable) {
1215 if (IS_I915G(dev) || IS_I915GM(dev)) {
1216 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1217 temp &= ~DEVEN_MCHBAR_EN;
1218 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1219 } else {
1220 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1221 temp &= ~1;
1222 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1223 }
1224 }
1225
1226 if (dev_priv->mch_res.start)
1227 release_resource(&dev_priv->mch_res);
1228}
1229
Dave Airlie28d52042009-09-21 14:33:58 +10001230/* true = enable decode, false = disable decoder */
1231static unsigned int i915_vga_set_decode(void *cookie, bool state)
1232{
1233 struct drm_device *dev = cookie;
1234
1235 intel_modeset_vga_set_state(dev, state);
1236 if (state)
1237 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1238 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1239 else
1240 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1241}
1242
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001243static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1244{
1245 struct drm_device *dev = pci_get_drvdata(pdev);
1246 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1247 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001248 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001249 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001250 /* i915 resume handler doesn't set to D0 */
1251 pci_set_power_state(dev->pdev, PCI_D0);
1252 i915_resume(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001253 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001254 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001255 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001256 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001257 i915_suspend(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001258 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001259 }
1260}
1261
1262static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1263{
1264 struct drm_device *dev = pci_get_drvdata(pdev);
1265 bool can_switch;
1266
1267 spin_lock(&dev->count_lock);
1268 can_switch = (dev->open_count == 0);
1269 spin_unlock(&dev->count_lock);
1270 return can_switch;
1271}
1272
Chris Wilson2c7111d2011-03-29 10:40:27 +01001273static int i915_load_modeset_init(struct drm_device *dev)
1274{
1275 struct drm_i915_private *dev_priv = dev->dev_private;
1276 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001277
Bryan Freed6d139a82010-10-14 09:14:51 +01001278 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001279 if (ret)
1280 DRM_INFO("failed to find VBIOS tables\n");
1281
Chris Wilson934f9922011-01-20 13:09:12 +00001282 /* If we have > 1 VGA cards, then we need to arbitrate access
1283 * to the common VGA resources.
1284 *
1285 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1286 * then we do not take part in VGA arbitration and the
1287 * vga_client_register() fails with -ENODEV.
1288 */
Dave Airlie28d52042009-09-21 14:33:58 +10001289 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
Chris Wilson934f9922011-01-20 13:09:12 +00001290 if (ret && ret != -ENODEV)
Chris Wilson2c7111d2011-03-29 10:40:27 +01001291 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001292
Jesse Barnes723bfd72010-10-07 16:01:13 -07001293 intel_register_dsm_handler();
1294
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001295 ret = vga_switcheroo_register_client(dev->pdev,
1296 i915_switcheroo_set_state,
Dave Airlie8d608aa2010-12-07 08:57:57 +10001297 NULL,
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001298 i915_switcheroo_can_switch);
1299 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001300 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001301
Chris Wilson9797fbf2012-04-24 15:47:39 +01001302 /* Initialise stolen first so that we may reserve preallocated
1303 * objects for the BIOS to KMS transition.
1304 */
1305 ret = i915_gem_init_stolen(dev);
1306 if (ret)
1307 goto cleanup_vga_switcheroo;
1308
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001309 intel_modeset_init(dev);
1310
Chris Wilson1070a422012-04-24 15:47:41 +01001311 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001312 if (ret)
Chris Wilson9797fbf2012-04-24 15:47:39 +01001313 goto cleanup_gem_stolen;
Jesse Barnes79e53942008-11-07 14:24:08 -08001314
Chris Wilson2c7111d2011-03-29 10:40:27 +01001315 intel_modeset_gem_init(dev);
1316
1317 ret = drm_irq_install(dev);
1318 if (ret)
1319 goto cleanup_gem;
1320
Jesse Barnes79e53942008-11-07 14:24:08 -08001321 /* Always safe in the mode setting case. */
1322 /* FIXME: do pre/post-mode set stuff in core KMS code */
1323 dev->vblank_disable_allowed = 1;
1324
Chris Wilson5a793952010-06-06 10:50:03 +01001325 ret = intel_fbdev_init(dev);
1326 if (ret)
1327 goto cleanup_irq;
1328
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001329 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001330
1331 /* We're off and running w/KMS */
1332 dev_priv->mm.suspended = 0;
1333
Jesse Barnes79e53942008-11-07 14:24:08 -08001334 return 0;
1335
Chris Wilson5a793952010-06-06 10:50:03 +01001336cleanup_irq:
1337 drm_irq_uninstall(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001338cleanup_gem:
1339 mutex_lock(&dev->struct_mutex);
1340 i915_gem_cleanup_ringbuffer(dev);
1341 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01001342 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001343cleanup_gem_stolen:
1344 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001345cleanup_vga_switcheroo:
1346 vga_switcheroo_unregister_client(dev->pdev);
1347cleanup_vga_client:
1348 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001349out:
1350 return ret;
1351}
1352
Dave Airlie7c1c2872008-11-28 14:22:24 +10001353int i915_master_create(struct drm_device *dev, struct drm_master *master)
1354{
1355 struct drm_i915_master_private *master_priv;
1356
Eric Anholt9a298b22009-03-24 12:23:04 -07001357 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001358 if (!master_priv)
1359 return -ENOMEM;
1360
1361 master->driver_priv = master_priv;
1362 return 0;
1363}
1364
1365void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1366{
1367 struct drm_i915_master_private *master_priv = master->driver_priv;
1368
1369 if (!master_priv)
1370 return;
1371
Eric Anholt9a298b22009-03-24 12:23:04 -07001372 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001373
1374 master->driver_priv = NULL;
1375}
1376
Jesse Barnes7648fa92010-05-20 14:28:11 -07001377static void i915_pineview_get_mem_freq(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08001378{
1379 drm_i915_private_t *dev_priv = dev->dev_private;
1380 u32 tmp;
1381
Shaohua Li7662c8b2009-06-26 11:23:55 +08001382 tmp = I915_READ(CLKCFG);
1383
1384 switch (tmp & CLKCFG_FSB_MASK) {
1385 case CLKCFG_FSB_533:
1386 dev_priv->fsb_freq = 533; /* 133*4 */
1387 break;
1388 case CLKCFG_FSB_800:
1389 dev_priv->fsb_freq = 800; /* 200*4 */
1390 break;
1391 case CLKCFG_FSB_667:
1392 dev_priv->fsb_freq = 667; /* 167*4 */
1393 break;
1394 case CLKCFG_FSB_400:
1395 dev_priv->fsb_freq = 400; /* 100*4 */
1396 break;
1397 }
1398
1399 switch (tmp & CLKCFG_MEM_MASK) {
1400 case CLKCFG_MEM_533:
1401 dev_priv->mem_freq = 533;
1402 break;
1403 case CLKCFG_MEM_667:
1404 dev_priv->mem_freq = 667;
1405 break;
1406 case CLKCFG_MEM_800:
1407 dev_priv->mem_freq = 800;
1408 break;
1409 }
Li Peng95534262010-05-18 18:58:44 +08001410
1411 /* detect pineview DDR3 setting */
1412 tmp = I915_READ(CSHRDDR3CTL);
1413 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
Shaohua Li7662c8b2009-06-26 11:23:55 +08001414}
1415
Jesse Barnes7648fa92010-05-20 14:28:11 -07001416static void i915_ironlake_get_mem_freq(struct drm_device *dev)
1417{
1418 drm_i915_private_t *dev_priv = dev->dev_private;
1419 u16 ddrpll, csipll;
1420
1421 ddrpll = I915_READ16(DDRMPLL1);
1422 csipll = I915_READ16(CSIPLL0);
1423
1424 switch (ddrpll & 0xff) {
1425 case 0xc:
1426 dev_priv->mem_freq = 800;
1427 break;
1428 case 0x10:
1429 dev_priv->mem_freq = 1066;
1430 break;
1431 case 0x14:
1432 dev_priv->mem_freq = 1333;
1433 break;
1434 case 0x18:
1435 dev_priv->mem_freq = 1600;
1436 break;
1437 default:
1438 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
1439 ddrpll & 0xff);
1440 dev_priv->mem_freq = 0;
1441 break;
1442 }
1443
1444 dev_priv->r_t = dev_priv->mem_freq;
1445
1446 switch (csipll & 0x3ff) {
1447 case 0x00c:
1448 dev_priv->fsb_freq = 3200;
1449 break;
1450 case 0x00e:
1451 dev_priv->fsb_freq = 3733;
1452 break;
1453 case 0x010:
1454 dev_priv->fsb_freq = 4266;
1455 break;
1456 case 0x012:
1457 dev_priv->fsb_freq = 4800;
1458 break;
1459 case 0x014:
1460 dev_priv->fsb_freq = 5333;
1461 break;
1462 case 0x016:
1463 dev_priv->fsb_freq = 5866;
1464 break;
1465 case 0x018:
1466 dev_priv->fsb_freq = 6400;
1467 break;
1468 default:
1469 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
1470 csipll & 0x3ff);
1471 dev_priv->fsb_freq = 0;
1472 break;
1473 }
1474
1475 if (dev_priv->fsb_freq == 3200) {
1476 dev_priv->c_m = 0;
1477 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
1478 dev_priv->c_m = 1;
1479 } else {
1480 dev_priv->c_m = 2;
1481 }
1482}
1483
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001484static const struct cparams {
1485 u16 i;
1486 u16 t;
1487 u16 m;
1488 u16 c;
1489} cparams[] = {
Jesse Barnes7648fa92010-05-20 14:28:11 -07001490 { 1, 1333, 301, 28664 },
1491 { 1, 1066, 294, 24460 },
1492 { 1, 800, 294, 25192 },
1493 { 0, 1333, 276, 27605 },
1494 { 0, 1066, 276, 27605 },
1495 { 0, 800, 231, 23784 },
1496};
1497
1498unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
1499{
1500 u64 total_count, diff, ret;
1501 u32 count1, count2, count3, m = 0, c = 0;
1502 unsigned long now = jiffies_to_msecs(jiffies), diff1;
1503 int i;
1504
1505 diff1 = now - dev_priv->last_time1;
1506
Eugeni Dodonov4ed0b572011-11-10 13:55:15 -02001507 /* Prevent division-by-zero if we are asking too fast.
1508 * Also, we don't get interesting results if we are polling
1509 * faster than once in 10ms, so just return the saved value
1510 * in such cases.
1511 */
1512 if (diff1 <= 10)
1513 return dev_priv->chipset_power;
1514
Jesse Barnes7648fa92010-05-20 14:28:11 -07001515 count1 = I915_READ(DMIEC);
1516 count2 = I915_READ(DDREC);
1517 count3 = I915_READ(CSIEC);
1518
1519 total_count = count1 + count2 + count3;
1520
1521 /* FIXME: handle per-counter overflow */
1522 if (total_count < dev_priv->last_count1) {
1523 diff = ~0UL - dev_priv->last_count1;
1524 diff += total_count;
1525 } else {
1526 diff = total_count - dev_priv->last_count1;
1527 }
1528
1529 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
1530 if (cparams[i].i == dev_priv->c_m &&
1531 cparams[i].t == dev_priv->r_t) {
1532 m = cparams[i].m;
1533 c = cparams[i].c;
1534 break;
1535 }
1536 }
1537
Jesse Barnesd270ae32010-09-27 10:35:44 -07001538 diff = div_u64(diff, diff1);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001539 ret = ((m * diff) + c);
Jesse Barnesd270ae32010-09-27 10:35:44 -07001540 ret = div_u64(ret, 10);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001541
1542 dev_priv->last_count1 = total_count;
1543 dev_priv->last_time1 = now;
1544
Eugeni Dodonov4ed0b572011-11-10 13:55:15 -02001545 dev_priv->chipset_power = ret;
1546
Jesse Barnes7648fa92010-05-20 14:28:11 -07001547 return ret;
1548}
1549
1550unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
1551{
1552 unsigned long m, x, b;
1553 u32 tsfs;
1554
1555 tsfs = I915_READ(TSFS);
1556
1557 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
1558 x = I915_READ8(TR1);
1559
1560 b = tsfs & TSFS_INTR_MASK;
1561
1562 return ((m * x) / 127) - b;
1563}
1564
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001565static u16 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
Jesse Barnes7648fa92010-05-20 14:28:11 -07001566{
Chris Wilsonfaa60c42010-11-23 13:50:14 +00001567 static const struct v_table {
1568 u16 vd; /* in .1 mil */
1569 u16 vm; /* in .1 mil */
1570 } v_table[] = {
1571 { 0, 0, },
1572 { 375, 0, },
1573 { 500, 0, },
1574 { 625, 0, },
1575 { 750, 0, },
1576 { 875, 0, },
1577 { 1000, 0, },
1578 { 1125, 0, },
1579 { 4125, 3000, },
1580 { 4125, 3000, },
1581 { 4125, 3000, },
1582 { 4125, 3000, },
1583 { 4125, 3000, },
1584 { 4125, 3000, },
1585 { 4125, 3000, },
1586 { 4125, 3000, },
1587 { 4125, 3000, },
1588 { 4125, 3000, },
1589 { 4125, 3000, },
1590 { 4125, 3000, },
1591 { 4125, 3000, },
1592 { 4125, 3000, },
1593 { 4125, 3000, },
1594 { 4125, 3000, },
1595 { 4125, 3000, },
1596 { 4125, 3000, },
1597 { 4125, 3000, },
1598 { 4125, 3000, },
1599 { 4125, 3000, },
1600 { 4125, 3000, },
1601 { 4125, 3000, },
1602 { 4125, 3000, },
1603 { 4250, 3125, },
1604 { 4375, 3250, },
1605 { 4500, 3375, },
1606 { 4625, 3500, },
1607 { 4750, 3625, },
1608 { 4875, 3750, },
1609 { 5000, 3875, },
1610 { 5125, 4000, },
1611 { 5250, 4125, },
1612 { 5375, 4250, },
1613 { 5500, 4375, },
1614 { 5625, 4500, },
1615 { 5750, 4625, },
1616 { 5875, 4750, },
1617 { 6000, 4875, },
1618 { 6125, 5000, },
1619 { 6250, 5125, },
1620 { 6375, 5250, },
1621 { 6500, 5375, },
1622 { 6625, 5500, },
1623 { 6750, 5625, },
1624 { 6875, 5750, },
1625 { 7000, 5875, },
1626 { 7125, 6000, },
1627 { 7250, 6125, },
1628 { 7375, 6250, },
1629 { 7500, 6375, },
1630 { 7625, 6500, },
1631 { 7750, 6625, },
1632 { 7875, 6750, },
1633 { 8000, 6875, },
1634 { 8125, 7000, },
1635 { 8250, 7125, },
1636 { 8375, 7250, },
1637 { 8500, 7375, },
1638 { 8625, 7500, },
1639 { 8750, 7625, },
1640 { 8875, 7750, },
1641 { 9000, 7875, },
1642 { 9125, 8000, },
1643 { 9250, 8125, },
1644 { 9375, 8250, },
1645 { 9500, 8375, },
1646 { 9625, 8500, },
1647 { 9750, 8625, },
1648 { 9875, 8750, },
1649 { 10000, 8875, },
1650 { 10125, 9000, },
1651 { 10250, 9125, },
1652 { 10375, 9250, },
1653 { 10500, 9375, },
1654 { 10625, 9500, },
1655 { 10750, 9625, },
1656 { 10875, 9750, },
1657 { 11000, 9875, },
1658 { 11125, 10000, },
1659 { 11250, 10125, },
1660 { 11375, 10250, },
1661 { 11500, 10375, },
1662 { 11625, 10500, },
1663 { 11750, 10625, },
1664 { 11875, 10750, },
1665 { 12000, 10875, },
1666 { 12125, 11000, },
1667 { 12250, 11125, },
1668 { 12375, 11250, },
1669 { 12500, 11375, },
1670 { 12625, 11500, },
1671 { 12750, 11625, },
1672 { 12875, 11750, },
1673 { 13000, 11875, },
1674 { 13125, 12000, },
1675 { 13250, 12125, },
1676 { 13375, 12250, },
1677 { 13500, 12375, },
1678 { 13625, 12500, },
1679 { 13750, 12625, },
1680 { 13875, 12750, },
1681 { 14000, 12875, },
1682 { 14125, 13000, },
1683 { 14250, 13125, },
1684 { 14375, 13250, },
1685 { 14500, 13375, },
1686 { 14625, 13500, },
1687 { 14750, 13625, },
1688 { 14875, 13750, },
1689 { 15000, 13875, },
1690 { 15125, 14000, },
1691 { 15250, 14125, },
1692 { 15375, 14250, },
1693 { 15500, 14375, },
1694 { 15625, 14500, },
1695 { 15750, 14625, },
1696 { 15875, 14750, },
1697 { 16000, 14875, },
1698 { 16125, 15000, },
1699 };
1700 if (dev_priv->info->is_mobile)
1701 return v_table[pxvid].vm;
1702 else
1703 return v_table[pxvid].vd;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001704}
1705
1706void i915_update_gfx_val(struct drm_i915_private *dev_priv)
1707{
1708 struct timespec now, diff1;
1709 u64 diff;
1710 unsigned long diffms;
1711 u32 count;
1712
Chris Wilson582be6b2012-04-30 19:35:02 +01001713 if (dev_priv->info->gen != 5)
1714 return;
1715
Jesse Barnes7648fa92010-05-20 14:28:11 -07001716 getrawmonotonic(&now);
1717 diff1 = timespec_sub(now, dev_priv->last_time2);
1718
1719 /* Don't divide by 0 */
1720 diffms = diff1.tv_sec * 1000 + diff1.tv_nsec / 1000000;
1721 if (!diffms)
1722 return;
1723
1724 count = I915_READ(GFXEC);
1725
1726 if (count < dev_priv->last_count2) {
1727 diff = ~0UL - dev_priv->last_count2;
1728 diff += count;
1729 } else {
1730 diff = count - dev_priv->last_count2;
1731 }
1732
1733 dev_priv->last_count2 = count;
1734 dev_priv->last_time2 = now;
1735
1736 /* More magic constants... */
1737 diff = diff * 1181;
Jesse Barnesd270ae32010-09-27 10:35:44 -07001738 diff = div_u64(diff, diffms * 10);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001739 dev_priv->gfx_power = diff;
1740}
1741
1742unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
1743{
1744 unsigned long t, corr, state1, corr2, state2;
1745 u32 pxvid, ext_v;
1746
1747 pxvid = I915_READ(PXVFREQ_BASE + (dev_priv->cur_delay * 4));
1748 pxvid = (pxvid >> 24) & 0x7f;
1749 ext_v = pvid_to_extvid(dev_priv, pxvid);
1750
1751 state1 = ext_v;
1752
1753 t = i915_mch_val(dev_priv);
1754
1755 /* Revel in the empirically derived constants */
1756
1757 /* Correction factor in 1/100000 units */
1758 if (t > 80)
1759 corr = ((t * 2349) + 135940);
1760 else if (t >= 50)
1761 corr = ((t * 964) + 29317);
1762 else /* < 50 */
1763 corr = ((t * 301) + 1004);
1764
1765 corr = corr * ((150142 * state1) / 10000 - 78642);
1766 corr /= 100000;
1767 corr2 = (corr * dev_priv->corr);
1768
1769 state2 = (corr2 * state1) / 10000;
1770 state2 /= 100; /* convert to mW */
1771
1772 i915_update_gfx_val(dev_priv);
1773
1774 return dev_priv->gfx_power + state2;
1775}
1776
1777/* Global for IPS driver to get at the current i915 device */
1778static struct drm_i915_private *i915_mch_dev;
1779/*
1780 * Lock protecting IPS related data structures
1781 * - i915_mch_dev
1782 * - dev_priv->max_delay
1783 * - dev_priv->min_delay
1784 * - dev_priv->fmax
1785 * - dev_priv->gpu_busy
1786 */
Chris Wilson995b6762010-08-20 13:23:26 +01001787static DEFINE_SPINLOCK(mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001788
1789/**
1790 * i915_read_mch_val - return value for IPS use
1791 *
1792 * Calculate and return a value for the IPS driver to use when deciding whether
1793 * we have thermal and power headroom to increase CPU or GPU power budget.
1794 */
1795unsigned long i915_read_mch_val(void)
1796{
Akshay Joshi0206e352011-08-16 15:34:10 -04001797 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001798 unsigned long chipset_val, graphics_val, ret = 0;
1799
Akshay Joshi0206e352011-08-16 15:34:10 -04001800 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001801 if (!i915_mch_dev)
1802 goto out_unlock;
1803 dev_priv = i915_mch_dev;
1804
1805 chipset_val = i915_chipset_val(dev_priv);
1806 graphics_val = i915_gfx_val(dev_priv);
1807
1808 ret = chipset_val + graphics_val;
1809
1810out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001811 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001812
Akshay Joshi0206e352011-08-16 15:34:10 -04001813 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001814}
1815EXPORT_SYMBOL_GPL(i915_read_mch_val);
1816
1817/**
1818 * i915_gpu_raise - raise GPU frequency limit
1819 *
1820 * Raise the limit; IPS indicates we have thermal headroom.
1821 */
1822bool i915_gpu_raise(void)
1823{
Akshay Joshi0206e352011-08-16 15:34:10 -04001824 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001825 bool ret = true;
1826
Akshay Joshi0206e352011-08-16 15:34:10 -04001827 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001828 if (!i915_mch_dev) {
1829 ret = false;
1830 goto out_unlock;
1831 }
1832 dev_priv = i915_mch_dev;
1833
1834 if (dev_priv->max_delay > dev_priv->fmax)
1835 dev_priv->max_delay--;
1836
1837out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001838 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001839
Akshay Joshi0206e352011-08-16 15:34:10 -04001840 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001841}
1842EXPORT_SYMBOL_GPL(i915_gpu_raise);
1843
1844/**
1845 * i915_gpu_lower - lower GPU frequency limit
1846 *
1847 * IPS indicates we're close to a thermal limit, so throttle back the GPU
1848 * frequency maximum.
1849 */
1850bool i915_gpu_lower(void)
1851{
Akshay Joshi0206e352011-08-16 15:34:10 -04001852 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001853 bool ret = true;
1854
Akshay Joshi0206e352011-08-16 15:34:10 -04001855 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001856 if (!i915_mch_dev) {
1857 ret = false;
1858 goto out_unlock;
1859 }
1860 dev_priv = i915_mch_dev;
1861
1862 if (dev_priv->max_delay < dev_priv->min_delay)
1863 dev_priv->max_delay++;
1864
1865out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001866 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001867
Akshay Joshi0206e352011-08-16 15:34:10 -04001868 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001869}
1870EXPORT_SYMBOL_GPL(i915_gpu_lower);
1871
1872/**
1873 * i915_gpu_busy - indicate GPU business to IPS
1874 *
1875 * Tell the IPS driver whether or not the GPU is busy.
1876 */
1877bool i915_gpu_busy(void)
1878{
Akshay Joshi0206e352011-08-16 15:34:10 -04001879 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001880 bool ret = false;
1881
Akshay Joshi0206e352011-08-16 15:34:10 -04001882 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001883 if (!i915_mch_dev)
1884 goto out_unlock;
1885 dev_priv = i915_mch_dev;
1886
1887 ret = dev_priv->busy;
1888
1889out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001890 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001891
Akshay Joshi0206e352011-08-16 15:34:10 -04001892 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001893}
1894EXPORT_SYMBOL_GPL(i915_gpu_busy);
1895
1896/**
1897 * i915_gpu_turbo_disable - disable graphics turbo
1898 *
1899 * Disable graphics turbo by resetting the max frequency and setting the
1900 * current frequency to the default.
1901 */
1902bool i915_gpu_turbo_disable(void)
1903{
Akshay Joshi0206e352011-08-16 15:34:10 -04001904 struct drm_i915_private *dev_priv;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001905 bool ret = true;
1906
Akshay Joshi0206e352011-08-16 15:34:10 -04001907 spin_lock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001908 if (!i915_mch_dev) {
1909 ret = false;
1910 goto out_unlock;
1911 }
1912 dev_priv = i915_mch_dev;
1913
1914 dev_priv->max_delay = dev_priv->fstart;
1915
1916 if (!ironlake_set_drps(dev_priv->dev, dev_priv->fstart))
1917 ret = false;
1918
1919out_unlock:
Akshay Joshi0206e352011-08-16 15:34:10 -04001920 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07001921
Akshay Joshi0206e352011-08-16 15:34:10 -04001922 return ret;
Jesse Barnes7648fa92010-05-20 14:28:11 -07001923}
1924EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
1925
Jesse Barnes79e53942008-11-07 14:24:08 -08001926/**
Eric Anholt63ee41d2010-12-20 18:40:06 -08001927 * Tells the intel_ips driver that the i915 driver is now loaded, if
1928 * IPS got loaded first.
1929 *
1930 * This awkward dance is so that neither module has to depend on the
1931 * other in order for IPS to do the appropriate communication of
1932 * GPU turbo limits to i915.
1933 */
1934static void
1935ips_ping_for_i915_load(void)
1936{
1937 void (*link)(void);
1938
1939 link = symbol_get(ips_link_to_i915_driver);
1940 if (link) {
1941 link();
1942 symbol_put(ips_link_to_i915_driver);
1943 }
1944}
1945
Adam Jacksone2b665c2012-03-14 11:22:10 -04001946static void
1947i915_mtrr_setup(struct drm_i915_private *dev_priv, unsigned long base,
1948 unsigned long size)
1949{
Chris Wilson23f54be2012-03-23 17:38:49 +00001950 dev_priv->mm.gtt_mtrr = -1;
1951
Adam Jackson9e984bc12012-03-14 11:22:11 -04001952#if defined(CONFIG_X86_PAT)
1953 if (cpu_has_pat)
1954 return;
1955#endif
1956
Adam Jacksone2b665c2012-03-14 11:22:10 -04001957 /* Set up a WC MTRR for non-PAT systems. This is more common than
1958 * one would think, because the kernel disables PAT on first
1959 * generation Core chips because WC PAT gets overridden by a UC
1960 * MTRR if present. Even if a UC MTRR isn't present.
1961 */
1962 dev_priv->mm.gtt_mtrr = mtrr_add(base, size, MTRR_TYPE_WRCOMB, 1);
1963 if (dev_priv->mm.gtt_mtrr < 0) {
1964 DRM_INFO("MTRR allocation failed. Graphics "
1965 "performance may suffer.\n");
1966 }
1967}
1968
Eric Anholt63ee41d2010-12-20 18:40:06 -08001969/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001970 * i915_driver_load - setup chip and create an initial config
1971 * @dev: DRM device
1972 * @flags: startup flags
1973 *
1974 * The driver load routine has to do several things:
1975 * - drive output discovery via intel_modeset_init()
1976 * - initialize the memory manager
1977 * - allocate initial config memory
1978 * - setup the DRM framebuffer with the allocated memory
1979 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001980int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001981{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001982 struct drm_i915_private *dev_priv;
Daniel Vetter26394d92012-03-26 21:33:18 +02001983 struct intel_device_info *info;
Kristian Høgsbergcfdf1fa2009-12-16 15:16:16 -05001984 int ret = 0, mmio_bar;
Daniel Vetter9021f282012-03-26 09:45:41 +02001985 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001986
Daniel Vetter26394d92012-03-26 21:33:18 +02001987 info = (struct intel_device_info *) flags;
1988
1989 /* Refuse to load on gen6+ without kms enabled. */
1990 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET))
1991 return -ENODEV;
1992
Daniel Vetterac622a92010-09-08 21:26:07 +02001993
Dave Airlie22eae942005-11-10 22:16:34 +11001994 /* i915 has 4 more counters */
1995 dev->counters += 4;
1996 dev->types[6] = _DRM_STAT_IRQ;
1997 dev->types[7] = _DRM_STAT_PRIMARY;
1998 dev->types[8] = _DRM_STAT_SECONDARY;
1999 dev->types[9] = _DRM_STAT_DMA;
2000
Eric Anholt9a298b22009-03-24 12:23:04 -07002001 dev_priv = kzalloc(sizeof(drm_i915_private_t), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002002 if (dev_priv == NULL)
2003 return -ENOMEM;
2004
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002005 dev->dev_private = (void *)dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002006 dev_priv->dev = dev;
Daniel Vetter26394d92012-03-26 21:33:18 +02002007 dev_priv->info = info;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002008
Dave Airlieec2a4c32009-08-04 11:43:41 +10002009 if (i915_get_bridge_dev(dev)) {
2010 ret = -EIO;
2011 goto free_priv;
2012 }
2013
Dave Airlie466e69b2011-12-19 11:15:29 +00002014 pci_set_master(dev->pdev);
2015
Daniel Vetter9f82d232010-08-30 21:25:23 +02002016 /* overlay on gen2 is broken and can't address above 1G */
2017 if (IS_GEN2(dev))
2018 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
2019
Jan Niehusmann6927faf2011-03-01 23:24:16 +01002020 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
2021 * using 32bit addressing, overwriting memory if HWS is located
2022 * above 4GB.
2023 *
2024 * The documentation also mentions an issue with undefined
2025 * behaviour if any general state is accessed within a page above 4GB,
2026 * which also needs to be handled carefully.
2027 */
2028 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2029 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
2030
Chris Wilsonb4ce0f82010-10-28 11:26:06 +01002031 mmio_bar = IS_GEN2(dev) ? 1 : 0;
2032 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, 0);
2033 if (!dev_priv->regs) {
2034 DRM_ERROR("failed to map registers\n");
2035 ret = -EIO;
2036 goto put_bridge;
2037 }
2038
Chris Wilson71e93392010-10-27 18:46:52 +01002039 dev_priv->mm.gtt = intel_gtt_get();
2040 if (!dev_priv->mm.gtt) {
2041 DRM_ERROR("Failed to initialize GTT\n");
2042 ret = -ENODEV;
Keith Packarda7b85d22011-07-10 13:12:17 -07002043 goto out_rmmap;
Chris Wilson71e93392010-10-27 18:46:52 +01002044 }
2045
Daniel Vetter9021f282012-03-26 09:45:41 +02002046 aperture_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
Chris Wilson71e93392010-10-27 18:46:52 +01002047
Akshay Joshi0206e352011-08-16 15:34:10 -04002048 dev_priv->mm.gtt_mapping =
Daniel Vetter9021f282012-03-26 09:45:41 +02002049 io_mapping_create_wc(dev->agp->base, aperture_size);
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08002050 if (dev_priv->mm.gtt_mapping == NULL) {
2051 ret = -EIO;
2052 goto out_rmmap;
2053 }
2054
Daniel Vetter9021f282012-03-26 09:45:41 +02002055 i915_mtrr_setup(dev_priv, dev->agp->base, aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08002056
Chris Wilsone642abb2010-09-09 12:46:34 +01002057 /* The i915 workqueue is primarily used for batched retirement of
2058 * requests (and thus managing bo) once the task has been completed
2059 * by the GPU. i915_gem_retire_requests() is called directly when we
2060 * need high-priority retirement, such as waiting for an explicit
2061 * bo.
2062 *
2063 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08002064 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01002065 *
2066 * All tasks on the workqueue are expected to acquire the dev mutex
2067 * so there is no point in running more than one instance of the
2068 * workqueue at any time: max_active = 1 and NON_REENTRANT.
2069 */
2070 dev_priv->wq = alloc_workqueue("i915",
2071 WQ_UNBOUND | WQ_NON_REENTRANT,
2072 1);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002073 if (dev_priv->wq == NULL) {
2074 DRM_ERROR("Failed to create our workqueue.\n");
2075 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07002076 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002077 }
2078
Jesse Barnesf71d4af2011-06-28 13:00:41 -07002079 intel_irq_init(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08002080
Zhenyu Wangc48044112009-12-17 14:48:43 +08002081 /* Try to make sure MCHBAR is enabled before poking at it */
2082 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07002083 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01002084 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08002085
Bryan Freed6d139a82010-10-14 09:14:51 +01002086 /* Make sure the bios did its job and set up vital registers */
2087 intel_setup_bios(dev);
2088
Eric Anholt673a3942008-07-30 12:06:12 -07002089 i915_gem_load(dev);
2090
Keith Packard398c9cb2008-07-30 13:03:43 -07002091 /* Init HWS */
2092 if (!I915_NEED_GFX_HWS(dev)) {
2093 ret = i915_init_phys_hws(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00002094 if (ret)
2095 goto out_gem_unload;
Keith Packard398c9cb2008-07-30 13:03:43 -07002096 }
Eric Anholted4cb412008-07-29 12:10:39 -07002097
Jesse Barnes7648fa92010-05-20 14:28:11 -07002098 if (IS_PINEVIEW(dev))
2099 i915_pineview_get_mem_freq(dev);
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01002100 else if (IS_GEN5(dev))
Jesse Barnes7648fa92010-05-20 14:28:11 -07002101 i915_ironlake_get_mem_freq(dev);
Shaohua Li7662c8b2009-06-26 11:23:55 +08002102
Eric Anholted4cb412008-07-29 12:10:39 -07002103 /* On the 945G/GM, the chipset reports the MSI capability on the
2104 * integrated graphics even though the support isn't actually there
2105 * according to the published specs. It doesn't appear to function
2106 * correctly in testing on 945G.
2107 * This may be a side effect of MSI having been made available for PEG
2108 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07002109 *
2110 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08002111 * be lost or delayed, but we use them anyways to avoid
2112 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07002113 */
Keith Packardb60678a2008-12-08 11:12:28 -08002114 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08002115 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07002116
Daniel Vetter9f1f46a2011-12-14 13:57:03 +01002117 spin_lock_init(&dev_priv->gt_lock);
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002118 spin_lock_init(&dev_priv->irq_lock);
Jesse Barnes63eeaf32009-06-18 16:56:52 -07002119 spin_lock_init(&dev_priv->error_lock);
Ben Widawsky4912d042011-04-25 11:25:20 -07002120 spin_lock_init(&dev_priv->rps_lock);
Eric Anholted4cb412008-07-29 12:10:39 -07002121
Eugeni Dodonovc51ed782012-04-13 17:08:45 -03002122 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
Jesse Barnes27f82272011-09-02 12:54:37 -07002123 dev_priv->num_pipe = 3;
2124 else if (IS_MOBILE(dev) || !IS_GEN2(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002125 dev_priv->num_pipe = 2;
2126 else
2127 dev_priv->num_pipe = 1;
2128
2129 ret = drm_vblank_init(dev, dev_priv->num_pipe);
Chris Wilson56e2ea32010-11-08 17:10:29 +00002130 if (ret)
2131 goto out_gem_unload;
Keith Packard52440212008-11-18 09:30:25 -08002132
Ben Gamari11ed50e2009-09-14 17:48:45 -04002133 /* Start out suspended */
2134 dev_priv->mm.suspended = 1;
2135
Zhenyu Wang3bad0782010-04-07 16:15:53 +08002136 intel_detect_pch(dev);
2137
Jesse Barnes79e53942008-11-07 14:24:08 -08002138 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02002139 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002140 if (ret < 0) {
2141 DRM_ERROR("failed to init modeset\n");
Chris Wilson56e2ea32010-11-08 17:10:29 +00002142 goto out_gem_unload;
Jesse Barnes79e53942008-11-07 14:24:08 -08002143 }
2144 }
2145
Ben Widawsky0136db582012-04-10 21:17:01 -07002146 i915_setup_sysfs(dev);
2147
Matthew Garrett74a365b2009-03-19 21:35:39 +00002148 /* Must be done after probing outputs */
Chris Wilson44834a62010-08-19 16:09:23 +01002149 intel_opregion_init(dev);
2150 acpi_video_register();
Matthew Garrett74a365b2009-03-19 21:35:39 +00002151
Ben Gamarif65d9422009-09-14 17:48:44 -04002152 setup_timer(&dev_priv->hangcheck_timer, i915_hangcheck_elapsed,
2153 (unsigned long) dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07002154
Chris Wilson582be6b2012-04-30 19:35:02 +01002155 if (IS_GEN5(dev)) {
2156 spin_lock(&mchdev_lock);
2157 i915_mch_dev = dev_priv;
2158 dev_priv->mchdev_lock = &mchdev_lock;
2159 spin_unlock(&mchdev_lock);
Jesse Barnes7648fa92010-05-20 14:28:11 -07002160
Chris Wilson582be6b2012-04-30 19:35:02 +01002161 ips_ping_for_i915_load();
2162 }
Eric Anholt63ee41d2010-12-20 18:40:06 -08002163
Jesse Barnes79e53942008-11-07 14:24:08 -08002164 return 0;
2165
Chris Wilson56e2ea32010-11-08 17:10:29 +00002166out_gem_unload:
Keith Packarda7b85d22011-07-10 13:12:17 -07002167 if (dev_priv->mm.inactive_shrinker.shrink)
2168 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
2169
Chris Wilson56e2ea32010-11-08 17:10:29 +00002170 if (dev->pdev->msi_enabled)
2171 pci_disable_msi(dev->pdev);
2172
2173 intel_teardown_gmbus(dev);
2174 intel_teardown_mchbar(dev);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07002175 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07002176out_mtrrfree:
2177 if (dev_priv->mm.gtt_mtrr >= 0) {
2178 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
2179 dev->agp->agp_info.aper_size * 1024 * 1024);
2180 dev_priv->mm.gtt_mtrr = -1;
2181 }
Venkatesh Pallipadi66441072009-02-24 17:35:11 -08002182 io_mapping_free(dev_priv->mm.gtt_mapping);
Jesse Barnes79e53942008-11-07 14:24:08 -08002183out_rmmap:
Chris Wilson6dda5692010-10-29 21:02:18 +01002184 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10002185put_bridge:
2186 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002187free_priv:
Eric Anholt9a298b22009-03-24 12:23:04 -07002188 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002189 return ret;
2190}
2191
2192int i915_driver_unload(struct drm_device *dev)
2193{
2194 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02002195 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002196
Jesse Barnes7648fa92010-05-20 14:28:11 -07002197 spin_lock(&mchdev_lock);
2198 i915_mch_dev = NULL;
2199 spin_unlock(&mchdev_lock);
2200
Ben Widawsky0136db582012-04-10 21:17:01 -07002201 i915_teardown_sysfs(dev);
2202
Chris Wilson17250b72010-10-28 12:51:39 +01002203 if (dev_priv->mm.inactive_shrinker.shrink)
2204 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
2205
Daniel Vetterc911fc12010-08-20 21:23:20 +02002206 mutex_lock(&dev->struct_mutex);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002207 ret = i915_gpu_idle(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02002208 if (ret)
2209 DRM_ERROR("failed to idle hardware: %d\n", ret);
Ben Widawskyb2da9fe2012-04-26 16:02:58 -07002210 i915_gem_retire_requests(dev);
Daniel Vetterc911fc12010-08-20 21:23:20 +02002211 mutex_unlock(&dev->struct_mutex);
2212
Daniel Vetter75ef9da2010-08-21 00:25:16 +02002213 /* Cancel the retire work handler, which should be idle now. */
2214 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
2215
Eric Anholtab657db12009-01-23 12:57:47 -08002216 io_mapping_free(dev_priv->mm.gtt_mapping);
2217 if (dev_priv->mm.gtt_mtrr >= 0) {
2218 mtrr_del(dev_priv->mm.gtt_mtrr, dev->agp->base,
2219 dev->agp->agp_info.aper_size * 1024 * 1024);
2220 dev_priv->mm.gtt_mtrr = -1;
2221 }
2222
Chris Wilson44834a62010-08-19 16:09:23 +01002223 acpi_video_unregister();
2224
Jesse Barnes79e53942008-11-07 14:24:08 -08002225 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Chris Wilson7b4f3992010-10-04 15:33:04 +01002226 intel_fbdev_fini(dev);
Jesse Barnes3d8620c2010-03-26 11:07:21 -07002227 intel_modeset_cleanup(dev);
2228
Zhao Yakui6363ee62009-11-24 09:48:44 +08002229 /*
2230 * free the memory space allocated for the child device
2231 * config parsed from VBT
2232 */
2233 if (dev_priv->child_dev && dev_priv->child_dev_num) {
2234 kfree(dev_priv->child_dev);
2235 dev_priv->child_dev = NULL;
2236 dev_priv->child_dev_num = 0;
2237 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02002238
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10002239 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10002240 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08002241 }
2242
Daniel Vettera8b48992010-08-20 21:25:11 +02002243 /* Free error state after interrupts are fully disabled. */
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002244 del_timer_sync(&dev_priv->hangcheck_timer);
2245 cancel_work_sync(&dev_priv->error_work);
Daniel Vettera8b48992010-08-20 21:25:11 +02002246 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002247
Eric Anholted4cb412008-07-29 12:10:39 -07002248 if (dev->pdev->msi_enabled)
2249 pci_disable_msi(dev->pdev);
2250
Chris Wilson44834a62010-08-19 16:09:23 +01002251 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01002252
Jesse Barnes79e53942008-11-07 14:24:08 -08002253 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02002254 /* Flush any outstanding unpin_work. */
2255 flush_workqueue(dev_priv->wq);
2256
Jesse Barnes79e53942008-11-07 14:24:08 -08002257 mutex_lock(&dev->struct_mutex);
Hugh Dickinsecbec532011-06-27 16:18:20 -07002258 i915_gem_free_all_phys_object(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002259 i915_gem_cleanup_ringbuffer(dev);
2260 mutex_unlock(&dev->struct_mutex);
Daniel Vetter1d2a3142012-02-09 17:15:46 +01002261 i915_gem_cleanup_aliasing_ppgtt(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01002262 i915_gem_cleanup_stolen(dev);
Chris Wilsonfe669bf2010-11-23 12:09:30 +00002263 drm_mm_takedown(&dev_priv->mm.stolen);
Daniel Vetter02e792f2009-09-15 22:57:34 +02002264
2265 intel_cleanup_overlay(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01002266
2267 if (!I915_NEED_GFX_HWS(dev))
2268 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08002269 }
2270
Daniel Vetter701394c2010-10-10 18:54:08 +01002271 if (dev_priv->regs != NULL)
Chris Wilson6dda5692010-10-29 21:02:18 +01002272 pci_iounmap(dev->pdev, dev_priv->regs);
Daniel Vetter701394c2010-10-10 18:54:08 +01002273
Chris Wilsonf899fc62010-07-20 15:44:45 -07002274 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08002275 intel_teardown_mchbar(dev);
2276
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02002277 destroy_workqueue(dev_priv->wq);
2278
Dave Airlieec2a4c32009-08-04 11:43:41 +10002279 pci_dev_put(dev_priv->bridge_dev);
Eric Anholt9a298b22009-03-24 12:23:04 -07002280 kfree(dev->dev_private);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002281
Dave Airlie22eae942005-11-10 22:16:34 +11002282 return 0;
2283}
2284
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002285int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07002286{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002287 struct drm_i915_file_private *file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002288
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002289 DRM_DEBUG_DRIVER("\n");
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002290 file_priv = kmalloc(sizeof(*file_priv), GFP_KERNEL);
2291 if (!file_priv)
Eric Anholt673a3942008-07-30 12:06:12 -07002292 return -ENOMEM;
2293
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002294 file->driver_priv = file_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002295
Chris Wilson1c255952010-09-26 11:03:27 +01002296 spin_lock_init(&file_priv->mm.lock);
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002297 INIT_LIST_HEAD(&file_priv->mm.request_list);
Eric Anholt673a3942008-07-30 12:06:12 -07002298
2299 return 0;
2300}
2301
Jesse Barnes79e53942008-11-07 14:24:08 -08002302/**
2303 * i915_driver_lastclose - clean up after all DRM clients have exited
2304 * @dev: DRM device
2305 *
2306 * Take care of cleaning up after all DRM clients have exited. In the
2307 * mode setting case, we want to restore the kernel's initial mode (just
2308 * in case the last client left us in a bad state).
2309 *
Daniel Vetter9021f282012-03-26 09:45:41 +02002310 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08002311 * and DMA structures, since the kernel won't be using them, and clea
2312 * up any GEM state.
2313 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002314void i915_driver_lastclose(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10002316 drm_i915_private_t *dev_priv = dev->dev_private;
2317
Jesse Barnes79e53942008-11-07 14:24:08 -08002318 if (!dev_priv || drm_core_check_feature(dev, DRIVER_MODESET)) {
Dave Airliee8e7a2b2011-04-21 22:18:32 +01002319 intel_fb_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10002320 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10002321 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08002322 }
Dave Airlie144a75f2008-03-30 07:53:58 +10002323
Eric Anholt673a3942008-07-30 12:06:12 -07002324 i915_gem_lastclose(dev);
2325
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002326 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Eric Anholt6c340ea2007-08-25 20:23:09 +10002329void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Eric Anholtb9624422009-06-03 07:27:35 +00002331 i915_gem_release(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002334void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07002335{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002336 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07002337
Chris Wilsonf787a5f2010-09-24 16:02:42 +01002338 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07002339}
2340
Eric Anholtc153f452007-09-03 12:06:45 +10002341struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10002342 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2343 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
2344 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
2345 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
2346 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
2347 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
2348 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH),
2349 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01002350 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
2351 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
2352 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002353 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01002354 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02002355 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002356 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
2357 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
2358 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2359 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2360 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
2361 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED),
2362 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2363 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2364 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
2365 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED),
2366 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2367 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2368 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED),
2369 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED),
2370 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED),
2371 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED),
2372 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED),
2373 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED),
2374 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED),
2375 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED),
2376 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED),
2377 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED),
2378 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
2379 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED),
2380 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2381 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08002382 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2383 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 +10002384};
2385
2386int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10002387
Daniel Vetter9021f282012-03-26 09:45:41 +02002388/*
2389 * This is really ugly: Because old userspace abused the linux agp interface to
2390 * manage the gtt, we need to claim that all intel devices are agp. For
2391 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10002392 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10002393int i915_driver_device_is_agp(struct drm_device * dev)
Dave Airliecda17382005-07-10 17:31:26 +10002394{
2395 return 1;
2396}