i915_drv.c 74 KB

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