blob: 5dc37f0233b2a54710a37b8945ddff31040cef7a [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
Jesse Barnesd1d70672014-05-28 14:39:03 -070031#include <linux/async.h>
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/drm_fb_helper.h>
Daniel Vetter4f03b1f2014-09-10 12:43:49 +020035#include <drm/drm_legacy.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "i915_drv.h"
Chris Wilson1c5d22f2009-08-25 11:15:50 +010039#include "i915_trace.h"
Jordan Crousedcdb1672010-05-27 13:40:25 -060040#include <linux/pci.h>
Daniel Vettera4de0522014-06-05 16:20:46 +020041#include <linux/console.h>
42#include <linux/vt.h>
Dave Airlie28d52042009-09-21 14:33:58 +100043#include <linux/vgaarb.h>
Zhenyu Wangc48044112009-12-17 14:48:43 +080044#include <linux/acpi.h>
45#include <linux/pnp.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100046#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Chris Wilson44834a62010-08-19 16:09:23 +010048#include <acpi/video.h>
Paulo Zanoni8a187452013-12-06 20:32:13 -020049#include <linux/pm.h>
50#include <linux/pm_runtime.h>
Imre Deak4bdc7292014-05-20 19:47:20 +030051#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Daniel Vetter09422b22012-04-26 23:28:10 +020053#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
54
55#define BEGIN_LP_RING(n) \
56 intel_ring_begin(LP_RING(dev_priv), (n))
57
58#define OUT_RING(x) \
59 intel_ring_emit(LP_RING(dev_priv), x)
60
61#define ADVANCE_LP_RING() \
Chris Wilson09246732013-08-10 22:16:32 +010062 __intel_ring_advance(LP_RING(dev_priv))
Daniel Vetter09422b22012-04-26 23:28:10 +020063
64/**
65 * Lock test for when it's just for synchronization of ring access.
66 *
67 * In that case, we don't need to do it when GEM is initialized as nobody else
68 * has access to the ring.
69 */
70#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
Oscar Mateoee1b1e52014-05-22 14:13:35 +010071 if (LP_RING(dev->dev_private)->buffer->obj == NULL) \
Daniel Vetter09422b22012-04-26 23:28:10 +020072 LOCK_TEST_WITH_RETURN(dev, file); \
73} while (0)
74
Daniel Vetter316d3882012-04-26 23:28:15 +020075static inline u32
76intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
77{
78 if (I915_NEED_GFX_HWS(dev_priv->dev))
79 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
80 else
81 return intel_read_status_page(LP_RING(dev_priv), reg);
82}
83
84#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
Daniel Vetter09422b22012-04-26 23:28:10 +020085#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
86#define I915_BREADCRUMB_INDEX 0x21
87
Daniel Vetterd05c6172012-04-26 23:28:09 +020088void i915_update_dri1_breadcrumb(struct drm_device *dev)
89{
Jani Nikula4c8a4be2014-03-31 14:27:15 +030090 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd05c6172012-04-26 23:28:09 +020091 struct drm_i915_master_private *master_priv;
92
Daniel Vetter6c719fa2013-12-10 13:20:59 +010093 /*
94 * The dri breadcrumb update races against the drm master disappearing.
95 * Instead of trying to fix this (this is by far not the only ums issue)
96 * just don't do the update in kms mode.
97 */
98 if (drm_core_check_feature(dev, DRIVER_MODESET))
99 return;
100
Daniel Vetterd05c6172012-04-26 23:28:09 +0200101 if (dev->primary->master) {
102 master_priv = dev->primary->master->driver_priv;
103 if (master_priv->sarea_priv)
104 master_priv->sarea_priv->last_dispatch =
105 READ_BREADCRUMB(dev_priv);
106 }
107}
108
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000109static void i915_write_hws_pga(struct drm_device *dev)
110{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300111 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000112 u32 addr;
113
114 addr = dev_priv->status_page_dmah->busaddr;
115 if (INTEL_INFO(dev)->gen >= 4)
116 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
117 I915_WRITE(HWS_PGA, addr);
118}
119
Keith Packard398c9cb2008-07-30 13:03:43 -0700120/**
Keith Packard398c9cb2008-07-30 13:03:43 -0700121 * Frees the hardware status page, whether it's a physical address or a virtual
122 * address set up by the X Server.
123 */
Eric Anholt3043c602008-10-02 12:24:47 -0700124static void i915_free_hws(struct drm_device *dev)
Keith Packard398c9cb2008-07-30 13:03:43 -0700125{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300126 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100127 struct intel_engine_cs *ring = LP_RING(dev_priv);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000128
Keith Packard398c9cb2008-07-30 13:03:43 -0700129 if (dev_priv->status_page_dmah) {
130 drm_pci_free(dev, dev_priv->status_page_dmah);
131 dev_priv->status_page_dmah = NULL;
132 }
133
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000134 if (ring->status_page.gfx_addr) {
135 ring->status_page.gfx_addr = 0;
Daniel Vetter316d3882012-04-26 23:28:15 +0200136 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
Keith Packard398c9cb2008-07-30 13:03:43 -0700137 }
138
139 /* Need to rewrite hardware status page */
140 I915_WRITE(HWS_PGA, 0x1ffff000);
141}
142
Robin Schroer1a5036b2014-06-02 16:59:39 +0200143void i915_kernel_lost_context(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300145 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000146 struct drm_i915_master_private *master_priv;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100147 struct intel_engine_cs *ring = LP_RING(dev_priv);
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100148 struct intel_ringbuffer *ringbuf = ring->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jesse Barnes79e53942008-11-07 14:24:08 -0800150 /*
151 * We should never lose context on the ring with modesetting
152 * as we don't expose it to userspace
153 */
154 if (drm_core_check_feature(dev, DRIVER_MODESET))
155 return;
156
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100157 ringbuf->head = I915_READ_HEAD(ring) & HEAD_ADDR;
158 ringbuf->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
159 ringbuf->space = ringbuf->head - (ringbuf->tail + I915_RING_FREE_SPACE);
160 if (ringbuf->space < 0)
161 ringbuf->space += ringbuf->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Dave Airlie7c1c2872008-11-28 14:22:24 +1000163 if (!dev->primary->master)
164 return;
165
166 master_priv = dev->primary->master->driver_priv;
Oscar Mateo93b0a4e2014-05-22 14:13:36 +0100167 if (ringbuf->head == ringbuf->tail && master_priv->sarea_priv)
Dave Airlie7c1c2872008-11-28 14:22:24 +1000168 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Robin Schroer1a5036b2014-06-02 16:59:39 +0200171static int i915_dma_cleanup(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300173 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000174 int i;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* Make sure interrupts are disabled here because the uninstall ioctl
177 * may not have been called from userspace and after dev_private
178 * is freed, it's too late.
179 */
Eric Anholted4cb412008-07-29 12:10:39 -0700180 if (dev->irq_enabled)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200183 mutex_lock(&dev->struct_mutex);
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000184 for (i = 0; i < I915_NUM_RINGS; i++)
185 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
Dan Carpenteree0c6bf2010-06-23 13:19:55 +0200186 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Keith Packard398c9cb2008-07-30 13:03:43 -0700188 /* Clear the HWS virtual address at teardown */
189 if (I915_NEED_GFX_HWS(dev))
190 i915_free_hws(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return 0;
193}
194
Robin Schroer1a5036b2014-06-02 16:59:39 +0200195static int i915_initialize(struct drm_device *dev, drm_i915_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300197 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000198 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Chris Wilsone8616b62011-01-20 09:57:11 +0000199 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
David Herrmann9fc5cde2014-08-29 12:12:28 +0200201 master_priv->sarea = drm_legacy_getsarea(dev);
Dave Airlie3a03ac12009-01-11 09:03:49 +1000202 if (master_priv->sarea) {
203 master_priv->sarea_priv = (drm_i915_sarea_t *)
204 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
205 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800206 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
Dave Airlie3a03ac12009-01-11 09:03:49 +1000207 }
208
Eric Anholt673a3942008-07-30 12:06:12 -0700209 if (init->ring_size != 0) {
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100210 if (LP_RING(dev_priv)->buffer->obj != NULL) {
Eric Anholt673a3942008-07-30 12:06:12 -0700211 i915_dma_cleanup(dev);
212 DRM_ERROR("Client tried to initialize ringbuffer in "
213 "GEM mode\n");
214 return -EINVAL;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Chris Wilsone8616b62011-01-20 09:57:11 +0000217 ret = intel_render_ring_init_dri(dev,
218 init->ring_start,
219 init->ring_size);
220 if (ret) {
Eric Anholt673a3942008-07-30 12:06:12 -0700221 i915_dma_cleanup(dev);
Chris Wilsone8616b62011-01-20 09:57:11 +0000222 return ret;
Eric Anholt673a3942008-07-30 12:06:12 -0700223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200226 dev_priv->dri1.cpp = init->cpp;
227 dev_priv->dri1.back_offset = init->back_offset;
228 dev_priv->dri1.front_offset = init->front_offset;
229 dev_priv->dri1.current_page = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000230 if (master_priv->sarea_priv)
231 master_priv->sarea_priv->pf_current_page = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* Allow hardware batchbuffers unless told otherwise.
234 */
Daniel Vetter87813422012-05-02 11:49:32 +0200235 dev_priv->dri1.allow_batchbuffer = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return 0;
238}
239
Robin Schroer1a5036b2014-06-02 16:59:39 +0200240static int i915_dma_resume(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300242 struct drm_i915_private *dev_priv = dev->dev_private;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100243 struct intel_engine_cs *ring = LP_RING(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800245 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100247 if (ring->buffer->virtual_start == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 DRM_ERROR("can not ioremap virtual address for"
249 " ring buffer\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000250 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
253 /* Program Hardware Status Page */
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800254 if (!ring->status_page.page_addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 DRM_ERROR("Can not find hardware status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000256 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800258 DRM_DEBUG_DRIVER("hw status page @ %p\n",
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800259 ring->status_page.page_addr);
260 if (ring->status_page.gfx_addr != 0)
Chris Wilson78501ea2010-10-27 12:18:21 +0100261 intel_ring_setup_status_page(ring);
Wang Zhenyudc7a9312007-06-10 15:58:19 +1000262 else
Chris Wilson4cbf74c2011-02-25 22:26:23 +0000263 i915_write_hws_pga(dev);
Zou Nan hai8187a2b2010-05-21 09:08:55 +0800264
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800265 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 return 0;
268}
269
Eric Anholtc153f452007-09-03 12:06:45 +1000270static int i915_dma_init(struct drm_device *dev, void *data,
271 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Eric Anholtc153f452007-09-03 12:06:45 +1000273 drm_i915_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int retcode = 0;
275
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200276 if (drm_core_check_feature(dev, DRIVER_MODESET))
277 return -ENODEV;
278
Eric Anholtc153f452007-09-03 12:06:45 +1000279 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 case I915_INIT_DMA:
Jesse Barnesba8bbcf2007-11-22 14:14:14 +1000281 retcode = i915_initialize(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 case I915_CLEANUP_DMA:
284 retcode = i915_dma_cleanup(dev);
285 break;
286 case I915_RESUME_DMA:
Dave Airlie0d6aa602006-01-02 20:14:23 +1100287 retcode = i915_dma_resume(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 break;
289 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000290 retcode = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 }
293
294 return retcode;
295}
296
297/* Implement basically the same security restrictions as hardware does
298 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
299 *
300 * Most of the calculations below involve calculating the size of a
301 * particular instruction. It's important to get the size right as
302 * that tells us where the next instruction to check is. Any illegal
303 * instruction detected will be given a size of zero, which is a
304 * signal to abort the rest of the buffer.
305 */
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100306static int validate_cmd(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 switch (((cmd >> 29) & 0x7)) {
309 case 0x0:
310 switch ((cmd >> 23) & 0x3f) {
311 case 0x0:
312 return 1; /* MI_NOOP */
313 case 0x4:
314 return 1; /* MI_FLUSH */
315 default:
316 return 0; /* disallow everything else */
317 }
318 break;
319 case 0x1:
320 return 0; /* reserved */
321 case 0x2:
322 return (cmd & 0xff) + 2; /* 2d commands */
323 case 0x3:
324 if (((cmd >> 24) & 0x1f) <= 0x18)
325 return 1;
326
327 switch ((cmd >> 24) & 0x1f) {
328 case 0x1c:
329 return 1;
330 case 0x1d:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 switch ((cmd >> 16) & 0xff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 case 0x3:
333 return (cmd & 0x1f) + 2;
334 case 0x4:
335 return (cmd & 0xf) + 2;
336 default:
337 return (cmd & 0xffff) + 2;
338 }
339 case 0x1e:
340 if (cmd & (1 << 23))
341 return (cmd & 0xffff) + 1;
342 else
343 return 1;
344 case 0x1f:
345 if ((cmd & (1 << 23)) == 0) /* inline vertices */
346 return (cmd & 0x1ffff) + 2;
347 else if (cmd & (1 << 17)) /* indirect random */
348 if ((cmd & 0xffff) == 0)
349 return 0; /* unknown length, too hard */
350 else
351 return (((cmd & 0xffff) + 1) / 2) + 1;
352 else
353 return 2; /* indirect sequential */
354 default:
355 return 0;
356 }
357 default:
358 return 0;
359 }
360
361 return 0;
362}
363
Robin Schroer1a5036b2014-06-02 16:59:39 +0200364static int i915_emit_cmds(struct drm_device *dev, int *buffer, int dwords)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300366 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100367 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100369 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->buffer->size - 8)
Eric Anholt20caafa2007-08-25 19:22:43 +1000370 return -EINVAL;
Dave Airliede227f52006-01-25 15:31:43 +1100371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 for (i = 0; i < dwords;) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100373 int sz = validate_cmd(buffer[i]);
Robin Schroer1a5036b2014-06-02 16:59:39 +0200374
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100375 if (sz == 0 || i + sz > dwords)
Eric Anholt20caafa2007-08-25 19:22:43 +1000376 return -EINVAL;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100377 i += sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100380 ret = BEGIN_LP_RING((dwords+1)&~1);
381 if (ret)
382 return ret;
383
384 for (i = 0; i < dwords; i++)
385 OUT_RING(buffer[i]);
Dave Airliede227f52006-01-25 15:31:43 +1100386 if (dwords & 1)
387 OUT_RING(0);
388
389 ADVANCE_LP_RING();
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
Eric Anholt673a3942008-07-30 12:06:12 -0700394int
395i915_emit_box(struct drm_device *dev,
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000396 struct drm_clip_rect *box,
397 int DR1, int DR4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100399 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100400 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000402 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
403 box->y2 <= 0 || box->x2 <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 DRM_ERROR("Bad box %d,%d..%d,%d\n",
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000405 box->x1, box->y1, box->x2, box->y2);
Eric Anholt20caafa2007-08-25 19:22:43 +1000406 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100409 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100410 ret = BEGIN_LP_RING(4);
411 if (ret)
412 return ret;
413
Alan Hourihanec29b6692006-08-12 16:29:24 +1000414 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000415 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
416 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000417 OUT_RING(DR4);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000418 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100419 ret = BEGIN_LP_RING(6);
420 if (ret)
421 return ret;
422
Alan Hourihanec29b6692006-08-12 16:29:24 +1000423 OUT_RING(GFX_OP_DRAWRECT_INFO);
424 OUT_RING(DR1);
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000425 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
426 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
Alan Hourihanec29b6692006-08-12 16:29:24 +1000427 OUT_RING(DR4);
428 OUT_RING(0);
Alan Hourihanec29b6692006-08-12 16:29:24 +1000429 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100430 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return 0;
433}
434
Alan Hourihanec29b6692006-08-12 16:29:24 +1000435/* XXX: Emitting the counter should really be moved to part of the IRQ
436 * emit. For now, do it in both places:
437 */
438
Dave Airlie84b1fd12007-07-11 15:53:27 +1000439static void i915_emit_breadcrumb(struct drm_device *dev)
Dave Airliede227f52006-01-25 15:31:43 +1100440{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300441 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000442 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
Dave Airliede227f52006-01-25 15:31:43 +1100443
Daniel Vetter231f42a2012-11-02 19:55:05 +0100444 dev_priv->dri1.counter++;
445 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
446 dev_priv->dri1.counter = 0;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000447 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100448 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Dave Airliede227f52006-01-25 15:31:43 +1100449
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100450 if (BEGIN_LP_RING(4) == 0) {
451 OUT_RING(MI_STORE_DWORD_INDEX);
452 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100453 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100454 OUT_RING(0);
455 ADVANCE_LP_RING();
456 }
Dave Airliede227f52006-01-25 15:31:43 +1100457}
458
Robin Schroer1a5036b2014-06-02 16:59:39 +0200459static int i915_dispatch_cmdbuffer(struct drm_device *dev,
Eric Anholt201361a2009-03-11 12:30:04 -0700460 drm_i915_cmdbuffer_t *cmd,
461 struct drm_clip_rect *cliprects,
462 void *cmdbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 int nbox = cmd->num_cliprects;
465 int i = 0, count, ret;
466
467 if (cmd->sz & 0x3) {
468 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000469 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
472 i915_kernel_lost_context(dev);
473
474 count = nbox ? nbox : 1;
475
476 for (i = 0; i < count; i++) {
477 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000478 ret = i915_emit_box(dev, &cliprects[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 cmd->DR1, cmd->DR4);
480 if (ret)
481 return ret;
482 }
483
Eric Anholt201361a2009-03-11 12:30:04 -0700484 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (ret)
486 return ret;
487 }
488
Dave Airliede227f52006-01-25 15:31:43 +1100489 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 0;
491}
492
Robin Schroer1a5036b2014-06-02 16:59:39 +0200493static int i915_dispatch_batchbuffer(struct drm_device *dev,
494 drm_i915_batchbuffer_t *batch,
Eric Anholt201361a2009-03-11 12:30:04 -0700495 struct drm_clip_rect *cliprects)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100497 struct drm_i915_private *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int nbox = batch->num_cliprects;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100499 int i, count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if ((batch->start | batch->used) & 0x7) {
502 DRM_ERROR("alignment");
Eric Anholt20caafa2007-08-25 19:22:43 +1000503 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 i915_kernel_lost_context(dev);
507
508 count = nbox ? nbox : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 for (i = 0; i < count; i++) {
510 if (i < nbox) {
Chris Wilsonc4e7a412010-11-30 14:10:25 +0000511 ret = i915_emit_box(dev, &cliprects[i],
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100512 batch->DR1, batch->DR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (ret)
514 return ret;
515 }
516
Keith Packard0790d5e2008-07-30 12:28:47 -0700517 if (!IS_I830(dev) && !IS_845G(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100518 ret = BEGIN_LP_RING(2);
519 if (ret)
520 return ret;
521
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100522 if (INTEL_INFO(dev)->gen >= 4) {
Dave Airlie21f16282007-08-07 09:09:51 +1000523 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
524 OUT_RING(batch->start);
525 } else {
526 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
527 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 } else {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100530 ret = BEGIN_LP_RING(4);
531 if (ret)
532 return ret;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 OUT_RING(MI_BATCH_BUFFER);
535 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
536 OUT_RING(batch->start + batch->used - 4);
537 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100539 ADVANCE_LP_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541
Zou Nan hai1cafd342010-06-25 13:40:24 +0800542
Chris Wilsonf00a3dd2010-10-21 14:57:17 +0100543 if (IS_G4X(dev) || IS_GEN5(dev)) {
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100544 if (BEGIN_LP_RING(2) == 0) {
545 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
546 OUT_RING(MI_NOOP);
547 ADVANCE_LP_RING();
548 }
Zou Nan hai1cafd342010-06-25 13:40:24 +0800549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100551 i915_emit_breadcrumb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return 0;
553}
554
Robin Schroer1a5036b2014-06-02 16:59:39 +0200555static int i915_dispatch_flip(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300557 struct drm_i915_private *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000558 struct drm_i915_master_private *master_priv =
559 dev->primary->master->driver_priv;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100560 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dave Airlie7c1c2872008-11-28 14:22:24 +1000562 if (!master_priv->sarea_priv)
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400563 return -EINVAL;
564
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800565 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800566 __func__,
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200567 dev_priv->dri1.current_page,
yakui_zhaobe25ed92009-06-02 14:13:55 +0800568 master_priv->sarea_priv->pf_current_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Airlieaf6061a2008-05-07 12:15:39 +1000570 i915_kernel_lost_context(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100572 ret = BEGIN_LP_RING(10);
573 if (ret)
574 return ret;
575
Jesse Barnes585fb112008-07-29 11:54:06 -0700576 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000577 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dave Airlieaf6061a2008-05-07 12:15:39 +1000579 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
580 OUT_RING(0);
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200581 if (dev_priv->dri1.current_page == 0) {
582 OUT_RING(dev_priv->dri1.back_offset);
583 dev_priv->dri1.current_page = 1;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000584 } else {
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200585 OUT_RING(dev_priv->dri1.front_offset);
586 dev_priv->dri1.current_page = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000587 }
588 OUT_RING(0);
Jesse Barnesac741ab2008-04-22 16:03:07 +1000589
Dave Airlieaf6061a2008-05-07 12:15:39 +1000590 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
591 OUT_RING(0);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100592
Dave Airlieaf6061a2008-05-07 12:15:39 +1000593 ADVANCE_LP_RING();
Jesse Barnesac741ab2008-04-22 16:03:07 +1000594
Daniel Vetter231f42a2012-11-02 19:55:05 +0100595 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000596
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100597 if (BEGIN_LP_RING(4) == 0) {
598 OUT_RING(MI_STORE_DWORD_INDEX);
599 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100600 OUT_RING(dev_priv->dri1.counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100601 OUT_RING(0);
602 ADVANCE_LP_RING();
603 }
Jesse Barnesac741ab2008-04-22 16:03:07 +1000604
Daniel Vetter5d985ac2012-08-12 19:27:13 +0200605 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Chris Wilson1ec14ad2010-12-04 11:30:53 +0000609static int i915_quiescent(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 i915_kernel_lost_context(dev);
Chris Wilson3e960502012-11-27 16:22:54 +0000612 return intel_ring_idle(LP_RING(dev->dev_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Eric Anholtc153f452007-09-03 12:06:45 +1000615static int i915_flush_ioctl(struct drm_device *dev, void *data,
616 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Eric Anholt546b0972008-09-01 16:45:29 -0700618 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200620 if (drm_core_check_feature(dev, DRIVER_MODESET))
621 return -ENODEV;
622
Eric Anholt546b0972008-09-01 16:45:29 -0700623 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
624
625 mutex_lock(&dev->struct_mutex);
626 ret = i915_quiescent(dev);
627 mutex_unlock(&dev->struct_mutex);
628
629 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Eric Anholtc153f452007-09-03 12:06:45 +1000632static int i915_batchbuffer(struct drm_device *dev, void *data,
633 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300635 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100636 struct drm_i915_master_private *master_priv;
637 drm_i915_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000638 drm_i915_batchbuffer_t *batch = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 int ret;
Eric Anholt201361a2009-03-11 12:30:04 -0700640 struct drm_clip_rect *cliprects = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200642 if (drm_core_check_feature(dev, DRIVER_MODESET))
643 return -ENODEV;
644
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100645 master_priv = dev->primary->master->driver_priv;
646 sarea_priv = (drm_i915_sarea_t *) master_priv->sarea_priv;
647
Daniel Vetter87813422012-05-02 11:49:32 +0200648 if (!dev_priv->dri1.allow_batchbuffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 DRM_ERROR("Batchbuffer ioctl disabled\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000650 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800653 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800654 batch->start, batch->used, batch->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Eric Anholt546b0972008-09-01 16:45:29 -0700656 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Eric Anholt201361a2009-03-11 12:30:04 -0700658 if (batch->num_cliprects < 0)
659 return -EINVAL;
660
661 if (batch->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700662 cliprects = kcalloc(batch->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200663 sizeof(*cliprects),
Eric Anholt9a298b22009-03-24 12:23:04 -0700664 GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700665 if (cliprects == NULL)
666 return -ENOMEM;
667
668 ret = copy_from_user(cliprects, batch->cliprects,
669 batch->num_cliprects *
670 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200671 if (ret != 0) {
672 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700673 goto fail_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200674 }
Eric Anholt201361a2009-03-11 12:30:04 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Eric Anholt546b0972008-09-01 16:45:29 -0700677 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700678 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
Eric Anholt546b0972008-09-01 16:45:29 -0700679 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400681 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000682 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700683
684fail_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700685 kfree(cliprects);
Eric Anholt201361a2009-03-11 12:30:04 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return ret;
688}
689
Eric Anholtc153f452007-09-03 12:06:45 +1000690static int i915_cmdbuffer(struct drm_device *dev, void *data,
691 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300693 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100694 struct drm_i915_master_private *master_priv;
695 drm_i915_sarea_t *sarea_priv;
Eric Anholtc153f452007-09-03 12:06:45 +1000696 drm_i915_cmdbuffer_t *cmdbuf = data;
Eric Anholt201361a2009-03-11 12:30:04 -0700697 struct drm_clip_rect *cliprects = NULL;
698 void *batch_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int ret;
700
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800701 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +0800702 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200704 if (drm_core_check_feature(dev, DRIVER_MODESET))
705 return -ENODEV;
706
Daniel Vetter4d10cc02014-02-12 23:50:06 +0100707 master_priv = dev->primary->master->driver_priv;
708 sarea_priv = (drm_i915_sarea_t *) master_priv->sarea_priv;
709
Eric Anholt546b0972008-09-01 16:45:29 -0700710 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Eric Anholt201361a2009-03-11 12:30:04 -0700712 if (cmdbuf->num_cliprects < 0)
713 return -EINVAL;
714
Eric Anholt9a298b22009-03-24 12:23:04 -0700715 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
Eric Anholt201361a2009-03-11 12:30:04 -0700716 if (batch_data == NULL)
717 return -ENOMEM;
718
719 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
Dan Carpenter9927a402010-06-19 15:12:51 +0200720 if (ret != 0) {
721 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700722 goto fail_batch_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200723 }
Eric Anholt201361a2009-03-11 12:30:04 -0700724
725 if (cmdbuf->num_cliprects) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700726 cliprects = kcalloc(cmdbuf->num_cliprects,
Daniel Vetterb14c5672013-09-19 12:18:32 +0200727 sizeof(*cliprects), GFP_KERNEL);
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000728 if (cliprects == NULL) {
729 ret = -ENOMEM;
Eric Anholt201361a2009-03-11 12:30:04 -0700730 goto fail_batch_free;
Owain Ainswortha40e8d32010-02-09 14:25:55 +0000731 }
Eric Anholt201361a2009-03-11 12:30:04 -0700732
733 ret = copy_from_user(cliprects, cmdbuf->cliprects,
734 cmdbuf->num_cliprects *
735 sizeof(struct drm_clip_rect));
Dan Carpenter9927a402010-06-19 15:12:51 +0200736 if (ret != 0) {
737 ret = -EFAULT;
Eric Anholt201361a2009-03-11 12:30:04 -0700738 goto fail_clip_free;
Dan Carpenter9927a402010-06-19 15:12:51 +0200739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
Eric Anholt546b0972008-09-01 16:45:29 -0700742 mutex_lock(&dev->struct_mutex);
Eric Anholt201361a2009-03-11 12:30:04 -0700743 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
Eric Anholt546b0972008-09-01 16:45:29 -0700744 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (ret) {
746 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
Chris Wright355d7f32009-04-17 01:18:55 +0000747 goto fail_clip_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749
Kristian Høgsbergc99b0582008-08-20 11:20:13 -0400750 if (sarea_priv)
Keith Packard0baf8232008-11-08 11:44:14 +1000751 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
Eric Anholt201361a2009-03-11 12:30:04 -0700752
Eric Anholt201361a2009-03-11 12:30:04 -0700753fail_clip_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700754 kfree(cliprects);
Chris Wright355d7f32009-04-17 01:18:55 +0000755fail_batch_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700756 kfree(batch_data);
Eric Anholt201361a2009-03-11 12:30:04 -0700757
758 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Robin Schroer1a5036b2014-06-02 16:59:39 +0200761static int i915_emit_irq(struct drm_device *dev)
Daniel Vetter94888672012-04-26 23:28:08 +0200762{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300763 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200764 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
765
766 i915_kernel_lost_context(dev);
767
768 DRM_DEBUG_DRIVER("\n");
769
Daniel Vetter231f42a2012-11-02 19:55:05 +0100770 dev_priv->dri1.counter++;
771 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
772 dev_priv->dri1.counter = 1;
Daniel Vetter94888672012-04-26 23:28:08 +0200773 if (master_priv->sarea_priv)
Daniel Vetter231f42a2012-11-02 19:55:05 +0100774 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200775
776 if (BEGIN_LP_RING(4) == 0) {
777 OUT_RING(MI_STORE_DWORD_INDEX);
778 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
Daniel Vetter231f42a2012-11-02 19:55:05 +0100779 OUT_RING(dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200780 OUT_RING(MI_USER_INTERRUPT);
781 ADVANCE_LP_RING();
782 }
783
Daniel Vetter231f42a2012-11-02 19:55:05 +0100784 return dev_priv->dri1.counter;
Daniel Vetter94888672012-04-26 23:28:08 +0200785}
786
Robin Schroer1a5036b2014-06-02 16:59:39 +0200787static int i915_wait_irq(struct drm_device *dev, int irq_nr)
Daniel Vetter94888672012-04-26 23:28:08 +0200788{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300789 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200790 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
791 int ret = 0;
Oscar Mateoa4872ba2014-05-22 14:13:33 +0100792 struct intel_engine_cs *ring = LP_RING(dev_priv);
Daniel Vetter94888672012-04-26 23:28:08 +0200793
794 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
795 READ_BREADCRUMB(dev_priv));
796
797 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
798 if (master_priv->sarea_priv)
799 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
800 return 0;
801 }
802
803 if (master_priv->sarea_priv)
804 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
805
806 if (ring->irq_get(ring)) {
Daniel Vetterbfd83032013-12-11 11:34:41 +0100807 DRM_WAIT_ON(ret, ring->irq_queue, 3 * HZ,
Daniel Vetter94888672012-04-26 23:28:08 +0200808 READ_BREADCRUMB(dev_priv) >= irq_nr);
809 ring->irq_put(ring);
810 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
811 ret = -EBUSY;
812
813 if (ret == -EBUSY) {
814 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
Daniel Vetter231f42a2012-11-02 19:55:05 +0100815 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
Daniel Vetter94888672012-04-26 23:28:08 +0200816 }
817
818 return ret;
819}
820
821/* Needs the lock as it touches the ring.
822 */
823static int i915_irq_emit(struct drm_device *dev, void *data,
824 struct drm_file *file_priv)
825{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300826 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200827 drm_i915_irq_emit_t *emit = data;
828 int result;
829
830 if (drm_core_check_feature(dev, DRIVER_MODESET))
831 return -ENODEV;
832
Oscar Mateoee1b1e52014-05-22 14:13:35 +0100833 if (!dev_priv || !LP_RING(dev_priv)->buffer->virtual_start) {
Daniel Vetter94888672012-04-26 23:28:08 +0200834 DRM_ERROR("called with no initialization\n");
835 return -EINVAL;
836 }
837
838 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
839
840 mutex_lock(&dev->struct_mutex);
841 result = i915_emit_irq(dev);
842 mutex_unlock(&dev->struct_mutex);
843
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100844 if (copy_to_user(emit->irq_seq, &result, sizeof(int))) {
Daniel Vetter94888672012-04-26 23:28:08 +0200845 DRM_ERROR("copy_to_user\n");
846 return -EFAULT;
847 }
848
849 return 0;
850}
851
852/* Doesn't need the hardware lock.
853 */
854static int i915_irq_wait(struct drm_device *dev, void *data,
855 struct drm_file *file_priv)
856{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300857 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter94888672012-04-26 23:28:08 +0200858 drm_i915_irq_wait_t *irqwait = data;
859
860 if (drm_core_check_feature(dev, DRIVER_MODESET))
861 return -ENODEV;
862
863 if (!dev_priv) {
864 DRM_ERROR("called with no initialization\n");
865 return -EINVAL;
866 }
867
868 return i915_wait_irq(dev, irqwait->irq_seq);
869}
870
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200871static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
872 struct drm_file *file_priv)
873{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300874 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd1c1edb2012-04-26 23:28:01 +0200875 drm_i915_vblank_pipe_t *pipe = data;
876
877 if (drm_core_check_feature(dev, DRIVER_MODESET))
878 return -ENODEV;
879
880 if (!dev_priv) {
881 DRM_ERROR("called with no initialization\n");
882 return -EINVAL;
883 }
884
885 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
886
887 return 0;
888}
889
890/**
891 * Schedule buffer swap at given vertical blank.
892 */
893static int i915_vblank_swap(struct drm_device *dev, void *data,
894 struct drm_file *file_priv)
895{
896 /* The delayed swap mechanism was fundamentally racy, and has been
897 * removed. The model was that the client requested a delayed flip/swap
898 * from the kernel, then waited for vblank before continuing to perform
899 * rendering. The problem was that the kernel might wake the client
900 * up before it dispatched the vblank swap (since the lock has to be
901 * held while touching the ringbuffer), in which case the client would
902 * clear and start the next frame before the swap occurred, and
903 * flicker would occur in addition to likely missing the vblank.
904 *
905 * In the absence of this ioctl, userland falls back to a correct path
906 * of waiting for a vblank, then dispatching the swap on its own.
907 * Context switching to userland and back is plenty fast enough for
908 * meeting the requirements of vblank swapping.
909 */
910 return -EINVAL;
911}
912
Eric Anholtc153f452007-09-03 12:06:45 +1000913static int i915_flip_bufs(struct drm_device *dev, void *data,
914 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Eric Anholt546b0972008-09-01 16:45:29 -0700916 int ret;
917
Daniel Vettercd9d4e92012-04-24 08:29:42 +0200918 if (drm_core_check_feature(dev, DRIVER_MODESET))
919 return -ENODEV;
920
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800921 DRM_DEBUG_DRIVER("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Eric Anholt546b0972008-09-01 16:45:29 -0700923 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Eric Anholt546b0972008-09-01 16:45:29 -0700925 mutex_lock(&dev->struct_mutex);
926 ret = i915_dispatch_flip(dev);
927 mutex_unlock(&dev->struct_mutex);
928
929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Eric Anholtc153f452007-09-03 12:06:45 +1000932static int i915_getparam(struct drm_device *dev, void *data,
933 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Jani Nikula4c8a4be2014-03-31 14:27:15 +0300935 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000936 drm_i915_getparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int value;
938
939 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000940 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000941 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
Eric Anholtc153f452007-09-03 12:06:45 +1000944 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 case I915_PARAM_IRQ_ACTIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700946 value = dev->pdev->irq ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
948 case I915_PARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +0200949 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 break;
Dave Airlie0d6aa602006-01-02 20:14:23 +1100951 case I915_PARAM_LAST_DISPATCH:
952 value = READ_BREADCRUMB(dev_priv);
953 break;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400954 case I915_PARAM_CHIPSET_ID:
Ville Syrjäläffbab09b2013-10-04 14:53:40 +0300955 value = dev->pdev->device;
Kristian Høgsberged4c9c42008-08-20 11:08:52 -0400956 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700957 case I915_PARAM_HAS_GEM:
Daniel Vetter2e895b12012-04-23 16:50:51 +0200958 value = 1;
Eric Anholt673a3942008-07-30 12:06:12 -0700959 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800960 case I915_PARAM_NUM_FENCES_AVAIL:
961 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
962 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200963 case I915_PARAM_HAS_OVERLAY:
964 value = dev_priv->overlay ? 1 : 0;
965 break;
Jesse Barnese9560f72009-11-19 10:49:07 -0800966 case I915_PARAM_HAS_PAGEFLIPPING:
967 value = 1;
968 break;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500969 case I915_PARAM_HAS_EXECBUF2:
970 /* depends on GEM */
Daniel Vetter2e895b12012-04-23 16:50:51 +0200971 value = 1;
Jesse Barnes76446ca2009-12-17 22:05:42 -0500972 break;
Zou Nan haie3a815f2010-05-31 13:58:47 +0800973 case I915_PARAM_HAS_BSD:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100974 value = intel_ring_initialized(&dev_priv->ring[VCS]);
Zou Nan haie3a815f2010-05-31 13:58:47 +0800975 break;
Chris Wilson549f7362010-10-19 11:19:32 +0100976 case I915_PARAM_HAS_BLT:
Chris Wilsonedc912f2012-05-11 14:29:32 +0100977 value = intel_ring_initialized(&dev_priv->ring[BCS]);
Chris Wilson549f7362010-10-19 11:19:32 +0100978 break;
Xiang, Haihaoa1f2cc72013-05-28 19:22:34 -0700979 case I915_PARAM_HAS_VEBOX:
980 value = intel_ring_initialized(&dev_priv->ring[VECS]);
981 break;
Chris Wilsona00b10c2010-09-24 21:15:47 +0100982 case I915_PARAM_HAS_RELAXED_FENCING:
983 value = 1;
984 break;
Daniel Vetterbbf0c6b2010-12-05 11:30:40 +0100985 case I915_PARAM_HAS_COHERENT_RINGS:
986 value = 1;
987 break;
Chris Wilson72bfa192010-12-19 11:42:05 +0000988 case I915_PARAM_HAS_EXEC_CONSTANTS:
989 value = INTEL_INFO(dev)->gen >= 4;
990 break;
Chris Wilson271d81b2011-03-01 15:24:41 +0000991 case I915_PARAM_HAS_RELAXED_DELTA:
992 value = 1;
993 break;
Eric Anholtae662d32012-01-03 09:23:29 -0800994 case I915_PARAM_HAS_GEN7_SOL_RESET:
995 value = 1;
996 break;
Eugeni Dodonov3d29b842012-01-17 14:43:53 -0200997 case I915_PARAM_HAS_LLC:
998 value = HAS_LLC(dev);
999 break;
Chris Wilson651d7942013-08-08 14:41:10 +01001000 case I915_PARAM_HAS_WT:
1001 value = HAS_WT(dev);
1002 break;
Daniel Vetter777ee962012-02-15 23:50:25 +01001003 case I915_PARAM_HAS_ALIASING_PPGTT:
Daniel Vetter896ab1a2014-08-06 15:04:51 +02001004 value = USES_PPGTT(dev);
Daniel Vetter777ee962012-02-15 23:50:25 +01001005 break;
Ben Widawsky172cf152012-06-05 15:24:25 -07001006 case I915_PARAM_HAS_WAIT_TIMEOUT:
1007 value = 1;
1008 break;
Chris Wilson2fedbff2012-08-08 10:23:22 +01001009 case I915_PARAM_HAS_SEMAPHORES:
1010 value = i915_semaphore_is_enabled(dev);
1011 break;
Dave Airlieec6f1bb2012-08-16 10:15:34 +10001012 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
1013 value = 1;
1014 break;
Chris Wilsond7d4eed2012-10-17 12:09:54 +01001015 case I915_PARAM_HAS_SECURE_BATCHES:
1016 value = capable(CAP_SYS_ADMIN);
1017 break;
Daniel Vetterb45305f2012-12-17 16:21:27 +01001018 case I915_PARAM_HAS_PINNED_BATCHES:
1019 value = 1;
1020 break;
Daniel Vettered5982e2013-01-17 22:23:36 +01001021 case I915_PARAM_HAS_EXEC_NO_RELOC:
1022 value = 1;
1023 break;
Chris Wilsoneef90cc2013-01-08 10:53:17 +00001024 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
1025 value = 1;
1026 break;
Brad Volkind728c8e2014-02-18 10:15:56 -08001027 case I915_PARAM_CMD_PARSER_VERSION:
1028 value = i915_cmd_parser_get_version();
1029 break;
Chris Wilson6a2c4232014-11-04 04:51:40 -08001030 case I915_PARAM_HAS_COHERENT_PHYS_GTT:
1031 value = 1;
1032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 default:
Ben Widawskye29c32d2013-05-31 11:28:45 -07001034 DRM_DEBUG("Unknown parameter %d\n", param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001035 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
Daniel Vetter1d6ac182013-12-11 11:34:44 +01001038 if (copy_to_user(param->value, &value, sizeof(int))) {
1039 DRM_ERROR("copy_to_user failed\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001040 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 return 0;
1044}
1045
Eric Anholtc153f452007-09-03 12:06:45 +10001046static int i915_setparam(struct drm_device *dev, void *data,
1047 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001049 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001050 drm_i915_setparam_t *param = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001053 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
Eric Anholtc153f452007-09-03 12:06:45 +10001057 switch (param->param) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 break;
1060 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 break;
1062 case I915_SETPARAM_ALLOW_BATCHBUFFER:
Daniel Vetter87813422012-05-02 11:49:32 +02001063 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 break;
Jesse Barnes0f973f22009-01-26 17:10:45 -08001065 case I915_SETPARAM_NUM_USED_FENCES:
1066 if (param->value > dev_priv->num_fence_regs ||
1067 param->value < 0)
1068 return -EINVAL;
1069 /* Userspace can use first N regs */
1070 dev_priv->fence_reg_start = param->value;
1071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 default:
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001073 DRM_DEBUG_DRIVER("unknown parameter %d\n",
yakui_zhaobe25ed92009-06-02 14:13:55 +08001074 param->param);
Eric Anholt20caafa2007-08-25 19:22:43 +10001075 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 return 0;
1079}
1080
Eric Anholtc153f452007-09-03 12:06:45 +10001081static int i915_set_status_page(struct drm_device *dev, void *data,
1082 struct drm_file *file_priv)
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001083{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001084 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001085 drm_i915_hws_addr_t *hws = data;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01001086 struct intel_engine_cs *ring;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001087
Daniel Vettercd9d4e92012-04-24 08:29:42 +02001088 if (drm_core_check_feature(dev, DRIVER_MODESET))
1089 return -ENODEV;
1090
Zhenyu Wangb39d50e2008-02-19 20:59:09 +10001091 if (!I915_NEED_GFX_HWS(dev))
1092 return -EINVAL;
1093
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001094 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001095 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001096 return -EINVAL;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001097 }
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001098
Jesse Barnes79e53942008-11-07 14:24:08 -08001099 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1100 WARN(1, "tried to set status page when mode setting active\n");
1101 return 0;
1102 }
1103
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001104 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001105
Mika Kuoppala4f1ba0f2012-11-12 14:20:19 +02001106 ring = LP_RING(dev_priv);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001107 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
Eric Anholtc153f452007-09-03 12:06:45 +10001108
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001109 dev_priv->dri1.gfx_hws_cpu_addr =
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001110 ioremap_wc(dev_priv->gtt.mappable_base + hws->addr, 4096);
Daniel Vetter316d3882012-04-26 23:28:15 +02001111 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001112 i915_dma_cleanup(dev);
Eric Anholte20f9c62010-05-26 14:51:06 -07001113 ring->status_page.gfx_addr = 0;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001114 DRM_ERROR("can not ioremap virtual address for"
1115 " G33 hw status page\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001116 return -ENOMEM;
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001117 }
Daniel Vetter316d3882012-04-26 23:28:15 +02001118
1119 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
Zou Nan hai8187a2b2010-05-21 09:08:55 +08001120 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001121
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001122 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001123 ring->status_page.gfx_addr);
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001124 DRM_DEBUG_DRIVER("load hws at %p\n",
Eric Anholte20f9c62010-05-26 14:51:06 -07001125 ring->status_page.page_addr);
Wang Zhenyudc7a9312007-06-10 15:58:19 +10001126 return 0;
1127}
1128
Dave Airlieec2a4c32009-08-04 11:43:41 +10001129static int i915_get_bridge_dev(struct drm_device *dev)
1130{
1131 struct drm_i915_private *dev_priv = dev->dev_private;
1132
Akshay Joshi0206e352011-08-16 15:34:10 -04001133 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
Dave Airlieec2a4c32009-08-04 11:43:41 +10001134 if (!dev_priv->bridge_dev) {
1135 DRM_ERROR("bridge device not found\n");
1136 return -1;
1137 }
1138 return 0;
1139}
1140
Zhenyu Wangc48044112009-12-17 14:48:43 +08001141#define MCHBAR_I915 0x44
1142#define MCHBAR_I965 0x48
1143#define MCHBAR_SIZE (4*4096)
1144
1145#define DEVEN_REG 0x54
1146#define DEVEN_MCHBAR_EN (1 << 28)
1147
1148/* Allocate space for the MCH regs if needed, return nonzero on error */
1149static int
1150intel_alloc_mchbar_resource(struct drm_device *dev)
1151{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001152 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001153 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001154 u32 temp_lo, temp_hi = 0;
1155 u64 mchbar_addr;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001156 int ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001157
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001158 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001159 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1160 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1161 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1162
1163 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1164#ifdef CONFIG_PNP
1165 if (mchbar_addr &&
Chris Wilsona25c25c2010-08-20 14:36:45 +01001166 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1167 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001168#endif
1169
1170 /* Get some space for it */
Chris Wilsona25c25c2010-08-20 14:36:45 +01001171 dev_priv->mch_res.name = "i915 MCHBAR";
1172 dev_priv->mch_res.flags = IORESOURCE_MEM;
1173 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1174 &dev_priv->mch_res,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001175 MCHBAR_SIZE, MCHBAR_SIZE,
1176 PCIBIOS_MIN_MEM,
Chris Wilsona25c25c2010-08-20 14:36:45 +01001177 0, pcibios_align_resource,
Zhenyu Wangc48044112009-12-17 14:48:43 +08001178 dev_priv->bridge_dev);
1179 if (ret) {
1180 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1181 dev_priv->mch_res.start = 0;
Chris Wilsona25c25c2010-08-20 14:36:45 +01001182 return ret;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001183 }
1184
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001185 if (INTEL_INFO(dev)->gen >= 4)
Zhenyu Wangc48044112009-12-17 14:48:43 +08001186 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1187 upper_32_bits(dev_priv->mch_res.start));
1188
1189 pci_write_config_dword(dev_priv->bridge_dev, reg,
1190 lower_32_bits(dev_priv->mch_res.start));
Chris Wilsona25c25c2010-08-20 14:36:45 +01001191 return 0;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001192}
1193
1194/* Setup MCHBAR if possible, return true if we should disable it again */
1195static void
1196intel_setup_mchbar(struct drm_device *dev)
1197{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001198 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001199 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001200 u32 temp;
1201 bool enabled;
1202
Jesse Barnes11ea8b72014-03-03 14:27:57 -08001203 if (IS_VALLEYVIEW(dev))
1204 return;
1205
Zhenyu Wangc48044112009-12-17 14:48:43 +08001206 dev_priv->mchbar_need_disable = false;
1207
1208 if (IS_I915G(dev) || IS_I915GM(dev)) {
1209 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1210 enabled = !!(temp & DEVEN_MCHBAR_EN);
1211 } else {
1212 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1213 enabled = temp & 1;
1214 }
1215
1216 /* If it's already enabled, don't have to do anything */
1217 if (enabled)
1218 return;
1219
1220 if (intel_alloc_mchbar_resource(dev))
1221 return;
1222
1223 dev_priv->mchbar_need_disable = true;
1224
1225 /* Space is allocated or reserved, so enable it. */
1226 if (IS_I915G(dev) || IS_I915GM(dev)) {
1227 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1228 temp | DEVEN_MCHBAR_EN);
1229 } else {
1230 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1231 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1232 }
1233}
1234
1235static void
1236intel_teardown_mchbar(struct drm_device *dev)
1237{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001238 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001239 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
Zhenyu Wangc48044112009-12-17 14:48:43 +08001240 u32 temp;
1241
1242 if (dev_priv->mchbar_need_disable) {
1243 if (IS_I915G(dev) || IS_I915GM(dev)) {
1244 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1245 temp &= ~DEVEN_MCHBAR_EN;
1246 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1247 } else {
1248 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1249 temp &= ~1;
1250 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1251 }
1252 }
1253
1254 if (dev_priv->mch_res.start)
1255 release_resource(&dev_priv->mch_res);
1256}
1257
Dave Airlie28d52042009-09-21 14:33:58 +10001258/* true = enable decode, false = disable decoder */
1259static unsigned int i915_vga_set_decode(void *cookie, bool state)
1260{
1261 struct drm_device *dev = cookie;
1262
1263 intel_modeset_vga_set_state(dev, state);
1264 if (state)
1265 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1266 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1267 else
1268 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1269}
1270
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001271static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1272{
1273 struct drm_device *dev = pci_get_drvdata(pdev);
1274 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
Robin Schroer1a5036b2014-06-02 16:59:39 +02001275
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001276 if (state == VGA_SWITCHEROO_ON) {
Joe Perchesa70491c2012-03-18 13:00:11 -07001277 pr_info("switched on\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001278 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001279 /* i915 resume handler doesn't set to D0 */
1280 pci_set_power_state(dev->pdev, PCI_D0);
Imre Deakfc49b3d2014-10-23 19:23:27 +03001281 i915_resume_legacy(dev);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001282 dev->switch_power_state = DRM_SWITCH_POWER_ON;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001283 } else {
Joe Perchesa70491c2012-03-18 13:00:11 -07001284 pr_err("switched off\n");
Dave Airlie5bcf7192010-12-07 09:20:40 +10001285 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Imre Deakfc49b3d2014-10-23 19:23:27 +03001286 i915_suspend_legacy(dev, pmm);
Dave Airlie5bcf7192010-12-07 09:20:40 +10001287 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001288 }
1289}
1290
1291static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1292{
1293 struct drm_device *dev = pci_get_drvdata(pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001294
Daniel Vetterfc8fd402013-11-03 20:46:34 +01001295 /*
1296 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1297 * locking inversion with the driver load path. And the access here is
1298 * completely racy anyway. So don't bother with locking for now.
1299 */
1300 return dev->open_count == 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001301}
1302
Takashi Iwai26ec6852012-05-11 07:51:17 +02001303static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1304 .set_gpu_state = i915_switcheroo_set_state,
1305 .reprobe = NULL,
1306 .can_switch = i915_switcheroo_can_switch,
1307};
1308
Chris Wilson2c7111d2011-03-29 10:40:27 +01001309static int i915_load_modeset_init(struct drm_device *dev)
1310{
1311 struct drm_i915_private *dev_priv = dev->dev_private;
1312 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001313
Bryan Freed6d139a82010-10-14 09:14:51 +01001314 ret = intel_parse_bios(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001315 if (ret)
1316 DRM_INFO("failed to find VBIOS tables\n");
1317
Chris Wilson934f992c2011-01-20 13:09:12 +00001318 /* If we have > 1 VGA cards, then we need to arbitrate access
1319 * to the common VGA resources.
1320 *
1321 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1322 * then we do not take part in VGA arbitration and the
1323 * vga_client_register() fails with -ENODEV.
1324 */
Dave Airlieebff5fa92013-10-11 15:12:04 +10001325 ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
1326 if (ret && ret != -ENODEV)
1327 goto out;
Dave Airlie28d52042009-09-21 14:33:58 +10001328
Jesse Barnes723bfd72010-10-07 16:01:13 -07001329 intel_register_dsm_handler();
1330
Dave Airlie0d697042012-09-10 12:28:36 +10001331 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001332 if (ret)
Chris Wilson5a793952010-06-06 10:50:03 +01001333 goto cleanup_vga_client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001334
Chris Wilson9797fbf2012-04-24 15:47:39 +01001335 /* Initialise stolen first so that we may reserve preallocated
1336 * objects for the BIOS to KMS transition.
1337 */
1338 ret = i915_gem_init_stolen(dev);
1339 if (ret)
1340 goto cleanup_vga_switcheroo;
1341
Imre Deake13192f2014-02-18 00:02:15 +02001342 intel_power_domains_init_hw(dev_priv);
1343
Daniel Vetter2aeb7d32014-09-30 10:56:43 +02001344 ret = intel_irq_install(dev_priv);
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001345 if (ret)
1346 goto cleanup_gem_stolen;
1347
1348 /* Important: The output setup functions called by modeset_init need
1349 * working irqs for e.g. gmbus and dp aux transfers. */
Jesse Barnesb01f2c32009-12-11 11:07:17 -08001350 intel_modeset_init(dev);
1351
Chris Wilson1070a422012-04-24 15:47:41 +01001352 ret = i915_gem_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001353 if (ret)
Imre Deak713028b2014-04-25 17:28:00 +03001354 goto cleanup_irq;
Chris Wilson2c7111d2011-03-29 10:40:27 +01001355
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001356 intel_modeset_gem_init(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001357
Jesse Barnes79e53942008-11-07 14:24:08 -08001358 /* Always safe in the mode setting case. */
1359 /* FIXME: do pre/post-mode set stuff in core KMS code */
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001360 dev->vblank_disable_allowed = true;
Imre Deak713028b2014-04-25 17:28:00 +03001361 if (INTEL_INFO(dev)->num_pipes == 0)
Ben Widawskye3c74752013-04-05 13:12:39 -07001362 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001363
Chris Wilson5a793952010-06-06 10:50:03 +01001364 ret = intel_fbdev_init(dev);
1365 if (ret)
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001366 goto cleanup_gem;
1367
1368 /* Only enable hotplug handling once the fbdev is fully set up. */
Daniel Vetterb9632912014-09-30 10:56:44 +02001369 intel_hpd_init(dev_priv);
Daniel Vetter20afbda2012-12-11 14:05:07 +01001370
1371 /*
1372 * Some ports require correctly set-up hpd registers for detection to
1373 * work properly (leading to ghost connected connector status), e.g. VGA
1374 * on gm45. Hence we can only set up the initial fbdev config after hpd
1375 * irqs are fully enabled. Now we should scan for the initial config
1376 * only once hotplug handling is enabled, but due to screwed-up locking
1377 * around kms/fbdev init we can't protect the fdbev initial config
1378 * scanning against hotplug events. Hence do this first and ignore the
1379 * tiny window where we will loose hotplug notifactions.
1380 */
Jesse Barnesd1d70672014-05-28 14:39:03 -07001381 async_schedule(intel_fbdev_initial_config, dev_priv);
Daniel Vetter20afbda2012-12-11 14:05:07 +01001382
Dave Airlieeb1f8e42010-05-07 06:42:51 +00001383 drm_kms_helper_poll_init(dev);
Chris Wilson87acb0a2010-10-19 10:13:00 +01001384
Jesse Barnes79e53942008-11-07 14:24:08 -08001385 return 0;
1386
Chris Wilson2c7111d2011-03-29 10:40:27 +01001387cleanup_gem:
1388 mutex_lock(&dev->struct_mutex);
1389 i915_gem_cleanup_ringbuffer(dev);
Ben Widawsky55d23282013-05-25 12:26:39 -07001390 i915_gem_context_fini(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01001391 mutex_unlock(&dev->struct_mutex);
Imre Deak713028b2014-04-25 17:28:00 +03001392cleanup_irq:
Daniel Vetter52d7ece2012-12-01 21:03:22 +01001393 drm_irq_uninstall(dev);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001394cleanup_gem_stolen:
1395 i915_gem_cleanup_stolen(dev);
Chris Wilson5a793952010-06-06 10:50:03 +01001396cleanup_vga_switcheroo:
1397 vga_switcheroo_unregister_client(dev->pdev);
1398cleanup_vga_client:
1399 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001400out:
1401 return ret;
1402}
1403
Dave Airlie7c1c2872008-11-28 14:22:24 +10001404int i915_master_create(struct drm_device *dev, struct drm_master *master)
1405{
1406 struct drm_i915_master_private *master_priv;
1407
Eric Anholt9a298b22009-03-24 12:23:04 -07001408 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001409 if (!master_priv)
1410 return -ENOMEM;
1411
1412 master->driver_priv = master_priv;
1413 return 0;
1414}
1415
1416void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1417{
1418 struct drm_i915_master_private *master_priv = master->driver_priv;
1419
1420 if (!master_priv)
1421 return;
1422
Eric Anholt9a298b22009-03-24 12:23:04 -07001423 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001424
1425 master->driver_priv = NULL;
1426}
1427
Daniel Vetter243eaf32013-12-17 10:00:54 +01001428#if IS_ENABLED(CONFIG_FB)
Chris Wilsonf96de582013-12-16 15:57:40 +00001429static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
Daniel Vettere1887192012-06-12 11:28:17 +02001430{
1431 struct apertures_struct *ap;
1432 struct pci_dev *pdev = dev_priv->dev->pdev;
1433 bool primary;
Chris Wilsonf96de582013-12-16 15:57:40 +00001434 int ret;
Daniel Vettere1887192012-06-12 11:28:17 +02001435
1436 ap = alloc_apertures(1);
1437 if (!ap)
Chris Wilsonf96de582013-12-16 15:57:40 +00001438 return -ENOMEM;
Daniel Vettere1887192012-06-12 11:28:17 +02001439
Ben Widawskydabb7a92013-01-17 12:45:16 -08001440 ap->ranges[0].base = dev_priv->gtt.mappable_base;
Ben Widawskyf64e2922013-05-25 12:26:36 -07001441 ap->ranges[0].size = dev_priv->gtt.mappable_end;
Ben Widawsky93d18792013-01-17 12:45:17 -08001442
Daniel Vettere1887192012-06-12 11:28:17 +02001443 primary =
1444 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1445
Chris Wilsonf96de582013-12-16 15:57:40 +00001446 ret = remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
Daniel Vettere1887192012-06-12 11:28:17 +02001447
1448 kfree(ap);
Chris Wilsonf96de582013-12-16 15:57:40 +00001449
1450 return ret;
Daniel Vettere1887192012-06-12 11:28:17 +02001451}
Daniel Vetter4520f532013-10-09 09:18:51 +02001452#else
Chris Wilsonf96de582013-12-16 15:57:40 +00001453static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
Daniel Vetter4520f532013-10-09 09:18:51 +02001454{
Chris Wilsonf96de582013-12-16 15:57:40 +00001455 return 0;
Daniel Vetter4520f532013-10-09 09:18:51 +02001456}
1457#endif
Daniel Vettere1887192012-06-12 11:28:17 +02001458
Daniel Vettera4de0522014-06-05 16:20:46 +02001459#if !defined(CONFIG_VGA_CONSOLE)
1460static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1461{
1462 return 0;
1463}
1464#elif !defined(CONFIG_DUMMY_CONSOLE)
1465static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1466{
1467 return -ENODEV;
1468}
1469#else
1470static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
1471{
Daniel Vetter1bb9e632014-07-08 10:02:43 +02001472 int ret = 0;
Daniel Vettera4de0522014-06-05 16:20:46 +02001473
1474 DRM_INFO("Replacing VGA console driver\n");
1475
1476 console_lock();
Daniel Vetter1bb9e632014-07-08 10:02:43 +02001477 if (con_is_bound(&vga_con))
1478 ret = do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES - 1, 1);
Daniel Vettera4de0522014-06-05 16:20:46 +02001479 if (ret == 0) {
1480 ret = do_unregister_con_driver(&vga_con);
1481
1482 /* Ignore "already unregistered". */
1483 if (ret == -ENODEV)
1484 ret = 0;
1485 }
1486 console_unlock();
1487
1488 return ret;
1489}
1490#endif
1491
Daniel Vetterc96ea642012-08-08 22:01:51 +02001492static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1493{
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001494 const struct intel_device_info *info = &dev_priv->info;
Daniel Vetterc96ea642012-08-08 22:01:51 +02001495
Damien Lespiaue2a58002013-04-23 16:38:34 +01001496#define PRINT_S(name) "%s"
1497#define SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001498#define PRINT_FLAG(name) info->name ? #name "," : ""
1499#define SEP_COMMA ,
Ville Syrjälä19c656a2014-06-13 15:39:56 +03001500 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x rev=0x%02x flags="
Damien Lespiaue2a58002013-04-23 16:38:34 +01001501 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
Daniel Vetterc96ea642012-08-08 22:01:51 +02001502 info->gen,
1503 dev_priv->dev->pdev->device,
Ville Syrjälä19c656a2014-06-13 15:39:56 +03001504 dev_priv->dev->pdev->revision,
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001505 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
Damien Lespiaue2a58002013-04-23 16:38:34 +01001506#undef PRINT_S
1507#undef SEP_EMPTY
Damien Lespiau79fc46d2013-04-23 16:37:17 +01001508#undef PRINT_FLAG
1509#undef SEP_COMMA
Daniel Vetterc96ea642012-08-08 22:01:51 +02001510}
1511
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001512/*
1513 * Determine various intel_device_info fields at runtime.
1514 *
1515 * Use it when either:
1516 * - it's judged too laborious to fill n static structures with the limit
1517 * when a simple if statement does the job,
1518 * - run-time checks (eg read fuse/strap registers) are needed.
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001519 *
1520 * This function needs to be called:
1521 * - after the MMIO has been setup as we are reading registers,
1522 * - after the PCH has been detected,
1523 * - before the first usage of the fields it can tweak.
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001524 */
1525static void intel_device_info_runtime_init(struct drm_device *dev)
1526{
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001527 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001528 struct intel_device_info *info;
Damien Lespiaud615a162014-03-03 17:31:48 +00001529 enum pipe pipe;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001530
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001531 info = (struct intel_device_info *)&dev_priv->info;
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001532
Damien Lespiau1fc8ac32014-02-12 19:13:31 +00001533 if (IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen == 9)
Damien Lespiau055e3932014-08-18 13:49:10 +01001534 for_each_pipe(dev_priv, pipe)
Damien Lespiaud615a162014-03-03 17:31:48 +00001535 info->num_sprites[pipe] = 2;
1536 else
Damien Lespiau055e3932014-08-18 13:49:10 +01001537 for_each_pipe(dev_priv, pipe)
Damien Lespiaud615a162014-03-03 17:31:48 +00001538 info->num_sprites[pipe] = 1;
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001539
Damien Lespiaua0bae572014-02-10 17:20:55 +00001540 if (i915.disable_display) {
1541 DRM_INFO("Display disabled (module parameter)\n");
1542 info->num_pipes = 0;
1543 } else if (info->num_pipes > 0 &&
1544 (INTEL_INFO(dev)->gen == 7 || INTEL_INFO(dev)->gen == 8) &&
1545 !IS_VALLEYVIEW(dev)) {
Damien Lespiau658ac4c2014-02-10 17:19:45 +00001546 u32 fuse_strap = I915_READ(FUSE_STRAP);
1547 u32 sfuse_strap = I915_READ(SFUSE_STRAP);
1548
1549 /*
1550 * SFUSE_STRAP is supposed to have a bit signalling the display
1551 * is fused off. Unfortunately it seems that, at least in
1552 * certain cases, fused off display means that PCH display
1553 * reads don't land anywhere. In that case, we read 0s.
1554 *
1555 * On CPT/PPT, we can detect this case as SFUSE_STRAP_FUSE_LOCK
1556 * should be set when taking over after the firmware.
1557 */
1558 if (fuse_strap & ILK_INTERNAL_DISPLAY_DISABLE ||
1559 sfuse_strap & SFUSE_STRAP_DISPLAY_DISABLED ||
1560 (dev_priv->pch_type == PCH_CPT &&
1561 !(sfuse_strap & SFUSE_STRAP_FUSE_LOCK))) {
1562 DRM_INFO("Display fused off, disabling\n");
1563 info->num_pipes = 0;
1564 }
1565 }
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001566}
1567
Eric Anholt63ee41d2010-12-20 18:40:06 -08001568/**
Jesse Barnes79e53942008-11-07 14:24:08 -08001569 * i915_driver_load - setup chip and create an initial config
1570 * @dev: DRM device
1571 * @flags: startup flags
1572 *
1573 * The driver load routine has to do several things:
1574 * - drive output discovery via intel_modeset_init()
1575 * - initialize the memory manager
1576 * - allocate initial config memory
1577 * - setup the DRM framebuffer with the allocated memory
1578 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001579int i915_driver_load(struct drm_device *dev, unsigned long flags)
Dave Airlie22eae942005-11-10 22:16:34 +11001580{
Luca Tettamantiea059a12010-04-08 21:41:59 +02001581 struct drm_i915_private *dev_priv;
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001582 struct intel_device_info *info, *device_info;
Chris Wilson934d6082012-09-14 11:57:46 +01001583 int ret = 0, mmio_bar, mmio_size;
Daniel Vetter9021f282012-03-26 09:45:41 +02001584 uint32_t aperture_size;
Chris Wilsonfe669bf2010-11-23 12:09:30 +00001585
Daniel Vetter26394d92012-03-26 21:33:18 +02001586 info = (struct intel_device_info *) flags;
1587
1588 /* Refuse to load on gen6+ without kms enabled. */
Jani Nikulae147acc2013-10-10 15:25:37 +03001589 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET)) {
1590 DRM_INFO("Your hardware requires kernel modesetting (KMS)\n");
1591 DRM_INFO("See CONFIG_DRM_I915_KMS, nomodeset, and i915.modeset parameters\n");
Daniel Vetter26394d92012-03-26 21:33:18 +02001592 return -ENODEV;
Jani Nikulae147acc2013-10-10 15:25:37 +03001593 }
Daniel Vetter26394d92012-03-26 21:33:18 +02001594
Daniel Vetter24986ee2013-12-11 11:34:33 +01001595 /* UMS needs agp support. */
1596 if (!drm_core_check_feature(dev, DRIVER_MODESET) && !dev->agp)
1597 return -EINVAL;
1598
Daniel Vetterb14c5672013-09-19 12:18:32 +02001599 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001600 if (dev_priv == NULL)
1601 return -ENOMEM;
1602
Damien Lespiau755f68f2014-07-10 14:52:43 +01001603 dev->dev_private = dev_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001604 dev_priv->dev = dev;
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001605
Chris Wilson87f1f462014-08-09 19:18:42 +01001606 /* Setup the write-once "constant" device info */
Damien Lespiau5c969aa2014-02-07 19:12:48 +00001607 device_info = (struct intel_device_info *)&dev_priv->info;
Chris Wilson87f1f462014-08-09 19:18:42 +01001608 memcpy(device_info, info, sizeof(dev_priv->info));
1609 device_info->device_id = dev->pdev->device;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001610
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001611 spin_lock_init(&dev_priv->irq_lock);
1612 spin_lock_init(&dev_priv->gpu_error.lock);
Daniel Vetter07f11d42014-09-15 14:35:09 +02001613 mutex_init(&dev_priv->backlight_lock);
Chris Wilson907b28c2013-07-19 20:36:52 +01001614 spin_lock_init(&dev_priv->uncore.lock);
Daniel Vetterc20e8352013-07-24 22:40:23 +02001615 spin_lock_init(&dev_priv->mm.object_stat_lock);
Sourab Gupta84c33a62014-06-02 16:47:17 +05301616 spin_lock_init(&dev_priv->mmio_flip_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001617 mutex_init(&dev_priv->dpio_lock);
Konstantin Khlebnikov7dcd2672013-07-17 10:22:58 +04001618 mutex_init(&dev_priv->modeset_restore_lock);
1619
Daniel Vetterf742a552013-12-06 10:17:53 +01001620 intel_pm_setup(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03001621
Damien Lespiau07144422013-10-15 18:55:40 +01001622 intel_display_crc_init(dev);
1623
Daniel Vetterc96ea642012-08-08 22:01:51 +02001624 i915_dump_device_info(dev_priv);
1625
Paulo Zanonied1c9e22013-08-12 14:34:08 -03001626 /* Not all pre-production machines fall into this category, only the
1627 * very first ones. Almost everything should work, except for maybe
1628 * suspend/resume. And we don't implement workarounds that affect only
1629 * pre-production machines. */
1630 if (IS_HSW_EARLY_SDV(dev))
1631 DRM_INFO("This is an early pre-production Haswell machine. "
1632 "It may not be fully functional.\n");
1633
Dave Airlieec2a4c32009-08-04 11:43:41 +10001634 if (i915_get_bridge_dev(dev)) {
1635 ret = -EIO;
1636 goto free_priv;
1637 }
1638
Ben Widawsky1e1bd0f2013-04-08 18:43:49 -07001639 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1640 /* Before gen4, the registers and the GTT are behind different BARs.
1641 * However, from gen4 onwards, the registers and the GTT are shared
1642 * in the same BAR, so we want to restrict this ioremap from
1643 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1644 * the register BAR remains the same size for all the earlier
1645 * generations up to Ironlake.
1646 */
1647 if (info->gen < 5)
1648 mmio_size = 512*1024;
1649 else
1650 mmio_size = 2*1024*1024;
1651
1652 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
1653 if (!dev_priv->regs) {
1654 DRM_ERROR("failed to map registers\n");
1655 ret = -EIO;
1656 goto put_bridge;
1657 }
1658
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001659 /* This must be called before any calls to HAS_PCH_* */
1660 intel_detect_pch(dev);
1661
1662 intel_uncore_init(dev);
1663
Ben Widawskye76e9ae2012-11-04 09:21:27 -08001664 ret = i915_gem_gtt_init(dev);
1665 if (ret)
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001666 goto out_regs;
Daniel Vettere1887192012-06-12 11:28:17 +02001667
Daniel Vettera4de0522014-06-05 16:20:46 +02001668 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1669 ret = i915_kick_out_vgacon(dev_priv);
1670 if (ret) {
1671 DRM_ERROR("failed to remove conflicting VGA console\n");
1672 goto out_gtt;
1673 }
1674
Chris Wilsonf96de582013-12-16 15:57:40 +00001675 ret = i915_kick_out_firmware_fb(dev_priv);
1676 if (ret) {
1677 DRM_ERROR("failed to remove conflicting framebuffer drivers\n");
1678 goto out_gtt;
1679 }
Daniel Vettera4de0522014-06-05 16:20:46 +02001680 }
Daniel Vettere1887192012-06-12 11:28:17 +02001681
Dave Airlie466e69b2011-12-19 11:15:29 +00001682 pci_set_master(dev->pdev);
1683
Daniel Vetter9f82d232010-08-30 21:25:23 +02001684 /* overlay on gen2 is broken and can't address above 1G */
1685 if (IS_GEN2(dev))
1686 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1687
Jan Niehusmann6927faf2011-03-01 23:24:16 +01001688 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1689 * using 32bit addressing, overwriting memory if HWS is located
1690 * above 4GB.
1691 *
1692 * The documentation also mentions an issue with undefined
1693 * behaviour if any general state is accessed within a page above 4GB,
1694 * which also needs to be handled carefully.
1695 */
1696 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1697 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1698
Ben Widawsky93d18792013-01-17 12:45:17 -08001699 aperture_size = dev_priv->gtt.mappable_end;
Chris Wilson71e93392010-10-27 18:46:52 +01001700
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001701 dev_priv->gtt.mappable =
1702 io_mapping_create_wc(dev_priv->gtt.mappable_base,
Daniel Vetterdd2757f2012-06-07 15:55:57 +02001703 aperture_size);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001704 if (dev_priv->gtt.mappable == NULL) {
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001705 ret = -EIO;
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001706 goto out_gtt;
Venkatesh Pallipadi6644107d2009-02-24 17:35:11 -08001707 }
1708
Ben Widawsky911bdf02013-06-27 16:30:23 -07001709 dev_priv->gtt.mtrr = arch_phys_wc_add(dev_priv->gtt.mappable_base,
1710 aperture_size);
Eric Anholtab657db12009-01-23 12:57:47 -08001711
Chris Wilsone642abb2010-09-09 12:46:34 +01001712 /* The i915 workqueue is primarily used for batched retirement of
1713 * requests (and thus managing bo) once the task has been completed
1714 * by the GPU. i915_gem_retire_requests() is called directly when we
1715 * need high-priority retirement, such as waiting for an explicit
1716 * bo.
1717 *
1718 * It is also used for periodic low-priority events, such as
Eric Anholtdf9c2042010-11-18 09:31:12 +08001719 * idle-timers and recording error state.
Chris Wilsone642abb2010-09-09 12:46:34 +01001720 *
1721 * All tasks on the workqueue are expected to acquire the dev mutex
1722 * so there is no point in running more than one instance of the
Tejun Heo53621862012-08-22 16:40:57 -07001723 * workqueue at any time. Use an ordered one.
Chris Wilsone642abb2010-09-09 12:46:34 +01001724 */
Tejun Heo53621862012-08-22 16:40:57 -07001725 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001726 if (dev_priv->wq == NULL) {
1727 DRM_ERROR("Failed to create our workqueue.\n");
1728 ret = -ENOMEM;
Keith Packarda7b85d22011-07-10 13:12:17 -07001729 goto out_mtrrfree;
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001730 }
1731
Dave Airlie0e32b392014-05-02 14:02:48 +10001732 dev_priv->dp_wq = alloc_ordered_workqueue("i915-dp", 0);
1733 if (dev_priv->dp_wq == NULL) {
1734 DRM_ERROR("Failed to create our dp workqueue.\n");
1735 ret = -ENOMEM;
1736 goto out_freewq;
1737 }
1738
Daniel Vetterb9632912014-09-30 10:56:44 +02001739 intel_irq_init(dev_priv);
Ben Widawsky78511f22013-10-04 21:22:49 -07001740 intel_uncore_sanitize(dev);
Jesse Barnes9880b7a2009-02-06 10:22:41 -08001741
Zhenyu Wangc48044112009-12-17 14:48:43 +08001742 /* Try to make sure MCHBAR is enabled before poking at it */
1743 intel_setup_mchbar(dev);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001744 intel_setup_gmbus(dev);
Chris Wilson44834a62010-08-19 16:09:23 +01001745 intel_opregion_setup(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001746
Bryan Freed6d139a82010-10-14 09:14:51 +01001747 intel_setup_bios(dev);
1748
Eric Anholt673a3942008-07-30 12:06:12 -07001749 i915_gem_load(dev);
1750
Eric Anholted4cb412008-07-29 12:10:39 -07001751 /* On the 945G/GM, the chipset reports the MSI capability on the
1752 * integrated graphics even though the support isn't actually there
1753 * according to the published specs. It doesn't appear to function
1754 * correctly in testing on 945G.
1755 * This may be a side effect of MSI having been made available for PEG
1756 * and the registers being closely associated.
Keith Packardd1ed6292008-10-17 00:44:42 -07001757 *
1758 * According to chipset errata, on the 965GM, MSI interrupts may
Keith Packardb60678a2008-12-08 11:12:28 -08001759 * be lost or delayed, but we use them anyways to avoid
1760 * stuck interrupts on some machines.
Eric Anholted4cb412008-07-29 12:10:39 -07001761 */
Keith Packardb60678a2008-12-08 11:12:28 -08001762 if (!IS_I945G(dev) && !IS_I945GM(dev))
Eric Anholtd3e74d02008-11-03 14:46:17 -08001763 pci_enable_msi(dev->pdev);
Eric Anholted4cb412008-07-29 12:10:39 -07001764
Damien Lespiau22d3fd462014-02-07 19:12:49 +00001765 intel_device_info_runtime_init(dev);
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001766
Ben Widawskye3c74752013-04-05 13:12:39 -07001767 if (INTEL_INFO(dev)->num_pipes) {
1768 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1769 if (ret)
1770 goto out_gem_unload;
1771 }
Keith Packard52440212008-11-18 09:30:25 -08001772
Imre Deakda7e29b2014-02-18 00:02:02 +02001773 intel_power_domains_init(dev_priv);
Wang Xingchaoa38911a2013-05-30 22:07:11 +08001774
Jesse Barnes79e53942008-11-07 14:24:08 -08001775 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter53984632010-09-22 23:44:24 +02001776 ret = i915_load_modeset_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001777 if (ret < 0) {
1778 DRM_ERROR("failed to init modeset\n");
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001779 goto out_power_well;
Jesse Barnes79e53942008-11-07 14:24:08 -08001780 }
Daniel Vetterdb1b76c2013-07-09 16:51:37 +02001781 } else {
1782 /* Start out suspended in ums mode. */
1783 dev_priv->ums.mm_suspended = 1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001784 }
1785
Ben Widawsky0136db52012-04-10 21:17:01 -07001786 i915_setup_sysfs(dev);
1787
Ben Widawskye3c74752013-04-05 13:12:39 -07001788 if (INTEL_INFO(dev)->num_pipes) {
1789 /* Must be done after probing outputs */
1790 intel_opregion_init(dev);
Rafael J. Wysocki8e5c2b72013-07-25 21:43:39 +02001791 acpi_video_register();
Ben Widawskye3c74752013-04-05 13:12:39 -07001792 }
Matthew Garrett74a365b2009-03-19 21:35:39 +00001793
Daniel Vettereb48eb02012-04-26 23:28:12 +02001794 if (IS_GEN5(dev))
1795 intel_gpu_ips_init(dev_priv);
Eric Anholt63ee41d2010-12-20 18:40:06 -08001796
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001797 intel_runtime_pm_enable(dev_priv);
Paulo Zanoni8a187452013-12-06 20:32:13 -02001798
Jesse Barnes79e53942008-11-07 14:24:08 -08001799 return 0;
1800
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001801out_power_well:
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001802 intel_power_domains_fini(dev_priv);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001803 drm_vblank_cleanup(dev);
Chris Wilson56e2ea32010-11-08 17:10:29 +00001804out_gem_unload:
Imre Deak4bdc7292014-05-20 19:47:20 +03001805 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1806 unregister_shrinker(&dev_priv->mm.shrinker);
Keith Packarda7b85d22011-07-10 13:12:17 -07001807
Chris Wilson56e2ea32010-11-08 17:10:29 +00001808 if (dev->pdev->msi_enabled)
1809 pci_disable_msi(dev->pdev);
1810
1811 intel_teardown_gmbus(dev);
1812 intel_teardown_mchbar(dev);
Stanislaw Gruszka22accca2014-01-25 10:13:37 +01001813 pm_qos_remove_request(&dev_priv->pm_qos);
Dave Airlie0e32b392014-05-02 14:02:48 +10001814 destroy_workqueue(dev_priv->dp_wq);
1815out_freewq:
Eric Anholt9c9fe1f2009-08-03 16:09:16 -07001816 destroy_workqueue(dev_priv->wq);
Keith Packarda7b85d22011-07-10 13:12:17 -07001817out_mtrrfree:
Ben Widawsky911bdf02013-06-27 16:30:23 -07001818 arch_phys_wc_del(dev_priv->gtt.mtrr);
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001819 io_mapping_free(dev_priv->gtt.mappable);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001820out_gtt:
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001821 i915_global_gtt_cleanup(dev);
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001822out_regs:
Ben Widawskyc3d685a2013-10-08 16:31:03 -07001823 intel_uncore_fini(dev);
Chris Wilson6dda5692010-10-29 21:02:18 +01001824 pci_iounmap(dev->pdev, dev_priv->regs);
Dave Airlieec2a4c32009-08-04 11:43:41 +10001825put_bridge:
1826 pci_dev_put(dev_priv->bridge_dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001827free_priv:
Chris Wilsoncbb47d12013-09-23 17:33:20 -03001828 if (dev_priv->slab)
1829 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001830 kfree(dev_priv);
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001831 return ret;
1832}
1833
1834int i915_driver_unload(struct drm_device *dev)
1835{
1836 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterc911fc12010-08-20 21:23:20 +02001837 int ret;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001838
Chris Wilsonce58c322013-12-02 11:26:07 -02001839 ret = i915_gem_suspend(dev);
1840 if (ret) {
1841 DRM_ERROR("failed to idle hardware: %d\n", ret);
1842 return ret;
1843 }
1844
Daniel Vetter41373cd2014-09-30 10:56:41 +02001845 intel_power_domains_fini(dev_priv);
Paulo Zanoni8a187452013-12-06 20:32:13 -02001846
Daniel Vettereb48eb02012-04-26 23:28:12 +02001847 intel_gpu_ips_teardown();
Jesse Barnes7648fa92010-05-20 14:28:11 -07001848
Ben Widawsky0136db52012-04-10 21:17:01 -07001849 i915_teardown_sysfs(dev);
1850
Imre Deak4bdc7292014-05-20 19:47:20 +03001851 WARN_ON(unregister_oom_notifier(&dev_priv->mm.oom_notifier));
1852 unregister_shrinker(&dev_priv->mm.shrinker);
Chris Wilson17250b72010-10-28 12:51:39 +01001853
Ben Widawsky5d4545a2013-01-17 12:45:15 -08001854 io_mapping_free(dev_priv->gtt.mappable);
Ben Widawsky911bdf02013-06-27 16:30:23 -07001855 arch_phys_wc_del(dev_priv->gtt.mtrr);
Eric Anholtab657db12009-01-23 12:57:47 -08001856
Chris Wilson44834a62010-08-19 16:09:23 +01001857 acpi_video_unregister();
1858
Paulo Zanoni2ebfaf52014-10-15 14:15:04 -03001859 if (drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson7b4f3992010-10-04 15:33:04 +01001860 intel_fbdev_fini(dev);
Paulo Zanoni2ebfaf52014-10-15 14:15:04 -03001861
1862 drm_vblank_cleanup(dev);
1863
1864 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Jesse Barnes3d8620c2010-03-26 11:07:21 -07001865 intel_modeset_cleanup(dev);
1866
Zhao Yakui6363ee62009-11-24 09:48:44 +08001867 /*
1868 * free the memory space allocated for the child device
1869 * config parsed from VBT
1870 */
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03001871 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1872 kfree(dev_priv->vbt.child_dev);
1873 dev_priv->vbt.child_dev = NULL;
1874 dev_priv->vbt.child_dev_num = 0;
Zhao Yakui6363ee62009-11-24 09:48:44 +08001875 }
Daniel Vetter6c0d93502010-08-20 18:26:46 +02001876
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001877 vga_switcheroo_unregister_client(dev->pdev);
Dave Airlie28d52042009-09-21 14:33:58 +10001878 vga_client_register(dev->pdev, NULL, NULL, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08001879 }
1880
Daniel Vettera8b48992010-08-20 21:25:11 +02001881 /* Free error state after interrupts are fully disabled. */
Daniel Vetter99584db2012-11-14 17:14:04 +01001882 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
1883 cancel_work_sync(&dev_priv->gpu_error.work);
Daniel Vettera8b48992010-08-20 21:25:11 +02001884 i915_destroy_error_state(dev);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001885
Eric Anholted4cb412008-07-29 12:10:39 -07001886 if (dev->pdev->msi_enabled)
1887 pci_disable_msi(dev->pdev);
1888
Chris Wilson44834a62010-08-19 16:09:23 +01001889 intel_opregion_fini(dev);
Matthew Garrett8ee1c3d2008-08-05 19:37:25 +01001890
Jesse Barnes79e53942008-11-07 14:24:08 -08001891 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter67e77c52010-08-20 22:26:30 +02001892 /* Flush any outstanding unpin_work. */
1893 flush_workqueue(dev_priv->wq);
1894
Jesse Barnes79e53942008-11-07 14:24:08 -08001895 mutex_lock(&dev->struct_mutex);
1896 i915_gem_cleanup_ringbuffer(dev);
Daniel Vetter55a66622012-06-19 21:55:32 +02001897 i915_gem_context_fini(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001898 mutex_unlock(&dev->struct_mutex);
Chris Wilson9797fbf2012-04-24 15:47:39 +01001899 i915_gem_cleanup_stolen(dev);
Keith Packardc2873e92010-10-07 09:20:12 +01001900
1901 if (!I915_NEED_GFX_HWS(dev))
1902 i915_free_hws(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08001903 }
1904
Chris Wilsonf899fc62010-07-20 15:44:45 -07001905 intel_teardown_gmbus(dev);
Zhenyu Wangc48044112009-12-17 14:48:43 +08001906 intel_teardown_mchbar(dev);
1907
Dave Airlie0e32b392014-05-02 14:02:48 +10001908 destroy_workqueue(dev_priv->dp_wq);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001909 destroy_workqueue(dev_priv->wq);
Daniel Vetter9ee32fea2012-12-01 13:53:48 +01001910 pm_qos_remove_request(&dev_priv->pm_qos);
Daniel Vetterbc0c7f12010-08-20 18:18:48 +02001911
Daniel Vetter90d0a0e2014-08-06 15:04:56 +02001912 i915_global_gtt_cleanup(dev);
Imre Deak6640aab2013-05-22 17:47:13 +03001913
Chris Wilsonaec347a2013-08-26 13:46:09 +01001914 intel_uncore_fini(dev);
1915 if (dev_priv->regs != NULL)
1916 pci_iounmap(dev->pdev, dev_priv->regs);
1917
Chris Wilson42dcedd2012-11-15 11:32:30 +00001918 if (dev_priv->slab)
1919 kmem_cache_destroy(dev_priv->slab);
Eric Anholt9a298b22009-03-24 12:23:04 -07001920
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001921 pci_dev_put(dev_priv->bridge_dev);
Daniel Vetter2206e6a2014-05-13 22:21:59 +02001922 kfree(dev_priv);
Dave Airlie22eae942005-11-10 22:16:34 +11001923
1924 return 0;
1925}
1926
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001927int i915_driver_open(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001928{
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001929 int ret;
Eric Anholt673a3942008-07-30 12:06:12 -07001930
Chris Wilsonb29c19b2013-09-25 17:34:56 +01001931 ret = i915_gem_open(dev, file);
1932 if (ret)
1933 return ret;
Ben Widawsky254f9652012-06-04 14:42:42 -07001934
Eric Anholt673a3942008-07-30 12:06:12 -07001935 return 0;
1936}
1937
Jesse Barnes79e53942008-11-07 14:24:08 -08001938/**
1939 * i915_driver_lastclose - clean up after all DRM clients have exited
1940 * @dev: DRM device
1941 *
1942 * Take care of cleaning up after all DRM clients have exited. In the
1943 * mode setting case, we want to restore the kernel's initial mode (just
1944 * in case the last client left us in a bad state).
1945 *
Daniel Vetter9021f282012-03-26 09:45:41 +02001946 * Additionally, in the non-mode setting case, we'll tear down the GTT
Jesse Barnes79e53942008-11-07 14:24:08 -08001947 * and DMA structures, since the kernel won't be using them, and clea
1948 * up any GEM state.
1949 */
Robin Schroer1a5036b2014-06-02 16:59:39 +02001950void i915_driver_lastclose(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Jani Nikula4c8a4be2014-03-31 14:27:15 +03001952 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesba8bbcf2007-11-22 14:14:14 +10001953
Daniel Vettere8aeaee2012-07-21 16:47:09 +02001954 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1955 * goes right around and calls lastclose. Check for this and don't clean
1956 * up anything. */
1957 if (!dev_priv)
1958 return;
1959
1960 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
Daniel Vetter0632fef2013-10-08 17:44:49 +02001961 intel_fbdev_restore_mode(dev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001962 vga_switcheroo_process_delayed_switch();
Dave Airlie144a75f2008-03-30 07:53:58 +10001963 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001964 }
Dave Airlie144a75f2008-03-30 07:53:58 +10001965
Eric Anholt673a3942008-07-30 12:06:12 -07001966 i915_gem_lastclose(dev);
1967
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001968 i915_dma_cleanup(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
John Harrison2885f6a2014-06-26 18:23:52 +01001971void i915_driver_preclose(struct drm_device *dev, struct drm_file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Chris Wilson0d1430a2013-12-04 14:52:06 +00001973 mutex_lock(&dev->struct_mutex);
John Harrison2885f6a2014-06-26 18:23:52 +01001974 i915_gem_context_close(dev, file);
1975 i915_gem_release(dev, file);
Chris Wilson0d1430a2013-12-04 14:52:06 +00001976 mutex_unlock(&dev->struct_mutex);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +03001977
1978 if (drm_core_check_feature(dev, DRIVER_MODESET))
1979 intel_modeset_preclose(dev, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001982void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -07001983{
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001984 struct drm_i915_file_private *file_priv = file->driver_priv;
Eric Anholt673a3942008-07-30 12:06:12 -07001985
Zhao Yakuia8ebba72014-04-17 10:37:40 +08001986 if (file_priv && file_priv->bsd_ring)
1987 file_priv->bsd_ring = NULL;
Chris Wilsonf787a5f2010-09-24 16:02:42 +01001988 kfree(file_priv);
Eric Anholt673a3942008-07-30 12:06:12 -07001989}
1990
Rob Clarkbaa70942013-08-02 13:27:49 -04001991const struct drm_ioctl_desc i915_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001992 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1993 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1994 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1995 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1996 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1997 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02001998 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10001999 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01002000 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
2001 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
2002 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002003 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
Daniel Vetterb2c606f2012-01-17 12:50:12 +01002004 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Daniel Vetterd1c1edb2012-04-26 23:28:01 +02002005 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002006 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
2007 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
2008 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
2009 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2010 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002011 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002012 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
2013 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002014 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
2015 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2016 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2017 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002018 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
2019 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002020 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2021 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2022 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2023 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2024 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2025 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2026 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2027 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2028 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2029 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002030 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002031 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airlie1b2f1482010-08-14 20:20:34 +10002032 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2033 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Jesse Barnes8ea30862012-01-03 08:05:39 -08002034 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
2035 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, intel_sprite_get_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
Kristian Høgsberg10ba5012013-08-25 18:29:01 +02002036 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
2037 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2038 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
2039 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Mika Kuoppalab6359912013-10-30 15:44:16 +02002040 DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_get_reset_stats_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Chris Wilson5cc9ed42014-05-16 14:22:37 +01002041 DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
Dave Airliec94f7022005-07-07 21:03:38 +10002042};
2043
Damien Lespiauf95aeb12014-06-09 14:39:49 +01002044int i915_max_ioctl = ARRAY_SIZE(i915_ioctls);
Dave Airliecda17382005-07-10 17:31:26 +10002045
Daniel Vetter9021f282012-03-26 09:45:41 +02002046/*
2047 * This is really ugly: Because old userspace abused the linux agp interface to
2048 * manage the gtt, we need to claim that all intel devices are agp. For
2049 * otherwise the drm core refuses to initialize the agp support code.
Dave Airliecda17382005-07-10 17:31:26 +10002050 */
Robin Schroer1a5036b2014-06-02 16:59:39 +02002051int i915_driver_device_is_agp(struct drm_device *dev)
Dave Airliecda17382005-07-10 17:31:26 +10002052{
2053 return 1;
2054}