i915_drv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* i915_drv.c -- i830,i845,i855,i865,i915 driver -*- linux-c -*-
  2. */
  3. /*
  4. *
  5. * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
  6. * All Rights Reserved.
  7. *
  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:
  15. *
  16. * The above copyright notice and this permission notice (including the
  17. * next paragraph) shall be included in all copies or substantial portions
  18. * of the Software.
  19. *
  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.
  27. *
  28. */
  29. #include <linux/device.h>
  30. #include "drmP.h"
  31. #include "drm.h"
  32. #include "i915_drm.h"
  33. #include "i915_drv.h"
  34. #include "drm_pciids.h"
  35. #include <linux/console.h>
  36. #include "drm_crtc_helper.h"
  37. static int i915_modeset = -1;
  38. module_param_named(modeset, i915_modeset, int, 0400);
  39. unsigned int i915_fbpercrtc = 0;
  40. module_param_named(fbpercrtc, i915_fbpercrtc, int, 0400);
  41. unsigned int i915_powersave = 1;
  42. module_param_named(powersave, i915_powersave, int, 0400);
  43. static struct drm_driver driver;
  44. static struct pci_device_id pciidlist[] = {
  45. i915_PCI_IDS
  46. };
  47. #if defined(CONFIG_DRM_I915_KMS)
  48. MODULE_DEVICE_TABLE(pci, pciidlist);
  49. #endif
  50. static int i915_suspend(struct drm_device *dev, pm_message_t state)
  51. {
  52. struct drm_i915_private *dev_priv = dev->dev_private;
  53. if (!dev || !dev_priv) {
  54. DRM_ERROR("dev: %p, dev_priv: %p\n", dev, dev_priv);
  55. DRM_ERROR("DRM not initialized, aborting suspend.\n");
  56. return -ENODEV;
  57. }
  58. if (state.event == PM_EVENT_PRETHAW)
  59. return 0;
  60. pci_save_state(dev->pdev);
  61. /* If KMS is active, we do the leavevt stuff here */
  62. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  63. if (i915_gem_idle(dev))
  64. dev_err(&dev->pdev->dev,
  65. "GEM idle failed, resume may fail\n");
  66. drm_irq_uninstall(dev);
  67. }
  68. i915_save_state(dev);
  69. intel_opregion_free(dev, 1);
  70. if (state.event == PM_EVENT_SUSPEND) {
  71. /* Shut down the device */
  72. pci_disable_device(dev->pdev);
  73. pci_set_power_state(dev->pdev, PCI_D3hot);
  74. }
  75. dev_priv->suspended = 1;
  76. return 0;
  77. }
  78. static int i915_resume(struct drm_device *dev)
  79. {
  80. struct drm_i915_private *dev_priv = dev->dev_private;
  81. int ret = 0;
  82. if (pci_enable_device(dev->pdev))
  83. return -1;
  84. pci_set_master(dev->pdev);
  85. i915_restore_state(dev);
  86. intel_opregion_init(dev, 1);
  87. /* KMS EnterVT equivalent */
  88. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  89. mutex_lock(&dev->struct_mutex);
  90. dev_priv->mm.suspended = 0;
  91. ret = i915_gem_init_ringbuffer(dev);
  92. if (ret != 0)
  93. ret = -1;
  94. mutex_unlock(&dev->struct_mutex);
  95. drm_irq_install(dev);
  96. }
  97. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  98. /* Resume the modeset for every activated CRTC */
  99. drm_helper_resume_force_mode(dev);
  100. }
  101. dev_priv->suspended = 0;
  102. return ret;
  103. }
  104. /**
  105. * i965_reset - reset chip after a hang
  106. * @dev: drm device to reset
  107. * @flags: reset domains
  108. *
  109. * Reset the chip. Useful if a hang is detected. Returns zero on successful
  110. * reset or otherwise an error code.
  111. *
  112. * Procedure is fairly simple:
  113. * - reset the chip using the reset reg
  114. * - re-init context state
  115. * - re-init hardware status page
  116. * - re-init ring buffer
  117. * - re-init interrupt state
  118. * - re-init display
  119. */
  120. int i965_reset(struct drm_device *dev, u8 flags)
  121. {
  122. drm_i915_private_t *dev_priv = dev->dev_private;
  123. unsigned long timeout;
  124. u8 gdrst;
  125. /*
  126. * We really should only reset the display subsystem if we actually
  127. * need to
  128. */
  129. bool need_display = true;
  130. mutex_lock(&dev->struct_mutex);
  131. /*
  132. * Clear request list
  133. */
  134. i915_gem_retire_requests(dev);
  135. if (need_display)
  136. i915_save_display(dev);
  137. if (IS_I965G(dev) || IS_G4X(dev)) {
  138. /*
  139. * Set the domains we want to reset, then the reset bit (bit 0).
  140. * Clear the reset bit after a while and wait for hardware status
  141. * bit (bit 1) to be set
  142. */
  143. pci_read_config_byte(dev->pdev, GDRST, &gdrst);
  144. pci_write_config_byte(dev->pdev, GDRST, gdrst | flags | ((flags == GDRST_FULL) ? 0x1 : 0x0));
  145. udelay(50);
  146. pci_write_config_byte(dev->pdev, GDRST, gdrst & 0xfe);
  147. /* ...we don't want to loop forever though, 500ms should be plenty */
  148. timeout = jiffies + msecs_to_jiffies(500);
  149. do {
  150. udelay(100);
  151. pci_read_config_byte(dev->pdev, GDRST, &gdrst);
  152. } while ((gdrst & 0x1) && time_after(timeout, jiffies));
  153. if (gdrst & 0x1) {
  154. WARN(true, "i915: Failed to reset chip\n");
  155. mutex_unlock(&dev->struct_mutex);
  156. return -EIO;
  157. }
  158. } else {
  159. DRM_ERROR("Error occurred. Don't know how to reset this chip.\n");
  160. return -ENODEV;
  161. }
  162. /* Ok, now get things going again... */
  163. /*
  164. * Everything depends on having the GTT running, so we need to start
  165. * there. Fortunately we don't need to do this unless we reset the
  166. * chip at a PCI level.
  167. *
  168. * Next we need to restore the context, but we don't use those
  169. * yet either...
  170. *
  171. * Ring buffer needs to be re-initialized in the KMS case, or if X
  172. * was running at the time of the reset (i.e. we weren't VT
  173. * switched away).
  174. */
  175. if (drm_core_check_feature(dev, DRIVER_MODESET) ||
  176. !dev_priv->mm.suspended) {
  177. drm_i915_ring_buffer_t *ring = &dev_priv->ring;
  178. struct drm_gem_object *obj = ring->ring_obj;
  179. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  180. dev_priv->mm.suspended = 0;
  181. /* Stop the ring if it's running. */
  182. I915_WRITE(PRB0_CTL, 0);
  183. I915_WRITE(PRB0_TAIL, 0);
  184. I915_WRITE(PRB0_HEAD, 0);
  185. /* Initialize the ring. */
  186. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  187. I915_WRITE(PRB0_CTL,
  188. ((obj->size - 4096) & RING_NR_PAGES) |
  189. RING_NO_REPORT |
  190. RING_VALID);
  191. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  192. i915_kernel_lost_context(dev);
  193. else {
  194. ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  195. ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
  196. ring->space = ring->head - (ring->tail + 8);
  197. if (ring->space < 0)
  198. ring->space += ring->Size;
  199. }
  200. mutex_unlock(&dev->struct_mutex);
  201. drm_irq_uninstall(dev);
  202. drm_irq_install(dev);
  203. mutex_lock(&dev->struct_mutex);
  204. }
  205. /*
  206. * Display needs restore too...
  207. */
  208. if (need_display)
  209. i915_restore_display(dev);
  210. mutex_unlock(&dev->struct_mutex);
  211. return 0;
  212. }
  213. static int __devinit
  214. i915_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  215. {
  216. return drm_get_dev(pdev, ent, &driver);
  217. }
  218. static void
  219. i915_pci_remove(struct pci_dev *pdev)
  220. {
  221. struct drm_device *dev = pci_get_drvdata(pdev);
  222. drm_put_dev(dev);
  223. }
  224. static int
  225. i915_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  226. {
  227. struct drm_device *dev = pci_get_drvdata(pdev);
  228. return i915_suspend(dev, state);
  229. }
  230. static int
  231. i915_pci_resume(struct pci_dev *pdev)
  232. {
  233. struct drm_device *dev = pci_get_drvdata(pdev);
  234. return i915_resume(dev);
  235. }
  236. static struct vm_operations_struct i915_gem_vm_ops = {
  237. .fault = i915_gem_fault,
  238. .open = drm_gem_vm_open,
  239. .close = drm_gem_vm_close,
  240. };
  241. static struct drm_driver driver = {
  242. /* don't use mtrr's here, the Xserver or user space app should
  243. * deal with them for intel hardware.
  244. */
  245. .driver_features =
  246. DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | /* DRIVER_USE_MTRR |*/
  247. DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM,
  248. .load = i915_driver_load,
  249. .unload = i915_driver_unload,
  250. .open = i915_driver_open,
  251. .lastclose = i915_driver_lastclose,
  252. .preclose = i915_driver_preclose,
  253. .postclose = i915_driver_postclose,
  254. .suspend = i915_suspend,
  255. .resume = i915_resume,
  256. .device_is_agp = i915_driver_device_is_agp,
  257. .enable_vblank = i915_enable_vblank,
  258. .disable_vblank = i915_disable_vblank,
  259. .irq_preinstall = i915_driver_irq_preinstall,
  260. .irq_postinstall = i915_driver_irq_postinstall,
  261. .irq_uninstall = i915_driver_irq_uninstall,
  262. .irq_handler = i915_driver_irq_handler,
  263. .reclaim_buffers = drm_core_reclaim_buffers,
  264. .get_map_ofs = drm_core_get_map_ofs,
  265. .get_reg_ofs = drm_core_get_reg_ofs,
  266. .master_create = i915_master_create,
  267. .master_destroy = i915_master_destroy,
  268. #if defined(CONFIG_DEBUG_FS)
  269. .debugfs_init = i915_debugfs_init,
  270. .debugfs_cleanup = i915_debugfs_cleanup,
  271. #endif
  272. .gem_init_object = i915_gem_init_object,
  273. .gem_free_object = i915_gem_free_object,
  274. .gem_vm_ops = &i915_gem_vm_ops,
  275. .ioctls = i915_ioctls,
  276. .fops = {
  277. .owner = THIS_MODULE,
  278. .open = drm_open,
  279. .release = drm_release,
  280. .ioctl = drm_ioctl,
  281. .mmap = drm_gem_mmap,
  282. .poll = drm_poll,
  283. .fasync = drm_fasync,
  284. #ifdef CONFIG_COMPAT
  285. .compat_ioctl = i915_compat_ioctl,
  286. #endif
  287. },
  288. .pci_driver = {
  289. .name = DRIVER_NAME,
  290. .id_table = pciidlist,
  291. .probe = i915_pci_probe,
  292. .remove = i915_pci_remove,
  293. #ifdef CONFIG_PM
  294. .resume = i915_pci_resume,
  295. .suspend = i915_pci_suspend,
  296. #endif
  297. },
  298. .name = DRIVER_NAME,
  299. .desc = DRIVER_DESC,
  300. .date = DRIVER_DATE,
  301. .major = DRIVER_MAJOR,
  302. .minor = DRIVER_MINOR,
  303. .patchlevel = DRIVER_PATCHLEVEL,
  304. };
  305. static int __init i915_init(void)
  306. {
  307. driver.num_ioctls = i915_max_ioctl;
  308. i915_gem_shrinker_init();
  309. /*
  310. * If CONFIG_DRM_I915_KMS is set, default to KMS unless
  311. * explicitly disabled with the module pararmeter.
  312. *
  313. * Otherwise, just follow the parameter (defaulting to off).
  314. *
  315. * Allow optional vga_text_mode_force boot option to override
  316. * the default behavior.
  317. */
  318. #if defined(CONFIG_DRM_I915_KMS)
  319. if (i915_modeset != 0)
  320. driver.driver_features |= DRIVER_MODESET;
  321. #endif
  322. if (i915_modeset == 1)
  323. driver.driver_features |= DRIVER_MODESET;
  324. #ifdef CONFIG_VGA_CONSOLE
  325. if (vgacon_text_force() && i915_modeset == -1)
  326. driver.driver_features &= ~DRIVER_MODESET;
  327. #endif
  328. return drm_init(&driver);
  329. }
  330. static void __exit i915_exit(void)
  331. {
  332. i915_gem_shrinker_exit();
  333. drm_exit(&driver);
  334. }
  335. module_init(i915_init);
  336. module_exit(i915_exit);
  337. MODULE_AUTHOR(DRIVER_AUTHOR);
  338. MODULE_DESCRIPTION(DRIVER_DESC);
  339. MODULE_LICENSE("GPL and additional rights");