i915_drv.c 74 KB

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