i915_drv.c 29 KB

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