i915_drv.c 74 KB

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