i915_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 <linux/console.h>
  35. #include "drm_crtc_helper.h"
  36. static int i915_modeset = -1;
  37. module_param_named(modeset, i915_modeset, int, 0400);
  38. unsigned int i915_fbpercrtc = 0;
  39. module_param_named(fbpercrtc, i915_fbpercrtc, int, 0400);
  40. unsigned int i915_powersave = 1;
  41. module_param_named(powersave, i915_powersave, int, 0400);
  42. static struct drm_driver driver;
  43. #define INTEL_VGA_DEVICE(id) { \
  44. .class = PCI_CLASS_DISPLAY_VGA << 8, \
  45. .class_mask = 0xffff00, \
  46. .vendor = 0x8086, \
  47. .device = id, \
  48. .subvendor = PCI_ANY_ID, \
  49. .subdevice = PCI_ANY_ID, \
  50. .driver_data = 0 }
  51. static struct pci_device_id pciidlist[] = {
  52. INTEL_VGA_DEVICE(0x3577),
  53. INTEL_VGA_DEVICE(0x2562),
  54. INTEL_VGA_DEVICE(0x3582),
  55. INTEL_VGA_DEVICE(0x2572),
  56. INTEL_VGA_DEVICE(0x2582),
  57. INTEL_VGA_DEVICE(0x258a),
  58. INTEL_VGA_DEVICE(0x2592),
  59. INTEL_VGA_DEVICE(0x2772),
  60. INTEL_VGA_DEVICE(0x27a2),
  61. INTEL_VGA_DEVICE(0x27ae),
  62. INTEL_VGA_DEVICE(0x2972),
  63. INTEL_VGA_DEVICE(0x2982),
  64. INTEL_VGA_DEVICE(0x2992),
  65. INTEL_VGA_DEVICE(0x29a2),
  66. INTEL_VGA_DEVICE(0x29b2),
  67. INTEL_VGA_DEVICE(0x29c2),
  68. INTEL_VGA_DEVICE(0x29d2),
  69. INTEL_VGA_DEVICE(0x2a02),
  70. INTEL_VGA_DEVICE(0x2a12),
  71. INTEL_VGA_DEVICE(0x2a42),
  72. INTEL_VGA_DEVICE(0x2e02),
  73. INTEL_VGA_DEVICE(0x2e12),
  74. INTEL_VGA_DEVICE(0x2e22),
  75. INTEL_VGA_DEVICE(0x2e32),
  76. INTEL_VGA_DEVICE(0x2e42),
  77. INTEL_VGA_DEVICE(0xa001),
  78. INTEL_VGA_DEVICE(0xa011),
  79. INTEL_VGA_DEVICE(0x35e8),
  80. INTEL_VGA_DEVICE(0x0042),
  81. INTEL_VGA_DEVICE(0x0046),
  82. {0, 0, 0}
  83. };
  84. #if defined(CONFIG_DRM_I915_KMS)
  85. MODULE_DEVICE_TABLE(pci, pciidlist);
  86. #endif
  87. static int i915_suspend(struct drm_device *dev, pm_message_t state)
  88. {
  89. struct drm_i915_private *dev_priv = dev->dev_private;
  90. if (!dev || !dev_priv) {
  91. DRM_ERROR("dev: %p, dev_priv: %p\n", dev, dev_priv);
  92. DRM_ERROR("DRM not initialized, aborting suspend.\n");
  93. return -ENODEV;
  94. }
  95. if (state.event == PM_EVENT_PRETHAW)
  96. return 0;
  97. pci_save_state(dev->pdev);
  98. /* If KMS is active, we do the leavevt stuff here */
  99. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  100. if (i915_gem_idle(dev))
  101. dev_err(&dev->pdev->dev,
  102. "GEM idle failed, resume may fail\n");
  103. drm_irq_uninstall(dev);
  104. }
  105. i915_save_state(dev);
  106. intel_opregion_free(dev, 1);
  107. if (state.event == PM_EVENT_SUSPEND) {
  108. /* Shut down the device */
  109. pci_disable_device(dev->pdev);
  110. pci_set_power_state(dev->pdev, PCI_D3hot);
  111. }
  112. /* Modeset on resume, not lid events */
  113. dev_priv->modeset_on_lid = 0;
  114. return 0;
  115. }
  116. static int i915_resume(struct drm_device *dev)
  117. {
  118. struct drm_i915_private *dev_priv = dev->dev_private;
  119. int ret = 0;
  120. if (pci_enable_device(dev->pdev))
  121. return -1;
  122. pci_set_master(dev->pdev);
  123. i915_restore_state(dev);
  124. intel_opregion_init(dev, 1);
  125. /* KMS EnterVT equivalent */
  126. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  127. mutex_lock(&dev->struct_mutex);
  128. dev_priv->mm.suspended = 0;
  129. ret = i915_gem_init_ringbuffer(dev);
  130. if (ret != 0)
  131. ret = -1;
  132. mutex_unlock(&dev->struct_mutex);
  133. drm_irq_install(dev);
  134. }
  135. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  136. /* Resume the modeset for every activated CRTC */
  137. drm_helper_resume_force_mode(dev);
  138. }
  139. dev_priv->modeset_on_lid = 0;
  140. return ret;
  141. }
  142. /**
  143. * i965_reset - reset chip after a hang
  144. * @dev: drm device to reset
  145. * @flags: reset domains
  146. *
  147. * Reset the chip. Useful if a hang is detected. Returns zero on successful
  148. * reset or otherwise an error code.
  149. *
  150. * Procedure is fairly simple:
  151. * - reset the chip using the reset reg
  152. * - re-init context state
  153. * - re-init hardware status page
  154. * - re-init ring buffer
  155. * - re-init interrupt state
  156. * - re-init display
  157. */
  158. int i965_reset(struct drm_device *dev, u8 flags)
  159. {
  160. drm_i915_private_t *dev_priv = dev->dev_private;
  161. unsigned long timeout;
  162. u8 gdrst;
  163. /*
  164. * We really should only reset the display subsystem if we actually
  165. * need to
  166. */
  167. bool need_display = true;
  168. mutex_lock(&dev->struct_mutex);
  169. /*
  170. * Clear request list
  171. */
  172. i915_gem_retire_requests(dev);
  173. if (need_display)
  174. i915_save_display(dev);
  175. if (IS_I965G(dev) || IS_G4X(dev)) {
  176. /*
  177. * Set the domains we want to reset, then the reset bit (bit 0).
  178. * Clear the reset bit after a while and wait for hardware status
  179. * bit (bit 1) to be set
  180. */
  181. pci_read_config_byte(dev->pdev, GDRST, &gdrst);
  182. pci_write_config_byte(dev->pdev, GDRST, gdrst | flags | ((flags == GDRST_FULL) ? 0x1 : 0x0));
  183. udelay(50);
  184. pci_write_config_byte(dev->pdev, GDRST, gdrst & 0xfe);
  185. /* ...we don't want to loop forever though, 500ms should be plenty */
  186. timeout = jiffies + msecs_to_jiffies(500);
  187. do {
  188. udelay(100);
  189. pci_read_config_byte(dev->pdev, GDRST, &gdrst);
  190. } while ((gdrst & 0x1) && time_after(timeout, jiffies));
  191. if (gdrst & 0x1) {
  192. WARN(true, "i915: Failed to reset chip\n");
  193. mutex_unlock(&dev->struct_mutex);
  194. return -EIO;
  195. }
  196. } else {
  197. DRM_ERROR("Error occurred. Don't know how to reset this chip.\n");
  198. return -ENODEV;
  199. }
  200. /* Ok, now get things going again... */
  201. /*
  202. * Everything depends on having the GTT running, so we need to start
  203. * there. Fortunately we don't need to do this unless we reset the
  204. * chip at a PCI level.
  205. *
  206. * Next we need to restore the context, but we don't use those
  207. * yet either...
  208. *
  209. * Ring buffer needs to be re-initialized in the KMS case, or if X
  210. * was running at the time of the reset (i.e. we weren't VT
  211. * switched away).
  212. */
  213. if (drm_core_check_feature(dev, DRIVER_MODESET) ||
  214. !dev_priv->mm.suspended) {
  215. drm_i915_ring_buffer_t *ring = &dev_priv->ring;
  216. struct drm_gem_object *obj = ring->ring_obj;
  217. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  218. dev_priv->mm.suspended = 0;
  219. /* Stop the ring if it's running. */
  220. I915_WRITE(PRB0_CTL, 0);
  221. I915_WRITE(PRB0_TAIL, 0);
  222. I915_WRITE(PRB0_HEAD, 0);
  223. /* Initialize the ring. */
  224. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  225. I915_WRITE(PRB0_CTL,
  226. ((obj->size - 4096) & RING_NR_PAGES) |
  227. RING_NO_REPORT |
  228. RING_VALID);
  229. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  230. i915_kernel_lost_context(dev);
  231. else {
  232. ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  233. ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
  234. ring->space = ring->head - (ring->tail + 8);
  235. if (ring->space < 0)
  236. ring->space += ring->Size;
  237. }
  238. mutex_unlock(&dev->struct_mutex);
  239. drm_irq_uninstall(dev);
  240. drm_irq_install(dev);
  241. mutex_lock(&dev->struct_mutex);
  242. }
  243. /*
  244. * Display needs restore too...
  245. */
  246. if (need_display)
  247. i915_restore_display(dev);
  248. mutex_unlock(&dev->struct_mutex);
  249. return 0;
  250. }
  251. static int __devinit
  252. i915_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  253. {
  254. return drm_get_dev(pdev, ent, &driver);
  255. }
  256. static void
  257. i915_pci_remove(struct pci_dev *pdev)
  258. {
  259. struct drm_device *dev = pci_get_drvdata(pdev);
  260. drm_put_dev(dev);
  261. }
  262. static int
  263. i915_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  264. {
  265. struct drm_device *dev = pci_get_drvdata(pdev);
  266. return i915_suspend(dev, state);
  267. }
  268. static int
  269. i915_pci_resume(struct pci_dev *pdev)
  270. {
  271. struct drm_device *dev = pci_get_drvdata(pdev);
  272. return i915_resume(dev);
  273. }
  274. static int
  275. i915_pm_suspend(struct device *dev)
  276. {
  277. return i915_pci_suspend(to_pci_dev(dev), PMSG_SUSPEND);
  278. }
  279. static int
  280. i915_pm_resume(struct device *dev)
  281. {
  282. return i915_pci_resume(to_pci_dev(dev));
  283. }
  284. static int
  285. i915_pm_freeze(struct device *dev)
  286. {
  287. return i915_pci_suspend(to_pci_dev(dev), PMSG_FREEZE);
  288. }
  289. static int
  290. i915_pm_thaw(struct device *dev)
  291. {
  292. /* thaw during hibernate, do nothing! */
  293. return 0;
  294. }
  295. static int
  296. i915_pm_poweroff(struct device *dev)
  297. {
  298. return i915_pci_suspend(to_pci_dev(dev), PMSG_HIBERNATE);
  299. }
  300. static int
  301. i915_pm_restore(struct device *dev)
  302. {
  303. return i915_pci_resume(to_pci_dev(dev));
  304. }
  305. const struct dev_pm_ops i915_pm_ops = {
  306. .suspend = i915_pm_suspend,
  307. .resume = i915_pm_resume,
  308. .freeze = i915_pm_freeze,
  309. .thaw = i915_pm_thaw,
  310. .poweroff = i915_pm_poweroff,
  311. .restore = i915_pm_restore,
  312. };
  313. static struct vm_operations_struct i915_gem_vm_ops = {
  314. .fault = i915_gem_fault,
  315. .open = drm_gem_vm_open,
  316. .close = drm_gem_vm_close,
  317. };
  318. static struct drm_driver driver = {
  319. /* don't use mtrr's here, the Xserver or user space app should
  320. * deal with them for intel hardware.
  321. */
  322. .driver_features =
  323. DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | /* DRIVER_USE_MTRR |*/
  324. DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM,
  325. .load = i915_driver_load,
  326. .unload = i915_driver_unload,
  327. .open = i915_driver_open,
  328. .lastclose = i915_driver_lastclose,
  329. .preclose = i915_driver_preclose,
  330. .postclose = i915_driver_postclose,
  331. .device_is_agp = i915_driver_device_is_agp,
  332. .enable_vblank = i915_enable_vblank,
  333. .disable_vblank = i915_disable_vblank,
  334. .irq_preinstall = i915_driver_irq_preinstall,
  335. .irq_postinstall = i915_driver_irq_postinstall,
  336. .irq_uninstall = i915_driver_irq_uninstall,
  337. .irq_handler = i915_driver_irq_handler,
  338. .reclaim_buffers = drm_core_reclaim_buffers,
  339. .get_map_ofs = drm_core_get_map_ofs,
  340. .get_reg_ofs = drm_core_get_reg_ofs,
  341. .master_create = i915_master_create,
  342. .master_destroy = i915_master_destroy,
  343. #if defined(CONFIG_DEBUG_FS)
  344. .debugfs_init = i915_debugfs_init,
  345. .debugfs_cleanup = i915_debugfs_cleanup,
  346. #endif
  347. .gem_init_object = i915_gem_init_object,
  348. .gem_free_object = i915_gem_free_object,
  349. .gem_vm_ops = &i915_gem_vm_ops,
  350. .ioctls = i915_ioctls,
  351. .fops = {
  352. .owner = THIS_MODULE,
  353. .open = drm_open,
  354. .release = drm_release,
  355. .ioctl = drm_ioctl,
  356. .mmap = drm_gem_mmap,
  357. .poll = drm_poll,
  358. .fasync = drm_fasync,
  359. .read = drm_read,
  360. #ifdef CONFIG_COMPAT
  361. .compat_ioctl = i915_compat_ioctl,
  362. #endif
  363. },
  364. .pci_driver = {
  365. .name = DRIVER_NAME,
  366. .id_table = pciidlist,
  367. .probe = i915_pci_probe,
  368. .remove = i915_pci_remove,
  369. .driver.pm = &i915_pm_ops,
  370. },
  371. .name = DRIVER_NAME,
  372. .desc = DRIVER_DESC,
  373. .date = DRIVER_DATE,
  374. .major = DRIVER_MAJOR,
  375. .minor = DRIVER_MINOR,
  376. .patchlevel = DRIVER_PATCHLEVEL,
  377. };
  378. static int __init i915_init(void)
  379. {
  380. driver.num_ioctls = i915_max_ioctl;
  381. i915_gem_shrinker_init();
  382. /*
  383. * If CONFIG_DRM_I915_KMS is set, default to KMS unless
  384. * explicitly disabled with the module pararmeter.
  385. *
  386. * Otherwise, just follow the parameter (defaulting to off).
  387. *
  388. * Allow optional vga_text_mode_force boot option to override
  389. * the default behavior.
  390. */
  391. #if defined(CONFIG_DRM_I915_KMS)
  392. if (i915_modeset != 0)
  393. driver.driver_features |= DRIVER_MODESET;
  394. #endif
  395. if (i915_modeset == 1)
  396. driver.driver_features |= DRIVER_MODESET;
  397. #ifdef CONFIG_VGA_CONSOLE
  398. if (vgacon_text_force() && i915_modeset == -1)
  399. driver.driver_features &= ~DRIVER_MODESET;
  400. #endif
  401. return drm_init(&driver);
  402. }
  403. static void __exit i915_exit(void)
  404. {
  405. i915_gem_shrinker_exit();
  406. drm_exit(&driver);
  407. }
  408. module_init(i915_init);
  409. module_exit(i915_exit);
  410. MODULE_AUTHOR(DRIVER_AUTHOR);
  411. MODULE_DESCRIPTION(DRIVER_DESC);
  412. MODULE_LICENSE("GPL and additional rights");