i915_drv.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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 "intel_drv.h"
  35. #include <linux/console.h>
  36. #include <linux/module.h>
  37. #include "drm_crtc_helper.h"
  38. static int i915_modeset __read_mostly = -1;
  39. module_param_named(modeset, i915_modeset, int, 0400);
  40. MODULE_PARM_DESC(modeset,
  41. "Use kernel modesetting [KMS] (0=DRM_I915_KMS from .config, "
  42. "1=on, -1=force vga console preference [default])");
  43. unsigned int i915_fbpercrtc __always_unused = 0;
  44. module_param_named(fbpercrtc, i915_fbpercrtc, int, 0400);
  45. int i915_panel_ignore_lid __read_mostly = 0;
  46. module_param_named(panel_ignore_lid, i915_panel_ignore_lid, int, 0600);
  47. MODULE_PARM_DESC(panel_ignore_lid,
  48. "Override lid status (0=autodetect [default], 1=lid open, "
  49. "-1=lid closed)");
  50. unsigned int i915_powersave __read_mostly = 1;
  51. module_param_named(powersave, i915_powersave, int, 0600);
  52. MODULE_PARM_DESC(powersave,
  53. "Enable powersavings, fbc, downclocking, etc. (default: true)");
  54. int i915_semaphores __read_mostly = -1;
  55. module_param_named(semaphores, i915_semaphores, int, 0600);
  56. MODULE_PARM_DESC(semaphores,
  57. "Use semaphores for inter-ring sync (default: -1 (use per-chip defaults))");
  58. int i915_enable_rc6 __read_mostly = -1;
  59. module_param_named(i915_enable_rc6, i915_enable_rc6, int, 0600);
  60. MODULE_PARM_DESC(i915_enable_rc6,
  61. "Enable power-saving render C-state 6 (default: -1 (use per-chip default)");
  62. int i915_enable_fbc __read_mostly = -1;
  63. module_param_named(i915_enable_fbc, i915_enable_fbc, int, 0600);
  64. MODULE_PARM_DESC(i915_enable_fbc,
  65. "Enable frame buffer compression for power savings "
  66. "(default: -1 (use per-chip default))");
  67. unsigned int i915_lvds_downclock __read_mostly = 0;
  68. module_param_named(lvds_downclock, i915_lvds_downclock, int, 0400);
  69. MODULE_PARM_DESC(lvds_downclock,
  70. "Use panel (LVDS/eDP) downclocking for power savings "
  71. "(default: false)");
  72. int i915_panel_use_ssc __read_mostly = -1;
  73. module_param_named(lvds_use_ssc, i915_panel_use_ssc, int, 0600);
  74. MODULE_PARM_DESC(lvds_use_ssc,
  75. "Use Spread Spectrum Clock with panels [LVDS/eDP] "
  76. "(default: auto from VBT)");
  77. int i915_vbt_sdvo_panel_type __read_mostly = -1;
  78. module_param_named(vbt_sdvo_panel_type, i915_vbt_sdvo_panel_type, int, 0600);
  79. MODULE_PARM_DESC(vbt_sdvo_panel_type,
  80. "Override selection of SDVO panel mode in the VBT "
  81. "(default: auto)");
  82. static bool i915_try_reset __read_mostly = true;
  83. module_param_named(reset, i915_try_reset, bool, 0600);
  84. MODULE_PARM_DESC(reset, "Attempt GPU resets (default: true)");
  85. bool i915_enable_hangcheck __read_mostly = true;
  86. module_param_named(enable_hangcheck, i915_enable_hangcheck, bool, 0644);
  87. MODULE_PARM_DESC(enable_hangcheck,
  88. "Periodically check GPU activity for detecting hangs. "
  89. "WARNING: Disabling this can cause system wide hangs. "
  90. "(default: true)");
  91. static struct drm_driver driver;
  92. extern int intel_agp_enabled;
  93. #define INTEL_VGA_DEVICE(id, info) { \
  94. .class = PCI_BASE_CLASS_DISPLAY << 16, \
  95. .class_mask = 0xff0000, \
  96. .vendor = 0x8086, \
  97. .device = id, \
  98. .subvendor = PCI_ANY_ID, \
  99. .subdevice = PCI_ANY_ID, \
  100. .driver_data = (unsigned long) info }
  101. static const struct intel_device_info intel_i830_info = {
  102. .gen = 2, .is_mobile = 1, .cursor_needs_physical = 1,
  103. .has_overlay = 1, .overlay_needs_physical = 1,
  104. };
  105. static const struct intel_device_info intel_845g_info = {
  106. .gen = 2,
  107. .has_overlay = 1, .overlay_needs_physical = 1,
  108. };
  109. static const struct intel_device_info intel_i85x_info = {
  110. .gen = 2, .is_i85x = 1, .is_mobile = 1,
  111. .cursor_needs_physical = 1,
  112. .has_overlay = 1, .overlay_needs_physical = 1,
  113. };
  114. static const struct intel_device_info intel_i865g_info = {
  115. .gen = 2,
  116. .has_overlay = 1, .overlay_needs_physical = 1,
  117. };
  118. static const struct intel_device_info intel_i915g_info = {
  119. .gen = 3, .is_i915g = 1, .cursor_needs_physical = 1,
  120. .has_overlay = 1, .overlay_needs_physical = 1,
  121. };
  122. static const struct intel_device_info intel_i915gm_info = {
  123. .gen = 3, .is_mobile = 1,
  124. .cursor_needs_physical = 1,
  125. .has_overlay = 1, .overlay_needs_physical = 1,
  126. .supports_tv = 1,
  127. };
  128. static const struct intel_device_info intel_i945g_info = {
  129. .gen = 3, .has_hotplug = 1, .cursor_needs_physical = 1,
  130. .has_overlay = 1, .overlay_needs_physical = 1,
  131. };
  132. static const struct intel_device_info intel_i945gm_info = {
  133. .gen = 3, .is_i945gm = 1, .is_mobile = 1,
  134. .has_hotplug = 1, .cursor_needs_physical = 1,
  135. .has_overlay = 1, .overlay_needs_physical = 1,
  136. .supports_tv = 1,
  137. };
  138. static const struct intel_device_info intel_i965g_info = {
  139. .gen = 4, .is_broadwater = 1,
  140. .has_hotplug = 1,
  141. .has_overlay = 1,
  142. };
  143. static const struct intel_device_info intel_i965gm_info = {
  144. .gen = 4, .is_crestline = 1,
  145. .is_mobile = 1, .has_fbc = 1, .has_hotplug = 1,
  146. .has_overlay = 1,
  147. .supports_tv = 1,
  148. };
  149. static const struct intel_device_info intel_g33_info = {
  150. .gen = 3, .is_g33 = 1,
  151. .need_gfx_hws = 1, .has_hotplug = 1,
  152. .has_overlay = 1,
  153. };
  154. static const struct intel_device_info intel_g45_info = {
  155. .gen = 4, .is_g4x = 1, .need_gfx_hws = 1,
  156. .has_pipe_cxsr = 1, .has_hotplug = 1,
  157. .has_bsd_ring = 1,
  158. };
  159. static const struct intel_device_info intel_gm45_info = {
  160. .gen = 4, .is_g4x = 1,
  161. .is_mobile = 1, .need_gfx_hws = 1, .has_fbc = 1,
  162. .has_pipe_cxsr = 1, .has_hotplug = 1,
  163. .supports_tv = 1,
  164. .has_bsd_ring = 1,
  165. };
  166. static const struct intel_device_info intel_pineview_info = {
  167. .gen = 3, .is_g33 = 1, .is_pineview = 1, .is_mobile = 1,
  168. .need_gfx_hws = 1, .has_hotplug = 1,
  169. .has_overlay = 1,
  170. };
  171. static const struct intel_device_info intel_ironlake_d_info = {
  172. .gen = 5,
  173. .need_gfx_hws = 1, .has_pipe_cxsr = 1, .has_hotplug = 1,
  174. .has_bsd_ring = 1,
  175. };
  176. static const struct intel_device_info intel_ironlake_m_info = {
  177. .gen = 5, .is_mobile = 1,
  178. .need_gfx_hws = 1, .has_hotplug = 1,
  179. .has_fbc = 1,
  180. .has_bsd_ring = 1,
  181. };
  182. static const struct intel_device_info intel_sandybridge_d_info = {
  183. .gen = 6,
  184. .need_gfx_hws = 1, .has_hotplug = 1,
  185. .has_bsd_ring = 1,
  186. .has_blt_ring = 1,
  187. };
  188. static const struct intel_device_info intel_sandybridge_m_info = {
  189. .gen = 6, .is_mobile = 1,
  190. .need_gfx_hws = 1, .has_hotplug = 1,
  191. .has_fbc = 1,
  192. .has_bsd_ring = 1,
  193. .has_blt_ring = 1,
  194. };
  195. static const struct intel_device_info intel_ivybridge_d_info = {
  196. .is_ivybridge = 1, .gen = 7,
  197. .need_gfx_hws = 1, .has_hotplug = 1,
  198. .has_bsd_ring = 1,
  199. .has_blt_ring = 1,
  200. };
  201. static const struct intel_device_info intel_ivybridge_m_info = {
  202. .is_ivybridge = 1, .gen = 7, .is_mobile = 1,
  203. .need_gfx_hws = 1, .has_hotplug = 1,
  204. .has_fbc = 0, /* FBC is not enabled on Ivybridge mobile yet */
  205. .has_bsd_ring = 1,
  206. .has_blt_ring = 1,
  207. };
  208. static const struct pci_device_id pciidlist[] = { /* aka */
  209. INTEL_VGA_DEVICE(0x3577, &intel_i830_info), /* I830_M */
  210. INTEL_VGA_DEVICE(0x2562, &intel_845g_info), /* 845_G */
  211. INTEL_VGA_DEVICE(0x3582, &intel_i85x_info), /* I855_GM */
  212. INTEL_VGA_DEVICE(0x358e, &intel_i85x_info),
  213. INTEL_VGA_DEVICE(0x2572, &intel_i865g_info), /* I865_G */
  214. INTEL_VGA_DEVICE(0x2582, &intel_i915g_info), /* I915_G */
  215. INTEL_VGA_DEVICE(0x258a, &intel_i915g_info), /* E7221_G */
  216. INTEL_VGA_DEVICE(0x2592, &intel_i915gm_info), /* I915_GM */
  217. INTEL_VGA_DEVICE(0x2772, &intel_i945g_info), /* I945_G */
  218. INTEL_VGA_DEVICE(0x27a2, &intel_i945gm_info), /* I945_GM */
  219. INTEL_VGA_DEVICE(0x27ae, &intel_i945gm_info), /* I945_GME */
  220. INTEL_VGA_DEVICE(0x2972, &intel_i965g_info), /* I946_GZ */
  221. INTEL_VGA_DEVICE(0x2982, &intel_i965g_info), /* G35_G */
  222. INTEL_VGA_DEVICE(0x2992, &intel_i965g_info), /* I965_Q */
  223. INTEL_VGA_DEVICE(0x29a2, &intel_i965g_info), /* I965_G */
  224. INTEL_VGA_DEVICE(0x29b2, &intel_g33_info), /* Q35_G */
  225. INTEL_VGA_DEVICE(0x29c2, &intel_g33_info), /* G33_G */
  226. INTEL_VGA_DEVICE(0x29d2, &intel_g33_info), /* Q33_G */
  227. INTEL_VGA_DEVICE(0x2a02, &intel_i965gm_info), /* I965_GM */
  228. INTEL_VGA_DEVICE(0x2a12, &intel_i965gm_info), /* I965_GME */
  229. INTEL_VGA_DEVICE(0x2a42, &intel_gm45_info), /* GM45_G */
  230. INTEL_VGA_DEVICE(0x2e02, &intel_g45_info), /* IGD_E_G */
  231. INTEL_VGA_DEVICE(0x2e12, &intel_g45_info), /* Q45_G */
  232. INTEL_VGA_DEVICE(0x2e22, &intel_g45_info), /* G45_G */
  233. INTEL_VGA_DEVICE(0x2e32, &intel_g45_info), /* G41_G */
  234. INTEL_VGA_DEVICE(0x2e42, &intel_g45_info), /* B43_G */
  235. INTEL_VGA_DEVICE(0x2e92, &intel_g45_info), /* B43_G.1 */
  236. INTEL_VGA_DEVICE(0xa001, &intel_pineview_info),
  237. INTEL_VGA_DEVICE(0xa011, &intel_pineview_info),
  238. INTEL_VGA_DEVICE(0x0042, &intel_ironlake_d_info),
  239. INTEL_VGA_DEVICE(0x0046, &intel_ironlake_m_info),
  240. INTEL_VGA_DEVICE(0x0102, &intel_sandybridge_d_info),
  241. INTEL_VGA_DEVICE(0x0112, &intel_sandybridge_d_info),
  242. INTEL_VGA_DEVICE(0x0122, &intel_sandybridge_d_info),
  243. INTEL_VGA_DEVICE(0x0106, &intel_sandybridge_m_info),
  244. INTEL_VGA_DEVICE(0x0116, &intel_sandybridge_m_info),
  245. INTEL_VGA_DEVICE(0x0126, &intel_sandybridge_m_info),
  246. INTEL_VGA_DEVICE(0x010A, &intel_sandybridge_d_info),
  247. INTEL_VGA_DEVICE(0x0156, &intel_ivybridge_m_info), /* GT1 mobile */
  248. INTEL_VGA_DEVICE(0x0166, &intel_ivybridge_m_info), /* GT2 mobile */
  249. INTEL_VGA_DEVICE(0x0152, &intel_ivybridge_d_info), /* GT1 desktop */
  250. INTEL_VGA_DEVICE(0x0162, &intel_ivybridge_d_info), /* GT2 desktop */
  251. INTEL_VGA_DEVICE(0x015a, &intel_ivybridge_d_info), /* GT1 server */
  252. {0, 0, 0}
  253. };
  254. #if defined(CONFIG_DRM_I915_KMS)
  255. MODULE_DEVICE_TABLE(pci, pciidlist);
  256. #endif
  257. #define INTEL_PCH_DEVICE_ID_MASK 0xff00
  258. #define INTEL_PCH_IBX_DEVICE_ID_TYPE 0x3b00
  259. #define INTEL_PCH_CPT_DEVICE_ID_TYPE 0x1c00
  260. #define INTEL_PCH_PPT_DEVICE_ID_TYPE 0x1e00
  261. void intel_detect_pch(struct drm_device *dev)
  262. {
  263. struct drm_i915_private *dev_priv = dev->dev_private;
  264. struct pci_dev *pch;
  265. /*
  266. * The reason to probe ISA bridge instead of Dev31:Fun0 is to
  267. * make graphics device passthrough work easy for VMM, that only
  268. * need to expose ISA bridge to let driver know the real hardware
  269. * underneath. This is a requirement from virtualization team.
  270. */
  271. pch = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  272. if (pch) {
  273. if (pch->vendor == PCI_VENDOR_ID_INTEL) {
  274. int id;
  275. id = pch->device & INTEL_PCH_DEVICE_ID_MASK;
  276. if (id == INTEL_PCH_IBX_DEVICE_ID_TYPE) {
  277. dev_priv->pch_type = PCH_IBX;
  278. DRM_DEBUG_KMS("Found Ibex Peak PCH\n");
  279. } else if (id == INTEL_PCH_CPT_DEVICE_ID_TYPE) {
  280. dev_priv->pch_type = PCH_CPT;
  281. DRM_DEBUG_KMS("Found CougarPoint PCH\n");
  282. } else if (id == INTEL_PCH_PPT_DEVICE_ID_TYPE) {
  283. /* PantherPoint is CPT compatible */
  284. dev_priv->pch_type = PCH_CPT;
  285. DRM_DEBUG_KMS("Found PatherPoint PCH\n");
  286. }
  287. }
  288. pci_dev_put(pch);
  289. }
  290. }
  291. void __gen6_gt_force_wake_get(struct drm_i915_private *dev_priv)
  292. {
  293. int count;
  294. count = 0;
  295. while (count++ < 50 && (I915_READ_NOTRACE(FORCEWAKE_ACK) & 1))
  296. udelay(10);
  297. I915_WRITE_NOTRACE(FORCEWAKE, 1);
  298. POSTING_READ(FORCEWAKE);
  299. count = 0;
  300. while (count++ < 50 && (I915_READ_NOTRACE(FORCEWAKE_ACK) & 1) == 0)
  301. udelay(10);
  302. }
  303. void __gen6_gt_force_wake_mt_get(struct drm_i915_private *dev_priv)
  304. {
  305. int count;
  306. count = 0;
  307. while (count++ < 50 && (I915_READ_NOTRACE(FORCEWAKE_MT_ACK) & 1))
  308. udelay(10);
  309. I915_WRITE_NOTRACE(FORCEWAKE_MT, (1<<16) | 1);
  310. POSTING_READ(FORCEWAKE_MT);
  311. count = 0;
  312. while (count++ < 50 && (I915_READ_NOTRACE(FORCEWAKE_MT_ACK) & 1) == 0)
  313. udelay(10);
  314. }
  315. /*
  316. * Generally this is called implicitly by the register read function. However,
  317. * if some sequence requires the GT to not power down then this function should
  318. * be called at the beginning of the sequence followed by a call to
  319. * gen6_gt_force_wake_put() at the end of the sequence.
  320. */
  321. void gen6_gt_force_wake_get(struct drm_i915_private *dev_priv)
  322. {
  323. WARN_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  324. /* Forcewake is atomic in case we get in here without the lock */
  325. if (atomic_add_return(1, &dev_priv->forcewake_count) == 1)
  326. dev_priv->display.force_wake_get(dev_priv);
  327. }
  328. void __gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
  329. {
  330. I915_WRITE_NOTRACE(FORCEWAKE, 0);
  331. POSTING_READ(FORCEWAKE);
  332. }
  333. void __gen6_gt_force_wake_mt_put(struct drm_i915_private *dev_priv)
  334. {
  335. I915_WRITE_NOTRACE(FORCEWAKE_MT, (1<<16) | 0);
  336. POSTING_READ(FORCEWAKE_MT);
  337. }
  338. /*
  339. * see gen6_gt_force_wake_get()
  340. */
  341. void gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
  342. {
  343. WARN_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  344. if (atomic_dec_and_test(&dev_priv->forcewake_count))
  345. dev_priv->display.force_wake_put(dev_priv);
  346. }
  347. void __gen6_gt_wait_for_fifo(struct drm_i915_private *dev_priv)
  348. {
  349. if (dev_priv->gt_fifo_count < GT_FIFO_NUM_RESERVED_ENTRIES) {
  350. int loop = 500;
  351. u32 fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
  352. while (fifo <= GT_FIFO_NUM_RESERVED_ENTRIES && loop--) {
  353. udelay(10);
  354. fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
  355. }
  356. WARN_ON(loop < 0 && fifo <= GT_FIFO_NUM_RESERVED_ENTRIES);
  357. dev_priv->gt_fifo_count = fifo;
  358. }
  359. dev_priv->gt_fifo_count--;
  360. }
  361. static int i915_drm_freeze(struct drm_device *dev)
  362. {
  363. struct drm_i915_private *dev_priv = dev->dev_private;
  364. drm_kms_helper_poll_disable(dev);
  365. pci_save_state(dev->pdev);
  366. /* If KMS is active, we do the leavevt stuff here */
  367. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  368. int error = i915_gem_idle(dev);
  369. if (error) {
  370. dev_err(&dev->pdev->dev,
  371. "GEM idle failed, resume might fail\n");
  372. return error;
  373. }
  374. drm_irq_uninstall(dev);
  375. }
  376. i915_save_state(dev);
  377. intel_opregion_fini(dev);
  378. /* Modeset on resume, not lid events */
  379. dev_priv->modeset_on_lid = 0;
  380. return 0;
  381. }
  382. int i915_suspend(struct drm_device *dev, pm_message_t state)
  383. {
  384. int error;
  385. if (!dev || !dev->dev_private) {
  386. DRM_ERROR("dev: %p\n", dev);
  387. DRM_ERROR("DRM not initialized, aborting suspend.\n");
  388. return -ENODEV;
  389. }
  390. if (state.event == PM_EVENT_PRETHAW)
  391. return 0;
  392. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  393. return 0;
  394. error = i915_drm_freeze(dev);
  395. if (error)
  396. return error;
  397. if (state.event == PM_EVENT_SUSPEND) {
  398. /* Shut down the device */
  399. pci_disable_device(dev->pdev);
  400. pci_set_power_state(dev->pdev, PCI_D3hot);
  401. }
  402. return 0;
  403. }
  404. static int i915_drm_thaw(struct drm_device *dev)
  405. {
  406. struct drm_i915_private *dev_priv = dev->dev_private;
  407. int error = 0;
  408. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  409. mutex_lock(&dev->struct_mutex);
  410. i915_gem_restore_gtt_mappings(dev);
  411. mutex_unlock(&dev->struct_mutex);
  412. }
  413. i915_restore_state(dev);
  414. intel_opregion_setup(dev);
  415. /* KMS EnterVT equivalent */
  416. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  417. mutex_lock(&dev->struct_mutex);
  418. dev_priv->mm.suspended = 0;
  419. error = i915_gem_init_ringbuffer(dev);
  420. mutex_unlock(&dev->struct_mutex);
  421. if (HAS_PCH_SPLIT(dev))
  422. ironlake_init_pch_refclk(dev);
  423. drm_mode_config_reset(dev);
  424. drm_irq_install(dev);
  425. /* Resume the modeset for every activated CRTC */
  426. drm_helper_resume_force_mode(dev);
  427. if (IS_IRONLAKE_M(dev))
  428. ironlake_enable_rc6(dev);
  429. }
  430. intel_opregion_init(dev);
  431. dev_priv->modeset_on_lid = 0;
  432. return error;
  433. }
  434. int i915_resume(struct drm_device *dev)
  435. {
  436. int ret;
  437. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  438. return 0;
  439. if (pci_enable_device(dev->pdev))
  440. return -EIO;
  441. pci_set_master(dev->pdev);
  442. ret = i915_drm_thaw(dev);
  443. if (ret)
  444. return ret;
  445. drm_kms_helper_poll_enable(dev);
  446. return 0;
  447. }
  448. static int i8xx_do_reset(struct drm_device *dev, u8 flags)
  449. {
  450. struct drm_i915_private *dev_priv = dev->dev_private;
  451. if (IS_I85X(dev))
  452. return -ENODEV;
  453. I915_WRITE(D_STATE, I915_READ(D_STATE) | DSTATE_GFX_RESET_I830);
  454. POSTING_READ(D_STATE);
  455. if (IS_I830(dev) || IS_845G(dev)) {
  456. I915_WRITE(DEBUG_RESET_I830,
  457. DEBUG_RESET_DISPLAY |
  458. DEBUG_RESET_RENDER |
  459. DEBUG_RESET_FULL);
  460. POSTING_READ(DEBUG_RESET_I830);
  461. msleep(1);
  462. I915_WRITE(DEBUG_RESET_I830, 0);
  463. POSTING_READ(DEBUG_RESET_I830);
  464. }
  465. msleep(1);
  466. I915_WRITE(D_STATE, I915_READ(D_STATE) & ~DSTATE_GFX_RESET_I830);
  467. POSTING_READ(D_STATE);
  468. return 0;
  469. }
  470. static int i965_reset_complete(struct drm_device *dev)
  471. {
  472. u8 gdrst;
  473. pci_read_config_byte(dev->pdev, I965_GDRST, &gdrst);
  474. return gdrst & 0x1;
  475. }
  476. static int i965_do_reset(struct drm_device *dev, u8 flags)
  477. {
  478. u8 gdrst;
  479. /*
  480. * Set the domains we want to reset (GRDOM/bits 2 and 3) as
  481. * well as the reset bit (GR/bit 0). Setting the GR bit
  482. * triggers the reset; when done, the hardware will clear it.
  483. */
  484. pci_read_config_byte(dev->pdev, I965_GDRST, &gdrst);
  485. pci_write_config_byte(dev->pdev, I965_GDRST, gdrst | flags | 0x1);
  486. return wait_for(i965_reset_complete(dev), 500);
  487. }
  488. static int ironlake_do_reset(struct drm_device *dev, u8 flags)
  489. {
  490. struct drm_i915_private *dev_priv = dev->dev_private;
  491. u32 gdrst = I915_READ(MCHBAR_MIRROR_BASE + ILK_GDSR);
  492. I915_WRITE(MCHBAR_MIRROR_BASE + ILK_GDSR, gdrst | flags | 0x1);
  493. return wait_for(I915_READ(MCHBAR_MIRROR_BASE + ILK_GDSR) & 0x1, 500);
  494. }
  495. static int gen6_do_reset(struct drm_device *dev, u8 flags)
  496. {
  497. struct drm_i915_private *dev_priv = dev->dev_private;
  498. I915_WRITE(GEN6_GDRST, GEN6_GRDOM_FULL);
  499. return wait_for((I915_READ(GEN6_GDRST) & GEN6_GRDOM_FULL) == 0, 500);
  500. }
  501. /**
  502. * i965_reset - reset chip after a hang
  503. * @dev: drm device to reset
  504. * @flags: reset domains
  505. *
  506. * Reset the chip. Useful if a hang is detected. Returns zero on successful
  507. * reset or otherwise an error code.
  508. *
  509. * Procedure is fairly simple:
  510. * - reset the chip using the reset reg
  511. * - re-init context state
  512. * - re-init hardware status page
  513. * - re-init ring buffer
  514. * - re-init interrupt state
  515. * - re-init display
  516. */
  517. int i915_reset(struct drm_device *dev, u8 flags)
  518. {
  519. drm_i915_private_t *dev_priv = dev->dev_private;
  520. /*
  521. * We really should only reset the display subsystem if we actually
  522. * need to
  523. */
  524. bool need_display = true;
  525. int ret;
  526. if (!i915_try_reset)
  527. return 0;
  528. if (!mutex_trylock(&dev->struct_mutex))
  529. return -EBUSY;
  530. i915_gem_reset(dev);
  531. ret = -ENODEV;
  532. if (get_seconds() - dev_priv->last_gpu_reset < 5) {
  533. DRM_ERROR("GPU hanging too fast, declaring wedged!\n");
  534. } else switch (INTEL_INFO(dev)->gen) {
  535. case 7:
  536. case 6:
  537. ret = gen6_do_reset(dev, flags);
  538. /* If reset with a user forcewake, try to restore */
  539. if (atomic_read(&dev_priv->forcewake_count))
  540. __gen6_gt_force_wake_get(dev_priv);
  541. break;
  542. case 5:
  543. ret = ironlake_do_reset(dev, flags);
  544. break;
  545. case 4:
  546. ret = i965_do_reset(dev, flags);
  547. break;
  548. case 2:
  549. ret = i8xx_do_reset(dev, flags);
  550. break;
  551. }
  552. dev_priv->last_gpu_reset = get_seconds();
  553. if (ret) {
  554. DRM_ERROR("Failed to reset chip.\n");
  555. mutex_unlock(&dev->struct_mutex);
  556. return ret;
  557. }
  558. /* Ok, now get things going again... */
  559. /*
  560. * Everything depends on having the GTT running, so we need to start
  561. * there. Fortunately we don't need to do this unless we reset the
  562. * chip at a PCI level.
  563. *
  564. * Next we need to restore the context, but we don't use those
  565. * yet either...
  566. *
  567. * Ring buffer needs to be re-initialized in the KMS case, or if X
  568. * was running at the time of the reset (i.e. we weren't VT
  569. * switched away).
  570. */
  571. if (drm_core_check_feature(dev, DRIVER_MODESET) ||
  572. !dev_priv->mm.suspended) {
  573. dev_priv->mm.suspended = 0;
  574. dev_priv->ring[RCS].init(&dev_priv->ring[RCS]);
  575. if (HAS_BSD(dev))
  576. dev_priv->ring[VCS].init(&dev_priv->ring[VCS]);
  577. if (HAS_BLT(dev))
  578. dev_priv->ring[BCS].init(&dev_priv->ring[BCS]);
  579. mutex_unlock(&dev->struct_mutex);
  580. drm_irq_uninstall(dev);
  581. drm_mode_config_reset(dev);
  582. drm_irq_install(dev);
  583. mutex_lock(&dev->struct_mutex);
  584. }
  585. mutex_unlock(&dev->struct_mutex);
  586. /*
  587. * Perform a full modeset as on later generations, e.g. Ironlake, we may
  588. * need to retrain the display link and cannot just restore the register
  589. * values.
  590. */
  591. if (need_display) {
  592. mutex_lock(&dev->mode_config.mutex);
  593. drm_helper_resume_force_mode(dev);
  594. mutex_unlock(&dev->mode_config.mutex);
  595. }
  596. return 0;
  597. }
  598. static int __devinit
  599. i915_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  600. {
  601. /* Only bind to function 0 of the device. Early generations
  602. * used function 1 as a placeholder for multi-head. This causes
  603. * us confusion instead, especially on the systems where both
  604. * functions have the same PCI-ID!
  605. */
  606. if (PCI_FUNC(pdev->devfn))
  607. return -ENODEV;
  608. return drm_get_pci_dev(pdev, ent, &driver);
  609. }
  610. static void
  611. i915_pci_remove(struct pci_dev *pdev)
  612. {
  613. struct drm_device *dev = pci_get_drvdata(pdev);
  614. drm_put_dev(dev);
  615. }
  616. static int i915_pm_suspend(struct device *dev)
  617. {
  618. struct pci_dev *pdev = to_pci_dev(dev);
  619. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  620. int error;
  621. if (!drm_dev || !drm_dev->dev_private) {
  622. dev_err(dev, "DRM not initialized, aborting suspend.\n");
  623. return -ENODEV;
  624. }
  625. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  626. return 0;
  627. error = i915_drm_freeze(drm_dev);
  628. if (error)
  629. return error;
  630. pci_disable_device(pdev);
  631. pci_set_power_state(pdev, PCI_D3hot);
  632. return 0;
  633. }
  634. static int i915_pm_resume(struct device *dev)
  635. {
  636. struct pci_dev *pdev = to_pci_dev(dev);
  637. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  638. return i915_resume(drm_dev);
  639. }
  640. static int i915_pm_freeze(struct device *dev)
  641. {
  642. struct pci_dev *pdev = to_pci_dev(dev);
  643. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  644. if (!drm_dev || !drm_dev->dev_private) {
  645. dev_err(dev, "DRM not initialized, aborting suspend.\n");
  646. return -ENODEV;
  647. }
  648. return i915_drm_freeze(drm_dev);
  649. }
  650. static int i915_pm_thaw(struct device *dev)
  651. {
  652. struct pci_dev *pdev = to_pci_dev(dev);
  653. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  654. return i915_drm_thaw(drm_dev);
  655. }
  656. static int i915_pm_poweroff(struct device *dev)
  657. {
  658. struct pci_dev *pdev = to_pci_dev(dev);
  659. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  660. return i915_drm_freeze(drm_dev);
  661. }
  662. static const struct dev_pm_ops i915_pm_ops = {
  663. .suspend = i915_pm_suspend,
  664. .resume = i915_pm_resume,
  665. .freeze = i915_pm_freeze,
  666. .thaw = i915_pm_thaw,
  667. .poweroff = i915_pm_poweroff,
  668. .restore = i915_pm_resume,
  669. };
  670. static struct vm_operations_struct i915_gem_vm_ops = {
  671. .fault = i915_gem_fault,
  672. .open = drm_gem_vm_open,
  673. .close = drm_gem_vm_close,
  674. };
  675. static const struct file_operations i915_driver_fops = {
  676. .owner = THIS_MODULE,
  677. .open = drm_open,
  678. .release = drm_release,
  679. .unlocked_ioctl = drm_ioctl,
  680. .mmap = drm_gem_mmap,
  681. .poll = drm_poll,
  682. .fasync = drm_fasync,
  683. .read = drm_read,
  684. #ifdef CONFIG_COMPAT
  685. .compat_ioctl = i915_compat_ioctl,
  686. #endif
  687. .llseek = noop_llseek,
  688. };
  689. static struct drm_driver driver = {
  690. /* Don't use MTRRs here; the Xserver or userspace app should
  691. * deal with them for Intel hardware.
  692. */
  693. .driver_features =
  694. DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | /* DRIVER_USE_MTRR |*/
  695. DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM,
  696. .load = i915_driver_load,
  697. .unload = i915_driver_unload,
  698. .open = i915_driver_open,
  699. .lastclose = i915_driver_lastclose,
  700. .preclose = i915_driver_preclose,
  701. .postclose = i915_driver_postclose,
  702. /* Used in place of i915_pm_ops for non-DRIVER_MODESET */
  703. .suspend = i915_suspend,
  704. .resume = i915_resume,
  705. .device_is_agp = i915_driver_device_is_agp,
  706. .reclaim_buffers = drm_core_reclaim_buffers,
  707. .master_create = i915_master_create,
  708. .master_destroy = i915_master_destroy,
  709. #if defined(CONFIG_DEBUG_FS)
  710. .debugfs_init = i915_debugfs_init,
  711. .debugfs_cleanup = i915_debugfs_cleanup,
  712. #endif
  713. .gem_init_object = i915_gem_init_object,
  714. .gem_free_object = i915_gem_free_object,
  715. .gem_vm_ops = &i915_gem_vm_ops,
  716. .dumb_create = i915_gem_dumb_create,
  717. .dumb_map_offset = i915_gem_mmap_gtt,
  718. .dumb_destroy = i915_gem_dumb_destroy,
  719. .ioctls = i915_ioctls,
  720. .fops = &i915_driver_fops,
  721. .name = DRIVER_NAME,
  722. .desc = DRIVER_DESC,
  723. .date = DRIVER_DATE,
  724. .major = DRIVER_MAJOR,
  725. .minor = DRIVER_MINOR,
  726. .patchlevel = DRIVER_PATCHLEVEL,
  727. };
  728. static struct pci_driver i915_pci_driver = {
  729. .name = DRIVER_NAME,
  730. .id_table = pciidlist,
  731. .probe = i915_pci_probe,
  732. .remove = i915_pci_remove,
  733. .driver.pm = &i915_pm_ops,
  734. };
  735. static int __init i915_init(void)
  736. {
  737. if (!intel_agp_enabled) {
  738. DRM_ERROR("drm/i915 can't work without intel_agp module!\n");
  739. return -ENODEV;
  740. }
  741. driver.num_ioctls = i915_max_ioctl;
  742. /*
  743. * If CONFIG_DRM_I915_KMS is set, default to KMS unless
  744. * explicitly disabled with the module pararmeter.
  745. *
  746. * Otherwise, just follow the parameter (defaulting to off).
  747. *
  748. * Allow optional vga_text_mode_force boot option to override
  749. * the default behavior.
  750. */
  751. #if defined(CONFIG_DRM_I915_KMS)
  752. if (i915_modeset != 0)
  753. driver.driver_features |= DRIVER_MODESET;
  754. #endif
  755. if (i915_modeset == 1)
  756. driver.driver_features |= DRIVER_MODESET;
  757. #ifdef CONFIG_VGA_CONSOLE
  758. if (vgacon_text_force() && i915_modeset == -1)
  759. driver.driver_features &= ~DRIVER_MODESET;
  760. #endif
  761. if (!(driver.driver_features & DRIVER_MODESET))
  762. driver.get_vblank_timestamp = NULL;
  763. return drm_pci_init(&driver, &i915_pci_driver);
  764. }
  765. static void __exit i915_exit(void)
  766. {
  767. drm_pci_exit(&driver, &i915_pci_driver);
  768. }
  769. module_init(i915_init);
  770. module_exit(i915_exit);
  771. MODULE_AUTHOR(DRIVER_AUTHOR);
  772. MODULE_DESCRIPTION(DRIVER_DESC);
  773. MODULE_LICENSE("GPL and additional rights");
  774. /* We give fast paths for the really cool registers */
  775. #define NEEDS_FORCE_WAKE(dev_priv, reg) \
  776. (((dev_priv)->info->gen >= 6) && \
  777. ((reg) < 0x40000) && \
  778. ((reg) != FORCEWAKE) && \
  779. ((reg) != ECOBUS))
  780. #define __i915_read(x, y) \
  781. u##x i915_read##x(struct drm_i915_private *dev_priv, u32 reg) { \
  782. u##x val = 0; \
  783. if (NEEDS_FORCE_WAKE((dev_priv), (reg))) { \
  784. gen6_gt_force_wake_get(dev_priv); \
  785. val = read##y(dev_priv->regs + reg); \
  786. gen6_gt_force_wake_put(dev_priv); \
  787. } else { \
  788. val = read##y(dev_priv->regs + reg); \
  789. } \
  790. trace_i915_reg_rw(false, reg, val, sizeof(val)); \
  791. return val; \
  792. }
  793. __i915_read(8, b)
  794. __i915_read(16, w)
  795. __i915_read(32, l)
  796. __i915_read(64, q)
  797. #undef __i915_read
  798. #define __i915_write(x, y) \
  799. void i915_write##x(struct drm_i915_private *dev_priv, u32 reg, u##x val) { \
  800. trace_i915_reg_rw(true, reg, val, sizeof(val)); \
  801. if (NEEDS_FORCE_WAKE((dev_priv), (reg))) { \
  802. __gen6_gt_wait_for_fifo(dev_priv); \
  803. } \
  804. write##y(val, dev_priv->regs + reg); \
  805. }
  806. __i915_write(8, b)
  807. __i915_write(16, w)
  808. __i915_write(32, l)
  809. __i915_write(64, q)
  810. #undef __i915_write