i915_drv.c 73 KB

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