i915_drv.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641
  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/acpi.h>
  30. #include <linux/device.h>
  31. #include <linux/oom.h>
  32. #include <linux/module.h>
  33. #include <linux/pci.h>
  34. #include <linux/pm.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/pnp.h>
  37. #include <linux/slab.h>
  38. #include <linux/vgaarb.h>
  39. #include <linux/vga_switcheroo.h>
  40. #include <linux/vt.h>
  41. #include <acpi/video.h>
  42. #include <drm/drmP.h>
  43. #include <drm/drm_crtc_helper.h>
  44. #include <drm/i915_drm.h>
  45. #include "i915_drv.h"
  46. #include "i915_trace.h"
  47. #include "i915_vgpu.h"
  48. #include "intel_drv.h"
  49. static struct drm_driver driver;
  50. static unsigned int i915_load_fail_count;
  51. bool __i915_inject_load_failure(const char *func, int line)
  52. {
  53. if (i915_load_fail_count >= i915.inject_load_failure)
  54. return false;
  55. if (++i915_load_fail_count == i915.inject_load_failure) {
  56. DRM_INFO("Injecting failure at checkpoint %u [%s:%d]\n",
  57. i915.inject_load_failure, func, line);
  58. return true;
  59. }
  60. return false;
  61. }
  62. #define FDO_BUG_URL "https://bugs.freedesktop.org/enter_bug.cgi?product=DRI"
  63. #define FDO_BUG_MSG "Please file a bug at " FDO_BUG_URL " against DRM/Intel " \
  64. "providing the dmesg log by booting with drm.debug=0xf"
  65. void
  66. __i915_printk(struct drm_i915_private *dev_priv, const char *level,
  67. const char *fmt, ...)
  68. {
  69. static bool shown_bug_once;
  70. struct device *dev = dev_priv->drm.dev;
  71. bool is_error = level[1] <= KERN_ERR[1];
  72. bool is_debug = level[1] == KERN_DEBUG[1];
  73. struct va_format vaf;
  74. va_list args;
  75. if (is_debug && !(drm_debug & DRM_UT_DRIVER))
  76. return;
  77. va_start(args, fmt);
  78. vaf.fmt = fmt;
  79. vaf.va = &args;
  80. dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
  81. __builtin_return_address(0), &vaf);
  82. if (is_error && !shown_bug_once) {
  83. dev_notice(dev, "%s", FDO_BUG_MSG);
  84. shown_bug_once = true;
  85. }
  86. va_end(args);
  87. }
  88. static bool i915_error_injected(struct drm_i915_private *dev_priv)
  89. {
  90. return i915.inject_load_failure &&
  91. i915_load_fail_count == i915.inject_load_failure;
  92. }
  93. #define i915_load_error(dev_priv, fmt, ...) \
  94. __i915_printk(dev_priv, \
  95. i915_error_injected(dev_priv) ? KERN_DEBUG : KERN_ERR, \
  96. fmt, ##__VA_ARGS__)
  97. static enum intel_pch intel_virt_detect_pch(struct drm_device *dev)
  98. {
  99. enum intel_pch ret = PCH_NOP;
  100. /*
  101. * In a virtualized passthrough environment we can be in a
  102. * setup where the ISA bridge is not able to be passed through.
  103. * In this case, a south bridge can be emulated and we have to
  104. * make an educated guess as to which PCH is really there.
  105. */
  106. if (IS_GEN5(dev)) {
  107. ret = PCH_IBX;
  108. DRM_DEBUG_KMS("Assuming Ibex Peak PCH\n");
  109. } else if (IS_GEN6(dev) || IS_IVYBRIDGE(dev)) {
  110. ret = PCH_CPT;
  111. DRM_DEBUG_KMS("Assuming CouarPoint PCH\n");
  112. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  113. ret = PCH_LPT;
  114. DRM_DEBUG_KMS("Assuming LynxPoint PCH\n");
  115. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  116. ret = PCH_SPT;
  117. DRM_DEBUG_KMS("Assuming SunrisePoint PCH\n");
  118. }
  119. return ret;
  120. }
  121. static void intel_detect_pch(struct drm_device *dev)
  122. {
  123. struct drm_i915_private *dev_priv = to_i915(dev);
  124. struct pci_dev *pch = NULL;
  125. /* In all current cases, num_pipes is equivalent to the PCH_NOP setting
  126. * (which really amounts to a PCH but no South Display).
  127. */
  128. if (INTEL_INFO(dev)->num_pipes == 0) {
  129. dev_priv->pch_type = PCH_NOP;
  130. return;
  131. }
  132. /*
  133. * The reason to probe ISA bridge instead of Dev31:Fun0 is to
  134. * make graphics device passthrough work easy for VMM, that only
  135. * need to expose ISA bridge to let driver know the real hardware
  136. * underneath. This is a requirement from virtualization team.
  137. *
  138. * In some virtualized environments (e.g. XEN), there is irrelevant
  139. * ISA bridge in the system. To work reliably, we should scan trhough
  140. * all the ISA bridge devices and check for the first match, instead
  141. * of only checking the first one.
  142. */
  143. while ((pch = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, pch))) {
  144. if (pch->vendor == PCI_VENDOR_ID_INTEL) {
  145. unsigned short id = pch->device & INTEL_PCH_DEVICE_ID_MASK;
  146. dev_priv->pch_id = id;
  147. if (id == INTEL_PCH_IBX_DEVICE_ID_TYPE) {
  148. dev_priv->pch_type = PCH_IBX;
  149. DRM_DEBUG_KMS("Found Ibex Peak PCH\n");
  150. WARN_ON(!IS_GEN5(dev));
  151. } else if (id == INTEL_PCH_CPT_DEVICE_ID_TYPE) {
  152. dev_priv->pch_type = PCH_CPT;
  153. DRM_DEBUG_KMS("Found CougarPoint PCH\n");
  154. WARN_ON(!(IS_GEN6(dev) || IS_IVYBRIDGE(dev)));
  155. } else if (id == INTEL_PCH_PPT_DEVICE_ID_TYPE) {
  156. /* PantherPoint is CPT compatible */
  157. dev_priv->pch_type = PCH_CPT;
  158. DRM_DEBUG_KMS("Found PantherPoint PCH\n");
  159. WARN_ON(!(IS_GEN6(dev) || IS_IVYBRIDGE(dev)));
  160. } else if (id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
  161. dev_priv->pch_type = PCH_LPT;
  162. DRM_DEBUG_KMS("Found LynxPoint PCH\n");
  163. WARN_ON(!IS_HASWELL(dev) && !IS_BROADWELL(dev));
  164. WARN_ON(IS_HSW_ULT(dev) || IS_BDW_ULT(dev));
  165. } else if (id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  166. dev_priv->pch_type = PCH_LPT;
  167. DRM_DEBUG_KMS("Found LynxPoint LP PCH\n");
  168. WARN_ON(!IS_HASWELL(dev) && !IS_BROADWELL(dev));
  169. WARN_ON(!IS_HSW_ULT(dev) && !IS_BDW_ULT(dev));
  170. } else if (id == INTEL_PCH_SPT_DEVICE_ID_TYPE) {
  171. dev_priv->pch_type = PCH_SPT;
  172. DRM_DEBUG_KMS("Found SunrisePoint PCH\n");
  173. WARN_ON(!IS_SKYLAKE(dev) &&
  174. !IS_KABYLAKE(dev));
  175. } else if (id == INTEL_PCH_SPT_LP_DEVICE_ID_TYPE) {
  176. dev_priv->pch_type = PCH_SPT;
  177. DRM_DEBUG_KMS("Found SunrisePoint LP PCH\n");
  178. WARN_ON(!IS_SKYLAKE(dev) &&
  179. !IS_KABYLAKE(dev));
  180. } else if (id == INTEL_PCH_KBP_DEVICE_ID_TYPE) {
  181. dev_priv->pch_type = PCH_KBP;
  182. DRM_DEBUG_KMS("Found KabyPoint PCH\n");
  183. WARN_ON(!IS_KABYLAKE(dev));
  184. } else if ((id == INTEL_PCH_P2X_DEVICE_ID_TYPE) ||
  185. (id == INTEL_PCH_P3X_DEVICE_ID_TYPE) ||
  186. ((id == INTEL_PCH_QEMU_DEVICE_ID_TYPE) &&
  187. pch->subsystem_vendor ==
  188. PCI_SUBVENDOR_ID_REDHAT_QUMRANET &&
  189. pch->subsystem_device ==
  190. PCI_SUBDEVICE_ID_QEMU)) {
  191. dev_priv->pch_type = intel_virt_detect_pch(dev);
  192. } else
  193. continue;
  194. break;
  195. }
  196. }
  197. if (!pch)
  198. DRM_DEBUG_KMS("No PCH found.\n");
  199. pci_dev_put(pch);
  200. }
  201. bool i915_semaphore_is_enabled(struct drm_i915_private *dev_priv)
  202. {
  203. if (INTEL_GEN(dev_priv) < 6)
  204. return false;
  205. if (i915.semaphores >= 0)
  206. return i915.semaphores;
  207. /* TODO: make semaphores and Execlists play nicely together */
  208. if (i915.enable_execlists)
  209. return false;
  210. #ifdef CONFIG_INTEL_IOMMU
  211. /* Enable semaphores on SNB when IO remapping is off */
  212. if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped)
  213. return false;
  214. #endif
  215. return true;
  216. }
  217. static int i915_getparam(struct drm_device *dev, void *data,
  218. struct drm_file *file_priv)
  219. {
  220. struct drm_i915_private *dev_priv = to_i915(dev);
  221. drm_i915_getparam_t *param = data;
  222. int value;
  223. switch (param->param) {
  224. case I915_PARAM_IRQ_ACTIVE:
  225. case I915_PARAM_ALLOW_BATCHBUFFER:
  226. case I915_PARAM_LAST_DISPATCH:
  227. /* Reject all old ums/dri params. */
  228. return -ENODEV;
  229. case I915_PARAM_CHIPSET_ID:
  230. value = dev->pdev->device;
  231. break;
  232. case I915_PARAM_REVISION:
  233. value = dev->pdev->revision;
  234. break;
  235. case I915_PARAM_HAS_GEM:
  236. value = 1;
  237. break;
  238. case I915_PARAM_NUM_FENCES_AVAIL:
  239. value = dev_priv->num_fence_regs;
  240. break;
  241. case I915_PARAM_HAS_OVERLAY:
  242. value = dev_priv->overlay ? 1 : 0;
  243. break;
  244. case I915_PARAM_HAS_PAGEFLIPPING:
  245. value = 1;
  246. break;
  247. case I915_PARAM_HAS_EXECBUF2:
  248. /* depends on GEM */
  249. value = 1;
  250. break;
  251. case I915_PARAM_HAS_BSD:
  252. value = intel_engine_initialized(&dev_priv->engine[VCS]);
  253. break;
  254. case I915_PARAM_HAS_BLT:
  255. value = intel_engine_initialized(&dev_priv->engine[BCS]);
  256. break;
  257. case I915_PARAM_HAS_VEBOX:
  258. value = intel_engine_initialized(&dev_priv->engine[VECS]);
  259. break;
  260. case I915_PARAM_HAS_BSD2:
  261. value = intel_engine_initialized(&dev_priv->engine[VCS2]);
  262. break;
  263. case I915_PARAM_HAS_RELAXED_FENCING:
  264. value = 1;
  265. break;
  266. case I915_PARAM_HAS_COHERENT_RINGS:
  267. value = 1;
  268. break;
  269. case I915_PARAM_HAS_EXEC_CONSTANTS:
  270. value = INTEL_INFO(dev)->gen >= 4;
  271. break;
  272. case I915_PARAM_HAS_RELAXED_DELTA:
  273. value = 1;
  274. break;
  275. case I915_PARAM_HAS_GEN7_SOL_RESET:
  276. value = 1;
  277. break;
  278. case I915_PARAM_HAS_LLC:
  279. value = HAS_LLC(dev);
  280. break;
  281. case I915_PARAM_HAS_WT:
  282. value = HAS_WT(dev);
  283. break;
  284. case I915_PARAM_HAS_ALIASING_PPGTT:
  285. value = USES_PPGTT(dev);
  286. break;
  287. case I915_PARAM_HAS_WAIT_TIMEOUT:
  288. value = 1;
  289. break;
  290. case I915_PARAM_HAS_SEMAPHORES:
  291. value = i915_semaphore_is_enabled(dev_priv);
  292. break;
  293. case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
  294. value = 1;
  295. break;
  296. case I915_PARAM_HAS_SECURE_BATCHES:
  297. value = capable(CAP_SYS_ADMIN);
  298. break;
  299. case I915_PARAM_HAS_PINNED_BATCHES:
  300. value = 1;
  301. break;
  302. case I915_PARAM_HAS_EXEC_NO_RELOC:
  303. value = 1;
  304. break;
  305. case I915_PARAM_HAS_EXEC_HANDLE_LUT:
  306. value = 1;
  307. break;
  308. case I915_PARAM_CMD_PARSER_VERSION:
  309. value = i915_cmd_parser_get_version(dev_priv);
  310. break;
  311. case I915_PARAM_HAS_COHERENT_PHYS_GTT:
  312. value = 1;
  313. break;
  314. case I915_PARAM_MMAP_VERSION:
  315. value = 1;
  316. break;
  317. case I915_PARAM_SUBSLICE_TOTAL:
  318. value = INTEL_INFO(dev)->subslice_total;
  319. if (!value)
  320. return -ENODEV;
  321. break;
  322. case I915_PARAM_EU_TOTAL:
  323. value = INTEL_INFO(dev)->eu_total;
  324. if (!value)
  325. return -ENODEV;
  326. break;
  327. case I915_PARAM_HAS_GPU_RESET:
  328. value = i915.enable_hangcheck && intel_has_gpu_reset(dev_priv);
  329. break;
  330. case I915_PARAM_HAS_RESOURCE_STREAMER:
  331. value = HAS_RESOURCE_STREAMER(dev);
  332. break;
  333. case I915_PARAM_HAS_EXEC_SOFTPIN:
  334. value = 1;
  335. break;
  336. case I915_PARAM_HAS_POOLED_EU:
  337. value = HAS_POOLED_EU(dev);
  338. break;
  339. case I915_PARAM_MIN_EU_IN_POOL:
  340. value = INTEL_INFO(dev)->min_eu_in_pool;
  341. break;
  342. default:
  343. DRM_DEBUG("Unknown parameter %d\n", param->param);
  344. return -EINVAL;
  345. }
  346. if (put_user(value, param->value))
  347. return -EFAULT;
  348. return 0;
  349. }
  350. static int i915_get_bridge_dev(struct drm_device *dev)
  351. {
  352. struct drm_i915_private *dev_priv = to_i915(dev);
  353. dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  354. if (!dev_priv->bridge_dev) {
  355. DRM_ERROR("bridge device not found\n");
  356. return -1;
  357. }
  358. return 0;
  359. }
  360. /* Allocate space for the MCH regs if needed, return nonzero on error */
  361. static int
  362. intel_alloc_mchbar_resource(struct drm_device *dev)
  363. {
  364. struct drm_i915_private *dev_priv = to_i915(dev);
  365. int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
  366. u32 temp_lo, temp_hi = 0;
  367. u64 mchbar_addr;
  368. int ret;
  369. if (INTEL_INFO(dev)->gen >= 4)
  370. pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
  371. pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
  372. mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
  373. /* If ACPI doesn't have it, assume we need to allocate it ourselves */
  374. #ifdef CONFIG_PNP
  375. if (mchbar_addr &&
  376. pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
  377. return 0;
  378. #endif
  379. /* Get some space for it */
  380. dev_priv->mch_res.name = "i915 MCHBAR";
  381. dev_priv->mch_res.flags = IORESOURCE_MEM;
  382. ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
  383. &dev_priv->mch_res,
  384. MCHBAR_SIZE, MCHBAR_SIZE,
  385. PCIBIOS_MIN_MEM,
  386. 0, pcibios_align_resource,
  387. dev_priv->bridge_dev);
  388. if (ret) {
  389. DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
  390. dev_priv->mch_res.start = 0;
  391. return ret;
  392. }
  393. if (INTEL_INFO(dev)->gen >= 4)
  394. pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
  395. upper_32_bits(dev_priv->mch_res.start));
  396. pci_write_config_dword(dev_priv->bridge_dev, reg,
  397. lower_32_bits(dev_priv->mch_res.start));
  398. return 0;
  399. }
  400. /* Setup MCHBAR if possible, return true if we should disable it again */
  401. static void
  402. intel_setup_mchbar(struct drm_device *dev)
  403. {
  404. struct drm_i915_private *dev_priv = to_i915(dev);
  405. int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
  406. u32 temp;
  407. bool enabled;
  408. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  409. return;
  410. dev_priv->mchbar_need_disable = false;
  411. if (IS_I915G(dev) || IS_I915GM(dev)) {
  412. pci_read_config_dword(dev_priv->bridge_dev, DEVEN, &temp);
  413. enabled = !!(temp & DEVEN_MCHBAR_EN);
  414. } else {
  415. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
  416. enabled = temp & 1;
  417. }
  418. /* If it's already enabled, don't have to do anything */
  419. if (enabled)
  420. return;
  421. if (intel_alloc_mchbar_resource(dev))
  422. return;
  423. dev_priv->mchbar_need_disable = true;
  424. /* Space is allocated or reserved, so enable it. */
  425. if (IS_I915G(dev) || IS_I915GM(dev)) {
  426. pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
  427. temp | DEVEN_MCHBAR_EN);
  428. } else {
  429. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
  430. pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
  431. }
  432. }
  433. static void
  434. intel_teardown_mchbar(struct drm_device *dev)
  435. {
  436. struct drm_i915_private *dev_priv = to_i915(dev);
  437. int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
  438. if (dev_priv->mchbar_need_disable) {
  439. if (IS_I915G(dev) || IS_I915GM(dev)) {
  440. u32 deven_val;
  441. pci_read_config_dword(dev_priv->bridge_dev, DEVEN,
  442. &deven_val);
  443. deven_val &= ~DEVEN_MCHBAR_EN;
  444. pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
  445. deven_val);
  446. } else {
  447. u32 mchbar_val;
  448. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg,
  449. &mchbar_val);
  450. mchbar_val &= ~1;
  451. pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg,
  452. mchbar_val);
  453. }
  454. }
  455. if (dev_priv->mch_res.start)
  456. release_resource(&dev_priv->mch_res);
  457. }
  458. /* true = enable decode, false = disable decoder */
  459. static unsigned int i915_vga_set_decode(void *cookie, bool state)
  460. {
  461. struct drm_device *dev = cookie;
  462. intel_modeset_vga_set_state(dev, state);
  463. if (state)
  464. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  465. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  466. else
  467. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  468. }
  469. static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  470. {
  471. struct drm_device *dev = pci_get_drvdata(pdev);
  472. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  473. if (state == VGA_SWITCHEROO_ON) {
  474. pr_info("switched on\n");
  475. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  476. /* i915 resume handler doesn't set to D0 */
  477. pci_set_power_state(dev->pdev, PCI_D0);
  478. i915_resume_switcheroo(dev);
  479. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  480. } else {
  481. pr_info("switched off\n");
  482. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  483. i915_suspend_switcheroo(dev, pmm);
  484. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  485. }
  486. }
  487. static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
  488. {
  489. struct drm_device *dev = pci_get_drvdata(pdev);
  490. /*
  491. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  492. * locking inversion with the driver load path. And the access here is
  493. * completely racy anyway. So don't bother with locking for now.
  494. */
  495. return dev->open_count == 0;
  496. }
  497. static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
  498. .set_gpu_state = i915_switcheroo_set_state,
  499. .reprobe = NULL,
  500. .can_switch = i915_switcheroo_can_switch,
  501. };
  502. static void i915_gem_fini(struct drm_device *dev)
  503. {
  504. struct drm_i915_private *dev_priv = to_i915(dev);
  505. /*
  506. * Neither the BIOS, ourselves or any other kernel
  507. * expects the system to be in execlists mode on startup,
  508. * so we need to reset the GPU back to legacy mode. And the only
  509. * known way to disable logical contexts is through a GPU reset.
  510. *
  511. * So in order to leave the system in a known default configuration,
  512. * always reset the GPU upon unload. Afterwards we then clean up the
  513. * GEM state tracking, flushing off the requests and leaving the
  514. * system in a known idle state.
  515. *
  516. * Note that is of the upmost importance that the GPU is idle and
  517. * all stray writes are flushed *before* we dismantle the backing
  518. * storage for the pinned objects.
  519. *
  520. * However, since we are uncertain that reseting the GPU on older
  521. * machines is a good idea, we don't - just in case it leaves the
  522. * machine in an unusable condition.
  523. */
  524. if (HAS_HW_CONTEXTS(dev)) {
  525. int reset = intel_gpu_reset(dev_priv, ALL_ENGINES);
  526. WARN_ON(reset && reset != -ENODEV);
  527. }
  528. mutex_lock(&dev->struct_mutex);
  529. i915_gem_reset(dev);
  530. i915_gem_cleanup_engines(dev);
  531. i915_gem_context_fini(dev);
  532. mutex_unlock(&dev->struct_mutex);
  533. WARN_ON(!list_empty(&to_i915(dev)->context_list));
  534. }
  535. static int i915_load_modeset_init(struct drm_device *dev)
  536. {
  537. struct drm_i915_private *dev_priv = to_i915(dev);
  538. int ret;
  539. if (i915_inject_load_failure())
  540. return -ENODEV;
  541. ret = intel_bios_init(dev_priv);
  542. if (ret)
  543. DRM_INFO("failed to find VBIOS tables\n");
  544. /* If we have > 1 VGA cards, then we need to arbitrate access
  545. * to the common VGA resources.
  546. *
  547. * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
  548. * then we do not take part in VGA arbitration and the
  549. * vga_client_register() fails with -ENODEV.
  550. */
  551. ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
  552. if (ret && ret != -ENODEV)
  553. goto out;
  554. intel_register_dsm_handler();
  555. ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
  556. if (ret)
  557. goto cleanup_vga_client;
  558. /* must happen before intel_power_domains_init_hw() on VLV/CHV */
  559. intel_update_rawclk(dev_priv);
  560. intel_power_domains_init_hw(dev_priv, false);
  561. intel_csr_ucode_init(dev_priv);
  562. ret = intel_irq_install(dev_priv);
  563. if (ret)
  564. goto cleanup_csr;
  565. intel_setup_gmbus(dev);
  566. /* Important: The output setup functions called by modeset_init need
  567. * working irqs for e.g. gmbus and dp aux transfers. */
  568. intel_modeset_init(dev);
  569. intel_guc_init(dev);
  570. ret = i915_gem_init(dev);
  571. if (ret)
  572. goto cleanup_irq;
  573. intel_modeset_gem_init(dev);
  574. if (INTEL_INFO(dev)->num_pipes == 0)
  575. return 0;
  576. ret = intel_fbdev_init(dev);
  577. if (ret)
  578. goto cleanup_gem;
  579. /* Only enable hotplug handling once the fbdev is fully set up. */
  580. intel_hpd_init(dev_priv);
  581. drm_kms_helper_poll_init(dev);
  582. return 0;
  583. cleanup_gem:
  584. i915_gem_fini(dev);
  585. cleanup_irq:
  586. intel_guc_fini(dev);
  587. drm_irq_uninstall(dev);
  588. intel_teardown_gmbus(dev);
  589. cleanup_csr:
  590. intel_csr_ucode_fini(dev_priv);
  591. intel_power_domains_fini(dev_priv);
  592. vga_switcheroo_unregister_client(dev->pdev);
  593. cleanup_vga_client:
  594. vga_client_register(dev->pdev, NULL, NULL, NULL);
  595. out:
  596. return ret;
  597. }
  598. #if IS_ENABLED(CONFIG_FB)
  599. static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
  600. {
  601. struct apertures_struct *ap;
  602. struct pci_dev *pdev = dev_priv->drm.pdev;
  603. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  604. bool primary;
  605. int ret;
  606. ap = alloc_apertures(1);
  607. if (!ap)
  608. return -ENOMEM;
  609. ap->ranges[0].base = ggtt->mappable_base;
  610. ap->ranges[0].size = ggtt->mappable_end;
  611. primary =
  612. pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
  613. ret = remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
  614. kfree(ap);
  615. return ret;
  616. }
  617. #else
  618. static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
  619. {
  620. return 0;
  621. }
  622. #endif
  623. #if !defined(CONFIG_VGA_CONSOLE)
  624. static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
  625. {
  626. return 0;
  627. }
  628. #elif !defined(CONFIG_DUMMY_CONSOLE)
  629. static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
  630. {
  631. return -ENODEV;
  632. }
  633. #else
  634. static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
  635. {
  636. int ret = 0;
  637. DRM_INFO("Replacing VGA console driver\n");
  638. console_lock();
  639. if (con_is_bound(&vga_con))
  640. ret = do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES - 1, 1);
  641. if (ret == 0) {
  642. ret = do_unregister_con_driver(&vga_con);
  643. /* Ignore "already unregistered". */
  644. if (ret == -ENODEV)
  645. ret = 0;
  646. }
  647. console_unlock();
  648. return ret;
  649. }
  650. #endif
  651. static void intel_init_dpio(struct drm_i915_private *dev_priv)
  652. {
  653. /*
  654. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  655. * CHV x1 PHY (DP/HDMI D)
  656. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  657. */
  658. if (IS_CHERRYVIEW(dev_priv)) {
  659. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  660. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  661. } else if (IS_VALLEYVIEW(dev_priv)) {
  662. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  663. }
  664. }
  665. static int i915_workqueues_init(struct drm_i915_private *dev_priv)
  666. {
  667. /*
  668. * The i915 workqueue is primarily used for batched retirement of
  669. * requests (and thus managing bo) once the task has been completed
  670. * by the GPU. i915_gem_retire_requests() is called directly when we
  671. * need high-priority retirement, such as waiting for an explicit
  672. * bo.
  673. *
  674. * It is also used for periodic low-priority events, such as
  675. * idle-timers and recording error state.
  676. *
  677. * All tasks on the workqueue are expected to acquire the dev mutex
  678. * so there is no point in running more than one instance of the
  679. * workqueue at any time. Use an ordered one.
  680. */
  681. dev_priv->wq = alloc_ordered_workqueue("i915", 0);
  682. if (dev_priv->wq == NULL)
  683. goto out_err;
  684. dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
  685. if (dev_priv->hotplug.dp_wq == NULL)
  686. goto out_free_wq;
  687. return 0;
  688. out_free_wq:
  689. destroy_workqueue(dev_priv->wq);
  690. out_err:
  691. DRM_ERROR("Failed to allocate workqueues.\n");
  692. return -ENOMEM;
  693. }
  694. static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
  695. {
  696. destroy_workqueue(dev_priv->hotplug.dp_wq);
  697. destroy_workqueue(dev_priv->wq);
  698. }
  699. /**
  700. * i915_driver_init_early - setup state not requiring device access
  701. * @dev_priv: device private
  702. *
  703. * Initialize everything that is a "SW-only" state, that is state not
  704. * requiring accessing the device or exposing the driver via kernel internal
  705. * or userspace interfaces. Example steps belonging here: lock initialization,
  706. * system memory allocation, setting up device specific attributes and
  707. * function hooks not requiring accessing the device.
  708. */
  709. static int i915_driver_init_early(struct drm_i915_private *dev_priv,
  710. const struct pci_device_id *ent)
  711. {
  712. const struct intel_device_info *match_info =
  713. (struct intel_device_info *)ent->driver_data;
  714. struct intel_device_info *device_info;
  715. int ret = 0;
  716. if (i915_inject_load_failure())
  717. return -ENODEV;
  718. /* Setup the write-once "constant" device info */
  719. device_info = mkwrite_device_info(dev_priv);
  720. memcpy(device_info, match_info, sizeof(*device_info));
  721. device_info->device_id = dev_priv->drm.pdev->device;
  722. BUG_ON(device_info->gen > sizeof(device_info->gen_mask) * BITS_PER_BYTE);
  723. device_info->gen_mask = BIT(device_info->gen - 1);
  724. spin_lock_init(&dev_priv->irq_lock);
  725. spin_lock_init(&dev_priv->gpu_error.lock);
  726. mutex_init(&dev_priv->backlight_lock);
  727. spin_lock_init(&dev_priv->uncore.lock);
  728. spin_lock_init(&dev_priv->mm.object_stat_lock);
  729. spin_lock_init(&dev_priv->mmio_flip_lock);
  730. mutex_init(&dev_priv->sb_lock);
  731. mutex_init(&dev_priv->modeset_restore_lock);
  732. mutex_init(&dev_priv->av_mutex);
  733. mutex_init(&dev_priv->wm.wm_mutex);
  734. mutex_init(&dev_priv->pps_mutex);
  735. ret = i915_workqueues_init(dev_priv);
  736. if (ret < 0)
  737. return ret;
  738. ret = intel_gvt_init(dev_priv);
  739. if (ret < 0)
  740. goto err_workqueues;
  741. /* This must be called before any calls to HAS_PCH_* */
  742. intel_detect_pch(&dev_priv->drm);
  743. intel_pm_setup(&dev_priv->drm);
  744. intel_init_dpio(dev_priv);
  745. intel_power_domains_init(dev_priv);
  746. intel_irq_init(dev_priv);
  747. intel_init_display_hooks(dev_priv);
  748. intel_init_clock_gating_hooks(dev_priv);
  749. intel_init_audio_hooks(dev_priv);
  750. i915_gem_load_init(&dev_priv->drm);
  751. intel_display_crc_init(&dev_priv->drm);
  752. intel_device_info_dump(dev_priv);
  753. /* Not all pre-production machines fall into this category, only the
  754. * very first ones. Almost everything should work, except for maybe
  755. * suspend/resume. And we don't implement workarounds that affect only
  756. * pre-production machines. */
  757. if (IS_HSW_EARLY_SDV(dev_priv))
  758. DRM_INFO("This is an early pre-production Haswell machine. "
  759. "It may not be fully functional.\n");
  760. return 0;
  761. err_workqueues:
  762. i915_workqueues_cleanup(dev_priv);
  763. return ret;
  764. }
  765. /**
  766. * i915_driver_cleanup_early - cleanup the setup done in i915_driver_init_early()
  767. * @dev_priv: device private
  768. */
  769. static void i915_driver_cleanup_early(struct drm_i915_private *dev_priv)
  770. {
  771. i915_gem_load_cleanup(&dev_priv->drm);
  772. i915_workqueues_cleanup(dev_priv);
  773. }
  774. static int i915_mmio_setup(struct drm_device *dev)
  775. {
  776. struct drm_i915_private *dev_priv = to_i915(dev);
  777. int mmio_bar;
  778. int mmio_size;
  779. mmio_bar = IS_GEN2(dev) ? 1 : 0;
  780. /*
  781. * Before gen4, the registers and the GTT are behind different BARs.
  782. * However, from gen4 onwards, the registers and the GTT are shared
  783. * in the same BAR, so we want to restrict this ioremap from
  784. * clobbering the GTT which we want ioremap_wc instead. Fortunately,
  785. * the register BAR remains the same size for all the earlier
  786. * generations up to Ironlake.
  787. */
  788. if (INTEL_INFO(dev)->gen < 5)
  789. mmio_size = 512 * 1024;
  790. else
  791. mmio_size = 2 * 1024 * 1024;
  792. dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
  793. if (dev_priv->regs == NULL) {
  794. DRM_ERROR("failed to map registers\n");
  795. return -EIO;
  796. }
  797. /* Try to make sure MCHBAR is enabled before poking at it */
  798. intel_setup_mchbar(dev);
  799. return 0;
  800. }
  801. static void i915_mmio_cleanup(struct drm_device *dev)
  802. {
  803. struct drm_i915_private *dev_priv = to_i915(dev);
  804. intel_teardown_mchbar(dev);
  805. pci_iounmap(dev->pdev, dev_priv->regs);
  806. }
  807. /**
  808. * i915_driver_init_mmio - setup device MMIO
  809. * @dev_priv: device private
  810. *
  811. * Setup minimal device state necessary for MMIO accesses later in the
  812. * initialization sequence. The setup here should avoid any other device-wide
  813. * side effects or exposing the driver via kernel internal or user space
  814. * interfaces.
  815. */
  816. static int i915_driver_init_mmio(struct drm_i915_private *dev_priv)
  817. {
  818. struct drm_device *dev = &dev_priv->drm;
  819. int ret;
  820. if (i915_inject_load_failure())
  821. return -ENODEV;
  822. if (i915_get_bridge_dev(dev))
  823. return -EIO;
  824. ret = i915_mmio_setup(dev);
  825. if (ret < 0)
  826. goto put_bridge;
  827. intel_uncore_init(dev_priv);
  828. return 0;
  829. put_bridge:
  830. pci_dev_put(dev_priv->bridge_dev);
  831. return ret;
  832. }
  833. /**
  834. * i915_driver_cleanup_mmio - cleanup the setup done in i915_driver_init_mmio()
  835. * @dev_priv: device private
  836. */
  837. static void i915_driver_cleanup_mmio(struct drm_i915_private *dev_priv)
  838. {
  839. struct drm_device *dev = &dev_priv->drm;
  840. intel_uncore_fini(dev_priv);
  841. i915_mmio_cleanup(dev);
  842. pci_dev_put(dev_priv->bridge_dev);
  843. }
  844. static void intel_sanitize_options(struct drm_i915_private *dev_priv)
  845. {
  846. i915.enable_execlists =
  847. intel_sanitize_enable_execlists(dev_priv,
  848. i915.enable_execlists);
  849. /*
  850. * i915.enable_ppgtt is read-only, so do an early pass to validate the
  851. * user's requested state against the hardware/driver capabilities. We
  852. * do this now so that we can print out any log messages once rather
  853. * than every time we check intel_enable_ppgtt().
  854. */
  855. i915.enable_ppgtt =
  856. intel_sanitize_enable_ppgtt(dev_priv, i915.enable_ppgtt);
  857. DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
  858. }
  859. /**
  860. * i915_driver_init_hw - setup state requiring device access
  861. * @dev_priv: device private
  862. *
  863. * Setup state that requires accessing the device, but doesn't require
  864. * exposing the driver via kernel internal or userspace interfaces.
  865. */
  866. static int i915_driver_init_hw(struct drm_i915_private *dev_priv)
  867. {
  868. struct drm_device *dev = &dev_priv->drm;
  869. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  870. uint32_t aperture_size;
  871. int ret;
  872. if (i915_inject_load_failure())
  873. return -ENODEV;
  874. intel_device_info_runtime_init(dev_priv);
  875. intel_sanitize_options(dev_priv);
  876. ret = i915_ggtt_init_hw(dev);
  877. if (ret)
  878. return ret;
  879. ret = i915_ggtt_enable_hw(dev);
  880. if (ret) {
  881. DRM_ERROR("failed to enable GGTT\n");
  882. goto out_ggtt;
  883. }
  884. /* WARNING: Apparently we must kick fbdev drivers before vgacon,
  885. * otherwise the vga fbdev driver falls over. */
  886. ret = i915_kick_out_firmware_fb(dev_priv);
  887. if (ret) {
  888. DRM_ERROR("failed to remove conflicting framebuffer drivers\n");
  889. goto out_ggtt;
  890. }
  891. ret = i915_kick_out_vgacon(dev_priv);
  892. if (ret) {
  893. DRM_ERROR("failed to remove conflicting VGA console\n");
  894. goto out_ggtt;
  895. }
  896. pci_set_master(dev->pdev);
  897. /* overlay on gen2 is broken and can't address above 1G */
  898. if (IS_GEN2(dev)) {
  899. ret = dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
  900. if (ret) {
  901. DRM_ERROR("failed to set DMA mask\n");
  902. goto out_ggtt;
  903. }
  904. }
  905. /* 965GM sometimes incorrectly writes to hardware status page (HWS)
  906. * using 32bit addressing, overwriting memory if HWS is located
  907. * above 4GB.
  908. *
  909. * The documentation also mentions an issue with undefined
  910. * behaviour if any general state is accessed within a page above 4GB,
  911. * which also needs to be handled carefully.
  912. */
  913. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev)) {
  914. ret = dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
  915. if (ret) {
  916. DRM_ERROR("failed to set DMA mask\n");
  917. goto out_ggtt;
  918. }
  919. }
  920. aperture_size = ggtt->mappable_end;
  921. ggtt->mappable =
  922. io_mapping_create_wc(ggtt->mappable_base,
  923. aperture_size);
  924. if (!ggtt->mappable) {
  925. ret = -EIO;
  926. goto out_ggtt;
  927. }
  928. ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base,
  929. aperture_size);
  930. pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY,
  931. PM_QOS_DEFAULT_VALUE);
  932. intel_uncore_sanitize(dev_priv);
  933. intel_opregion_setup(dev_priv);
  934. i915_gem_load_init_fences(dev_priv);
  935. /* On the 945G/GM, the chipset reports the MSI capability on the
  936. * integrated graphics even though the support isn't actually there
  937. * according to the published specs. It doesn't appear to function
  938. * correctly in testing on 945G.
  939. * This may be a side effect of MSI having been made available for PEG
  940. * and the registers being closely associated.
  941. *
  942. * According to chipset errata, on the 965GM, MSI interrupts may
  943. * be lost or delayed, but we use them anyways to avoid
  944. * stuck interrupts on some machines.
  945. */
  946. if (!IS_I945G(dev) && !IS_I945GM(dev)) {
  947. if (pci_enable_msi(dev->pdev) < 0)
  948. DRM_DEBUG_DRIVER("can't enable MSI");
  949. }
  950. return 0;
  951. out_ggtt:
  952. i915_ggtt_cleanup_hw(dev);
  953. return ret;
  954. }
  955. /**
  956. * i915_driver_cleanup_hw - cleanup the setup done in i915_driver_init_hw()
  957. * @dev_priv: device private
  958. */
  959. static void i915_driver_cleanup_hw(struct drm_i915_private *dev_priv)
  960. {
  961. struct drm_device *dev = &dev_priv->drm;
  962. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  963. if (dev->pdev->msi_enabled)
  964. pci_disable_msi(dev->pdev);
  965. pm_qos_remove_request(&dev_priv->pm_qos);
  966. arch_phys_wc_del(ggtt->mtrr);
  967. io_mapping_free(ggtt->mappable);
  968. i915_ggtt_cleanup_hw(dev);
  969. }
  970. /**
  971. * i915_driver_register - register the driver with the rest of the system
  972. * @dev_priv: device private
  973. *
  974. * Perform any steps necessary to make the driver available via kernel
  975. * internal or userspace interfaces.
  976. */
  977. static void i915_driver_register(struct drm_i915_private *dev_priv)
  978. {
  979. struct drm_device *dev = &dev_priv->drm;
  980. i915_gem_shrinker_init(dev_priv);
  981. /*
  982. * Notify a valid surface after modesetting,
  983. * when running inside a VM.
  984. */
  985. if (intel_vgpu_active(dev_priv))
  986. I915_WRITE(vgtif_reg(display_ready), VGT_DRV_DISPLAY_READY);
  987. /* Reveal our presence to userspace */
  988. if (drm_dev_register(dev, 0) == 0) {
  989. i915_debugfs_register(dev_priv);
  990. i915_setup_sysfs(dev);
  991. } else
  992. DRM_ERROR("Failed to register driver for userspace access!\n");
  993. if (INTEL_INFO(dev_priv)->num_pipes) {
  994. /* Must be done after probing outputs */
  995. intel_opregion_register(dev_priv);
  996. acpi_video_register();
  997. }
  998. if (IS_GEN5(dev_priv))
  999. intel_gpu_ips_init(dev_priv);
  1000. i915_audio_component_init(dev_priv);
  1001. /*
  1002. * Some ports require correctly set-up hpd registers for detection to
  1003. * work properly (leading to ghost connected connector status), e.g. VGA
  1004. * on gm45. Hence we can only set up the initial fbdev config after hpd
  1005. * irqs are fully enabled. We do it last so that the async config
  1006. * cannot run before the connectors are registered.
  1007. */
  1008. intel_fbdev_initial_config_async(dev);
  1009. }
  1010. /**
  1011. * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
  1012. * @dev_priv: device private
  1013. */
  1014. static void i915_driver_unregister(struct drm_i915_private *dev_priv)
  1015. {
  1016. i915_audio_component_cleanup(dev_priv);
  1017. intel_gpu_ips_teardown();
  1018. acpi_video_unregister();
  1019. intel_opregion_unregister(dev_priv);
  1020. i915_teardown_sysfs(&dev_priv->drm);
  1021. i915_debugfs_unregister(dev_priv);
  1022. drm_dev_unregister(&dev_priv->drm);
  1023. i915_gem_shrinker_cleanup(dev_priv);
  1024. }
  1025. /**
  1026. * i915_driver_load - setup chip and create an initial config
  1027. * @dev: DRM device
  1028. * @flags: startup flags
  1029. *
  1030. * The driver load routine has to do several things:
  1031. * - drive output discovery via intel_modeset_init()
  1032. * - initialize the memory manager
  1033. * - allocate initial config memory
  1034. * - setup the DRM framebuffer with the allocated memory
  1035. */
  1036. int i915_driver_load(struct pci_dev *pdev, const struct pci_device_id *ent)
  1037. {
  1038. struct drm_i915_private *dev_priv;
  1039. int ret;
  1040. if (i915.nuclear_pageflip)
  1041. driver.driver_features |= DRIVER_ATOMIC;
  1042. ret = -ENOMEM;
  1043. dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
  1044. if (dev_priv)
  1045. ret = drm_dev_init(&dev_priv->drm, &driver, &pdev->dev);
  1046. if (ret) {
  1047. dev_printk(KERN_ERR, &pdev->dev,
  1048. "[" DRM_NAME ":%s] allocation failed\n", __func__);
  1049. kfree(dev_priv);
  1050. return ret;
  1051. }
  1052. dev_priv->drm.pdev = pdev;
  1053. dev_priv->drm.dev_private = dev_priv;
  1054. ret = pci_enable_device(pdev);
  1055. if (ret)
  1056. goto out_free_priv;
  1057. pci_set_drvdata(pdev, &dev_priv->drm);
  1058. ret = i915_driver_init_early(dev_priv, ent);
  1059. if (ret < 0)
  1060. goto out_pci_disable;
  1061. intel_runtime_pm_get(dev_priv);
  1062. ret = i915_driver_init_mmio(dev_priv);
  1063. if (ret < 0)
  1064. goto out_runtime_pm_put;
  1065. ret = i915_driver_init_hw(dev_priv);
  1066. if (ret < 0)
  1067. goto out_cleanup_mmio;
  1068. /*
  1069. * TODO: move the vblank init and parts of modeset init steps into one
  1070. * of the i915_driver_init_/i915_driver_register functions according
  1071. * to the role/effect of the given init step.
  1072. */
  1073. if (INTEL_INFO(dev_priv)->num_pipes) {
  1074. ret = drm_vblank_init(&dev_priv->drm,
  1075. INTEL_INFO(dev_priv)->num_pipes);
  1076. if (ret)
  1077. goto out_cleanup_hw;
  1078. }
  1079. ret = i915_load_modeset_init(&dev_priv->drm);
  1080. if (ret < 0)
  1081. goto out_cleanup_vblank;
  1082. i915_driver_register(dev_priv);
  1083. intel_runtime_pm_enable(dev_priv);
  1084. intel_runtime_pm_put(dev_priv);
  1085. return 0;
  1086. out_cleanup_vblank:
  1087. drm_vblank_cleanup(&dev_priv->drm);
  1088. out_cleanup_hw:
  1089. i915_driver_cleanup_hw(dev_priv);
  1090. out_cleanup_mmio:
  1091. i915_driver_cleanup_mmio(dev_priv);
  1092. out_runtime_pm_put:
  1093. intel_runtime_pm_put(dev_priv);
  1094. i915_driver_cleanup_early(dev_priv);
  1095. out_pci_disable:
  1096. pci_disable_device(pdev);
  1097. out_free_priv:
  1098. i915_load_error(dev_priv, "Device initialization failed (%d)\n", ret);
  1099. drm_dev_unref(&dev_priv->drm);
  1100. return ret;
  1101. }
  1102. void i915_driver_unload(struct drm_device *dev)
  1103. {
  1104. struct drm_i915_private *dev_priv = to_i915(dev);
  1105. intel_fbdev_fini(dev);
  1106. if (i915_gem_suspend(dev))
  1107. DRM_ERROR("failed to idle hardware; continuing to unload!\n");
  1108. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  1109. i915_driver_unregister(dev_priv);
  1110. drm_vblank_cleanup(dev);
  1111. intel_modeset_cleanup(dev);
  1112. /*
  1113. * free the memory space allocated for the child device
  1114. * config parsed from VBT
  1115. */
  1116. if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
  1117. kfree(dev_priv->vbt.child_dev);
  1118. dev_priv->vbt.child_dev = NULL;
  1119. dev_priv->vbt.child_dev_num = 0;
  1120. }
  1121. kfree(dev_priv->vbt.sdvo_lvds_vbt_mode);
  1122. dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
  1123. kfree(dev_priv->vbt.lfp_lvds_vbt_mode);
  1124. dev_priv->vbt.lfp_lvds_vbt_mode = NULL;
  1125. vga_switcheroo_unregister_client(dev->pdev);
  1126. vga_client_register(dev->pdev, NULL, NULL, NULL);
  1127. intel_csr_ucode_fini(dev_priv);
  1128. /* Free error state after interrupts are fully disabled. */
  1129. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  1130. i915_destroy_error_state(dev);
  1131. /* Flush any outstanding unpin_work. */
  1132. flush_workqueue(dev_priv->wq);
  1133. intel_guc_fini(dev);
  1134. i915_gem_fini(dev);
  1135. intel_fbc_cleanup_cfb(dev_priv);
  1136. intel_power_domains_fini(dev_priv);
  1137. i915_driver_cleanup_hw(dev_priv);
  1138. i915_driver_cleanup_mmio(dev_priv);
  1139. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  1140. i915_driver_cleanup_early(dev_priv);
  1141. }
  1142. static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
  1143. {
  1144. int ret;
  1145. ret = i915_gem_open(dev, file);
  1146. if (ret)
  1147. return ret;
  1148. return 0;
  1149. }
  1150. /**
  1151. * i915_driver_lastclose - clean up after all DRM clients have exited
  1152. * @dev: DRM device
  1153. *
  1154. * Take care of cleaning up after all DRM clients have exited. In the
  1155. * mode setting case, we want to restore the kernel's initial mode (just
  1156. * in case the last client left us in a bad state).
  1157. *
  1158. * Additionally, in the non-mode setting case, we'll tear down the GTT
  1159. * and DMA structures, since the kernel won't be using them, and clea
  1160. * up any GEM state.
  1161. */
  1162. static void i915_driver_lastclose(struct drm_device *dev)
  1163. {
  1164. intel_fbdev_restore_mode(dev);
  1165. vga_switcheroo_process_delayed_switch();
  1166. }
  1167. static void i915_driver_preclose(struct drm_device *dev, struct drm_file *file)
  1168. {
  1169. mutex_lock(&dev->struct_mutex);
  1170. i915_gem_context_close(dev, file);
  1171. i915_gem_release(dev, file);
  1172. mutex_unlock(&dev->struct_mutex);
  1173. }
  1174. static void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
  1175. {
  1176. struct drm_i915_file_private *file_priv = file->driver_priv;
  1177. kfree(file_priv);
  1178. }
  1179. static void intel_suspend_encoders(struct drm_i915_private *dev_priv)
  1180. {
  1181. struct drm_device *dev = &dev_priv->drm;
  1182. struct intel_encoder *encoder;
  1183. drm_modeset_lock_all(dev);
  1184. for_each_intel_encoder(dev, encoder)
  1185. if (encoder->suspend)
  1186. encoder->suspend(encoder);
  1187. drm_modeset_unlock_all(dev);
  1188. }
  1189. static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
  1190. bool rpm_resume);
  1191. static int vlv_suspend_complete(struct drm_i915_private *dev_priv);
  1192. static bool suspend_to_idle(struct drm_i915_private *dev_priv)
  1193. {
  1194. #if IS_ENABLED(CONFIG_ACPI_SLEEP)
  1195. if (acpi_target_system_state() < ACPI_STATE_S3)
  1196. return true;
  1197. #endif
  1198. return false;
  1199. }
  1200. static int i915_drm_suspend(struct drm_device *dev)
  1201. {
  1202. struct drm_i915_private *dev_priv = to_i915(dev);
  1203. pci_power_t opregion_target_state;
  1204. int error;
  1205. /* ignore lid events during suspend */
  1206. mutex_lock(&dev_priv->modeset_restore_lock);
  1207. dev_priv->modeset_restore = MODESET_SUSPENDED;
  1208. mutex_unlock(&dev_priv->modeset_restore_lock);
  1209. disable_rpm_wakeref_asserts(dev_priv);
  1210. /* We do a lot of poking in a lot of registers, make sure they work
  1211. * properly. */
  1212. intel_display_set_init_power(dev_priv, true);
  1213. drm_kms_helper_poll_disable(dev);
  1214. pci_save_state(dev->pdev);
  1215. error = i915_gem_suspend(dev);
  1216. if (error) {
  1217. dev_err(&dev->pdev->dev,
  1218. "GEM idle failed, resume might fail\n");
  1219. goto out;
  1220. }
  1221. intel_guc_suspend(dev);
  1222. intel_suspend_gt_powersave(dev_priv);
  1223. intel_display_suspend(dev);
  1224. intel_dp_mst_suspend(dev);
  1225. intel_runtime_pm_disable_interrupts(dev_priv);
  1226. intel_hpd_cancel_work(dev_priv);
  1227. intel_suspend_encoders(dev_priv);
  1228. intel_suspend_hw(dev);
  1229. i915_gem_suspend_gtt_mappings(dev);
  1230. i915_save_state(dev);
  1231. opregion_target_state = suspend_to_idle(dev_priv) ? PCI_D1 : PCI_D3cold;
  1232. intel_opregion_notify_adapter(dev_priv, opregion_target_state);
  1233. intel_uncore_forcewake_reset(dev_priv, false);
  1234. intel_opregion_unregister(dev_priv);
  1235. intel_fbdev_set_suspend(dev, FBINFO_STATE_SUSPENDED, true);
  1236. dev_priv->suspend_count++;
  1237. intel_display_set_init_power(dev_priv, false);
  1238. intel_csr_ucode_suspend(dev_priv);
  1239. out:
  1240. enable_rpm_wakeref_asserts(dev_priv);
  1241. return error;
  1242. }
  1243. static int i915_drm_suspend_late(struct drm_device *drm_dev, bool hibernation)
  1244. {
  1245. struct drm_i915_private *dev_priv = to_i915(drm_dev);
  1246. bool fw_csr;
  1247. int ret;
  1248. disable_rpm_wakeref_asserts(dev_priv);
  1249. fw_csr = !IS_BROXTON(dev_priv) &&
  1250. suspend_to_idle(dev_priv) && dev_priv->csr.dmc_payload;
  1251. /*
  1252. * In case of firmware assisted context save/restore don't manually
  1253. * deinit the power domains. This also means the CSR/DMC firmware will
  1254. * stay active, it will power down any HW resources as required and
  1255. * also enable deeper system power states that would be blocked if the
  1256. * firmware was inactive.
  1257. */
  1258. if (!fw_csr)
  1259. intel_power_domains_suspend(dev_priv);
  1260. ret = 0;
  1261. if (IS_BROXTON(dev_priv))
  1262. bxt_enable_dc9(dev_priv);
  1263. else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  1264. hsw_enable_pc8(dev_priv);
  1265. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1266. ret = vlv_suspend_complete(dev_priv);
  1267. if (ret) {
  1268. DRM_ERROR("Suspend complete failed: %d\n", ret);
  1269. if (!fw_csr)
  1270. intel_power_domains_init_hw(dev_priv, true);
  1271. goto out;
  1272. }
  1273. pci_disable_device(drm_dev->pdev);
  1274. /*
  1275. * During hibernation on some platforms the BIOS may try to access
  1276. * the device even though it's already in D3 and hang the machine. So
  1277. * leave the device in D0 on those platforms and hope the BIOS will
  1278. * power down the device properly. The issue was seen on multiple old
  1279. * GENs with different BIOS vendors, so having an explicit blacklist
  1280. * is inpractical; apply the workaround on everything pre GEN6. The
  1281. * platforms where the issue was seen:
  1282. * Lenovo Thinkpad X301, X61s, X60, T60, X41
  1283. * Fujitsu FSC S7110
  1284. * Acer Aspire 1830T
  1285. */
  1286. if (!(hibernation && INTEL_INFO(dev_priv)->gen < 6))
  1287. pci_set_power_state(drm_dev->pdev, PCI_D3hot);
  1288. dev_priv->suspended_to_idle = suspend_to_idle(dev_priv);
  1289. out:
  1290. enable_rpm_wakeref_asserts(dev_priv);
  1291. return ret;
  1292. }
  1293. int i915_suspend_switcheroo(struct drm_device *dev, pm_message_t state)
  1294. {
  1295. int error;
  1296. if (!dev) {
  1297. DRM_ERROR("dev: %p\n", dev);
  1298. DRM_ERROR("DRM not initialized, aborting suspend.\n");
  1299. return -ENODEV;
  1300. }
  1301. if (WARN_ON_ONCE(state.event != PM_EVENT_SUSPEND &&
  1302. state.event != PM_EVENT_FREEZE))
  1303. return -EINVAL;
  1304. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1305. return 0;
  1306. error = i915_drm_suspend(dev);
  1307. if (error)
  1308. return error;
  1309. return i915_drm_suspend_late(dev, false);
  1310. }
  1311. static int i915_drm_resume(struct drm_device *dev)
  1312. {
  1313. struct drm_i915_private *dev_priv = to_i915(dev);
  1314. int ret;
  1315. disable_rpm_wakeref_asserts(dev_priv);
  1316. ret = i915_ggtt_enable_hw(dev);
  1317. if (ret)
  1318. DRM_ERROR("failed to re-enable GGTT\n");
  1319. intel_csr_ucode_resume(dev_priv);
  1320. mutex_lock(&dev->struct_mutex);
  1321. i915_gem_restore_gtt_mappings(dev);
  1322. mutex_unlock(&dev->struct_mutex);
  1323. i915_restore_state(dev);
  1324. intel_opregion_setup(dev_priv);
  1325. intel_init_pch_refclk(dev);
  1326. drm_mode_config_reset(dev);
  1327. /*
  1328. * Interrupts have to be enabled before any batches are run. If not the
  1329. * GPU will hang. i915_gem_init_hw() will initiate batches to
  1330. * update/restore the context.
  1331. *
  1332. * Modeset enabling in intel_modeset_init_hw() also needs working
  1333. * interrupts.
  1334. */
  1335. intel_runtime_pm_enable_interrupts(dev_priv);
  1336. mutex_lock(&dev->struct_mutex);
  1337. if (i915_gem_init_hw(dev)) {
  1338. DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
  1339. atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  1340. }
  1341. mutex_unlock(&dev->struct_mutex);
  1342. intel_guc_resume(dev);
  1343. intel_modeset_init_hw(dev);
  1344. spin_lock_irq(&dev_priv->irq_lock);
  1345. if (dev_priv->display.hpd_irq_setup)
  1346. dev_priv->display.hpd_irq_setup(dev_priv);
  1347. spin_unlock_irq(&dev_priv->irq_lock);
  1348. intel_dp_mst_resume(dev);
  1349. intel_display_resume(dev);
  1350. /*
  1351. * ... but also need to make sure that hotplug processing
  1352. * doesn't cause havoc. Like in the driver load code we don't
  1353. * bother with the tiny race here where we might loose hotplug
  1354. * notifications.
  1355. * */
  1356. intel_hpd_init(dev_priv);
  1357. /* Config may have changed between suspend and resume */
  1358. drm_helper_hpd_irq_event(dev);
  1359. intel_opregion_register(dev_priv);
  1360. intel_fbdev_set_suspend(dev, FBINFO_STATE_RUNNING, false);
  1361. mutex_lock(&dev_priv->modeset_restore_lock);
  1362. dev_priv->modeset_restore = MODESET_DONE;
  1363. mutex_unlock(&dev_priv->modeset_restore_lock);
  1364. intel_opregion_notify_adapter(dev_priv, PCI_D0);
  1365. drm_kms_helper_poll_enable(dev);
  1366. enable_rpm_wakeref_asserts(dev_priv);
  1367. return 0;
  1368. }
  1369. static int i915_drm_resume_early(struct drm_device *dev)
  1370. {
  1371. struct drm_i915_private *dev_priv = to_i915(dev);
  1372. int ret;
  1373. /*
  1374. * We have a resume ordering issue with the snd-hda driver also
  1375. * requiring our device to be power up. Due to the lack of a
  1376. * parent/child relationship we currently solve this with an early
  1377. * resume hook.
  1378. *
  1379. * FIXME: This should be solved with a special hdmi sink device or
  1380. * similar so that power domains can be employed.
  1381. */
  1382. /*
  1383. * Note that we need to set the power state explicitly, since we
  1384. * powered off the device during freeze and the PCI core won't power
  1385. * it back up for us during thaw. Powering off the device during
  1386. * freeze is not a hard requirement though, and during the
  1387. * suspend/resume phases the PCI core makes sure we get here with the
  1388. * device powered on. So in case we change our freeze logic and keep
  1389. * the device powered we can also remove the following set power state
  1390. * call.
  1391. */
  1392. ret = pci_set_power_state(dev->pdev, PCI_D0);
  1393. if (ret) {
  1394. DRM_ERROR("failed to set PCI D0 power state (%d)\n", ret);
  1395. goto out;
  1396. }
  1397. /*
  1398. * Note that pci_enable_device() first enables any parent bridge
  1399. * device and only then sets the power state for this device. The
  1400. * bridge enabling is a nop though, since bridge devices are resumed
  1401. * first. The order of enabling power and enabling the device is
  1402. * imposed by the PCI core as described above, so here we preserve the
  1403. * same order for the freeze/thaw phases.
  1404. *
  1405. * TODO: eventually we should remove pci_disable_device() /
  1406. * pci_enable_enable_device() from suspend/resume. Due to how they
  1407. * depend on the device enable refcount we can't anyway depend on them
  1408. * disabling/enabling the device.
  1409. */
  1410. if (pci_enable_device(dev->pdev)) {
  1411. ret = -EIO;
  1412. goto out;
  1413. }
  1414. pci_set_master(dev->pdev);
  1415. disable_rpm_wakeref_asserts(dev_priv);
  1416. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1417. ret = vlv_resume_prepare(dev_priv, false);
  1418. if (ret)
  1419. DRM_ERROR("Resume prepare failed: %d, continuing anyway\n",
  1420. ret);
  1421. intel_uncore_early_sanitize(dev_priv, true);
  1422. if (IS_BROXTON(dev_priv)) {
  1423. if (!dev_priv->suspended_to_idle)
  1424. gen9_sanitize_dc_state(dev_priv);
  1425. bxt_disable_dc9(dev_priv);
  1426. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  1427. hsw_disable_pc8(dev_priv);
  1428. }
  1429. intel_uncore_sanitize(dev_priv);
  1430. if (IS_BROXTON(dev_priv) ||
  1431. !(dev_priv->suspended_to_idle && dev_priv->csr.dmc_payload))
  1432. intel_power_domains_init_hw(dev_priv, true);
  1433. enable_rpm_wakeref_asserts(dev_priv);
  1434. out:
  1435. dev_priv->suspended_to_idle = false;
  1436. return ret;
  1437. }
  1438. int i915_resume_switcheroo(struct drm_device *dev)
  1439. {
  1440. int ret;
  1441. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1442. return 0;
  1443. ret = i915_drm_resume_early(dev);
  1444. if (ret)
  1445. return ret;
  1446. return i915_drm_resume(dev);
  1447. }
  1448. /**
  1449. * i915_reset - reset chip after a hang
  1450. * @dev: drm device to reset
  1451. *
  1452. * Reset the chip. Useful if a hang is detected. Returns zero on successful
  1453. * reset or otherwise an error code.
  1454. *
  1455. * Procedure is fairly simple:
  1456. * - reset the chip using the reset reg
  1457. * - re-init context state
  1458. * - re-init hardware status page
  1459. * - re-init ring buffer
  1460. * - re-init interrupt state
  1461. * - re-init display
  1462. */
  1463. int i915_reset(struct drm_i915_private *dev_priv)
  1464. {
  1465. struct drm_device *dev = &dev_priv->drm;
  1466. struct i915_gpu_error *error = &dev_priv->gpu_error;
  1467. unsigned reset_counter;
  1468. int ret;
  1469. intel_reset_gt_powersave(dev_priv);
  1470. mutex_lock(&dev->struct_mutex);
  1471. /* Clear any previous failed attempts at recovery. Time to try again. */
  1472. atomic_andnot(I915_WEDGED, &error->reset_counter);
  1473. /* Clear the reset-in-progress flag and increment the reset epoch. */
  1474. reset_counter = atomic_inc_return(&error->reset_counter);
  1475. if (WARN_ON(__i915_reset_in_progress(reset_counter))) {
  1476. ret = -EIO;
  1477. goto error;
  1478. }
  1479. pr_notice("drm/i915: Resetting chip after gpu hang\n");
  1480. i915_gem_reset(dev);
  1481. ret = intel_gpu_reset(dev_priv, ALL_ENGINES);
  1482. if (ret) {
  1483. if (ret != -ENODEV)
  1484. DRM_ERROR("Failed to reset chip: %i\n", ret);
  1485. else
  1486. DRM_DEBUG_DRIVER("GPU reset disabled\n");
  1487. goto error;
  1488. }
  1489. intel_overlay_reset(dev_priv);
  1490. /* Ok, now get things going again... */
  1491. /*
  1492. * Everything depends on having the GTT running, so we need to start
  1493. * there. Fortunately we don't need to do this unless we reset the
  1494. * chip at a PCI level.
  1495. *
  1496. * Next we need to restore the context, but we don't use those
  1497. * yet either...
  1498. *
  1499. * Ring buffer needs to be re-initialized in the KMS case, or if X
  1500. * was running at the time of the reset (i.e. we weren't VT
  1501. * switched away).
  1502. */
  1503. ret = i915_gem_init_hw(dev);
  1504. if (ret) {
  1505. DRM_ERROR("Failed hw init on reset %d\n", ret);
  1506. goto error;
  1507. }
  1508. mutex_unlock(&dev->struct_mutex);
  1509. /*
  1510. * rps/rc6 re-init is necessary to restore state lost after the
  1511. * reset and the re-install of gt irqs. Skip for ironlake per
  1512. * previous concerns that it doesn't respond well to some forms
  1513. * of re-init after reset.
  1514. */
  1515. if (INTEL_INFO(dev)->gen > 5)
  1516. intel_enable_gt_powersave(dev_priv);
  1517. return 0;
  1518. error:
  1519. atomic_or(I915_WEDGED, &error->reset_counter);
  1520. mutex_unlock(&dev->struct_mutex);
  1521. return ret;
  1522. }
  1523. static int i915_pm_suspend(struct device *dev)
  1524. {
  1525. struct pci_dev *pdev = to_pci_dev(dev);
  1526. struct drm_device *drm_dev = pci_get_drvdata(pdev);
  1527. if (!drm_dev) {
  1528. dev_err(dev, "DRM not initialized, aborting suspend.\n");
  1529. return -ENODEV;
  1530. }
  1531. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1532. return 0;
  1533. return i915_drm_suspend(drm_dev);
  1534. }
  1535. static int i915_pm_suspend_late(struct device *dev)
  1536. {
  1537. struct drm_device *drm_dev = &dev_to_i915(dev)->drm;
  1538. /*
  1539. * We have a suspend ordering issue with the snd-hda driver also
  1540. * requiring our device to be power up. Due to the lack of a
  1541. * parent/child relationship we currently solve this with an late
  1542. * suspend hook.
  1543. *
  1544. * FIXME: This should be solved with a special hdmi sink device or
  1545. * similar so that power domains can be employed.
  1546. */
  1547. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1548. return 0;
  1549. return i915_drm_suspend_late(drm_dev, false);
  1550. }
  1551. static int i915_pm_poweroff_late(struct device *dev)
  1552. {
  1553. struct drm_device *drm_dev = &dev_to_i915(dev)->drm;
  1554. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1555. return 0;
  1556. return i915_drm_suspend_late(drm_dev, true);
  1557. }
  1558. static int i915_pm_resume_early(struct device *dev)
  1559. {
  1560. struct drm_device *drm_dev = &dev_to_i915(dev)->drm;
  1561. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1562. return 0;
  1563. return i915_drm_resume_early(drm_dev);
  1564. }
  1565. static int i915_pm_resume(struct device *dev)
  1566. {
  1567. struct drm_device *drm_dev = &dev_to_i915(dev)->drm;
  1568. if (drm_dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  1569. return 0;
  1570. return i915_drm_resume(drm_dev);
  1571. }
  1572. /* freeze: before creating the hibernation_image */
  1573. static int i915_pm_freeze(struct device *dev)
  1574. {
  1575. return i915_pm_suspend(dev);
  1576. }
  1577. static int i915_pm_freeze_late(struct device *dev)
  1578. {
  1579. int ret;
  1580. ret = i915_pm_suspend_late(dev);
  1581. if (ret)
  1582. return ret;
  1583. ret = i915_gem_freeze_late(dev_to_i915(dev));
  1584. if (ret)
  1585. return ret;
  1586. return 0;
  1587. }
  1588. /* thaw: called after creating the hibernation image, but before turning off. */
  1589. static int i915_pm_thaw_early(struct device *dev)
  1590. {
  1591. return i915_pm_resume_early(dev);
  1592. }
  1593. static int i915_pm_thaw(struct device *dev)
  1594. {
  1595. return i915_pm_resume(dev);
  1596. }
  1597. /* restore: called after loading the hibernation image. */
  1598. static int i915_pm_restore_early(struct device *dev)
  1599. {
  1600. return i915_pm_resume_early(dev);
  1601. }
  1602. static int i915_pm_restore(struct device *dev)
  1603. {
  1604. return i915_pm_resume(dev);
  1605. }
  1606. /*
  1607. * Save all Gunit registers that may be lost after a D3 and a subsequent
  1608. * S0i[R123] transition. The list of registers needing a save/restore is
  1609. * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
  1610. * registers in the following way:
  1611. * - Driver: saved/restored by the driver
  1612. * - Punit : saved/restored by the Punit firmware
  1613. * - No, w/o marking: no need to save/restore, since the register is R/O or
  1614. * used internally by the HW in a way that doesn't depend
  1615. * keeping the content across a suspend/resume.
  1616. * - Debug : used for debugging
  1617. *
  1618. * We save/restore all registers marked with 'Driver', with the following
  1619. * exceptions:
  1620. * - Registers out of use, including also registers marked with 'Debug'.
  1621. * These have no effect on the driver's operation, so we don't save/restore
  1622. * them to reduce the overhead.
  1623. * - Registers that are fully setup by an initialization function called from
  1624. * the resume path. For example many clock gating and RPS/RC6 registers.
  1625. * - Registers that provide the right functionality with their reset defaults.
  1626. *
  1627. * TODO: Except for registers that based on the above 3 criteria can be safely
  1628. * ignored, we save/restore all others, practically treating the HW context as
  1629. * a black-box for the driver. Further investigation is needed to reduce the
  1630. * saved/restored registers even further, by following the same 3 criteria.
  1631. */
  1632. static void vlv_save_gunit_s0ix_state(struct drm_i915_private *dev_priv)
  1633. {
  1634. struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
  1635. int i;
  1636. /* GAM 0x4000-0x4770 */
  1637. s->wr_watermark = I915_READ(GEN7_WR_WATERMARK);
  1638. s->gfx_prio_ctrl = I915_READ(GEN7_GFX_PRIO_CTRL);
  1639. s->arb_mode = I915_READ(ARB_MODE);
  1640. s->gfx_pend_tlb0 = I915_READ(GEN7_GFX_PEND_TLB0);
  1641. s->gfx_pend_tlb1 = I915_READ(GEN7_GFX_PEND_TLB1);
  1642. for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
  1643. s->lra_limits[i] = I915_READ(GEN7_LRA_LIMITS(i));
  1644. s->media_max_req_count = I915_READ(GEN7_MEDIA_MAX_REQ_COUNT);
  1645. s->gfx_max_req_count = I915_READ(GEN7_GFX_MAX_REQ_COUNT);
  1646. s->render_hwsp = I915_READ(RENDER_HWS_PGA_GEN7);
  1647. s->ecochk = I915_READ(GAM_ECOCHK);
  1648. s->bsd_hwsp = I915_READ(BSD_HWS_PGA_GEN7);
  1649. s->blt_hwsp = I915_READ(BLT_HWS_PGA_GEN7);
  1650. s->tlb_rd_addr = I915_READ(GEN7_TLB_RD_ADDR);
  1651. /* MBC 0x9024-0x91D0, 0x8500 */
  1652. s->g3dctl = I915_READ(VLV_G3DCTL);
  1653. s->gsckgctl = I915_READ(VLV_GSCKGCTL);
  1654. s->mbctl = I915_READ(GEN6_MBCTL);
  1655. /* GCP 0x9400-0x9424, 0x8100-0x810C */
  1656. s->ucgctl1 = I915_READ(GEN6_UCGCTL1);
  1657. s->ucgctl3 = I915_READ(GEN6_UCGCTL3);
  1658. s->rcgctl1 = I915_READ(GEN6_RCGCTL1);
  1659. s->rcgctl2 = I915_READ(GEN6_RCGCTL2);
  1660. s->rstctl = I915_READ(GEN6_RSTCTL);
  1661. s->misccpctl = I915_READ(GEN7_MISCCPCTL);
  1662. /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
  1663. s->gfxpause = I915_READ(GEN6_GFXPAUSE);
  1664. s->rpdeuhwtc = I915_READ(GEN6_RPDEUHWTC);
  1665. s->rpdeuc = I915_READ(GEN6_RPDEUC);
  1666. s->ecobus = I915_READ(ECOBUS);
  1667. s->pwrdwnupctl = I915_READ(VLV_PWRDWNUPCTL);
  1668. s->rp_down_timeout = I915_READ(GEN6_RP_DOWN_TIMEOUT);
  1669. s->rp_deucsw = I915_READ(GEN6_RPDEUCSW);
  1670. s->rcubmabdtmr = I915_READ(GEN6_RCUBMABDTMR);
  1671. s->rcedata = I915_READ(VLV_RCEDATA);
  1672. s->spare2gh = I915_READ(VLV_SPAREG2H);
  1673. /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
  1674. s->gt_imr = I915_READ(GTIMR);
  1675. s->gt_ier = I915_READ(GTIER);
  1676. s->pm_imr = I915_READ(GEN6_PMIMR);
  1677. s->pm_ier = I915_READ(GEN6_PMIER);
  1678. for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
  1679. s->gt_scratch[i] = I915_READ(GEN7_GT_SCRATCH(i));
  1680. /* GT SA CZ domain, 0x100000-0x138124 */
  1681. s->tilectl = I915_READ(TILECTL);
  1682. s->gt_fifoctl = I915_READ(GTFIFOCTL);
  1683. s->gtlc_wake_ctrl = I915_READ(VLV_GTLC_WAKE_CTRL);
  1684. s->gtlc_survive = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
  1685. s->pmwgicz = I915_READ(VLV_PMWGICZ);
  1686. /* Gunit-Display CZ domain, 0x182028-0x1821CF */
  1687. s->gu_ctl0 = I915_READ(VLV_GU_CTL0);
  1688. s->gu_ctl1 = I915_READ(VLV_GU_CTL1);
  1689. s->pcbr = I915_READ(VLV_PCBR);
  1690. s->clock_gate_dis2 = I915_READ(VLV_GUNIT_CLOCK_GATE2);
  1691. /*
  1692. * Not saving any of:
  1693. * DFT, 0x9800-0x9EC0
  1694. * SARB, 0xB000-0xB1FC
  1695. * GAC, 0x5208-0x524C, 0x14000-0x14C000
  1696. * PCI CFG
  1697. */
  1698. }
  1699. static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *dev_priv)
  1700. {
  1701. struct vlv_s0ix_state *s = &dev_priv->vlv_s0ix_state;
  1702. u32 val;
  1703. int i;
  1704. /* GAM 0x4000-0x4770 */
  1705. I915_WRITE(GEN7_WR_WATERMARK, s->wr_watermark);
  1706. I915_WRITE(GEN7_GFX_PRIO_CTRL, s->gfx_prio_ctrl);
  1707. I915_WRITE(ARB_MODE, s->arb_mode | (0xffff << 16));
  1708. I915_WRITE(GEN7_GFX_PEND_TLB0, s->gfx_pend_tlb0);
  1709. I915_WRITE(GEN7_GFX_PEND_TLB1, s->gfx_pend_tlb1);
  1710. for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
  1711. I915_WRITE(GEN7_LRA_LIMITS(i), s->lra_limits[i]);
  1712. I915_WRITE(GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
  1713. I915_WRITE(GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
  1714. I915_WRITE(RENDER_HWS_PGA_GEN7, s->render_hwsp);
  1715. I915_WRITE(GAM_ECOCHK, s->ecochk);
  1716. I915_WRITE(BSD_HWS_PGA_GEN7, s->bsd_hwsp);
  1717. I915_WRITE(BLT_HWS_PGA_GEN7, s->blt_hwsp);
  1718. I915_WRITE(GEN7_TLB_RD_ADDR, s->tlb_rd_addr);
  1719. /* MBC 0x9024-0x91D0, 0x8500 */
  1720. I915_WRITE(VLV_G3DCTL, s->g3dctl);
  1721. I915_WRITE(VLV_GSCKGCTL, s->gsckgctl);
  1722. I915_WRITE(GEN6_MBCTL, s->mbctl);
  1723. /* GCP 0x9400-0x9424, 0x8100-0x810C */
  1724. I915_WRITE(GEN6_UCGCTL1, s->ucgctl1);
  1725. I915_WRITE(GEN6_UCGCTL3, s->ucgctl3);
  1726. I915_WRITE(GEN6_RCGCTL1, s->rcgctl1);
  1727. I915_WRITE(GEN6_RCGCTL2, s->rcgctl2);
  1728. I915_WRITE(GEN6_RSTCTL, s->rstctl);
  1729. I915_WRITE(GEN7_MISCCPCTL, s->misccpctl);
  1730. /* GPM 0xA000-0xAA84, 0x8000-0x80FC */
  1731. I915_WRITE(GEN6_GFXPAUSE, s->gfxpause);
  1732. I915_WRITE(GEN6_RPDEUHWTC, s->rpdeuhwtc);
  1733. I915_WRITE(GEN6_RPDEUC, s->rpdeuc);
  1734. I915_WRITE(ECOBUS, s->ecobus);
  1735. I915_WRITE(VLV_PWRDWNUPCTL, s->pwrdwnupctl);
  1736. I915_WRITE(GEN6_RP_DOWN_TIMEOUT,s->rp_down_timeout);
  1737. I915_WRITE(GEN6_RPDEUCSW, s->rp_deucsw);
  1738. I915_WRITE(GEN6_RCUBMABDTMR, s->rcubmabdtmr);
  1739. I915_WRITE(VLV_RCEDATA, s->rcedata);
  1740. I915_WRITE(VLV_SPAREG2H, s->spare2gh);
  1741. /* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
  1742. I915_WRITE(GTIMR, s->gt_imr);
  1743. I915_WRITE(GTIER, s->gt_ier);
  1744. I915_WRITE(GEN6_PMIMR, s->pm_imr);
  1745. I915_WRITE(GEN6_PMIER, s->pm_ier);
  1746. for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
  1747. I915_WRITE(GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
  1748. /* GT SA CZ domain, 0x100000-0x138124 */
  1749. I915_WRITE(TILECTL, s->tilectl);
  1750. I915_WRITE(GTFIFOCTL, s->gt_fifoctl);
  1751. /*
  1752. * Preserve the GT allow wake and GFX force clock bit, they are not
  1753. * be restored, as they are used to control the s0ix suspend/resume
  1754. * sequence by the caller.
  1755. */
  1756. val = I915_READ(VLV_GTLC_WAKE_CTRL);
  1757. val &= VLV_GTLC_ALLOWWAKEREQ;
  1758. val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
  1759. I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
  1760. val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
  1761. val &= VLV_GFX_CLK_FORCE_ON_BIT;
  1762. val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
  1763. I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
  1764. I915_WRITE(VLV_PMWGICZ, s->pmwgicz);
  1765. /* Gunit-Display CZ domain, 0x182028-0x1821CF */
  1766. I915_WRITE(VLV_GU_CTL0, s->gu_ctl0);
  1767. I915_WRITE(VLV_GU_CTL1, s->gu_ctl1);
  1768. I915_WRITE(VLV_PCBR, s->pcbr);
  1769. I915_WRITE(VLV_GUNIT_CLOCK_GATE2, s->clock_gate_dis2);
  1770. }
  1771. int vlv_force_gfx_clock(struct drm_i915_private *dev_priv, bool force_on)
  1772. {
  1773. u32 val;
  1774. int err;
  1775. val = I915_READ(VLV_GTLC_SURVIVABILITY_REG);
  1776. val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
  1777. if (force_on)
  1778. val |= VLV_GFX_CLK_FORCE_ON_BIT;
  1779. I915_WRITE(VLV_GTLC_SURVIVABILITY_REG, val);
  1780. if (!force_on)
  1781. return 0;
  1782. err = intel_wait_for_register(dev_priv,
  1783. VLV_GTLC_SURVIVABILITY_REG,
  1784. VLV_GFX_CLK_STATUS_BIT,
  1785. VLV_GFX_CLK_STATUS_BIT,
  1786. 20);
  1787. if (err)
  1788. DRM_ERROR("timeout waiting for GFX clock force-on (%08x)\n",
  1789. I915_READ(VLV_GTLC_SURVIVABILITY_REG));
  1790. return err;
  1791. }
  1792. static int vlv_allow_gt_wake(struct drm_i915_private *dev_priv, bool allow)
  1793. {
  1794. u32 val;
  1795. int err = 0;
  1796. val = I915_READ(VLV_GTLC_WAKE_CTRL);
  1797. val &= ~VLV_GTLC_ALLOWWAKEREQ;
  1798. if (allow)
  1799. val |= VLV_GTLC_ALLOWWAKEREQ;
  1800. I915_WRITE(VLV_GTLC_WAKE_CTRL, val);
  1801. POSTING_READ(VLV_GTLC_WAKE_CTRL);
  1802. err = intel_wait_for_register(dev_priv,
  1803. VLV_GTLC_PW_STATUS,
  1804. VLV_GTLC_ALLOWWAKEACK,
  1805. allow,
  1806. 1);
  1807. if (err)
  1808. DRM_ERROR("timeout disabling GT waking\n");
  1809. return err;
  1810. }
  1811. static int vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
  1812. bool wait_for_on)
  1813. {
  1814. u32 mask;
  1815. u32 val;
  1816. int err;
  1817. mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
  1818. val = wait_for_on ? mask : 0;
  1819. if ((I915_READ(VLV_GTLC_PW_STATUS) & mask) == val)
  1820. return 0;
  1821. DRM_DEBUG_KMS("waiting for GT wells to go %s (%08x)\n",
  1822. onoff(wait_for_on),
  1823. I915_READ(VLV_GTLC_PW_STATUS));
  1824. /*
  1825. * RC6 transitioning can be delayed up to 2 msec (see
  1826. * valleyview_enable_rps), use 3 msec for safety.
  1827. */
  1828. err = intel_wait_for_register(dev_priv,
  1829. VLV_GTLC_PW_STATUS, mask, val,
  1830. 3);
  1831. if (err)
  1832. DRM_ERROR("timeout waiting for GT wells to go %s\n",
  1833. onoff(wait_for_on));
  1834. return err;
  1835. }
  1836. static void vlv_check_no_gt_access(struct drm_i915_private *dev_priv)
  1837. {
  1838. if (!(I915_READ(VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
  1839. return;
  1840. DRM_DEBUG_DRIVER("GT register access while GT waking disabled\n");
  1841. I915_WRITE(VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
  1842. }
  1843. static int vlv_suspend_complete(struct drm_i915_private *dev_priv)
  1844. {
  1845. u32 mask;
  1846. int err;
  1847. /*
  1848. * Bspec defines the following GT well on flags as debug only, so
  1849. * don't treat them as hard failures.
  1850. */
  1851. (void)vlv_wait_for_gt_wells(dev_priv, false);
  1852. mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
  1853. WARN_ON((I915_READ(VLV_GTLC_WAKE_CTRL) & mask) != mask);
  1854. vlv_check_no_gt_access(dev_priv);
  1855. err = vlv_force_gfx_clock(dev_priv, true);
  1856. if (err)
  1857. goto err1;
  1858. err = vlv_allow_gt_wake(dev_priv, false);
  1859. if (err)
  1860. goto err2;
  1861. if (!IS_CHERRYVIEW(dev_priv))
  1862. vlv_save_gunit_s0ix_state(dev_priv);
  1863. err = vlv_force_gfx_clock(dev_priv, false);
  1864. if (err)
  1865. goto err2;
  1866. return 0;
  1867. err2:
  1868. /* For safety always re-enable waking and disable gfx clock forcing */
  1869. vlv_allow_gt_wake(dev_priv, true);
  1870. err1:
  1871. vlv_force_gfx_clock(dev_priv, false);
  1872. return err;
  1873. }
  1874. static int vlv_resume_prepare(struct drm_i915_private *dev_priv,
  1875. bool rpm_resume)
  1876. {
  1877. struct drm_device *dev = &dev_priv->drm;
  1878. int err;
  1879. int ret;
  1880. /*
  1881. * If any of the steps fail just try to continue, that's the best we
  1882. * can do at this point. Return the first error code (which will also
  1883. * leave RPM permanently disabled).
  1884. */
  1885. ret = vlv_force_gfx_clock(dev_priv, true);
  1886. if (!IS_CHERRYVIEW(dev_priv))
  1887. vlv_restore_gunit_s0ix_state(dev_priv);
  1888. err = vlv_allow_gt_wake(dev_priv, true);
  1889. if (!ret)
  1890. ret = err;
  1891. err = vlv_force_gfx_clock(dev_priv, false);
  1892. if (!ret)
  1893. ret = err;
  1894. vlv_check_no_gt_access(dev_priv);
  1895. if (rpm_resume) {
  1896. intel_init_clock_gating(dev);
  1897. i915_gem_restore_fences(dev);
  1898. }
  1899. return ret;
  1900. }
  1901. static int intel_runtime_suspend(struct device *device)
  1902. {
  1903. struct pci_dev *pdev = to_pci_dev(device);
  1904. struct drm_device *dev = pci_get_drvdata(pdev);
  1905. struct drm_i915_private *dev_priv = to_i915(dev);
  1906. int ret;
  1907. if (WARN_ON_ONCE(!(dev_priv->rps.enabled && intel_enable_rc6())))
  1908. return -ENODEV;
  1909. if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev)))
  1910. return -ENODEV;
  1911. DRM_DEBUG_KMS("Suspending device\n");
  1912. /*
  1913. * We could deadlock here in case another thread holding struct_mutex
  1914. * calls RPM suspend concurrently, since the RPM suspend will wait
  1915. * first for this RPM suspend to finish. In this case the concurrent
  1916. * RPM resume will be followed by its RPM suspend counterpart. Still
  1917. * for consistency return -EAGAIN, which will reschedule this suspend.
  1918. */
  1919. if (!mutex_trylock(&dev->struct_mutex)) {
  1920. DRM_DEBUG_KMS("device lock contention, deffering suspend\n");
  1921. /*
  1922. * Bump the expiration timestamp, otherwise the suspend won't
  1923. * be rescheduled.
  1924. */
  1925. pm_runtime_mark_last_busy(device);
  1926. return -EAGAIN;
  1927. }
  1928. disable_rpm_wakeref_asserts(dev_priv);
  1929. /*
  1930. * We are safe here against re-faults, since the fault handler takes
  1931. * an RPM reference.
  1932. */
  1933. i915_gem_release_all_mmaps(dev_priv);
  1934. mutex_unlock(&dev->struct_mutex);
  1935. intel_guc_suspend(dev);
  1936. intel_runtime_pm_disable_interrupts(dev_priv);
  1937. ret = 0;
  1938. if (IS_BROXTON(dev_priv)) {
  1939. bxt_display_core_uninit(dev_priv);
  1940. bxt_enable_dc9(dev_priv);
  1941. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  1942. hsw_enable_pc8(dev_priv);
  1943. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1944. ret = vlv_suspend_complete(dev_priv);
  1945. }
  1946. if (ret) {
  1947. DRM_ERROR("Runtime suspend failed, disabling it (%d)\n", ret);
  1948. intel_runtime_pm_enable_interrupts(dev_priv);
  1949. enable_rpm_wakeref_asserts(dev_priv);
  1950. return ret;
  1951. }
  1952. intel_uncore_forcewake_reset(dev_priv, false);
  1953. enable_rpm_wakeref_asserts(dev_priv);
  1954. WARN_ON_ONCE(atomic_read(&dev_priv->pm.wakeref_count));
  1955. if (intel_uncore_arm_unclaimed_mmio_detection(dev_priv))
  1956. DRM_ERROR("Unclaimed access detected prior to suspending\n");
  1957. dev_priv->pm.suspended = true;
  1958. /*
  1959. * FIXME: We really should find a document that references the arguments
  1960. * used below!
  1961. */
  1962. if (IS_BROADWELL(dev_priv)) {
  1963. /*
  1964. * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
  1965. * being detected, and the call we do at intel_runtime_resume()
  1966. * won't be able to restore them. Since PCI_D3hot matches the
  1967. * actual specification and appears to be working, use it.
  1968. */
  1969. intel_opregion_notify_adapter(dev_priv, PCI_D3hot);
  1970. } else {
  1971. /*
  1972. * current versions of firmware which depend on this opregion
  1973. * notification have repurposed the D1 definition to mean
  1974. * "runtime suspended" vs. what you would normally expect (D3)
  1975. * to distinguish it from notifications that might be sent via
  1976. * the suspend path.
  1977. */
  1978. intel_opregion_notify_adapter(dev_priv, PCI_D1);
  1979. }
  1980. assert_forcewakes_inactive(dev_priv);
  1981. if (!IS_VALLEYVIEW(dev_priv) || !IS_CHERRYVIEW(dev_priv))
  1982. intel_hpd_poll_init(dev_priv);
  1983. DRM_DEBUG_KMS("Device suspended\n");
  1984. return 0;
  1985. }
  1986. static int intel_runtime_resume(struct device *device)
  1987. {
  1988. struct pci_dev *pdev = to_pci_dev(device);
  1989. struct drm_device *dev = pci_get_drvdata(pdev);
  1990. struct drm_i915_private *dev_priv = to_i915(dev);
  1991. int ret = 0;
  1992. if (WARN_ON_ONCE(!HAS_RUNTIME_PM(dev)))
  1993. return -ENODEV;
  1994. DRM_DEBUG_KMS("Resuming device\n");
  1995. WARN_ON_ONCE(atomic_read(&dev_priv->pm.wakeref_count));
  1996. disable_rpm_wakeref_asserts(dev_priv);
  1997. intel_opregion_notify_adapter(dev_priv, PCI_D0);
  1998. dev_priv->pm.suspended = false;
  1999. if (intel_uncore_unclaimed_mmio(dev_priv))
  2000. DRM_DEBUG_DRIVER("Unclaimed access during suspend, bios?\n");
  2001. intel_guc_resume(dev);
  2002. if (IS_GEN6(dev_priv))
  2003. intel_init_pch_refclk(dev);
  2004. if (IS_BROXTON(dev)) {
  2005. bxt_disable_dc9(dev_priv);
  2006. bxt_display_core_init(dev_priv, true);
  2007. if (dev_priv->csr.dmc_payload &&
  2008. (dev_priv->csr.allowed_dc_mask & DC_STATE_EN_UPTO_DC5))
  2009. gen9_enable_dc5(dev_priv);
  2010. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2011. hsw_disable_pc8(dev_priv);
  2012. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  2013. ret = vlv_resume_prepare(dev_priv, true);
  2014. }
  2015. /*
  2016. * No point of rolling back things in case of an error, as the best
  2017. * we can do is to hope that things will still work (and disable RPM).
  2018. */
  2019. i915_gem_init_swizzling(dev);
  2020. gen6_update_ring_freq(dev_priv);
  2021. intel_runtime_pm_enable_interrupts(dev_priv);
  2022. /*
  2023. * On VLV/CHV display interrupts are part of the display
  2024. * power well, so hpd is reinitialized from there. For
  2025. * everyone else do it here.
  2026. */
  2027. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
  2028. intel_hpd_init(dev_priv);
  2029. enable_rpm_wakeref_asserts(dev_priv);
  2030. if (ret)
  2031. DRM_ERROR("Runtime resume failed, disabling it (%d)\n", ret);
  2032. else
  2033. DRM_DEBUG_KMS("Device resumed\n");
  2034. return ret;
  2035. }
  2036. const struct dev_pm_ops i915_pm_ops = {
  2037. /*
  2038. * S0ix (via system suspend) and S3 event handlers [PMSG_SUSPEND,
  2039. * PMSG_RESUME]
  2040. */
  2041. .suspend = i915_pm_suspend,
  2042. .suspend_late = i915_pm_suspend_late,
  2043. .resume_early = i915_pm_resume_early,
  2044. .resume = i915_pm_resume,
  2045. /*
  2046. * S4 event handlers
  2047. * @freeze, @freeze_late : called (1) before creating the
  2048. * hibernation image [PMSG_FREEZE] and
  2049. * (2) after rebooting, before restoring
  2050. * the image [PMSG_QUIESCE]
  2051. * @thaw, @thaw_early : called (1) after creating the hibernation
  2052. * image, before writing it [PMSG_THAW]
  2053. * and (2) after failing to create or
  2054. * restore the image [PMSG_RECOVER]
  2055. * @poweroff, @poweroff_late: called after writing the hibernation
  2056. * image, before rebooting [PMSG_HIBERNATE]
  2057. * @restore, @restore_early : called after rebooting and restoring the
  2058. * hibernation image [PMSG_RESTORE]
  2059. */
  2060. .freeze = i915_pm_freeze,
  2061. .freeze_late = i915_pm_freeze_late,
  2062. .thaw_early = i915_pm_thaw_early,
  2063. .thaw = i915_pm_thaw,
  2064. .poweroff = i915_pm_suspend,
  2065. .poweroff_late = i915_pm_poweroff_late,
  2066. .restore_early = i915_pm_restore_early,
  2067. .restore = i915_pm_restore,
  2068. /* S0ix (via runtime suspend) event handlers */
  2069. .runtime_suspend = intel_runtime_suspend,
  2070. .runtime_resume = intel_runtime_resume,
  2071. };
  2072. static const struct vm_operations_struct i915_gem_vm_ops = {
  2073. .fault = i915_gem_fault,
  2074. .open = drm_gem_vm_open,
  2075. .close = drm_gem_vm_close,
  2076. };
  2077. static const struct file_operations i915_driver_fops = {
  2078. .owner = THIS_MODULE,
  2079. .open = drm_open,
  2080. .release = drm_release,
  2081. .unlocked_ioctl = drm_ioctl,
  2082. .mmap = drm_gem_mmap,
  2083. .poll = drm_poll,
  2084. .read = drm_read,
  2085. #ifdef CONFIG_COMPAT
  2086. .compat_ioctl = i915_compat_ioctl,
  2087. #endif
  2088. .llseek = noop_llseek,
  2089. };
  2090. static int
  2091. i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
  2092. struct drm_file *file)
  2093. {
  2094. return -ENODEV;
  2095. }
  2096. static const struct drm_ioctl_desc i915_ioctls[] = {
  2097. DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2098. DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
  2099. DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
  2100. DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
  2101. DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
  2102. DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
  2103. DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
  2104. DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2105. DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
  2106. DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
  2107. DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2108. DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
  2109. DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2110. DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2111. DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH),
  2112. DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
  2113. DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2114. DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2115. DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
  2116. DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_RENDER_ALLOW),
  2117. DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
  2118. DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
  2119. DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  2120. DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
  2121. DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
  2122. DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  2123. DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2124. DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2125. DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
  2126. DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
  2127. DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
  2128. DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
  2129. DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_RENDER_ALLOW),
  2130. DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
  2131. DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
  2132. DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_RENDER_ALLOW),
  2133. DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_RENDER_ALLOW),
  2134. DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
  2135. DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
  2136. DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
  2137. DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW),
  2138. DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs_ioctl, DRM_MASTER|DRM_CONTROL_ALLOW),
  2139. DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW),
  2140. DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW),
  2141. DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  2142. DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
  2143. DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
  2144. DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
  2145. DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_gem_context_reset_stats_ioctl, DRM_RENDER_ALLOW),
  2146. DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
  2147. DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
  2148. DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
  2149. };
  2150. static struct drm_driver driver = {
  2151. /* Don't use MTRRs here; the Xserver or userspace app should
  2152. * deal with them for Intel hardware.
  2153. */
  2154. .driver_features =
  2155. DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM | DRIVER_PRIME |
  2156. DRIVER_RENDER | DRIVER_MODESET,
  2157. .open = i915_driver_open,
  2158. .lastclose = i915_driver_lastclose,
  2159. .preclose = i915_driver_preclose,
  2160. .postclose = i915_driver_postclose,
  2161. .set_busid = drm_pci_set_busid,
  2162. .gem_free_object = i915_gem_free_object,
  2163. .gem_vm_ops = &i915_gem_vm_ops,
  2164. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  2165. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  2166. .gem_prime_export = i915_gem_prime_export,
  2167. .gem_prime_import = i915_gem_prime_import,
  2168. .dumb_create = i915_gem_dumb_create,
  2169. .dumb_map_offset = i915_gem_mmap_gtt,
  2170. .dumb_destroy = drm_gem_dumb_destroy,
  2171. .ioctls = i915_ioctls,
  2172. .num_ioctls = ARRAY_SIZE(i915_ioctls),
  2173. .fops = &i915_driver_fops,
  2174. .name = DRIVER_NAME,
  2175. .desc = DRIVER_DESC,
  2176. .date = DRIVER_DATE,
  2177. .major = DRIVER_MAJOR,
  2178. .minor = DRIVER_MINOR,
  2179. .patchlevel = DRIVER_PATCHLEVEL,
  2180. };