i915_drv.c 10 KB

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