]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/i915/i915_dma.c
drm: Add separate Kconfig option for fbdev helpers
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / i915 / i915_dma.c
CommitLineData
1da177e4
LT
1/* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
2 */
0d6aa60b 3/*
1da177e4
LT
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
bc54fd1a
DA
6 *
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 *
0d6aa60b 27 */
1da177e4 28
a70491cc
JP
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
760285e7
DH
31#include <drm/drmP.h>
32#include <drm/drm_crtc_helper.h>
33#include <drm/drm_fb_helper.h>
79e53945 34#include "intel_drv.h"
760285e7 35#include <drm/i915_drm.h>
1da177e4 36#include "i915_drv.h"
1c5d22f7 37#include "i915_trace.h"
dcdb1674 38#include <linux/pci.h>
28d52043 39#include <linux/vgaarb.h>
c4804411
ZW
40#include <linux/acpi.h>
41#include <linux/pnp.h>
6a9ee8af 42#include <linux/vga_switcheroo.h>
5a0e3ad6 43#include <linux/slab.h>
44834a67 44#include <acpi/video.h>
1da177e4 45
09422b2e
DV
46#define LP_RING(d) (&((struct drm_i915_private *)(d))->ring[RCS])
47
48#define BEGIN_LP_RING(n) \
49 intel_ring_begin(LP_RING(dev_priv), (n))
50
51#define OUT_RING(x) \
52 intel_ring_emit(LP_RING(dev_priv), x)
53
54#define ADVANCE_LP_RING() \
09246732 55 __intel_ring_advance(LP_RING(dev_priv))
09422b2e
DV
56
57/**
58 * Lock test for when it's just for synchronization of ring access.
59 *
60 * In that case, we don't need to do it when GEM is initialized as nobody else
61 * has access to the ring.
62 */
63#define RING_LOCK_TEST_WITH_RETURN(dev, file) do { \
64 if (LP_RING(dev->dev_private)->obj == NULL) \
65 LOCK_TEST_WITH_RETURN(dev, file); \
66} while (0)
67
316d3884
DV
68static inline u32
69intel_read_legacy_status_page(struct drm_i915_private *dev_priv, int reg)
70{
71 if (I915_NEED_GFX_HWS(dev_priv->dev))
72 return ioread32(dev_priv->dri1.gfx_hws_cpu_addr + reg);
73 else
74 return intel_read_status_page(LP_RING(dev_priv), reg);
75}
76
77#define READ_HWSP(dev_priv, reg) intel_read_legacy_status_page(dev_priv, reg)
09422b2e
DV
78#define READ_BREADCRUMB(dev_priv) READ_HWSP(dev_priv, I915_BREADCRUMB_INDEX)
79#define I915_BREADCRUMB_INDEX 0x21
80
d05c617e
DV
81void i915_update_dri1_breadcrumb(struct drm_device *dev)
82{
83 drm_i915_private_t *dev_priv = dev->dev_private;
84 struct drm_i915_master_private *master_priv;
85
86 if (dev->primary->master) {
87 master_priv = dev->primary->master->driver_priv;
88 if (master_priv->sarea_priv)
89 master_priv->sarea_priv->last_dispatch =
90 READ_BREADCRUMB(dev_priv);
91 }
92}
93
4cbf74cc
CW
94static void i915_write_hws_pga(struct drm_device *dev)
95{
96 drm_i915_private_t *dev_priv = dev->dev_private;
97 u32 addr;
98
99 addr = dev_priv->status_page_dmah->busaddr;
100 if (INTEL_INFO(dev)->gen >= 4)
101 addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
102 I915_WRITE(HWS_PGA, addr);
103}
104
398c9cb2
KP
105/**
106 * Frees the hardware status page, whether it's a physical address or a virtual
107 * address set up by the X Server.
108 */
3043c60c 109static void i915_free_hws(struct drm_device *dev)
398c9cb2
KP
110{
111 drm_i915_private_t *dev_priv = dev->dev_private;
1ec14ad3
CW
112 struct intel_ring_buffer *ring = LP_RING(dev_priv);
113
398c9cb2
KP
114 if (dev_priv->status_page_dmah) {
115 drm_pci_free(dev, dev_priv->status_page_dmah);
116 dev_priv->status_page_dmah = NULL;
117 }
118
1ec14ad3
CW
119 if (ring->status_page.gfx_addr) {
120 ring->status_page.gfx_addr = 0;
316d3884 121 iounmap(dev_priv->dri1.gfx_hws_cpu_addr);
398c9cb2
KP
122 }
123
124 /* Need to rewrite hardware status page */
125 I915_WRITE(HWS_PGA, 0x1ffff000);
126}
127
84b1fd10 128void i915_kernel_lost_context(struct drm_device * dev)
1da177e4
LT
129{
130 drm_i915_private_t *dev_priv = dev->dev_private;
7c1c2871 131 struct drm_i915_master_private *master_priv;
1ec14ad3 132 struct intel_ring_buffer *ring = LP_RING(dev_priv);
1da177e4 133
79e53945
JB
134 /*
135 * We should never lose context on the ring with modesetting
136 * as we don't expose it to userspace
137 */
138 if (drm_core_check_feature(dev, DRIVER_MODESET))
139 return;
140
8168bd48
CW
141 ring->head = I915_READ_HEAD(ring) & HEAD_ADDR;
142 ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
633cf8f5 143 ring->space = ring->head - (ring->tail + I915_RING_FREE_SPACE);
1da177e4 144 if (ring->space < 0)
8187a2b7 145 ring->space += ring->size;
1da177e4 146
7c1c2871
DA
147 if (!dev->primary->master)
148 return;
149
150 master_priv = dev->primary->master->driver_priv;
151 if (ring->head == ring->tail && master_priv->sarea_priv)
152 master_priv->sarea_priv->perf_boxes |= I915_BOX_RING_EMPTY;
1da177e4
LT
153}
154
84b1fd10 155static int i915_dma_cleanup(struct drm_device * dev)
1da177e4 156{
ba8bbcf6 157 drm_i915_private_t *dev_priv = dev->dev_private;
1ec14ad3
CW
158 int i;
159
1da177e4
LT
160 /* Make sure interrupts are disabled here because the uninstall ioctl
161 * may not have been called from userspace and after dev_private
162 * is freed, it's too late.
163 */
ed4cb414 164 if (dev->irq_enabled)
b5e89ed5 165 drm_irq_uninstall(dev);
1da177e4 166
ee0c6bfb 167 mutex_lock(&dev->struct_mutex);
1ec14ad3
CW
168 for (i = 0; i < I915_NUM_RINGS; i++)
169 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
ee0c6bfb 170 mutex_unlock(&dev->struct_mutex);
dc7a9319 171
398c9cb2
KP
172 /* Clear the HWS virtual address at teardown */
173 if (I915_NEED_GFX_HWS(dev))
174 i915_free_hws(dev);
1da177e4
LT
175
176 return 0;
177}
178
ba8bbcf6 179static int i915_initialize(struct drm_device * dev, drm_i915_init_t * init)
1da177e4 180{
ba8bbcf6 181 drm_i915_private_t *dev_priv = dev->dev_private;
7c1c2871 182 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
e8616b6c 183 int ret;
1da177e4 184
3a03ac1a
DA
185 master_priv->sarea = drm_getsarea(dev);
186 if (master_priv->sarea) {
187 master_priv->sarea_priv = (drm_i915_sarea_t *)
188 ((u8 *)master_priv->sarea->handle + init->sarea_priv_offset);
189 } else {
8a4c47f3 190 DRM_DEBUG_DRIVER("sarea not found assuming DRI2 userspace\n");
3a03ac1a
DA
191 }
192
673a394b 193 if (init->ring_size != 0) {
e8616b6c 194 if (LP_RING(dev_priv)->obj != NULL) {
673a394b
EA
195 i915_dma_cleanup(dev);
196 DRM_ERROR("Client tried to initialize ringbuffer in "
197 "GEM mode\n");
198 return -EINVAL;
199 }
1da177e4 200
e8616b6c
CW
201 ret = intel_render_ring_init_dri(dev,
202 init->ring_start,
203 init->ring_size);
204 if (ret) {
673a394b 205 i915_dma_cleanup(dev);
e8616b6c 206 return ret;
673a394b 207 }
1da177e4
LT
208 }
209
5d985ac8
DV
210 dev_priv->dri1.cpp = init->cpp;
211 dev_priv->dri1.back_offset = init->back_offset;
212 dev_priv->dri1.front_offset = init->front_offset;
213 dev_priv->dri1.current_page = 0;
7c1c2871
DA
214 if (master_priv->sarea_priv)
215 master_priv->sarea_priv->pf_current_page = 0;
1da177e4 216
1da177e4
LT
217 /* Allow hardware batchbuffers unless told otherwise.
218 */
8781342d 219 dev_priv->dri1.allow_batchbuffer = 1;
1da177e4 220
1da177e4
LT
221 return 0;
222}
223
84b1fd10 224static int i915_dma_resume(struct drm_device * dev)
1da177e4
LT
225{
226 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1ec14ad3 227 struct intel_ring_buffer *ring = LP_RING(dev_priv);
1da177e4 228
8a4c47f3 229 DRM_DEBUG_DRIVER("%s\n", __func__);
1da177e4 230
4225d0f2 231 if (ring->virtual_start == NULL) {
1da177e4
LT
232 DRM_ERROR("can not ioremap virtual address for"
233 " ring buffer\n");
20caafa6 234 return -ENOMEM;
1da177e4
LT
235 }
236
237 /* Program Hardware Status Page */
8187a2b7 238 if (!ring->status_page.page_addr) {
1da177e4 239 DRM_ERROR("Can not find hardware status page\n");
20caafa6 240 return -EINVAL;
1da177e4 241 }
8a4c47f3 242 DRM_DEBUG_DRIVER("hw status page @ %p\n",
8187a2b7
ZN
243 ring->status_page.page_addr);
244 if (ring->status_page.gfx_addr != 0)
78501eac 245 intel_ring_setup_status_page(ring);
dc7a9319 246 else
4cbf74cc 247 i915_write_hws_pga(dev);
8187a2b7 248
8a4c47f3 249 DRM_DEBUG_DRIVER("Enabled hardware status page\n");
1da177e4
LT
250
251 return 0;
252}
253
c153f45f
EA
254static int i915_dma_init(struct drm_device *dev, void *data,
255 struct drm_file *file_priv)
1da177e4 256{
c153f45f 257 drm_i915_init_t *init = data;
1da177e4
LT
258 int retcode = 0;
259
cd9d4e9f
DV
260 if (drm_core_check_feature(dev, DRIVER_MODESET))
261 return -ENODEV;
262
c153f45f 263 switch (init->func) {
1da177e4 264 case I915_INIT_DMA:
ba8bbcf6 265 retcode = i915_initialize(dev, init);
1da177e4
LT
266 break;
267 case I915_CLEANUP_DMA:
268 retcode = i915_dma_cleanup(dev);
269 break;
270 case I915_RESUME_DMA:
0d6aa60b 271 retcode = i915_dma_resume(dev);
1da177e4
LT
272 break;
273 default:
20caafa6 274 retcode = -EINVAL;
1da177e4
LT
275 break;
276 }
277
278 return retcode;
279}
280
281/* Implement basically the same security restrictions as hardware does
282 * for MI_BATCH_NON_SECURE. These can be made stricter at any time.
283 *
284 * Most of the calculations below involve calculating the size of a
285 * particular instruction. It's important to get the size right as
286 * that tells us where the next instruction to check is. Any illegal
287 * instruction detected will be given a size of zero, which is a
288 * signal to abort the rest of the buffer.
289 */
e1f99ce6 290static int validate_cmd(int cmd)
1da177e4
LT
291{
292 switch (((cmd >> 29) & 0x7)) {
293 case 0x0:
294 switch ((cmd >> 23) & 0x3f) {
295 case 0x0:
296 return 1; /* MI_NOOP */
297 case 0x4:
298 return 1; /* MI_FLUSH */
299 default:
300 return 0; /* disallow everything else */
301 }
302 break;
303 case 0x1:
304 return 0; /* reserved */
305 case 0x2:
306 return (cmd & 0xff) + 2; /* 2d commands */
307 case 0x3:
308 if (((cmd >> 24) & 0x1f) <= 0x18)
309 return 1;
310
311 switch ((cmd >> 24) & 0x1f) {
312 case 0x1c:
313 return 1;
314 case 0x1d:
b5e89ed5 315 switch ((cmd >> 16) & 0xff) {
1da177e4
LT
316 case 0x3:
317 return (cmd & 0x1f) + 2;
318 case 0x4:
319 return (cmd & 0xf) + 2;
320 default:
321 return (cmd & 0xffff) + 2;
322 }
323 case 0x1e:
324 if (cmd & (1 << 23))
325 return (cmd & 0xffff) + 1;
326 else
327 return 1;
328 case 0x1f:
329 if ((cmd & (1 << 23)) == 0) /* inline vertices */
330 return (cmd & 0x1ffff) + 2;
331 else if (cmd & (1 << 17)) /* indirect random */
332 if ((cmd & 0xffff) == 0)
333 return 0; /* unknown length, too hard */
334 else
335 return (((cmd & 0xffff) + 1) / 2) + 1;
336 else
337 return 2; /* indirect sequential */
338 default:
339 return 0;
340 }
341 default:
342 return 0;
343 }
344
345 return 0;
346}
347
201361a5 348static int i915_emit_cmds(struct drm_device * dev, int *buffer, int dwords)
1da177e4
LT
349{
350 drm_i915_private_t *dev_priv = dev->dev_private;
e1f99ce6 351 int i, ret;
1da177e4 352
1ec14ad3 353 if ((dwords+1) * sizeof(int) >= LP_RING(dev_priv)->size - 8)
20caafa6 354 return -EINVAL;
de227f5f 355
1da177e4 356 for (i = 0; i < dwords;) {
e1f99ce6
CW
357 int sz = validate_cmd(buffer[i]);
358 if (sz == 0 || i + sz > dwords)
20caafa6 359 return -EINVAL;
e1f99ce6 360 i += sz;
1da177e4
LT
361 }
362
e1f99ce6
CW
363 ret = BEGIN_LP_RING((dwords+1)&~1);
364 if (ret)
365 return ret;
366
367 for (i = 0; i < dwords; i++)
368 OUT_RING(buffer[i]);
de227f5f
DA
369 if (dwords & 1)
370 OUT_RING(0);
371
372 ADVANCE_LP_RING();
373
1da177e4
LT
374 return 0;
375}
376
673a394b
EA
377int
378i915_emit_box(struct drm_device *dev,
c4e7a414
CW
379 struct drm_clip_rect *box,
380 int DR1, int DR4)
1da177e4 381{
e1f99ce6 382 struct drm_i915_private *dev_priv = dev->dev_private;
e1f99ce6 383 int ret;
1da177e4 384
c4e7a414
CW
385 if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
386 box->y2 <= 0 || box->x2 <= 0) {
1da177e4 387 DRM_ERROR("Bad box %d,%d..%d,%d\n",
c4e7a414 388 box->x1, box->y1, box->x2, box->y2);
20caafa6 389 return -EINVAL;
1da177e4
LT
390 }
391
a6c45cf0 392 if (INTEL_INFO(dev)->gen >= 4) {
e1f99ce6
CW
393 ret = BEGIN_LP_RING(4);
394 if (ret)
395 return ret;
396
c29b669c 397 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
c4e7a414
CW
398 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
399 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
c29b669c 400 OUT_RING(DR4);
c29b669c 401 } else {
e1f99ce6
CW
402 ret = BEGIN_LP_RING(6);
403 if (ret)
404 return ret;
405
c29b669c
AH
406 OUT_RING(GFX_OP_DRAWRECT_INFO);
407 OUT_RING(DR1);
c4e7a414
CW
408 OUT_RING((box->x1 & 0xffff) | (box->y1 << 16));
409 OUT_RING(((box->x2 - 1) & 0xffff) | ((box->y2 - 1) << 16));
c29b669c
AH
410 OUT_RING(DR4);
411 OUT_RING(0);
c29b669c 412 }
e1f99ce6 413 ADVANCE_LP_RING();
1da177e4
LT
414
415 return 0;
416}
417
c29b669c
AH
418/* XXX: Emitting the counter should really be moved to part of the IRQ
419 * emit. For now, do it in both places:
420 */
421
84b1fd10 422static void i915_emit_breadcrumb(struct drm_device *dev)
de227f5f
DA
423{
424 drm_i915_private_t *dev_priv = dev->dev_private;
7c1c2871 425 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
de227f5f 426
231f42a4
DV
427 dev_priv->dri1.counter++;
428 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
429 dev_priv->dri1.counter = 0;
7c1c2871 430 if (master_priv->sarea_priv)
231f42a4 431 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
de227f5f 432
e1f99ce6
CW
433 if (BEGIN_LP_RING(4) == 0) {
434 OUT_RING(MI_STORE_DWORD_INDEX);
435 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
231f42a4 436 OUT_RING(dev_priv->dri1.counter);
e1f99ce6
CW
437 OUT_RING(0);
438 ADVANCE_LP_RING();
439 }
de227f5f
DA
440}
441
84b1fd10 442static int i915_dispatch_cmdbuffer(struct drm_device * dev,
201361a5
EA
443 drm_i915_cmdbuffer_t *cmd,
444 struct drm_clip_rect *cliprects,
445 void *cmdbuf)
1da177e4
LT
446{
447 int nbox = cmd->num_cliprects;
448 int i = 0, count, ret;
449
450 if (cmd->sz & 0x3) {
451 DRM_ERROR("alignment");
20caafa6 452 return -EINVAL;
1da177e4
LT
453 }
454
455 i915_kernel_lost_context(dev);
456
457 count = nbox ? nbox : 1;
458
459 for (i = 0; i < count; i++) {
460 if (i < nbox) {
c4e7a414 461 ret = i915_emit_box(dev, &cliprects[i],
1da177e4
LT
462 cmd->DR1, cmd->DR4);
463 if (ret)
464 return ret;
465 }
466
201361a5 467 ret = i915_emit_cmds(dev, cmdbuf, cmd->sz / 4);
1da177e4
LT
468 if (ret)
469 return ret;
470 }
471
de227f5f 472 i915_emit_breadcrumb(dev);
1da177e4
LT
473 return 0;
474}
475
84b1fd10 476static int i915_dispatch_batchbuffer(struct drm_device * dev,
201361a5
EA
477 drm_i915_batchbuffer_t * batch,
478 struct drm_clip_rect *cliprects)
1da177e4 479{
e1f99ce6 480 struct drm_i915_private *dev_priv = dev->dev_private;
1da177e4 481 int nbox = batch->num_cliprects;
e1f99ce6 482 int i, count, ret;
1da177e4
LT
483
484 if ((batch->start | batch->used) & 0x7) {
485 DRM_ERROR("alignment");
20caafa6 486 return -EINVAL;
1da177e4
LT
487 }
488
489 i915_kernel_lost_context(dev);
490
491 count = nbox ? nbox : 1;
1da177e4
LT
492 for (i = 0; i < count; i++) {
493 if (i < nbox) {
c4e7a414 494 ret = i915_emit_box(dev, &cliprects[i],
e1f99ce6 495 batch->DR1, batch->DR4);
1da177e4
LT
496 if (ret)
497 return ret;
498 }
499
0790d5e1 500 if (!IS_I830(dev) && !IS_845G(dev)) {
e1f99ce6
CW
501 ret = BEGIN_LP_RING(2);
502 if (ret)
503 return ret;
504
a6c45cf0 505 if (INTEL_INFO(dev)->gen >= 4) {
21f16289
DA
506 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6) | MI_BATCH_NON_SECURE_I965);
507 OUT_RING(batch->start);
508 } else {
509 OUT_RING(MI_BATCH_BUFFER_START | (2 << 6));
510 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
511 }
1da177e4 512 } else {
e1f99ce6
CW
513 ret = BEGIN_LP_RING(4);
514 if (ret)
515 return ret;
516
1da177e4
LT
517 OUT_RING(MI_BATCH_BUFFER);
518 OUT_RING(batch->start | MI_BATCH_NON_SECURE);
519 OUT_RING(batch->start + batch->used - 4);
520 OUT_RING(0);
1da177e4 521 }
e1f99ce6 522 ADVANCE_LP_RING();
1da177e4
LT
523 }
524
1cafd347 525
f00a3ddf 526 if (IS_G4X(dev) || IS_GEN5(dev)) {
e1f99ce6
CW
527 if (BEGIN_LP_RING(2) == 0) {
528 OUT_RING(MI_FLUSH | MI_NO_WRITE_FLUSH | MI_INVALIDATE_ISP);
529 OUT_RING(MI_NOOP);
530 ADVANCE_LP_RING();
531 }
1cafd347 532 }
1da177e4 533
e1f99ce6 534 i915_emit_breadcrumb(dev);
1da177e4
LT
535 return 0;
536}
537
af6061af 538static int i915_dispatch_flip(struct drm_device * dev)
1da177e4
LT
539{
540 drm_i915_private_t *dev_priv = dev->dev_private;
7c1c2871
DA
541 struct drm_i915_master_private *master_priv =
542 dev->primary->master->driver_priv;
e1f99ce6 543 int ret;
1da177e4 544
7c1c2871 545 if (!master_priv->sarea_priv)
c99b058f
KH
546 return -EINVAL;
547
8a4c47f3 548 DRM_DEBUG_DRIVER("%s: page=%d pfCurrentPage=%d\n",
be25ed9c 549 __func__,
5d985ac8 550 dev_priv->dri1.current_page,
be25ed9c 551 master_priv->sarea_priv->pf_current_page);
1da177e4 552
af6061af
DA
553 i915_kernel_lost_context(dev);
554
e1f99ce6
CW
555 ret = BEGIN_LP_RING(10);
556 if (ret)
557 return ret;
558
585fb111 559 OUT_RING(MI_FLUSH | MI_READ_FLUSH);
af6061af 560 OUT_RING(0);
1da177e4 561
af6061af
DA
562 OUT_RING(CMD_OP_DISPLAYBUFFER_INFO | ASYNC_FLIP);
563 OUT_RING(0);
5d985ac8
DV
564 if (dev_priv->dri1.current_page == 0) {
565 OUT_RING(dev_priv->dri1.back_offset);
566 dev_priv->dri1.current_page = 1;
1da177e4 567 } else {
5d985ac8
DV
568 OUT_RING(dev_priv->dri1.front_offset);
569 dev_priv->dri1.current_page = 0;
1da177e4 570 }
af6061af 571 OUT_RING(0);
1da177e4 572
af6061af
DA
573 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP);
574 OUT_RING(0);
e1f99ce6 575
af6061af 576 ADVANCE_LP_RING();
1da177e4 577
231f42a4 578 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter++;
1da177e4 579
e1f99ce6
CW
580 if (BEGIN_LP_RING(4) == 0) {
581 OUT_RING(MI_STORE_DWORD_INDEX);
582 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
231f42a4 583 OUT_RING(dev_priv->dri1.counter);
e1f99ce6
CW
584 OUT_RING(0);
585 ADVANCE_LP_RING();
586 }
1da177e4 587
5d985ac8 588 master_priv->sarea_priv->pf_current_page = dev_priv->dri1.current_page;
af6061af 589 return 0;
1da177e4
LT
590}
591
1ec14ad3 592static int i915_quiescent(struct drm_device *dev)
1da177e4 593{
1da177e4 594 i915_kernel_lost_context(dev);
3e960501 595 return intel_ring_idle(LP_RING(dev->dev_private));
1da177e4
LT
596}
597
c153f45f
EA
598static int i915_flush_ioctl(struct drm_device *dev, void *data,
599 struct drm_file *file_priv)
1da177e4 600{
546b0974
EA
601 int ret;
602
cd9d4e9f
DV
603 if (drm_core_check_feature(dev, DRIVER_MODESET))
604 return -ENODEV;
605
546b0974 606 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1da177e4 607
546b0974
EA
608 mutex_lock(&dev->struct_mutex);
609 ret = i915_quiescent(dev);
610 mutex_unlock(&dev->struct_mutex);
611
612 return ret;
1da177e4
LT
613}
614
c153f45f
EA
615static int i915_batchbuffer(struct drm_device *dev, void *data,
616 struct drm_file *file_priv)
1da177e4 617{
1da177e4 618 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7c1c2871 619 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1da177e4 620 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
7c1c2871 621 master_priv->sarea_priv;
c153f45f 622 drm_i915_batchbuffer_t *batch = data;
1da177e4 623 int ret;
201361a5 624 struct drm_clip_rect *cliprects = NULL;
1da177e4 625
cd9d4e9f
DV
626 if (drm_core_check_feature(dev, DRIVER_MODESET))
627 return -ENODEV;
628
8781342d 629 if (!dev_priv->dri1.allow_batchbuffer) {
1da177e4 630 DRM_ERROR("Batchbuffer ioctl disabled\n");
20caafa6 631 return -EINVAL;
1da177e4
LT
632 }
633
8a4c47f3 634 DRM_DEBUG_DRIVER("i915 batchbuffer, start %x used %d cliprects %d\n",
be25ed9c 635 batch->start, batch->used, batch->num_cliprects);
1da177e4 636
546b0974 637 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1da177e4 638
201361a5
EA
639 if (batch->num_cliprects < 0)
640 return -EINVAL;
641
642 if (batch->num_cliprects) {
9a298b2a 643 cliprects = kcalloc(batch->num_cliprects,
b14c5679 644 sizeof(*cliprects),
9a298b2a 645 GFP_KERNEL);
201361a5
EA
646 if (cliprects == NULL)
647 return -ENOMEM;
648
649 ret = copy_from_user(cliprects, batch->cliprects,
650 batch->num_cliprects *
651 sizeof(struct drm_clip_rect));
9927a403
DC
652 if (ret != 0) {
653 ret = -EFAULT;
201361a5 654 goto fail_free;
9927a403 655 }
201361a5 656 }
1da177e4 657
546b0974 658 mutex_lock(&dev->struct_mutex);
201361a5 659 ret = i915_dispatch_batchbuffer(dev, batch, cliprects);
546b0974 660 mutex_unlock(&dev->struct_mutex);
1da177e4 661
c99b058f 662 if (sarea_priv)
0baf823a 663 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
201361a5
EA
664
665fail_free:
9a298b2a 666 kfree(cliprects);
201361a5 667
1da177e4
LT
668 return ret;
669}
670
c153f45f
EA
671static int i915_cmdbuffer(struct drm_device *dev, void *data,
672 struct drm_file *file_priv)
1da177e4 673{
1da177e4 674 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7c1c2871 675 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
1da177e4 676 drm_i915_sarea_t *sarea_priv = (drm_i915_sarea_t *)
7c1c2871 677 master_priv->sarea_priv;
c153f45f 678 drm_i915_cmdbuffer_t *cmdbuf = data;
201361a5
EA
679 struct drm_clip_rect *cliprects = NULL;
680 void *batch_data;
1da177e4
LT
681 int ret;
682
8a4c47f3 683 DRM_DEBUG_DRIVER("i915 cmdbuffer, buf %p sz %d cliprects %d\n",
be25ed9c 684 cmdbuf->buf, cmdbuf->sz, cmdbuf->num_cliprects);
1da177e4 685
cd9d4e9f
DV
686 if (drm_core_check_feature(dev, DRIVER_MODESET))
687 return -ENODEV;
688
546b0974 689 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1da177e4 690
201361a5
EA
691 if (cmdbuf->num_cliprects < 0)
692 return -EINVAL;
693
9a298b2a 694 batch_data = kmalloc(cmdbuf->sz, GFP_KERNEL);
201361a5
EA
695 if (batch_data == NULL)
696 return -ENOMEM;
697
698 ret = copy_from_user(batch_data, cmdbuf->buf, cmdbuf->sz);
9927a403
DC
699 if (ret != 0) {
700 ret = -EFAULT;
201361a5 701 goto fail_batch_free;
9927a403 702 }
201361a5
EA
703
704 if (cmdbuf->num_cliprects) {
9a298b2a 705 cliprects = kcalloc(cmdbuf->num_cliprects,
b14c5679 706 sizeof(*cliprects), GFP_KERNEL);
a40e8d31
OA
707 if (cliprects == NULL) {
708 ret = -ENOMEM;
201361a5 709 goto fail_batch_free;
a40e8d31 710 }
201361a5
EA
711
712 ret = copy_from_user(cliprects, cmdbuf->cliprects,
713 cmdbuf->num_cliprects *
714 sizeof(struct drm_clip_rect));
9927a403
DC
715 if (ret != 0) {
716 ret = -EFAULT;
201361a5 717 goto fail_clip_free;
9927a403 718 }
1da177e4
LT
719 }
720
546b0974 721 mutex_lock(&dev->struct_mutex);
201361a5 722 ret = i915_dispatch_cmdbuffer(dev, cmdbuf, cliprects, batch_data);
546b0974 723 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
724 if (ret) {
725 DRM_ERROR("i915_dispatch_cmdbuffer failed\n");
355d7f37 726 goto fail_clip_free;
1da177e4
LT
727 }
728
c99b058f 729 if (sarea_priv)
0baf823a 730 sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
201361a5 731
201361a5 732fail_clip_free:
9a298b2a 733 kfree(cliprects);
355d7f37 734fail_batch_free:
9a298b2a 735 kfree(batch_data);
201361a5
EA
736
737 return ret;
1da177e4
LT
738}
739
9488867a
DV
740static int i915_emit_irq(struct drm_device * dev)
741{
742 drm_i915_private_t *dev_priv = dev->dev_private;
743 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
744
745 i915_kernel_lost_context(dev);
746
747 DRM_DEBUG_DRIVER("\n");
748
231f42a4
DV
749 dev_priv->dri1.counter++;
750 if (dev_priv->dri1.counter > 0x7FFFFFFFUL)
751 dev_priv->dri1.counter = 1;
9488867a 752 if (master_priv->sarea_priv)
231f42a4 753 master_priv->sarea_priv->last_enqueue = dev_priv->dri1.counter;
9488867a
DV
754
755 if (BEGIN_LP_RING(4) == 0) {
756 OUT_RING(MI_STORE_DWORD_INDEX);
757 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
231f42a4 758 OUT_RING(dev_priv->dri1.counter);
9488867a
DV
759 OUT_RING(MI_USER_INTERRUPT);
760 ADVANCE_LP_RING();
761 }
762
231f42a4 763 return dev_priv->dri1.counter;
9488867a
DV
764}
765
766static int i915_wait_irq(struct drm_device * dev, int irq_nr)
767{
768 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
769 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
770 int ret = 0;
771 struct intel_ring_buffer *ring = LP_RING(dev_priv);
772
773 DRM_DEBUG_DRIVER("irq_nr=%d breadcrumb=%d\n", irq_nr,
774 READ_BREADCRUMB(dev_priv));
775
776 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
777 if (master_priv->sarea_priv)
778 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
779 return 0;
780 }
781
782 if (master_priv->sarea_priv)
783 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
784
785 if (ring->irq_get(ring)) {
786 DRM_WAIT_ON(ret, ring->irq_queue, 3 * DRM_HZ,
787 READ_BREADCRUMB(dev_priv) >= irq_nr);
788 ring->irq_put(ring);
789 } else if (wait_for(READ_BREADCRUMB(dev_priv) >= irq_nr, 3000))
790 ret = -EBUSY;
791
792 if (ret == -EBUSY) {
793 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
231f42a4 794 READ_BREADCRUMB(dev_priv), (int)dev_priv->dri1.counter);
9488867a
DV
795 }
796
797 return ret;
798}
799
800/* Needs the lock as it touches the ring.
801 */
802static int i915_irq_emit(struct drm_device *dev, void *data,
803 struct drm_file *file_priv)
804{
805 drm_i915_private_t *dev_priv = dev->dev_private;
806 drm_i915_irq_emit_t *emit = data;
807 int result;
808
809 if (drm_core_check_feature(dev, DRIVER_MODESET))
810 return -ENODEV;
811
812 if (!dev_priv || !LP_RING(dev_priv)->virtual_start) {
813 DRM_ERROR("called with no initialization\n");
814 return -EINVAL;
815 }
816
817 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
818
819 mutex_lock(&dev->struct_mutex);
820 result = i915_emit_irq(dev);
821 mutex_unlock(&dev->struct_mutex);
822
823 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
824 DRM_ERROR("copy_to_user\n");
825 return -EFAULT;
826 }
827
828 return 0;
829}
830
831/* Doesn't need the hardware lock.
832 */
833static int i915_irq_wait(struct drm_device *dev, void *data,
834 struct drm_file *file_priv)
835{
836 drm_i915_private_t *dev_priv = dev->dev_private;
837 drm_i915_irq_wait_t *irqwait = data;
838
839 if (drm_core_check_feature(dev, DRIVER_MODESET))
840 return -ENODEV;
841
842 if (!dev_priv) {
843 DRM_ERROR("called with no initialization\n");
844 return -EINVAL;
845 }
846
847 return i915_wait_irq(dev, irqwait->irq_seq);
848}
849
d1c1edbc
DV
850static int i915_vblank_pipe_get(struct drm_device *dev, void *data,
851 struct drm_file *file_priv)
852{
853 drm_i915_private_t *dev_priv = dev->dev_private;
854 drm_i915_vblank_pipe_t *pipe = data;
855
856 if (drm_core_check_feature(dev, DRIVER_MODESET))
857 return -ENODEV;
858
859 if (!dev_priv) {
860 DRM_ERROR("called with no initialization\n");
861 return -EINVAL;
862 }
863
864 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
865
866 return 0;
867}
868
869/**
870 * Schedule buffer swap at given vertical blank.
871 */
872static int i915_vblank_swap(struct drm_device *dev, void *data,
873 struct drm_file *file_priv)
874{
875 /* The delayed swap mechanism was fundamentally racy, and has been
876 * removed. The model was that the client requested a delayed flip/swap
877 * from the kernel, then waited for vblank before continuing to perform
878 * rendering. The problem was that the kernel might wake the client
879 * up before it dispatched the vblank swap (since the lock has to be
880 * held while touching the ringbuffer), in which case the client would
881 * clear and start the next frame before the swap occurred, and
882 * flicker would occur in addition to likely missing the vblank.
883 *
884 * In the absence of this ioctl, userland falls back to a correct path
885 * of waiting for a vblank, then dispatching the swap on its own.
886 * Context switching to userland and back is plenty fast enough for
887 * meeting the requirements of vblank swapping.
888 */
889 return -EINVAL;
890}
891
c153f45f
EA
892static int i915_flip_bufs(struct drm_device *dev, void *data,
893 struct drm_file *file_priv)
1da177e4 894{
546b0974
EA
895 int ret;
896
cd9d4e9f
DV
897 if (drm_core_check_feature(dev, DRIVER_MODESET))
898 return -ENODEV;
899
8a4c47f3 900 DRM_DEBUG_DRIVER("%s\n", __func__);
1da177e4 901
546b0974 902 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
1da177e4 903
546b0974
EA
904 mutex_lock(&dev->struct_mutex);
905 ret = i915_dispatch_flip(dev);
906 mutex_unlock(&dev->struct_mutex);
907
908 return ret;
1da177e4
LT
909}
910
c153f45f
EA
911static int i915_getparam(struct drm_device *dev, void *data,
912 struct drm_file *file_priv)
1da177e4 913{
1da177e4 914 drm_i915_private_t *dev_priv = dev->dev_private;
c153f45f 915 drm_i915_getparam_t *param = data;
1da177e4
LT
916 int value;
917
918 if (!dev_priv) {
3e684eae 919 DRM_ERROR("called with no initialization\n");
20caafa6 920 return -EINVAL;
1da177e4
LT
921 }
922
c153f45f 923 switch (param->param) {
1da177e4 924 case I915_PARAM_IRQ_ACTIVE:
0a3e67a4 925 value = dev->pdev->irq ? 1 : 0;
1da177e4
LT
926 break;
927 case I915_PARAM_ALLOW_BATCHBUFFER:
8781342d 928 value = dev_priv->dri1.allow_batchbuffer ? 1 : 0;
1da177e4 929 break;
0d6aa60b
DA
930 case I915_PARAM_LAST_DISPATCH:
931 value = READ_BREADCRUMB(dev_priv);
932 break;
ed4c9c4a 933 case I915_PARAM_CHIPSET_ID:
ffbab09b 934 value = dev->pdev->device;
ed4c9c4a 935 break;
673a394b 936 case I915_PARAM_HAS_GEM:
2e895b17 937 value = 1;
673a394b 938 break;
0f973f27
JB
939 case I915_PARAM_NUM_FENCES_AVAIL:
940 value = dev_priv->num_fence_regs - dev_priv->fence_reg_start;
941 break;
02e792fb
DV
942 case I915_PARAM_HAS_OVERLAY:
943 value = dev_priv->overlay ? 1 : 0;
944 break;
e9560f7c
JB
945 case I915_PARAM_HAS_PAGEFLIPPING:
946 value = 1;
947 break;
76446cac
JB
948 case I915_PARAM_HAS_EXECBUF2:
949 /* depends on GEM */
2e895b17 950 value = 1;
76446cac 951 break;
e3a815fc 952 case I915_PARAM_HAS_BSD:
edc912f5 953 value = intel_ring_initialized(&dev_priv->ring[VCS]);
e3a815fc 954 break;
549f7365 955 case I915_PARAM_HAS_BLT:
edc912f5 956 value = intel_ring_initialized(&dev_priv->ring[BCS]);
549f7365 957 break;
a1f2cc73
XH
958 case I915_PARAM_HAS_VEBOX:
959 value = intel_ring_initialized(&dev_priv->ring[VECS]);
960 break;
a00b10c3
CW
961 case I915_PARAM_HAS_RELAXED_FENCING:
962 value = 1;
963 break;
bbf0c6b3
DV
964 case I915_PARAM_HAS_COHERENT_RINGS:
965 value = 1;
966 break;
72bfa19c
CW
967 case I915_PARAM_HAS_EXEC_CONSTANTS:
968 value = INTEL_INFO(dev)->gen >= 4;
969 break;
271d81b8
CW
970 case I915_PARAM_HAS_RELAXED_DELTA:
971 value = 1;
972 break;
ae662d31
EA
973 case I915_PARAM_HAS_GEN7_SOL_RESET:
974 value = 1;
975 break;
3d29b842
ED
976 case I915_PARAM_HAS_LLC:
977 value = HAS_LLC(dev);
978 break;
651d794f
CW
979 case I915_PARAM_HAS_WT:
980 value = HAS_WT(dev);
981 break;
777ee96f
DV
982 case I915_PARAM_HAS_ALIASING_PPGTT:
983 value = dev_priv->mm.aliasing_ppgtt ? 1 : 0;
984 break;
172cf15d
BW
985 case I915_PARAM_HAS_WAIT_TIMEOUT:
986 value = 1;
987 break;
2fedbff9
CW
988 case I915_PARAM_HAS_SEMAPHORES:
989 value = i915_semaphore_is_enabled(dev);
990 break;
ec6f1bb9
DA
991 case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
992 value = 1;
993 break;
d7d4eedd
CW
994 case I915_PARAM_HAS_SECURE_BATCHES:
995 value = capable(CAP_SYS_ADMIN);
996 break;
b45305fc
DV
997 case I915_PARAM_HAS_PINNED_BATCHES:
998 value = 1;
999 break;
ed5982e6
DV
1000 case I915_PARAM_HAS_EXEC_NO_RELOC:
1001 value = 1;
1002 break;
eef90ccb
CW
1003 case I915_PARAM_HAS_EXEC_HANDLE_LUT:
1004 value = 1;
1005 break;
1da177e4 1006 default:
e29c32da 1007 DRM_DEBUG("Unknown parameter %d\n", param->param);
20caafa6 1008 return -EINVAL;
1da177e4
LT
1009 }
1010
c153f45f 1011 if (DRM_COPY_TO_USER(param->value, &value, sizeof(int))) {
1da177e4 1012 DRM_ERROR("DRM_COPY_TO_USER failed\n");
20caafa6 1013 return -EFAULT;
1da177e4
LT
1014 }
1015
1016 return 0;
1017}
1018
c153f45f
EA
1019static int i915_setparam(struct drm_device *dev, void *data,
1020 struct drm_file *file_priv)
1da177e4 1021{
1da177e4 1022 drm_i915_private_t *dev_priv = dev->dev_private;
c153f45f 1023 drm_i915_setparam_t *param = data;
1da177e4
LT
1024
1025 if (!dev_priv) {
3e684eae 1026 DRM_ERROR("called with no initialization\n");
20caafa6 1027 return -EINVAL;
1da177e4
LT
1028 }
1029
c153f45f 1030 switch (param->param) {
1da177e4 1031 case I915_SETPARAM_USE_MI_BATCHBUFFER_START:
1da177e4
LT
1032 break;
1033 case I915_SETPARAM_TEX_LRU_LOG_GRANULARITY:
1da177e4
LT
1034 break;
1035 case I915_SETPARAM_ALLOW_BATCHBUFFER:
8781342d 1036 dev_priv->dri1.allow_batchbuffer = param->value ? 1 : 0;
1da177e4 1037 break;
0f973f27
JB
1038 case I915_SETPARAM_NUM_USED_FENCES:
1039 if (param->value > dev_priv->num_fence_regs ||
1040 param->value < 0)
1041 return -EINVAL;
1042 /* Userspace can use first N regs */
1043 dev_priv->fence_reg_start = param->value;
1044 break;
1da177e4 1045 default:
8a4c47f3 1046 DRM_DEBUG_DRIVER("unknown parameter %d\n",
be25ed9c 1047 param->param);
20caafa6 1048 return -EINVAL;
1da177e4
LT
1049 }
1050
1051 return 0;
1052}
1053
c153f45f
EA
1054static int i915_set_status_page(struct drm_device *dev, void *data,
1055 struct drm_file *file_priv)
dc7a9319 1056{
dc7a9319 1057 drm_i915_private_t *dev_priv = dev->dev_private;
c153f45f 1058 drm_i915_hws_addr_t *hws = data;
4f1ba0f8 1059 struct intel_ring_buffer *ring;
b39d50e5 1060
cd9d4e9f
DV
1061 if (drm_core_check_feature(dev, DRIVER_MODESET))
1062 return -ENODEV;
1063
b39d50e5
ZW
1064 if (!I915_NEED_GFX_HWS(dev))
1065 return -EINVAL;
dc7a9319
WZ
1066
1067 if (!dev_priv) {
3e684eae 1068 DRM_ERROR("called with no initialization\n");
20caafa6 1069 return -EINVAL;
dc7a9319 1070 }
dc7a9319 1071
79e53945
JB
1072 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1073 WARN(1, "tried to set status page when mode setting active\n");
1074 return 0;
1075 }
1076
8a4c47f3 1077 DRM_DEBUG_DRIVER("set status page addr 0x%08x\n", (u32)hws->addr);
c153f45f 1078
4f1ba0f8 1079 ring = LP_RING(dev_priv);
8187a2b7 1080 ring->status_page.gfx_addr = hws->addr & (0x1ffff<<12);
dc7a9319 1081
dd2757f8 1082 dev_priv->dri1.gfx_hws_cpu_addr =
5d4545ae 1083 ioremap_wc(dev_priv->gtt.mappable_base + hws->addr, 4096);
316d3884 1084 if (dev_priv->dri1.gfx_hws_cpu_addr == NULL) {
dc7a9319 1085 i915_dma_cleanup(dev);
e20f9c64 1086 ring->status_page.gfx_addr = 0;
dc7a9319
WZ
1087 DRM_ERROR("can not ioremap virtual address for"
1088 " G33 hw status page\n");
20caafa6 1089 return -ENOMEM;
dc7a9319 1090 }
316d3884
DV
1091
1092 memset_io(dev_priv->dri1.gfx_hws_cpu_addr, 0, PAGE_SIZE);
8187a2b7 1093 I915_WRITE(HWS_PGA, ring->status_page.gfx_addr);
dc7a9319 1094
8a4c47f3 1095 DRM_DEBUG_DRIVER("load hws HWS_PGA with gfx mem 0x%x\n",
e20f9c64 1096 ring->status_page.gfx_addr);
8a4c47f3 1097 DRM_DEBUG_DRIVER("load hws at %p\n",
e20f9c64 1098 ring->status_page.page_addr);
dc7a9319
WZ
1099 return 0;
1100}
1101
ec2a4c3f
DA
1102static int i915_get_bridge_dev(struct drm_device *dev)
1103{
1104 struct drm_i915_private *dev_priv = dev->dev_private;
1105
0206e353 1106 dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
ec2a4c3f
DA
1107 if (!dev_priv->bridge_dev) {
1108 DRM_ERROR("bridge device not found\n");
1109 return -1;
1110 }
1111 return 0;
1112}
1113
c4804411
ZW
1114#define MCHBAR_I915 0x44
1115#define MCHBAR_I965 0x48
1116#define MCHBAR_SIZE (4*4096)
1117
1118#define DEVEN_REG 0x54
1119#define DEVEN_MCHBAR_EN (1 << 28)
1120
1121/* Allocate space for the MCH regs if needed, return nonzero on error */
1122static int
1123intel_alloc_mchbar_resource(struct drm_device *dev)
1124{
1125 drm_i915_private_t *dev_priv = dev->dev_private;
a6c45cf0 1126 int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
c4804411
ZW
1127 u32 temp_lo, temp_hi = 0;
1128 u64 mchbar_addr;
a25c25c2 1129 int ret;
c4804411 1130
a6c45cf0 1131 if (INTEL_INFO(dev)->gen >= 4)
c4804411
ZW
1132 pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
1133 pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
1134 mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
1135
1136 /* If ACPI doesn't have it, assume we need to allocate it ourselves */
1137#ifdef CONFIG_PNP
1138 if (mchbar_addr &&
a25c25c2
CW
1139 pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
1140 return 0;
c4804411
ZW
1141#endif
1142
1143 /* Get some space for it */
a25c25c2
CW
1144 dev_priv->mch_res.name = "i915 MCHBAR";
1145 dev_priv->mch_res.flags = IORESOURCE_MEM;
1146 ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
1147 &dev_priv->mch_res,
c4804411
ZW
1148 MCHBAR_SIZE, MCHBAR_SIZE,
1149 PCIBIOS_MIN_MEM,
a25c25c2 1150 0, pcibios_align_resource,
c4804411
ZW
1151 dev_priv->bridge_dev);
1152 if (ret) {
1153 DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
1154 dev_priv->mch_res.start = 0;
a25c25c2 1155 return ret;
c4804411
ZW
1156 }
1157
a6c45cf0 1158 if (INTEL_INFO(dev)->gen >= 4)
c4804411
ZW
1159 pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
1160 upper_32_bits(dev_priv->mch_res.start));
1161
1162 pci_write_config_dword(dev_priv->bridge_dev, reg,
1163 lower_32_bits(dev_priv->mch_res.start));
a25c25c2 1164 return 0;
c4804411
ZW
1165}
1166
1167/* Setup MCHBAR if possible, return true if we should disable it again */
1168static void
1169intel_setup_mchbar(struct drm_device *dev)
1170{
1171 drm_i915_private_t *dev_priv = dev->dev_private;
a6c45cf0 1172 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
c4804411
ZW
1173 u32 temp;
1174 bool enabled;
1175
1176 dev_priv->mchbar_need_disable = false;
1177
1178 if (IS_I915G(dev) || IS_I915GM(dev)) {
1179 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1180 enabled = !!(temp & DEVEN_MCHBAR_EN);
1181 } else {
1182 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1183 enabled = temp & 1;
1184 }
1185
1186 /* If it's already enabled, don't have to do anything */
1187 if (enabled)
1188 return;
1189
1190 if (intel_alloc_mchbar_resource(dev))
1191 return;
1192
1193 dev_priv->mchbar_need_disable = true;
1194
1195 /* Space is allocated or reserved, so enable it. */
1196 if (IS_I915G(dev) || IS_I915GM(dev)) {
1197 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
1198 temp | DEVEN_MCHBAR_EN);
1199 } else {
1200 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1201 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
1202 }
1203}
1204
1205static void
1206intel_teardown_mchbar(struct drm_device *dev)
1207{
1208 drm_i915_private_t *dev_priv = dev->dev_private;
a6c45cf0 1209 int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
c4804411
ZW
1210 u32 temp;
1211
1212 if (dev_priv->mchbar_need_disable) {
1213 if (IS_I915G(dev) || IS_I915GM(dev)) {
1214 pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
1215 temp &= ~DEVEN_MCHBAR_EN;
1216 pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
1217 } else {
1218 pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
1219 temp &= ~1;
1220 pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
1221 }
1222 }
1223
1224 if (dev_priv->mch_res.start)
1225 release_resource(&dev_priv->mch_res);
1226}
1227
28d52043
DA
1228/* true = enable decode, false = disable decoder */
1229static unsigned int i915_vga_set_decode(void *cookie, bool state)
1230{
1231 struct drm_device *dev = cookie;
1232
1233 intel_modeset_vga_set_state(dev, state);
1234 if (state)
1235 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1236 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1237 else
1238 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1239}
1240
6a9ee8af
DA
1241static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1242{
1243 struct drm_device *dev = pci_get_drvdata(pdev);
1244 pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
1245 if (state == VGA_SWITCHEROO_ON) {
a70491cc 1246 pr_info("switched on\n");
5bcf719b 1247 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
6a9ee8af
DA
1248 /* i915 resume handler doesn't set to D0 */
1249 pci_set_power_state(dev->pdev, PCI_D0);
1250 i915_resume(dev);
5bcf719b 1251 dev->switch_power_state = DRM_SWITCH_POWER_ON;
6a9ee8af 1252 } else {
a70491cc 1253 pr_err("switched off\n");
5bcf719b 1254 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
6a9ee8af 1255 i915_suspend(dev, pmm);
5bcf719b 1256 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
6a9ee8af
DA
1257 }
1258}
1259
1260static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
1261{
1262 struct drm_device *dev = pci_get_drvdata(pdev);
1263 bool can_switch;
1264
1265 spin_lock(&dev->count_lock);
1266 can_switch = (dev->open_count == 0);
1267 spin_unlock(&dev->count_lock);
1268 return can_switch;
1269}
1270
26ec685f
TI
1271static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
1272 .set_gpu_state = i915_switcheroo_set_state,
1273 .reprobe = NULL,
1274 .can_switch = i915_switcheroo_can_switch,
1275};
1276
2c7111db
CW
1277static int i915_load_modeset_init(struct drm_device *dev)
1278{
1279 struct drm_i915_private *dev_priv = dev->dev_private;
1280 int ret;
79e53945 1281
6d139a87 1282 ret = intel_parse_bios(dev);
79e53945
JB
1283 if (ret)
1284 DRM_INFO("failed to find VBIOS tables\n");
1285
934f992c
CW
1286 /* If we have > 1 VGA cards, then we need to arbitrate access
1287 * to the common VGA resources.
1288 *
1289 * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
1290 * then we do not take part in VGA arbitration and the
1291 * vga_client_register() fails with -ENODEV.
1292 */
81b5c7bc
AW
1293 if (!HAS_PCH_SPLIT(dev)) {
1294 ret = vga_client_register(dev->pdev, dev, NULL,
1295 i915_vga_set_decode);
1296 if (ret && ret != -ENODEV)
1297 goto out;
1298 }
28d52043 1299
723bfd70
JB
1300 intel_register_dsm_handler();
1301
0d69704a 1302 ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
6a9ee8af 1303 if (ret)
5a79395b 1304 goto cleanup_vga_client;
6a9ee8af 1305
9797fbfb
CW
1306 /* Initialise stolen first so that we may reserve preallocated
1307 * objects for the BIOS to KMS transition.
1308 */
1309 ret = i915_gem_init_stolen(dev);
1310 if (ret)
1311 goto cleanup_vga_switcheroo;
1312
52d7eced
DV
1313 ret = drm_irq_install(dev);
1314 if (ret)
1315 goto cleanup_gem_stolen;
1316
a1485320
VS
1317 intel_init_power_well(dev);
1318
1319 /* Keep VGA alive until i915_disable_vga_mem() */
1320 intel_display_power_get(dev, POWER_DOMAIN_VGA);
1321
52d7eced
DV
1322 /* Important: The output setup functions called by modeset_init need
1323 * working irqs for e.g. gmbus and dp aux transfers. */
b01f2c3a
JB
1324 intel_modeset_init(dev);
1325
1070a42b 1326 ret = i915_gem_init(dev);
79e53945 1327 if (ret)
a1485320 1328 goto cleanup_power;
2c7111db 1329
073f34d9
JB
1330 INIT_WORK(&dev_priv->console_resume_work, intel_console_resume);
1331
52d7eced 1332 intel_modeset_gem_init(dev);
2c7111db 1333
79e53945
JB
1334 /* Always safe in the mode setting case. */
1335 /* FIXME: do pre/post-mode set stuff in core KMS code */
ba0bf120 1336 dev->vblank_disable_allowed = true;
ce352550
VS
1337 if (INTEL_INFO(dev)->num_pipes == 0) {
1338 intel_display_power_put(dev, POWER_DOMAIN_VGA);
e3c74757 1339 return 0;
ce352550 1340 }
79e53945 1341
5a79395b
CW
1342 ret = intel_fbdev_init(dev);
1343 if (ret)
52d7eced
DV
1344 goto cleanup_gem;
1345
20afbda2
DV
1346 /* Only enable hotplug handling once the fbdev is fully set up. */
1347 intel_hpd_init(dev);
1348
1349 /*
1350 * Some ports require correctly set-up hpd registers for detection to
1351 * work properly (leading to ghost connected connector status), e.g. VGA
1352 * on gm45. Hence we can only set up the initial fbdev config after hpd
1353 * irqs are fully enabled. Now we should scan for the initial config
1354 * only once hotplug handling is enabled, but due to screwed-up locking
1355 * around kms/fbdev init we can't protect the fdbev initial config
1356 * scanning against hotplug events. Hence do this first and ignore the
1357 * tiny window where we will loose hotplug notifactions.
1358 */
1359 intel_fbdev_initial_config(dev);
1360
6e1b4fda
VS
1361 /*
1362 * Must do this after fbcon init so that
1363 * vgacon_save_screen() works during the handover.
1364 */
1365 i915_disable_vga_mem(dev);
ce352550 1366 intel_display_power_put(dev, POWER_DOMAIN_VGA);
6e1b4fda 1367
52d7eced
DV
1368 /* Only enable hotplug handling once the fbdev is fully set up. */
1369 dev_priv->enable_hotplug_processing = true;
5a79395b 1370
eb1f8e4f 1371 drm_kms_helper_poll_init(dev);
87acb0a5 1372
79e53945
JB
1373 return 0;
1374
2c7111db
CW
1375cleanup_gem:
1376 mutex_lock(&dev->struct_mutex);
1377 i915_gem_cleanup_ringbuffer(dev);
55d23285 1378 i915_gem_context_fini(dev);
2c7111db 1379 mutex_unlock(&dev->struct_mutex);
1d2a314c 1380 i915_gem_cleanup_aliasing_ppgtt(dev);
93bd8649 1381 drm_mm_takedown(&dev_priv->gtt.base.mm);
a1485320
VS
1382cleanup_power:
1383 intel_display_power_put(dev, POWER_DOMAIN_VGA);
52d7eced 1384 drm_irq_uninstall(dev);
9797fbfb
CW
1385cleanup_gem_stolen:
1386 i915_gem_cleanup_stolen(dev);
5a79395b
CW
1387cleanup_vga_switcheroo:
1388 vga_switcheroo_unregister_client(dev->pdev);
1389cleanup_vga_client:
1390 vga_client_register(dev->pdev, NULL, NULL, NULL);
79e53945
JB
1391out:
1392 return ret;
1393}
1394
7c1c2871
DA
1395int i915_master_create(struct drm_device *dev, struct drm_master *master)
1396{
1397 struct drm_i915_master_private *master_priv;
1398
9a298b2a 1399 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
7c1c2871
DA
1400 if (!master_priv)
1401 return -ENOMEM;
1402
1403 master->driver_priv = master_priv;
1404 return 0;
1405}
1406
1407void i915_master_destroy(struct drm_device *dev, struct drm_master *master)
1408{
1409 struct drm_i915_master_private *master_priv = master->driver_priv;
1410
1411 if (!master_priv)
1412 return;
1413
9a298b2a 1414 kfree(master_priv);
7c1c2871
DA
1415
1416 master->driver_priv = NULL;
1417}
1418
e188719a
DV
1419static void i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
1420{
1421 struct apertures_struct *ap;
1422 struct pci_dev *pdev = dev_priv->dev->pdev;
1423 bool primary;
1424
1425 ap = alloc_apertures(1);
1426 if (!ap)
1427 return;
1428
dabb7a91 1429 ap->ranges[0].base = dev_priv->gtt.mappable_base;
f64e2922 1430 ap->ranges[0].size = dev_priv->gtt.mappable_end;
93d18799 1431
e188719a
DV
1432 primary =
1433 pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
1434
1435 remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
1436
1437 kfree(ap);
1438}
1439
c96ea64e
DV
1440static void i915_dump_device_info(struct drm_i915_private *dev_priv)
1441{
1442 const struct intel_device_info *info = dev_priv->info;
1443
e2a5800a
DL
1444#define PRINT_S(name) "%s"
1445#define SEP_EMPTY
79fc46df
DL
1446#define PRINT_FLAG(name) info->name ? #name "," : ""
1447#define SEP_COMMA ,
c96ea64e 1448 DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x flags="
e2a5800a 1449 DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
c96ea64e
DV
1450 info->gen,
1451 dev_priv->dev->pdev->device,
79fc46df 1452 DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
e2a5800a
DL
1453#undef PRINT_S
1454#undef SEP_EMPTY
79fc46df
DL
1455#undef PRINT_FLAG
1456#undef SEP_COMMA
c96ea64e
DV
1457}
1458
79e53945
JB
1459/**
1460 * i915_driver_load - setup chip and create an initial config
1461 * @dev: DRM device
1462 * @flags: startup flags
1463 *
1464 * The driver load routine has to do several things:
1465 * - drive output discovery via intel_modeset_init()
1466 * - initialize the memory manager
1467 * - allocate initial config memory
1468 * - setup the DRM framebuffer with the allocated memory
1469 */
84b1fd10 1470int i915_driver_load(struct drm_device *dev, unsigned long flags)
22eae947 1471{
ea059a1e 1472 struct drm_i915_private *dev_priv;
26394d92 1473 struct intel_device_info *info;
934d6086 1474 int ret = 0, mmio_bar, mmio_size;
9021f284 1475 uint32_t aperture_size;
fe669bf8 1476
26394d92
DV
1477 info = (struct intel_device_info *) flags;
1478
1479 /* Refuse to load on gen6+ without kms enabled. */
e147accb
JN
1480 if (info->gen >= 6 && !drm_core_check_feature(dev, DRIVER_MODESET)) {
1481 DRM_INFO("Your hardware requires kernel modesetting (KMS)\n");
1482 DRM_INFO("See CONFIG_DRM_I915_KMS, nomodeset, and i915.modeset parameters\n");
26394d92 1483 return -ENODEV;
e147accb 1484 }
26394d92 1485
b14c5679 1486 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
ba8bbcf6
JB
1487 if (dev_priv == NULL)
1488 return -ENOMEM;
1489
ba8bbcf6 1490 dev->dev_private = (void *)dev_priv;
673a394b 1491 dev_priv->dev = dev;
26394d92 1492 dev_priv->info = info;
ba8bbcf6 1493
7dcd2677
KK
1494 spin_lock_init(&dev_priv->irq_lock);
1495 spin_lock_init(&dev_priv->gpu_error.lock);
7dcd2677 1496 spin_lock_init(&dev_priv->backlight.lock);
907b28c5 1497 spin_lock_init(&dev_priv->uncore.lock);
c20e8355 1498 spin_lock_init(&dev_priv->mm.object_stat_lock);
7dcd2677
KK
1499 mutex_init(&dev_priv->dpio_lock);
1500 mutex_init(&dev_priv->rps.hw_lock);
1501 mutex_init(&dev_priv->modeset_restore_lock);
1502
c67a470b
PZ
1503 mutex_init(&dev_priv->pc8.lock);
1504 dev_priv->pc8.requirements_met = false;
1505 dev_priv->pc8.gpu_idle = false;
1506 dev_priv->pc8.irqs_disabled = false;
1507 dev_priv->pc8.enabled = false;
1508 dev_priv->pc8.disable_count = 2; /* requirements_met + gpu_idle */
1509 INIT_DELAYED_WORK(&dev_priv->pc8.enable_work, hsw_enable_pc8_work);
1510
c96ea64e
DV
1511 i915_dump_device_info(dev_priv);
1512
ed1c9e2c
PZ
1513 /* Not all pre-production machines fall into this category, only the
1514 * very first ones. Almost everything should work, except for maybe
1515 * suspend/resume. And we don't implement workarounds that affect only
1516 * pre-production machines. */
1517 if (IS_HSW_EARLY_SDV(dev))
1518 DRM_INFO("This is an early pre-production Haswell machine. "
1519 "It may not be fully functional.\n");
1520
ec2a4c3f
DA
1521 if (i915_get_bridge_dev(dev)) {
1522 ret = -EIO;
1523 goto free_priv;
1524 }
1525
1e1bd0fd
BW
1526 mmio_bar = IS_GEN2(dev) ? 1 : 0;
1527 /* Before gen4, the registers and the GTT are behind different BARs.
1528 * However, from gen4 onwards, the registers and the GTT are shared
1529 * in the same BAR, so we want to restrict this ioremap from
1530 * clobbering the GTT which we want ioremap_wc instead. Fortunately,
1531 * the register BAR remains the same size for all the earlier
1532 * generations up to Ironlake.
1533 */
1534 if (info->gen < 5)
1535 mmio_size = 512*1024;
1536 else
1537 mmio_size = 2*1024*1024;
1538
1539 dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
1540 if (!dev_priv->regs) {
1541 DRM_ERROR("failed to map registers\n");
1542 ret = -EIO;
1543 goto put_bridge;
1544 }
1545
907b28c5 1546 intel_uncore_early_sanitize(dev);
1e1bd0fd 1547
c3d685a7
BW
1548 /* This must be called before any calls to HAS_PCH_* */
1549 intel_detect_pch(dev);
1550
1551 intel_uncore_init(dev);
1552
e76e9aeb
BW
1553 ret = i915_gem_gtt_init(dev);
1554 if (ret)
cbb47d17 1555 goto out_regs;
e188719a 1556
1623392a
CW
1557 if (drm_core_check_feature(dev, DRIVER_MODESET))
1558 i915_kick_out_firmware_fb(dev_priv);
e188719a 1559
466e69b8
DA
1560 pci_set_master(dev->pdev);
1561
9f82d238
DV
1562 /* overlay on gen2 is broken and can't address above 1G */
1563 if (IS_GEN2(dev))
1564 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
1565
6927faf3
JN
1566 /* 965GM sometimes incorrectly writes to hardware status page (HWS)
1567 * using 32bit addressing, overwriting memory if HWS is located
1568 * above 4GB.
1569 *
1570 * The documentation also mentions an issue with undefined
1571 * behaviour if any general state is accessed within a page above 4GB,
1572 * which also needs to be handled carefully.
1573 */
1574 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1575 dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
1576
93d18799 1577 aperture_size = dev_priv->gtt.mappable_end;
71e9339c 1578
5d4545ae
BW
1579 dev_priv->gtt.mappable =
1580 io_mapping_create_wc(dev_priv->gtt.mappable_base,
dd2757f8 1581 aperture_size);
5d4545ae 1582 if (dev_priv->gtt.mappable == NULL) {
6644107d 1583 ret = -EIO;
cbb47d17 1584 goto out_gtt;
6644107d
VP
1585 }
1586
911bdf0a
BW
1587 dev_priv->gtt.mtrr = arch_phys_wc_add(dev_priv->gtt.mappable_base,
1588 aperture_size);
19966754 1589
e642abbf
CW
1590 /* The i915 workqueue is primarily used for batched retirement of
1591 * requests (and thus managing bo) once the task has been completed
1592 * by the GPU. i915_gem_retire_requests() is called directly when we
1593 * need high-priority retirement, such as waiting for an explicit
1594 * bo.
1595 *
1596 * It is also used for periodic low-priority events, such as
df9c2042 1597 * idle-timers and recording error state.
e642abbf
CW
1598 *
1599 * All tasks on the workqueue are expected to acquire the dev mutex
1600 * so there is no point in running more than one instance of the
53621860 1601 * workqueue at any time. Use an ordered one.
e642abbf 1602 */
53621860 1603 dev_priv->wq = alloc_ordered_workqueue("i915", 0);
9c9fe1f8
EA
1604 if (dev_priv->wq == NULL) {
1605 DRM_ERROR("Failed to create our workqueue.\n");
1606 ret = -ENOMEM;
a7b85d2a 1607 goto out_mtrrfree;
9c9fe1f8
EA
1608 }
1609
f71d4af4 1610 intel_irq_init(dev);
907b28c5 1611 intel_pm_init(dev);
78511f2a 1612 intel_uncore_sanitize(dev);
9880b7a5 1613
c4804411
ZW
1614 /* Try to make sure MCHBAR is enabled before poking at it */
1615 intel_setup_mchbar(dev);
f899fc64 1616 intel_setup_gmbus(dev);
44834a67 1617 intel_opregion_setup(dev);
c4804411 1618
6d139a87
BF
1619 intel_setup_bios(dev);
1620
673a394b
EA
1621 i915_gem_load(dev);
1622
ed4cb414
EA
1623 /* On the 945G/GM, the chipset reports the MSI capability on the
1624 * integrated graphics even though the support isn't actually there
1625 * according to the published specs. It doesn't appear to function
1626 * correctly in testing on 945G.
1627 * This may be a side effect of MSI having been made available for PEG
1628 * and the registers being closely associated.
d1ed629f
KP
1629 *
1630 * According to chipset errata, on the 965GM, MSI interrupts may
b60678a7
KP
1631 * be lost or delayed, but we use them anyways to avoid
1632 * stuck interrupts on some machines.
ed4cb414 1633 */
b60678a7 1634 if (!IS_I945G(dev) && !IS_I945GM(dev))
d3e74d02 1635 pci_enable_msi(dev->pdev);
ed4cb414 1636
7f1f3851
JB
1637 dev_priv->num_plane = 1;
1638 if (IS_VALLEYVIEW(dev))
1639 dev_priv->num_plane = 2;
1640
e3c74757
BW
1641 if (INTEL_INFO(dev)->num_pipes) {
1642 ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
1643 if (ret)
1644 goto out_gem_unload;
1645 }
52440211 1646
a38911a3
WX
1647 if (HAS_POWER_WELL(dev))
1648 i915_init_power_well(dev);
1649
79e53945 1650 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
53984635 1651 ret = i915_load_modeset_init(dev);
79e53945
JB
1652 if (ret < 0) {
1653 DRM_ERROR("failed to init modeset\n");
cbb47d17 1654 goto out_power_well;
79e53945 1655 }
db1b76ca
DV
1656 } else {
1657 /* Start out suspended in ums mode. */
1658 dev_priv->ums.mm_suspended = 1;
79e53945
JB
1659 }
1660
0136db58
BW
1661 i915_setup_sysfs(dev);
1662
e3c74757
BW
1663 if (INTEL_INFO(dev)->num_pipes) {
1664 /* Must be done after probing outputs */
1665 intel_opregion_init(dev);
8e5c2b77 1666 acpi_video_register();
e3c74757 1667 }
74a365b3 1668
eb48eb00
DV
1669 if (IS_GEN5(dev))
1670 intel_gpu_ips_init(dev_priv);
63ee41d7 1671
79e53945
JB
1672 return 0;
1673
cbb47d17
CW
1674out_power_well:
1675 if (HAS_POWER_WELL(dev))
1676 i915_remove_power_well(dev);
1677 drm_vblank_cleanup(dev);
56e2ea34 1678out_gem_unload:
7dc19d5a 1679 if (dev_priv->mm.inactive_shrinker.scan_objects)
a7b85d2a
KP
1680 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1681
56e2ea34
CW
1682 if (dev->pdev->msi_enabled)
1683 pci_disable_msi(dev->pdev);
1684
1685 intel_teardown_gmbus(dev);
1686 intel_teardown_mchbar(dev);
9c9fe1f8 1687 destroy_workqueue(dev_priv->wq);
a7b85d2a 1688out_mtrrfree:
911bdf0a 1689 arch_phys_wc_del(dev_priv->gtt.mtrr);
5d4545ae 1690 io_mapping_free(dev_priv->gtt.mappable);
cbb47d17
CW
1691out_gtt:
1692 list_del(&dev_priv->gtt.base.global_link);
1693 drm_mm_takedown(&dev_priv->gtt.base.mm);
853ba5d2 1694 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
cbb47d17 1695out_regs:
c3d685a7 1696 intel_uncore_fini(dev);
6dda569f 1697 pci_iounmap(dev->pdev, dev_priv->regs);
ec2a4c3f
DA
1698put_bridge:
1699 pci_dev_put(dev_priv->bridge_dev);
79e53945 1700free_priv:
cbb47d17
CW
1701 if (dev_priv->slab)
1702 kmem_cache_destroy(dev_priv->slab);
9a298b2a 1703 kfree(dev_priv);
ba8bbcf6
JB
1704 return ret;
1705}
1706
1707int i915_driver_unload(struct drm_device *dev)
1708{
1709 struct drm_i915_private *dev_priv = dev->dev_private;
c911fc1c 1710 int ret;
ba8bbcf6 1711
eb48eb00 1712 intel_gpu_ips_teardown();
7648fa99 1713
79f8dea1
PZ
1714 if (HAS_POWER_WELL(dev)) {
1715 /* The i915.ko module is still not prepared to be loaded when
1716 * the power well is not enabled, so just enable it in case
1717 * we're going to unload/reload. */
1718 intel_set_power_well(dev, true);
a38911a3 1719 i915_remove_power_well(dev);
79f8dea1 1720 }
a38911a3 1721
0136db58
BW
1722 i915_teardown_sysfs(dev);
1723
7dc19d5a 1724 if (dev_priv->mm.inactive_shrinker.scan_objects)
17250b71
CW
1725 unregister_shrinker(&dev_priv->mm.inactive_shrinker);
1726
c911fc1c 1727 mutex_lock(&dev->struct_mutex);
b2da9fe5 1728 ret = i915_gpu_idle(dev);
c911fc1c
DV
1729 if (ret)
1730 DRM_ERROR("failed to idle hardware: %d\n", ret);
b2da9fe5 1731 i915_gem_retire_requests(dev);
c911fc1c
DV
1732 mutex_unlock(&dev->struct_mutex);
1733
75ef9da2
DV
1734 /* Cancel the retire work handler, which should be idle now. */
1735 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
1736
5d4545ae 1737 io_mapping_free(dev_priv->gtt.mappable);
911bdf0a 1738 arch_phys_wc_del(dev_priv->gtt.mtrr);
ab657db1 1739
44834a67
CW
1740 acpi_video_unregister();
1741
79e53945 1742 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
7b4f3990 1743 intel_fbdev_fini(dev);
3d8620cc 1744 intel_modeset_cleanup(dev);
073f34d9 1745 cancel_work_sync(&dev_priv->console_resume_work);
3d8620cc 1746
6363ee6f
ZY
1747 /*
1748 * free the memory space allocated for the child device
1749 * config parsed from VBT
1750 */
41aa3448
RV
1751 if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
1752 kfree(dev_priv->vbt.child_dev);
1753 dev_priv->vbt.child_dev = NULL;
1754 dev_priv->vbt.child_dev_num = 0;
6363ee6f 1755 }
6c0d9350 1756
6a9ee8af 1757 vga_switcheroo_unregister_client(dev->pdev);
28d52043 1758 vga_client_register(dev->pdev, NULL, NULL, NULL);
79e53945
JB
1759 }
1760
a8b4899e 1761 /* Free error state after interrupts are fully disabled. */
99584db3
DV
1762 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
1763 cancel_work_sync(&dev_priv->gpu_error.work);
a8b4899e 1764 i915_destroy_error_state(dev);
bc0c7f14 1765
c67a470b
PZ
1766 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
1767
ed4cb414
EA
1768 if (dev->pdev->msi_enabled)
1769 pci_disable_msi(dev->pdev);
1770
44834a67 1771 intel_opregion_fini(dev);
8ee1c3db 1772
79e53945 1773 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
67e77c5a
DV
1774 /* Flush any outstanding unpin_work. */
1775 flush_workqueue(dev_priv->wq);
1776
79e53945 1777 mutex_lock(&dev->struct_mutex);
ecbec53b 1778 i915_gem_free_all_phys_object(dev);
79e53945 1779 i915_gem_cleanup_ringbuffer(dev);
55a66628 1780 i915_gem_context_fini(dev);
79e53945 1781 mutex_unlock(&dev->struct_mutex);
1d2a314c 1782 i915_gem_cleanup_aliasing_ppgtt(dev);
9797fbfb 1783 i915_gem_cleanup_stolen(dev);
c2873e96
KP
1784
1785 if (!I915_NEED_GFX_HWS(dev))
1786 i915_free_hws(dev);
79e53945
JB
1787 }
1788
a7bbbd63
BW
1789 list_del(&dev_priv->gtt.base.global_link);
1790 WARN_ON(!list_empty(&dev_priv->vm_list));
93bd8649 1791 drm_mm_takedown(&dev_priv->gtt.base.mm);
701394cc 1792
cbb47d17
CW
1793 drm_vblank_cleanup(dev);
1794
f899fc64 1795 intel_teardown_gmbus(dev);
c4804411
ZW
1796 intel_teardown_mchbar(dev);
1797
bc0c7f14 1798 destroy_workqueue(dev_priv->wq);
9ee32fea 1799 pm_qos_remove_request(&dev_priv->pm_qos);
bc0c7f14 1800
853ba5d2 1801 dev_priv->gtt.base.cleanup(&dev_priv->gtt.base);
6640aab6 1802
aec347ab
CW
1803 intel_uncore_fini(dev);
1804 if (dev_priv->regs != NULL)
1805 pci_iounmap(dev->pdev, dev_priv->regs);
1806
42dcedd4
CW
1807 if (dev_priv->slab)
1808 kmem_cache_destroy(dev_priv->slab);
bc0c7f14 1809
ec2a4c3f 1810 pci_dev_put(dev_priv->bridge_dev);
9a298b2a 1811 kfree(dev->dev_private);
ba8bbcf6 1812
22eae947
DA
1813 return 0;
1814}
1815
f787a5f5 1816int i915_driver_open(struct drm_device *dev, struct drm_file *file)
673a394b 1817{
b29c19b6 1818 int ret;
673a394b 1819
b29c19b6
CW
1820 ret = i915_gem_open(dev, file);
1821 if (ret)
1822 return ret;
254f965c 1823
673a394b
EA
1824 return 0;
1825}
1826
79e53945
JB
1827/**
1828 * i915_driver_lastclose - clean up after all DRM clients have exited
1829 * @dev: DRM device
1830 *
1831 * Take care of cleaning up after all DRM clients have exited. In the
1832 * mode setting case, we want to restore the kernel's initial mode (just
1833 * in case the last client left us in a bad state).
1834 *
9021f284 1835 * Additionally, in the non-mode setting case, we'll tear down the GTT
79e53945
JB
1836 * and DMA structures, since the kernel won't be using them, and clea
1837 * up any GEM state.
1838 */
84b1fd10 1839void i915_driver_lastclose(struct drm_device * dev)
1da177e4 1840{
ba8bbcf6
JB
1841 drm_i915_private_t *dev_priv = dev->dev_private;
1842
e8aeaee7
DV
1843 /* On gen6+ we refuse to init without kms enabled, but then the drm core
1844 * goes right around and calls lastclose. Check for this and don't clean
1845 * up anything. */
1846 if (!dev_priv)
1847 return;
1848
1849 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
e8e7a2b8 1850 intel_fb_restore_mode(dev);
6a9ee8af 1851 vga_switcheroo_process_delayed_switch();
144a75fa 1852 return;
79e53945 1853 }
144a75fa 1854
673a394b
EA
1855 i915_gem_lastclose(dev);
1856
b5e89ed5 1857 i915_dma_cleanup(dev);
1da177e4
LT
1858}
1859
6c340eac 1860void i915_driver_preclose(struct drm_device * dev, struct drm_file *file_priv)
1da177e4 1861{
254f965c 1862 i915_gem_context_close(dev, file_priv);
b962442e 1863 i915_gem_release(dev, file_priv);
1da177e4
LT
1864}
1865
f787a5f5 1866void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
673a394b 1867{
f787a5f5 1868 struct drm_i915_file_private *file_priv = file->driver_priv;
673a394b 1869
f787a5f5 1870 kfree(file_priv);
673a394b
EA
1871}
1872
baa70943 1873const struct drm_ioctl_desc i915_ioctls[] = {
1b2f1489
DA
1874 DRM_IOCTL_DEF_DRV(I915_INIT, i915_dma_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1875 DRM_IOCTL_DEF_DRV(I915_FLUSH, i915_flush_ioctl, DRM_AUTH),
1876 DRM_IOCTL_DEF_DRV(I915_FLIP, i915_flip_bufs, DRM_AUTH),
1877 DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, i915_batchbuffer, DRM_AUTH),
1878 DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, i915_irq_emit, DRM_AUTH),
1879 DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, i915_irq_wait, DRM_AUTH),
10ba5012 1880 DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
1b2f1489 1881 DRM_IOCTL_DEF_DRV(I915_SETPARAM, i915_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
b2c606fe
DV
1882 DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
1883 DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
1884 DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1b2f1489 1885 DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, i915_cmdbuffer, DRM_AUTH),
b2c606fe 1886 DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
d1c1edbc 1887 DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1b2f1489
DA
1888 DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, i915_vblank_pipe_get, DRM_AUTH),
1889 DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, i915_vblank_swap, DRM_AUTH),
1890 DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, i915_set_status_page, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1891 DRM_IOCTL_DEF_DRV(I915_GEM_INIT, i915_gem_init_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1892 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH|DRM_UNLOCKED),
10ba5012 1893 DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1b2f1489
DA
1894 DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
1895 DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_unpin_ioctl, DRM_AUTH|DRM_ROOT_ONLY|DRM_UNLOCKED),
10ba5012
KH
1896 DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1897 DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1898 DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1899 DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1b2f1489
DA
1900 DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, i915_gem_entervt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
1901 DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, i915_gem_leavevt_ioctl, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY|DRM_UNLOCKED),
10ba5012
KH
1902 DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1903 DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1904 DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1905 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1906 DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1907 DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1908 DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1909 DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1910 DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1911 DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1b2f1489 1912 DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, DRM_UNLOCKED),
10ba5012 1913 DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1b2f1489
DA
1914 DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1915 DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
8ea30864
JB
1916 DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
1917 DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, intel_sprite_get_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW|DRM_UNLOCKED),
10ba5012
KH
1918 DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
1919 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1920 DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
1921 DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_UNLOCKED|DRM_RENDER_ALLOW),
c94f7029
DA
1922};
1923
1924int i915_max_ioctl = DRM_ARRAY_SIZE(i915_ioctls);
cda17380 1925
9021f284
DV
1926/*
1927 * This is really ugly: Because old userspace abused the linux agp interface to
1928 * manage the gtt, we need to claim that all intel devices are agp. For
1929 * otherwise the drm core refuses to initialize the agp support code.
cda17380 1930 */
84b1fd10 1931int i915_driver_device_is_agp(struct drm_device * dev)
cda17380
DA
1932{
1933 return 1;
1934}