i915_drv.c 27 KB

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