i915_drv.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. unsigned long irqflags;
  332. spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
  333. if (dev_priv->forcewake_count++ == 0)
  334. dev_priv->display.force_wake_get(dev_priv);
  335. spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
  336. }
  337. static void gen6_gt_check_fifodbg(struct drm_i915_private *dev_priv)
  338. {
  339. u32 gtfifodbg;
  340. gtfifodbg = I915_READ_NOTRACE(GTFIFODBG);
  341. if (WARN(gtfifodbg & GT_FIFO_CPU_ERROR_MASK,
  342. "MMIO read or write has been dropped %x\n", gtfifodbg))
  343. I915_WRITE_NOTRACE(GTFIFODBG, GT_FIFO_CPU_ERROR_MASK);
  344. }
  345. void __gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
  346. {
  347. I915_WRITE_NOTRACE(FORCEWAKE, 0);
  348. /* The below doubles as a POSTING_READ */
  349. gen6_gt_check_fifodbg(dev_priv);
  350. }
  351. void __gen6_gt_force_wake_mt_put(struct drm_i915_private *dev_priv)
  352. {
  353. I915_WRITE_NOTRACE(FORCEWAKE_MT, (1<<16) | 0);
  354. /* The below doubles as a POSTING_READ */
  355. gen6_gt_check_fifodbg(dev_priv);
  356. }
  357. /*
  358. * see gen6_gt_force_wake_get()
  359. */
  360. void gen6_gt_force_wake_put(struct drm_i915_private *dev_priv)
  361. {
  362. unsigned long irqflags;
  363. spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
  364. if (--dev_priv->forcewake_count == 0)
  365. dev_priv->display.force_wake_put(dev_priv);
  366. spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
  367. }
  368. int __gen6_gt_wait_for_fifo(struct drm_i915_private *dev_priv)
  369. {
  370. int ret = 0;
  371. if (dev_priv->gt_fifo_count < GT_FIFO_NUM_RESERVED_ENTRIES) {
  372. int loop = 500;
  373. u32 fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
  374. while (fifo <= GT_FIFO_NUM_RESERVED_ENTRIES && loop--) {
  375. udelay(10);
  376. fifo = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
  377. }
  378. if (WARN_ON(loop < 0 && fifo <= GT_FIFO_NUM_RESERVED_ENTRIES))
  379. ++ret;
  380. dev_priv->gt_fifo_count = fifo;
  381. }
  382. dev_priv->gt_fifo_count--;
  383. return ret;
  384. }
  385. static int i915_drm_freeze(struct drm_device *dev)
  386. {
  387. struct drm_i915_private *dev_priv = dev->dev_private;
  388. drm_kms_helper_poll_disable(dev);
  389. pci_save_state(dev->pdev);
  390. /* If KMS is active, we do the leavevt stuff here */
  391. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  392. int error = i915_gem_idle(dev);
  393. if (error) {
  394. dev_err(&dev->pdev->dev,
  395. "GEM idle failed, resume might fail\n");
  396. return error;
  397. }
  398. drm_irq_uninstall(dev);
  399. }
  400. i915_save_state(dev);
  401. intel_opregion_fini(dev);
  402. /* Modeset on resume, not lid events */
  403. dev_priv->modeset_on_lid = 0;
  404. console_lock();
  405. intel_fbdev_set_suspend(dev, 1);
  406. console_unlock();
  407. return 0;
  408. }
  409. int i915_suspend(struct drm_device *dev, pm_message_t state)
  410. {
  411. int error;
  412. if (!dev || !dev->dev_private) {
  413. DRM_ERROR("dev: %p\n", dev);
  414. DRM_ERROR("DRM not initialized, aborting suspend.\n");
  415. return -ENODEV;
  416. }
  417. if (state.event == PM_EVENT_PRETHAW)
  418. return 0;
  419. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  420. return 0;
  421. error = i915_drm_freeze(dev);
  422. if (error)
  423. return error;
  424. if (state.event == PM_EVENT_SUSPEND) {
  425. /* Shut down the device */
  426. pci_disable_device(dev->pdev);
  427. pci_set_power_state(dev->pdev, PCI_D3hot);
  428. }
  429. return 0;
  430. }
  431. static int i915_drm_thaw(struct drm_device *dev)
  432. {
  433. struct drm_i915_private *dev_priv = dev->dev_private;
  434. int error = 0;
  435. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  436. mutex_lock(&dev->struct_mutex);
  437. i915_gem_restore_gtt_mappings(dev);
  438. mutex_unlock(&dev->struct_mutex);
  439. }
  440. i915_restore_state(dev);
  441. intel_opregion_setup(dev);
  442. /* KMS EnterVT equivalent */
  443. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  444. mutex_lock(&dev->struct_mutex);
  445. dev_priv->mm.suspended = 0;
  446. error = i915_gem_init_hw(dev);
  447. mutex_unlock(&dev->struct_mutex);
  448. if (HAS_PCH_SPLIT(dev))
  449. ironlake_init_pch_refclk(dev);
  450. drm_mode_config_reset(dev);
  451. drm_irq_install(dev);
  452. /* Resume the modeset for every activated CRTC */
  453. drm_helper_resume_force_mode(dev);
  454. if (IS_IRONLAKE_M(dev))
  455. ironlake_enable_rc6(dev);
  456. }
  457. intel_opregion_init(dev);
  458. dev_priv->modeset_on_lid = 0;
  459. console_lock();
  460. intel_fbdev_set_suspend(dev, 0);
  461. console_unlock();
  462. return error;
  463. }
  464. int i915_resume(struct drm_device *dev)
  465. {
  466. int ret;
  467. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  468. return 0;
  469. if (pci_enable_device(dev->pdev))
  470. return -EIO;
  471. pci_set_master(dev->pdev);
  472. ret = i915_drm_thaw(dev);
  473. if (ret)
  474. return ret;
  475. drm_kms_helper_poll_enable(dev);
  476. return 0;
  477. }
  478. static int i8xx_do_reset(struct drm_device *dev, u8 flags)
  479. {
  480. struct drm_i915_private *dev_priv = dev->dev_private;
  481. if (IS_I85X(dev))
  482. return -ENODEV;
  483. I915_WRITE(D_STATE, I915_READ(D_STATE) | DSTATE_GFX_RESET_I830);
  484. POSTING_READ(D_STATE);
  485. if (IS_I830(dev) || IS_845G(dev)) {
  486. I915_WRITE(DEBUG_RESET_I830,
  487. DEBUG_RESET_DISPLAY |
  488. DEBUG_RESET_RENDER |
  489. DEBUG_RESET_FULL);
  490. POSTING_READ(DEBUG_RESET_I830);
  491. msleep(1);
  492. I915_WRITE(DEBUG_RESET_I830, 0);
  493. POSTING_READ(DEBUG_RESET_I830);
  494. }
  495. msleep(1);
  496. I915_WRITE(D_STATE, I915_READ(D_STATE) & ~DSTATE_GFX_RESET_I830);
  497. POSTING_READ(D_STATE);
  498. return 0;
  499. }
  500. static int i965_reset_complete(struct drm_device *dev)
  501. {
  502. u8 gdrst;
  503. pci_read_config_byte(dev->pdev, I965_GDRST, &gdrst);
  504. return gdrst & 0x1;
  505. }
  506. static int i965_do_reset(struct drm_device *dev, u8 flags)
  507. {
  508. u8 gdrst;
  509. /*
  510. * Set the domains we want to reset (GRDOM/bits 2 and 3) as
  511. * well as the reset bit (GR/bit 0). Setting the GR bit
  512. * triggers the reset; when done, the hardware will clear it.
  513. */
  514. pci_read_config_byte(dev->pdev, I965_GDRST, &gdrst);
  515. pci_write_config_byte(dev->pdev, I965_GDRST, gdrst | flags | 0x1);
  516. return wait_for(i965_reset_complete(dev), 500);
  517. }
  518. static int ironlake_do_reset(struct drm_device *dev, u8 flags)
  519. {
  520. struct drm_i915_private *dev_priv = dev->dev_private;
  521. u32 gdrst = I915_READ(MCHBAR_MIRROR_BASE + ILK_GDSR);
  522. I915_WRITE(MCHBAR_MIRROR_BASE + ILK_GDSR, gdrst | flags | 0x1);
  523. return wait_for(I915_READ(MCHBAR_MIRROR_BASE + ILK_GDSR) & 0x1, 500);
  524. }
  525. static int gen6_do_reset(struct drm_device *dev, u8 flags)
  526. {
  527. struct drm_i915_private *dev_priv = dev->dev_private;
  528. int ret;
  529. unsigned long irqflags;
  530. /* Hold gt_lock across reset to prevent any register access
  531. * with forcewake not set correctly
  532. */
  533. spin_lock_irqsave(&dev_priv->gt_lock, irqflags);
  534. /* Reset the chip */
  535. /* GEN6_GDRST is not in the gt power well, no need to check
  536. * for fifo space for the write or forcewake the chip for
  537. * the read
  538. */
  539. I915_WRITE_NOTRACE(GEN6_GDRST, GEN6_GRDOM_FULL);
  540. /* Spin waiting for the device to ack the reset request */
  541. ret = wait_for((I915_READ_NOTRACE(GEN6_GDRST) & GEN6_GRDOM_FULL) == 0, 500);
  542. /* If reset with a user forcewake, try to restore, otherwise turn it off */
  543. if (dev_priv->forcewake_count)
  544. dev_priv->display.force_wake_get(dev_priv);
  545. else
  546. dev_priv->display.force_wake_put(dev_priv);
  547. /* Restore fifo count */
  548. dev_priv->gt_fifo_count = I915_READ_NOTRACE(GT_FIFO_FREE_ENTRIES);
  549. spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags);
  550. return ret;
  551. }
  552. /**
  553. * i915_reset - reset chip after a hang
  554. * @dev: drm device to reset
  555. * @flags: reset domains
  556. *
  557. * Reset the chip. Useful if a hang is detected. Returns zero on successful
  558. * reset or otherwise an error code.
  559. *
  560. * Procedure is fairly simple:
  561. * - reset the chip using the reset reg
  562. * - re-init context state
  563. * - re-init hardware status page
  564. * - re-init ring buffer
  565. * - re-init interrupt state
  566. * - re-init display
  567. */
  568. int i915_reset(struct drm_device *dev, u8 flags)
  569. {
  570. drm_i915_private_t *dev_priv = dev->dev_private;
  571. /*
  572. * We really should only reset the display subsystem if we actually
  573. * need to
  574. */
  575. bool need_display = true;
  576. int ret;
  577. if (!i915_try_reset)
  578. return 0;
  579. if (!mutex_trylock(&dev->struct_mutex))
  580. return -EBUSY;
  581. i915_gem_reset(dev);
  582. ret = -ENODEV;
  583. if (get_seconds() - dev_priv->last_gpu_reset < 5) {
  584. DRM_ERROR("GPU hanging too fast, declaring wedged!\n");
  585. } else switch (INTEL_INFO(dev)->gen) {
  586. case 7:
  587. case 6:
  588. ret = gen6_do_reset(dev, flags);
  589. break;
  590. case 5:
  591. ret = ironlake_do_reset(dev, flags);
  592. break;
  593. case 4:
  594. ret = i965_do_reset(dev, flags);
  595. break;
  596. case 2:
  597. ret = i8xx_do_reset(dev, flags);
  598. break;
  599. }
  600. dev_priv->last_gpu_reset = get_seconds();
  601. if (ret) {
  602. DRM_ERROR("Failed to reset chip.\n");
  603. mutex_unlock(&dev->struct_mutex);
  604. return ret;
  605. }
  606. /* Ok, now get things going again... */
  607. /*
  608. * Everything depends on having the GTT running, so we need to start
  609. * there. Fortunately we don't need to do this unless we reset the
  610. * chip at a PCI level.
  611. *
  612. * Next we need to restore the context, but we don't use those
  613. * yet either...
  614. *
  615. * Ring buffer needs to be re-initialized in the KMS case, or if X
  616. * was running at the time of the reset (i.e. we weren't VT
  617. * switched away).
  618. */
  619. if (drm_core_check_feature(dev, DRIVER_MODESET) ||
  620. !dev_priv->mm.suspended) {
  621. dev_priv->mm.suspended = 0;
  622. i915_gem_init_swizzling(dev);
  623. dev_priv->ring[RCS].init(&dev_priv->ring[RCS]);
  624. if (HAS_BSD(dev))
  625. dev_priv->ring[VCS].init(&dev_priv->ring[VCS]);
  626. if (HAS_BLT(dev))
  627. dev_priv->ring[BCS].init(&dev_priv->ring[BCS]);
  628. i915_gem_init_ppgtt(dev);
  629. mutex_unlock(&dev->struct_mutex);
  630. drm_irq_uninstall(dev);
  631. drm_mode_config_reset(dev);
  632. drm_irq_install(dev);
  633. mutex_lock(&dev->struct_mutex);
  634. }
  635. mutex_unlock(&dev->struct_mutex);
  636. /*
  637. * Perform a full modeset as on later generations, e.g. Ironlake, we may
  638. * need to retrain the display link and cannot just restore the register
  639. * values.
  640. */
  641. if (need_display) {
  642. mutex_lock(&dev->mode_config.mutex);
  643. drm_helper_resume_force_mode(dev);
  644. mutex_unlock(&dev->mode_config.mutex);
  645. }
  646. return 0;
  647. }
  648. static int __devinit
  649. i915_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  650. {
  651. /* Only bind to function 0 of the device. Early generations
  652. * used function 1 as a placeholder for multi-head. This causes
  653. * us confusion instead, especially on the systems where both
  654. * functions have the same PCI-ID!
  655. */
  656. if (PCI_FUNC(pdev->devfn))
  657. return -ENODEV;
  658. return drm_get_pci_dev(pdev, ent, &driver);
  659. }
  660. static void
  661. i915_pci_remove(struct pci_dev *pdev)
  662. {
  663. struct drm_device *dev = pci_get_drvdata(pdev);
  664. drm_put_dev(dev);
  665. }
  666. static int i915_pm_suspend(struct device *dev)
  667. {
  668. struct pci_dev *pdev = to_pci_dev(dev);
  669. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  670. int error;
  671. if (!drm_dev || !drm_dev->dev_private) {
  672. dev_err(dev, "DRM not initialized, aborting suspend.\n");
  673. return -ENODEV;
  674. }
  675. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  676. return 0;
  677. error = i915_drm_freeze(drm_dev);
  678. if (error)
  679. return error;
  680. pci_disable_device(pdev);
  681. pci_set_power_state(pdev, PCI_D3hot);
  682. return 0;
  683. }
  684. static int i915_pm_resume(struct device *dev)
  685. {
  686. struct pci_dev *pdev = to_pci_dev(dev);
  687. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  688. return i915_resume(drm_dev);
  689. }
  690. static int i915_pm_freeze(struct device *dev)
  691. {
  692. struct pci_dev *pdev = to_pci_dev(dev);
  693. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  694. if (!drm_dev || !drm_dev->dev_private) {
  695. dev_err(dev, "DRM not initialized, aborting suspend.\n");
  696. return -ENODEV;
  697. }
  698. return i915_drm_freeze(drm_dev);
  699. }
  700. static int i915_pm_thaw(struct device *dev)
  701. {
  702. struct pci_dev *pdev = to_pci_dev(dev);
  703. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  704. return i915_drm_thaw(drm_dev);
  705. }
  706. static int i915_pm_poweroff(struct device *dev)
  707. {
  708. struct pci_dev *pdev = to_pci_dev(dev);
  709. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  710. return i915_drm_freeze(drm_dev);
  711. }
  712. static const struct dev_pm_ops i915_pm_ops = {
  713. .suspend = i915_pm_suspend,
  714. .resume = i915_pm_resume,
  715. .freeze = i915_pm_freeze,
  716. .thaw = i915_pm_thaw,
  717. .poweroff = i915_pm_poweroff,
  718. .restore = i915_pm_resume,
  719. };
  720. static struct vm_operations_struct i915_gem_vm_ops = {
  721. .fault = i915_gem_fault,
  722. .open = drm_gem_vm_open,
  723. .close = drm_gem_vm_close,
  724. };
  725. static const struct file_operations i915_driver_fops = {
  726. .owner = THIS_MODULE,
  727. .open = drm_open,
  728. .release = drm_release,
  729. .unlocked_ioctl = drm_ioctl,
  730. .mmap = drm_gem_mmap,
  731. .poll = drm_poll,
  732. .fasync = drm_fasync,
  733. .read = drm_read,
  734. #ifdef CONFIG_COMPAT
  735. .compat_ioctl = i915_compat_ioctl,
  736. #endif
  737. .llseek = noop_llseek,
  738. };
  739. static struct drm_driver driver = {
  740. /* Don't use MTRRs here; the Xserver or userspace app should
  741. * deal with them for Intel hardware.
  742. */
  743. .driver_features =
  744. DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | /* DRIVER_USE_MTRR |*/
  745. DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM,
  746. .load = i915_driver_load,
  747. .unload = i915_driver_unload,
  748. .open = i915_driver_open,
  749. .lastclose = i915_driver_lastclose,
  750. .preclose = i915_driver_preclose,
  751. .postclose = i915_driver_postclose,
  752. /* Used in place of i915_pm_ops for non-DRIVER_MODESET */
  753. .suspend = i915_suspend,
  754. .resume = i915_resume,
  755. .device_is_agp = i915_driver_device_is_agp,
  756. .reclaim_buffers = drm_core_reclaim_buffers,
  757. .master_create = i915_master_create,
  758. .master_destroy = i915_master_destroy,
  759. #if defined(CONFIG_DEBUG_FS)
  760. .debugfs_init = i915_debugfs_init,
  761. .debugfs_cleanup = i915_debugfs_cleanup,
  762. #endif
  763. .gem_init_object = i915_gem_init_object,
  764. .gem_free_object = i915_gem_free_object,
  765. .gem_vm_ops = &i915_gem_vm_ops,
  766. .dumb_create = i915_gem_dumb_create,
  767. .dumb_map_offset = i915_gem_mmap_gtt,
  768. .dumb_destroy = i915_gem_dumb_destroy,
  769. .ioctls = i915_ioctls,
  770. .fops = &i915_driver_fops,
  771. .name = DRIVER_NAME,
  772. .desc = DRIVER_DESC,
  773. .date = DRIVER_DATE,
  774. .major = DRIVER_MAJOR,
  775. .minor = DRIVER_MINOR,
  776. .patchlevel = DRIVER_PATCHLEVEL,
  777. };
  778. static struct pci_driver i915_pci_driver = {
  779. .name = DRIVER_NAME,
  780. .id_table = pciidlist,
  781. .probe = i915_pci_probe,
  782. .remove = i915_pci_remove,
  783. .driver.pm = &i915_pm_ops,
  784. };
  785. static int __init i915_init(void)
  786. {
  787. if (!intel_agp_enabled) {
  788. DRM_ERROR("drm/i915 can't work without intel_agp module!\n");
  789. return -ENODEV;
  790. }
  791. driver.num_ioctls = i915_max_ioctl;
  792. /*
  793. * If CONFIG_DRM_I915_KMS is set, default to KMS unless
  794. * explicitly disabled with the module pararmeter.
  795. *
  796. * Otherwise, just follow the parameter (defaulting to off).
  797. *
  798. * Allow optional vga_text_mode_force boot option to override
  799. * the default behavior.
  800. */
  801. #if defined(CONFIG_DRM_I915_KMS)
  802. if (i915_modeset != 0)
  803. driver.driver_features |= DRIVER_MODESET;
  804. #endif
  805. if (i915_modeset == 1)
  806. driver.driver_features |= DRIVER_MODESET;
  807. #ifdef CONFIG_VGA_CONSOLE
  808. if (vgacon_text_force() && i915_modeset == -1)
  809. driver.driver_features &= ~DRIVER_MODESET;
  810. #endif
  811. if (!(driver.driver_features & DRIVER_MODESET))
  812. driver.get_vblank_timestamp = NULL;
  813. return drm_pci_init(&driver, &i915_pci_driver);
  814. }
  815. static void __exit i915_exit(void)
  816. {
  817. drm_pci_exit(&driver, &i915_pci_driver);
  818. }
  819. module_init(i915_init);
  820. module_exit(i915_exit);
  821. MODULE_AUTHOR(DRIVER_AUTHOR);
  822. MODULE_DESCRIPTION(DRIVER_DESC);
  823. MODULE_LICENSE("GPL and additional rights");
  824. #define __i915_read(x, y) \
  825. u##x i915_read##x(struct drm_i915_private *dev_priv, u32 reg) { \
  826. u##x val = 0; \
  827. if (NEEDS_FORCE_WAKE((dev_priv), (reg))) { \
  828. unsigned long irqflags; \
  829. spin_lock_irqsave(&dev_priv->gt_lock, irqflags); \
  830. if (dev_priv->forcewake_count == 0) \
  831. dev_priv->display.force_wake_get(dev_priv); \
  832. val = read##y(dev_priv->regs + reg); \
  833. if (dev_priv->forcewake_count == 0) \
  834. dev_priv->display.force_wake_put(dev_priv); \
  835. spin_unlock_irqrestore(&dev_priv->gt_lock, irqflags); \
  836. } else { \
  837. val = read##y(dev_priv->regs + reg); \
  838. } \
  839. trace_i915_reg_rw(false, reg, val, sizeof(val)); \
  840. return val; \
  841. }
  842. __i915_read(8, b)
  843. __i915_read(16, w)
  844. __i915_read(32, l)
  845. __i915_read(64, q)
  846. #undef __i915_read
  847. #define __i915_write(x, y) \
  848. void i915_write##x(struct drm_i915_private *dev_priv, u32 reg, u##x val) { \
  849. u32 __fifo_ret = 0; \
  850. trace_i915_reg_rw(true, reg, val, sizeof(val)); \
  851. if (NEEDS_FORCE_WAKE((dev_priv), (reg))) { \
  852. __fifo_ret = __gen6_gt_wait_for_fifo(dev_priv); \
  853. } \
  854. write##y(val, dev_priv->regs + reg); \
  855. if (unlikely(__fifo_ret)) { \
  856. gen6_gt_check_fifodbg(dev_priv); \
  857. } \
  858. }
  859. __i915_write(8, b)
  860. __i915_write(16, w)
  861. __i915_write(32, l)
  862. __i915_write(64, q)
  863. #undef __i915_write