1 /* i915_drv.c -- i830,i845,i855,i865,i915 driver -*- linux-c -*-
5 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include <linux/device.h>
36 #include <linux/console.h>
37 #include "drm_crtc_helper.h"
39 static int i915_modeset = -1;
40 module_param_named(modeset, i915_modeset, int, 0400);
42 unsigned int i915_fbpercrtc = 0;
43 module_param_named(fbpercrtc, i915_fbpercrtc, int, 0400);
45 unsigned int i915_powersave = 1;
46 module_param_named(powersave, i915_powersave, int, 0400);
48 static struct drm_driver driver;
50 #define INTEL_VGA_DEVICE(id) { \
51 .class = PCI_CLASS_DISPLAY_VGA << 8, \
52 .class_mask = 0xffff00, \
55 .subvendor = PCI_ANY_ID, \
56 .subdevice = PCI_ANY_ID, \
59 static struct pci_device_id pciidlist[] = {
60 INTEL_VGA_DEVICE(0x3577),
61 INTEL_VGA_DEVICE(0x2562),
62 INTEL_VGA_DEVICE(0x3582),
63 INTEL_VGA_DEVICE(0x2572),
64 INTEL_VGA_DEVICE(0x2582),
65 INTEL_VGA_DEVICE(0x258a),
66 INTEL_VGA_DEVICE(0x2592),
67 INTEL_VGA_DEVICE(0x2772),
68 INTEL_VGA_DEVICE(0x27a2),
69 INTEL_VGA_DEVICE(0x27ae),
70 INTEL_VGA_DEVICE(0x2972),
71 INTEL_VGA_DEVICE(0x2982),
72 INTEL_VGA_DEVICE(0x2992),
73 INTEL_VGA_DEVICE(0x29a2),
74 INTEL_VGA_DEVICE(0x29b2),
75 INTEL_VGA_DEVICE(0x29c2),
76 INTEL_VGA_DEVICE(0x29d2),
77 INTEL_VGA_DEVICE(0x2a02),
78 INTEL_VGA_DEVICE(0x2a12),
79 INTEL_VGA_DEVICE(0x2a42),
80 INTEL_VGA_DEVICE(0x2e02),
81 INTEL_VGA_DEVICE(0x2e12),
82 INTEL_VGA_DEVICE(0x2e22),
83 INTEL_VGA_DEVICE(0x2e32),
84 INTEL_VGA_DEVICE(0x2e42),
85 INTEL_VGA_DEVICE(0xa001),
86 INTEL_VGA_DEVICE(0xa011),
87 INTEL_VGA_DEVICE(0x35e8),
88 INTEL_VGA_DEVICE(0x0042),
89 INTEL_VGA_DEVICE(0x0046),
93 #if defined(CONFIG_DRM_I915_KMS)
94 MODULE_DEVICE_TABLE(pci, pciidlist);
97 static int i915_suspend(struct drm_device *dev, pm_message_t state)
99 struct drm_i915_private *dev_priv = dev->dev_private;
101 if (!dev || !dev_priv) {
102 DRM_ERROR("dev: %p, dev_priv: %p\n", dev, dev_priv);
103 DRM_ERROR("DRM not initialized, aborting suspend.\n");
107 if (state.event == PM_EVENT_PRETHAW)
110 pci_save_state(dev->pdev);
112 /* If KMS is active, we do the leavevt stuff here */
113 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
114 if (i915_gem_idle(dev))
115 dev_err(&dev->pdev->dev,
116 "GEM idle failed, resume may fail\n");
117 drm_irq_uninstall(dev);
120 i915_save_state(dev);
122 intel_opregion_free(dev, 1);
124 if (state.event == PM_EVENT_SUSPEND) {
125 /* Shut down the device */
126 pci_disable_device(dev->pdev);
127 pci_set_power_state(dev->pdev, PCI_D3hot);
130 /* Modeset on resume, not lid events */
131 dev_priv->modeset_on_lid = 0;
136 static int i915_resume(struct drm_device *dev)
138 struct drm_i915_private *dev_priv = dev->dev_private;
141 if (pci_enable_device(dev->pdev))
143 pci_set_master(dev->pdev);
145 i915_restore_state(dev);
147 intel_opregion_init(dev, 1);
149 /* KMS EnterVT equivalent */
150 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
151 mutex_lock(&dev->struct_mutex);
152 dev_priv->mm.suspended = 0;
154 ret = i915_gem_init_ringbuffer(dev);
157 mutex_unlock(&dev->struct_mutex);
159 drm_irq_install(dev);
161 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
162 /* Resume the modeset for every activated CRTC */
163 drm_helper_resume_force_mode(dev);
166 dev_priv->modeset_on_lid = 0;
172 * i965_reset - reset chip after a hang
173 * @dev: drm device to reset
174 * @flags: reset domains
176 * Reset the chip. Useful if a hang is detected. Returns zero on successful
177 * reset or otherwise an error code.
179 * Procedure is fairly simple:
180 * - reset the chip using the reset reg
181 * - re-init context state
182 * - re-init hardware status page
183 * - re-init ring buffer
184 * - re-init interrupt state
187 int i965_reset(struct drm_device *dev, u8 flags)
189 drm_i915_private_t *dev_priv = dev->dev_private;
190 unsigned long timeout;
193 * We really should only reset the display subsystem if we actually
196 bool need_display = true;
198 mutex_lock(&dev->struct_mutex);
203 i915_gem_retire_requests(dev);
206 i915_save_display(dev);
208 if (IS_I965G(dev) || IS_G4X(dev)) {
210 * Set the domains we want to reset, then the reset bit (bit 0).
211 * Clear the reset bit after a while and wait for hardware status
212 * bit (bit 1) to be set
214 pci_read_config_byte(dev->pdev, GDRST, &gdrst);
215 pci_write_config_byte(dev->pdev, GDRST, gdrst | flags | ((flags == GDRST_FULL) ? 0x1 : 0x0));
217 pci_write_config_byte(dev->pdev, GDRST, gdrst & 0xfe);
219 /* ...we don't want to loop forever though, 500ms should be plenty */
220 timeout = jiffies + msecs_to_jiffies(500);
223 pci_read_config_byte(dev->pdev, GDRST, &gdrst);
224 } while ((gdrst & 0x1) && time_after(timeout, jiffies));
227 WARN(true, "i915: Failed to reset chip\n");
228 mutex_unlock(&dev->struct_mutex);
232 DRM_ERROR("Error occurred. Don't know how to reset this chip.\n");
236 /* Ok, now get things going again... */
239 * Everything depends on having the GTT running, so we need to start
240 * there. Fortunately we don't need to do this unless we reset the
241 * chip at a PCI level.
243 * Next we need to restore the context, but we don't use those
246 * Ring buffer needs to be re-initialized in the KMS case, or if X
247 * was running at the time of the reset (i.e. we weren't VT
250 if (drm_core_check_feature(dev, DRIVER_MODESET) ||
251 !dev_priv->mm.suspended) {
252 drm_i915_ring_buffer_t *ring = &dev_priv->ring;
253 struct drm_gem_object *obj = ring->ring_obj;
254 struct drm_i915_gem_object *obj_priv = obj->driver_private;
255 dev_priv->mm.suspended = 0;
257 /* Stop the ring if it's running. */
258 I915_WRITE(PRB0_CTL, 0);
259 I915_WRITE(PRB0_TAIL, 0);
260 I915_WRITE(PRB0_HEAD, 0);
262 /* Initialize the ring. */
263 I915_WRITE(PRB0_START, obj_priv->gtt_offset);
265 ((obj->size - 4096) & RING_NR_PAGES) |
268 if (!drm_core_check_feature(dev, DRIVER_MODESET))
269 i915_kernel_lost_context(dev);
271 ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
272 ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
273 ring->space = ring->head - (ring->tail + 8);
275 ring->space += ring->Size;
278 mutex_unlock(&dev->struct_mutex);
279 drm_irq_uninstall(dev);
280 drm_irq_install(dev);
281 mutex_lock(&dev->struct_mutex);
285 * Display needs restore too...
288 i915_restore_display(dev);
290 mutex_unlock(&dev->struct_mutex);
296 i915_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
298 return drm_get_dev(pdev, ent, &driver);
302 i915_pci_remove(struct pci_dev *pdev)
304 struct drm_device *dev = pci_get_drvdata(pdev);
310 i915_pci_suspend(struct pci_dev *pdev, pm_message_t state)
312 struct drm_device *dev = pci_get_drvdata(pdev);
314 return i915_suspend(dev, state);
318 i915_pci_resume(struct pci_dev *pdev)
320 struct drm_device *dev = pci_get_drvdata(pdev);
322 return i915_resume(dev);
326 i915_pm_suspend(struct device *dev)
328 return i915_pci_suspend(to_pci_dev(dev), PMSG_SUSPEND);
332 i915_pm_resume(struct device *dev)
334 return i915_pci_resume(to_pci_dev(dev));
338 i915_pm_freeze(struct device *dev)
340 return i915_pci_suspend(to_pci_dev(dev), PMSG_FREEZE);
344 i915_pm_thaw(struct device *dev)
346 /* thaw during hibernate, do nothing! */
351 i915_pm_poweroff(struct device *dev)
353 return i915_pci_suspend(to_pci_dev(dev), PMSG_HIBERNATE);
357 i915_pm_restore(struct device *dev)
359 return i915_pci_resume(to_pci_dev(dev));
362 const struct dev_pm_ops i915_pm_ops = {
363 .suspend = i915_pm_suspend,
364 .resume = i915_pm_resume,
365 .freeze = i915_pm_freeze,
366 .thaw = i915_pm_thaw,
367 .poweroff = i915_pm_poweroff,
368 .restore = i915_pm_restore,
371 static struct vm_operations_struct i915_gem_vm_ops = {
372 .fault = i915_gem_fault,
373 .open = drm_gem_vm_open,
374 .close = drm_gem_vm_close,
377 static struct drm_driver driver = {
378 /* don't use mtrr's here, the Xserver or user space app should
379 * deal with them for intel hardware.
382 DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | /* DRIVER_USE_MTRR |*/
383 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM,
384 .load = i915_driver_load,
385 .unload = i915_driver_unload,
386 .open = i915_driver_open,
387 .lastclose = i915_driver_lastclose,
388 .preclose = i915_driver_preclose,
389 .postclose = i915_driver_postclose,
390 .device_is_agp = i915_driver_device_is_agp,
391 .enable_vblank = i915_enable_vblank,
392 .disable_vblank = i915_disable_vblank,
393 .irq_preinstall = i915_driver_irq_preinstall,
394 .irq_postinstall = i915_driver_irq_postinstall,
395 .irq_uninstall = i915_driver_irq_uninstall,
396 .irq_handler = i915_driver_irq_handler,
397 .reclaim_buffers = drm_core_reclaim_buffers,
398 .get_map_ofs = drm_core_get_map_ofs,
399 .get_reg_ofs = drm_core_get_reg_ofs,
400 .master_create = i915_master_create,
401 .master_destroy = i915_master_destroy,
402 #if defined(CONFIG_DEBUG_FS)
403 .debugfs_init = i915_debugfs_init,
404 .debugfs_cleanup = i915_debugfs_cleanup,
406 .gem_init_object = i915_gem_init_object,
407 .gem_free_object = i915_gem_free_object,
408 .gem_vm_ops = &i915_gem_vm_ops,
409 .ioctls = i915_ioctls,
411 .owner = THIS_MODULE,
413 .release = drm_release,
415 .mmap = drm_gem_mmap,
417 .fasync = drm_fasync,
420 .compat_ioctl = i915_compat_ioctl,
426 .id_table = pciidlist,
427 .probe = i915_pci_probe,
428 .remove = i915_pci_remove,
429 .driver.pm = &i915_pm_ops,
435 .major = DRIVER_MAJOR,
436 .minor = DRIVER_MINOR,
437 .patchlevel = DRIVER_PATCHLEVEL,
440 static int __init i915_init(void)
442 driver.num_ioctls = i915_max_ioctl;
444 i915_gem_shrinker_init();
447 * If CONFIG_DRM_I915_KMS is set, default to KMS unless
448 * explicitly disabled with the module pararmeter.
450 * Otherwise, just follow the parameter (defaulting to off).
452 * Allow optional vga_text_mode_force boot option to override
453 * the default behavior.
455 #if defined(CONFIG_DRM_I915_KMS)
456 if (i915_modeset != 0)
457 driver.driver_features |= DRIVER_MODESET;
459 if (i915_modeset == 1)
460 driver.driver_features |= DRIVER_MODESET;
462 #ifdef CONFIG_VGA_CONSOLE
463 if (vgacon_text_force() && i915_modeset == -1)
464 driver.driver_features &= ~DRIVER_MODESET;
467 return drm_init(&driver);
470 static void __exit i915_exit(void)
472 i915_gem_shrinker_exit();
476 module_init(i915_init);
477 module_exit(i915_exit);
479 MODULE_AUTHOR(DRIVER_AUTHOR);
480 MODULE_DESCRIPTION(DRIVER_DESC);
481 MODULE_LICENSE("GPL and additional rights");