i915_drv.c 74 KB

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