i915_drv.c 73 KB

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