i915_dma.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  1. /* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
  2. */
  3. /*
  4. * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  22. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  23. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <drm/drmP.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include <drm/drm_fb_helper.h>
  32. #include <drm/drm_legacy.h>
  33. #include "intel_drv.h"
  34. #include <drm/i915_drm.h>
  35. #include "i915_drv.h"
  36. #include "i915_vgpu.h"
  37. #include "i915_trace.h"
  38. #include <linux/pci.h>
  39. #include <linux/console.h>
  40. #include <linux/vt.h>
  41. #include <linux/vgaarb.h>
  42. #include <linux/acpi.h>
  43. #include <linux/pnp.h>
  44. #include <linux/vga_switcheroo.h>
  45. #include <linux/slab.h>
  46. #include <acpi/video.h>
  47. #include <linux/pm.h>
  48. #include <linux/pm_runtime.h>
  49. #include <linux/oom.h>
  50. static unsigned int i915_load_fail_count;
  51. bool __i915_inject_load_failure(const char *func, int line)
  52. {
  53. if (i915_load_fail_count >= i915.inject_load_failure)
  54. return false;
  55. if (++i915_load_fail_count == i915.inject_load_failure) {
  56. DRM_INFO("Injecting failure at checkpoint %u [%s:%d]\n",
  57. i915.inject_load_failure, func, line);
  58. return true;
  59. }
  60. return false;
  61. }
  62. #define FDO_BUG_URL "https://bugs.freedesktop.org/enter_bug.cgi?product=DRI"
  63. #define FDO_BUG_MSG "Please file a bug at " FDO_BUG_URL " against DRM/Intel " \
  64. "providing the dmesg log by booting with drm.debug=0xf"
  65. void
  66. __i915_printk(struct drm_i915_private *dev_priv, const char *level,
  67. const char *fmt, ...)
  68. {
  69. static bool shown_bug_once;
  70. struct device *dev = dev_priv->dev->dev;
  71. bool is_error = level[1] <= KERN_ERR[1];
  72. bool is_debug = level[1] == KERN_DEBUG[1];
  73. struct va_format vaf;
  74. va_list args;
  75. if (is_debug && !(drm_debug & DRM_UT_DRIVER))
  76. return;
  77. va_start(args, fmt);
  78. vaf.fmt = fmt;
  79. vaf.va = &args;
  80. dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
  81. __builtin_return_address(0), &vaf);
  82. if (is_error && !shown_bug_once) {
  83. dev_notice(dev, "%s", FDO_BUG_MSG);
  84. shown_bug_once = true;
  85. }
  86. va_end(args);
  87. }
  88. static bool i915_error_injected(struct drm_i915_private *dev_priv)
  89. {
  90. return i915.inject_load_failure &&
  91. i915_load_fail_count == i915.inject_load_failure;
  92. }
  93. #define i915_load_error(dev_priv, fmt, ...) \
  94. __i915_printk(dev_priv, \
  95. i915_error_injected(dev_priv) ? KERN_DEBUG : KERN_ERR, \
  96. fmt, ##__VA_ARGS__)
  97. static int i915_getparam(struct drm_device *dev, void *data,
  98. struct drm_file *file_priv)
  99. {
  100. struct drm_i915_private *dev_priv = dev->dev_private;
  101. drm_i915_getparam_t *param = data;
  102. int value;
  103. switch (param->param) {
  104. case I915_PARAM_IRQ_ACTIVE:
  105. case I915_PARAM_ALLOW_BATCHBUFFER:
  106. case I915_PARAM_LAST_DISPATCH:
  107. /* Reject all old ums/dri params. */
  108. return -ENODEV;
  109. case I915_PARAM_CHIPSET_ID:
  110. value = dev->pdev->device;
  111. break;
  112. case I915_PARAM_REVISION:
  113. value = dev->pdev->revision;
  114. break;
  115. case I915_PARAM_HAS_GEM:
  116. value = 1;
  117. break;
  118. case I915_PARAM_NUM_FENCES_AVAIL:
  119. value = dev_priv->num_fence_regs;
  120. break;
  121. case I915_PARAM_HAS_OVERLAY:
  122. value = dev_priv->overlay ? 1 : 0;
  123. break;
  124. case I915_PARAM_HAS_PAGEFLIPPING:
  125. value = 1;
  126. break;
  127. case I915_PARAM_HAS_EXECBUF2:
  128. /* depends on GEM */
  129. value = 1;
  130. break;
  131. case I915_PARAM_HAS_BSD:
  132. value = intel_engine_initialized(&dev_priv->engine[VCS]);
  133. break;
  134. case I915_PARAM_HAS_BLT:
  135. value = intel_engine_initialized(&dev_priv->engine[BCS]);
  136. break;
  137. case I915_PARAM_HAS_VEBOX:
  138. value = intel_engine_initialized(&dev_priv->engine[VECS]);
  139. break;
  140. case I915_PARAM_HAS_BSD2:
  141. value = intel_engine_initialized(&dev_priv->engine[VCS2]);
  142. break;
  143. case I915_PARAM_HAS_RELAXED_FENCING:
  144. value = 1;
  145. break;
  146. case I915_PARAM_HAS_COHERENT_RINGS:
  147. value = 1;
  148. break;
  149. case I915_PARAM_HAS_EXEC_CONSTANTS:
  150. value = INTEL_INFO(dev)->gen >= 4;
  151. break;
  152. case I915_PARAM_HAS_RELAXED_DELTA:
  153. value = 1;
  154. break;
  155. case I915_PARAM_HAS_GEN7_SOL_RESET:
  156. value = 1;
  157. break;
  158. case I915_PARAM_HAS_LLC:
  159. value = HAS_LLC(dev);
  160. break;
  161. case I915_PARAM_HAS_WT:
  162. value = HAS_WT(dev);
  163. break;
  164. case I915_PARAM_HAS_ALIASING_PPGTT:
  165. value = USES_PPGTT(dev);
  166. break;
  167. case I915_PARAM_HAS_WAIT_TIMEOUT:
  168. value = 1;
  169. break;
  170. case I915_PARAM_HAS_SEMAPHORES:
  171. value = i915_semaphore_is_enabled(dev);
  172. break;
  173. case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
  174. value = 1;
  175. break;
  176. case I915_PARAM_HAS_SECURE_BATCHES:
  177. value = capable(CAP_SYS_ADMIN);
  178. break;
  179. case I915_PARAM_HAS_PINNED_BATCHES:
  180. value = 1;
  181. break;
  182. case I915_PARAM_HAS_EXEC_NO_RELOC:
  183. value = 1;
  184. break;
  185. case I915_PARAM_HAS_EXEC_HANDLE_LUT:
  186. value = 1;
  187. break;
  188. case I915_PARAM_CMD_PARSER_VERSION:
  189. value = i915_cmd_parser_get_version();
  190. break;
  191. case I915_PARAM_HAS_COHERENT_PHYS_GTT:
  192. value = 1;
  193. break;
  194. case I915_PARAM_MMAP_VERSION:
  195. value = 1;
  196. break;
  197. case I915_PARAM_SUBSLICE_TOTAL:
  198. value = INTEL_INFO(dev)->subslice_total;
  199. if (!value)
  200. return -ENODEV;
  201. break;
  202. case I915_PARAM_EU_TOTAL:
  203. value = INTEL_INFO(dev)->eu_total;
  204. if (!value)
  205. return -ENODEV;
  206. break;
  207. case I915_PARAM_HAS_GPU_RESET:
  208. value = i915.enable_hangcheck &&
  209. intel_has_gpu_reset(dev);
  210. break;
  211. case I915_PARAM_HAS_RESOURCE_STREAMER:
  212. value = HAS_RESOURCE_STREAMER(dev);
  213. break;
  214. case I915_PARAM_HAS_EXEC_SOFTPIN:
  215. value = 1;
  216. break;
  217. default:
  218. DRM_DEBUG("Unknown parameter %d\n", param->param);
  219. return -EINVAL;
  220. }
  221. if (copy_to_user(param->value, &value, sizeof(int))) {
  222. DRM_ERROR("copy_to_user failed\n");
  223. return -EFAULT;
  224. }
  225. return 0;
  226. }
  227. static int i915_get_bridge_dev(struct drm_device *dev)
  228. {
  229. struct drm_i915_private *dev_priv = dev->dev_private;
  230. dev_priv->bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  231. if (!dev_priv->bridge_dev) {
  232. DRM_ERROR("bridge device not found\n");
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. /* Allocate space for the MCH regs if needed, return nonzero on error */
  238. static int
  239. intel_alloc_mchbar_resource(struct drm_device *dev)
  240. {
  241. struct drm_i915_private *dev_priv = dev->dev_private;
  242. int reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
  243. u32 temp_lo, temp_hi = 0;
  244. u64 mchbar_addr;
  245. int ret;
  246. if (INTEL_INFO(dev)->gen >= 4)
  247. pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
  248. pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
  249. mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
  250. /* If ACPI doesn't have it, assume we need to allocate it ourselves */
  251. #ifdef CONFIG_PNP
  252. if (mchbar_addr &&
  253. pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE))
  254. return 0;
  255. #endif
  256. /* Get some space for it */
  257. dev_priv->mch_res.name = "i915 MCHBAR";
  258. dev_priv->mch_res.flags = IORESOURCE_MEM;
  259. ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus,
  260. &dev_priv->mch_res,
  261. MCHBAR_SIZE, MCHBAR_SIZE,
  262. PCIBIOS_MIN_MEM,
  263. 0, pcibios_align_resource,
  264. dev_priv->bridge_dev);
  265. if (ret) {
  266. DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
  267. dev_priv->mch_res.start = 0;
  268. return ret;
  269. }
  270. if (INTEL_INFO(dev)->gen >= 4)
  271. pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
  272. upper_32_bits(dev_priv->mch_res.start));
  273. pci_write_config_dword(dev_priv->bridge_dev, reg,
  274. lower_32_bits(dev_priv->mch_res.start));
  275. return 0;
  276. }
  277. /* Setup MCHBAR if possible, return true if we should disable it again */
  278. static void
  279. intel_setup_mchbar(struct drm_device *dev)
  280. {
  281. struct drm_i915_private *dev_priv = dev->dev_private;
  282. int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
  283. u32 temp;
  284. bool enabled;
  285. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  286. return;
  287. dev_priv->mchbar_need_disable = false;
  288. if (IS_I915G(dev) || IS_I915GM(dev)) {
  289. pci_read_config_dword(dev_priv->bridge_dev, DEVEN, &temp);
  290. enabled = !!(temp & DEVEN_MCHBAR_EN);
  291. } else {
  292. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
  293. enabled = temp & 1;
  294. }
  295. /* If it's already enabled, don't have to do anything */
  296. if (enabled)
  297. return;
  298. if (intel_alloc_mchbar_resource(dev))
  299. return;
  300. dev_priv->mchbar_need_disable = true;
  301. /* Space is allocated or reserved, so enable it. */
  302. if (IS_I915G(dev) || IS_I915GM(dev)) {
  303. pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
  304. temp | DEVEN_MCHBAR_EN);
  305. } else {
  306. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
  307. pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
  308. }
  309. }
  310. static void
  311. intel_teardown_mchbar(struct drm_device *dev)
  312. {
  313. struct drm_i915_private *dev_priv = dev->dev_private;
  314. int mchbar_reg = INTEL_INFO(dev)->gen >= 4 ? MCHBAR_I965 : MCHBAR_I915;
  315. if (dev_priv->mchbar_need_disable) {
  316. if (IS_I915G(dev) || IS_I915GM(dev)) {
  317. u32 deven_val;
  318. pci_read_config_dword(dev_priv->bridge_dev, DEVEN,
  319. &deven_val);
  320. deven_val &= ~DEVEN_MCHBAR_EN;
  321. pci_write_config_dword(dev_priv->bridge_dev, DEVEN,
  322. deven_val);
  323. } else {
  324. u32 mchbar_val;
  325. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg,
  326. &mchbar_val);
  327. mchbar_val &= ~1;
  328. pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg,
  329. mchbar_val);
  330. }
  331. }
  332. if (dev_priv->mch_res.start)
  333. release_resource(&dev_priv->mch_res);
  334. }
  335. /* true = enable decode, false = disable decoder */
  336. static unsigned int i915_vga_set_decode(void *cookie, bool state)
  337. {
  338. struct drm_device *dev = cookie;
  339. intel_modeset_vga_set_state(dev, state);
  340. if (state)
  341. return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
  342. VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  343. else
  344. return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
  345. }
  346. static void i915_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
  347. {
  348. struct drm_device *dev = pci_get_drvdata(pdev);
  349. pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
  350. if (state == VGA_SWITCHEROO_ON) {
  351. pr_info("switched on\n");
  352. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  353. /* i915 resume handler doesn't set to D0 */
  354. pci_set_power_state(dev->pdev, PCI_D0);
  355. i915_resume_switcheroo(dev);
  356. dev->switch_power_state = DRM_SWITCH_POWER_ON;
  357. } else {
  358. pr_info("switched off\n");
  359. dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
  360. i915_suspend_switcheroo(dev, pmm);
  361. dev->switch_power_state = DRM_SWITCH_POWER_OFF;
  362. }
  363. }
  364. static bool i915_switcheroo_can_switch(struct pci_dev *pdev)
  365. {
  366. struct drm_device *dev = pci_get_drvdata(pdev);
  367. /*
  368. * FIXME: open_count is protected by drm_global_mutex but that would lead to
  369. * locking inversion with the driver load path. And the access here is
  370. * completely racy anyway. So don't bother with locking for now.
  371. */
  372. return dev->open_count == 0;
  373. }
  374. static const struct vga_switcheroo_client_ops i915_switcheroo_ops = {
  375. .set_gpu_state = i915_switcheroo_set_state,
  376. .reprobe = NULL,
  377. .can_switch = i915_switcheroo_can_switch,
  378. };
  379. static int i915_load_modeset_init(struct drm_device *dev)
  380. {
  381. struct drm_i915_private *dev_priv = dev->dev_private;
  382. int ret;
  383. if (i915_inject_load_failure())
  384. return -ENODEV;
  385. ret = intel_bios_init(dev_priv);
  386. if (ret)
  387. DRM_INFO("failed to find VBIOS tables\n");
  388. /* If we have > 1 VGA cards, then we need to arbitrate access
  389. * to the common VGA resources.
  390. *
  391. * If we are a secondary display controller (!PCI_DISPLAY_CLASS_VGA),
  392. * then we do not take part in VGA arbitration and the
  393. * vga_client_register() fails with -ENODEV.
  394. */
  395. ret = vga_client_register(dev->pdev, dev, NULL, i915_vga_set_decode);
  396. if (ret && ret != -ENODEV)
  397. goto out;
  398. intel_register_dsm_handler();
  399. ret = vga_switcheroo_register_client(dev->pdev, &i915_switcheroo_ops, false);
  400. if (ret)
  401. goto cleanup_vga_client;
  402. intel_power_domains_init_hw(dev_priv, false);
  403. intel_csr_ucode_init(dev_priv);
  404. ret = intel_irq_install(dev_priv);
  405. if (ret)
  406. goto cleanup_csr;
  407. intel_setup_gmbus(dev);
  408. /* Important: The output setup functions called by modeset_init need
  409. * working irqs for e.g. gmbus and dp aux transfers. */
  410. intel_modeset_init(dev);
  411. intel_guc_ucode_init(dev);
  412. ret = i915_gem_init(dev);
  413. if (ret)
  414. goto cleanup_irq;
  415. intel_modeset_gem_init(dev);
  416. if (INTEL_INFO(dev)->num_pipes == 0)
  417. return 0;
  418. ret = intel_fbdev_init(dev);
  419. if (ret)
  420. goto cleanup_gem;
  421. /* Only enable hotplug handling once the fbdev is fully set up. */
  422. intel_hpd_init(dev_priv);
  423. /*
  424. * Some ports require correctly set-up hpd registers for detection to
  425. * work properly (leading to ghost connected connector status), e.g. VGA
  426. * on gm45. Hence we can only set up the initial fbdev config after hpd
  427. * irqs are fully enabled. Now we should scan for the initial config
  428. * only once hotplug handling is enabled, but due to screwed-up locking
  429. * around kms/fbdev init we can't protect the fdbev initial config
  430. * scanning against hotplug events. Hence do this first and ignore the
  431. * tiny window where we will loose hotplug notifactions.
  432. */
  433. intel_fbdev_initial_config_async(dev);
  434. drm_kms_helper_poll_init(dev);
  435. return 0;
  436. cleanup_gem:
  437. mutex_lock(&dev->struct_mutex);
  438. i915_gem_cleanup_engines(dev);
  439. i915_gem_context_fini(dev);
  440. mutex_unlock(&dev->struct_mutex);
  441. cleanup_irq:
  442. intel_guc_ucode_fini(dev);
  443. drm_irq_uninstall(dev);
  444. intel_teardown_gmbus(dev);
  445. cleanup_csr:
  446. intel_csr_ucode_fini(dev_priv);
  447. intel_power_domains_fini(dev_priv);
  448. vga_switcheroo_unregister_client(dev->pdev);
  449. cleanup_vga_client:
  450. vga_client_register(dev->pdev, NULL, NULL, NULL);
  451. out:
  452. return ret;
  453. }
  454. #if IS_ENABLED(CONFIG_FB)
  455. static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
  456. {
  457. struct apertures_struct *ap;
  458. struct pci_dev *pdev = dev_priv->dev->pdev;
  459. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  460. bool primary;
  461. int ret;
  462. ap = alloc_apertures(1);
  463. if (!ap)
  464. return -ENOMEM;
  465. ap->ranges[0].base = ggtt->mappable_base;
  466. ap->ranges[0].size = ggtt->mappable_end;
  467. primary =
  468. pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
  469. ret = remove_conflicting_framebuffers(ap, "inteldrmfb", primary);
  470. kfree(ap);
  471. return ret;
  472. }
  473. #else
  474. static int i915_kick_out_firmware_fb(struct drm_i915_private *dev_priv)
  475. {
  476. return 0;
  477. }
  478. #endif
  479. #if !defined(CONFIG_VGA_CONSOLE)
  480. static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
  481. {
  482. return 0;
  483. }
  484. #elif !defined(CONFIG_DUMMY_CONSOLE)
  485. static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
  486. {
  487. return -ENODEV;
  488. }
  489. #else
  490. static int i915_kick_out_vgacon(struct drm_i915_private *dev_priv)
  491. {
  492. int ret = 0;
  493. DRM_INFO("Replacing VGA console driver\n");
  494. console_lock();
  495. if (con_is_bound(&vga_con))
  496. ret = do_take_over_console(&dummy_con, 0, MAX_NR_CONSOLES - 1, 1);
  497. if (ret == 0) {
  498. ret = do_unregister_con_driver(&vga_con);
  499. /* Ignore "already unregistered". */
  500. if (ret == -ENODEV)
  501. ret = 0;
  502. }
  503. console_unlock();
  504. return ret;
  505. }
  506. #endif
  507. static void i915_dump_device_info(struct drm_i915_private *dev_priv)
  508. {
  509. const struct intel_device_info *info = &dev_priv->info;
  510. #define PRINT_S(name) "%s"
  511. #define SEP_EMPTY
  512. #define PRINT_FLAG(name) info->name ? #name "," : ""
  513. #define SEP_COMMA ,
  514. DRM_DEBUG_DRIVER("i915 device info: gen=%i, pciid=0x%04x rev=0x%02x flags="
  515. DEV_INFO_FOR_EACH_FLAG(PRINT_S, SEP_EMPTY),
  516. info->gen,
  517. dev_priv->dev->pdev->device,
  518. dev_priv->dev->pdev->revision,
  519. DEV_INFO_FOR_EACH_FLAG(PRINT_FLAG, SEP_COMMA));
  520. #undef PRINT_S
  521. #undef SEP_EMPTY
  522. #undef PRINT_FLAG
  523. #undef SEP_COMMA
  524. }
  525. static void cherryview_sseu_info_init(struct drm_device *dev)
  526. {
  527. struct drm_i915_private *dev_priv = dev->dev_private;
  528. struct intel_device_info *info;
  529. u32 fuse, eu_dis;
  530. info = (struct intel_device_info *)&dev_priv->info;
  531. fuse = I915_READ(CHV_FUSE_GT);
  532. info->slice_total = 1;
  533. if (!(fuse & CHV_FGT_DISABLE_SS0)) {
  534. info->subslice_per_slice++;
  535. eu_dis = fuse & (CHV_FGT_EU_DIS_SS0_R0_MASK |
  536. CHV_FGT_EU_DIS_SS0_R1_MASK);
  537. info->eu_total += 8 - hweight32(eu_dis);
  538. }
  539. if (!(fuse & CHV_FGT_DISABLE_SS1)) {
  540. info->subslice_per_slice++;
  541. eu_dis = fuse & (CHV_FGT_EU_DIS_SS1_R0_MASK |
  542. CHV_FGT_EU_DIS_SS1_R1_MASK);
  543. info->eu_total += 8 - hweight32(eu_dis);
  544. }
  545. info->subslice_total = info->subslice_per_slice;
  546. /*
  547. * CHV expected to always have a uniform distribution of EU
  548. * across subslices.
  549. */
  550. info->eu_per_subslice = info->subslice_total ?
  551. info->eu_total / info->subslice_total :
  552. 0;
  553. /*
  554. * CHV supports subslice power gating on devices with more than
  555. * one subslice, and supports EU power gating on devices with
  556. * more than one EU pair per subslice.
  557. */
  558. info->has_slice_pg = 0;
  559. info->has_subslice_pg = (info->subslice_total > 1);
  560. info->has_eu_pg = (info->eu_per_subslice > 2);
  561. }
  562. static void gen9_sseu_info_init(struct drm_device *dev)
  563. {
  564. struct drm_i915_private *dev_priv = dev->dev_private;
  565. struct intel_device_info *info;
  566. int s_max = 3, ss_max = 4, eu_max = 8;
  567. int s, ss;
  568. u32 fuse2, s_enable, ss_disable, eu_disable;
  569. u8 eu_mask = 0xff;
  570. info = (struct intel_device_info *)&dev_priv->info;
  571. fuse2 = I915_READ(GEN8_FUSE2);
  572. s_enable = (fuse2 & GEN8_F2_S_ENA_MASK) >>
  573. GEN8_F2_S_ENA_SHIFT;
  574. ss_disable = (fuse2 & GEN9_F2_SS_DIS_MASK) >>
  575. GEN9_F2_SS_DIS_SHIFT;
  576. info->slice_total = hweight32(s_enable);
  577. /*
  578. * The subslice disable field is global, i.e. it applies
  579. * to each of the enabled slices.
  580. */
  581. info->subslice_per_slice = ss_max - hweight32(ss_disable);
  582. info->subslice_total = info->slice_total *
  583. info->subslice_per_slice;
  584. /*
  585. * Iterate through enabled slices and subslices to
  586. * count the total enabled EU.
  587. */
  588. for (s = 0; s < s_max; s++) {
  589. if (!(s_enable & (0x1 << s)))
  590. /* skip disabled slice */
  591. continue;
  592. eu_disable = I915_READ(GEN9_EU_DISABLE(s));
  593. for (ss = 0; ss < ss_max; ss++) {
  594. int eu_per_ss;
  595. if (ss_disable & (0x1 << ss))
  596. /* skip disabled subslice */
  597. continue;
  598. eu_per_ss = eu_max - hweight8((eu_disable >> (ss*8)) &
  599. eu_mask);
  600. /*
  601. * Record which subslice(s) has(have) 7 EUs. we
  602. * can tune the hash used to spread work among
  603. * subslices if they are unbalanced.
  604. */
  605. if (eu_per_ss == 7)
  606. info->subslice_7eu[s] |= 1 << ss;
  607. info->eu_total += eu_per_ss;
  608. }
  609. }
  610. /*
  611. * SKL is expected to always have a uniform distribution
  612. * of EU across subslices with the exception that any one
  613. * EU in any one subslice may be fused off for die
  614. * recovery. BXT is expected to be perfectly uniform in EU
  615. * distribution.
  616. */
  617. info->eu_per_subslice = info->subslice_total ?
  618. DIV_ROUND_UP(info->eu_total,
  619. info->subslice_total) : 0;
  620. /*
  621. * SKL supports slice power gating on devices with more than
  622. * one slice, and supports EU power gating on devices with
  623. * more than one EU pair per subslice. BXT supports subslice
  624. * power gating on devices with more than one subslice, and
  625. * supports EU power gating on devices with more than one EU
  626. * pair per subslice.
  627. */
  628. info->has_slice_pg = ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  629. (info->slice_total > 1));
  630. info->has_subslice_pg = (IS_BROXTON(dev) && (info->subslice_total > 1));
  631. info->has_eu_pg = (info->eu_per_subslice > 2);
  632. }
  633. static void broadwell_sseu_info_init(struct drm_device *dev)
  634. {
  635. struct drm_i915_private *dev_priv = dev->dev_private;
  636. struct intel_device_info *info;
  637. const int s_max = 3, ss_max = 3, eu_max = 8;
  638. int s, ss;
  639. u32 fuse2, eu_disable[s_max], s_enable, ss_disable;
  640. fuse2 = I915_READ(GEN8_FUSE2);
  641. s_enable = (fuse2 & GEN8_F2_S_ENA_MASK) >> GEN8_F2_S_ENA_SHIFT;
  642. ss_disable = (fuse2 & GEN8_F2_SS_DIS_MASK) >> GEN8_F2_SS_DIS_SHIFT;
  643. eu_disable[0] = I915_READ(GEN8_EU_DISABLE0) & GEN8_EU_DIS0_S0_MASK;
  644. eu_disable[1] = (I915_READ(GEN8_EU_DISABLE0) >> GEN8_EU_DIS0_S1_SHIFT) |
  645. ((I915_READ(GEN8_EU_DISABLE1) & GEN8_EU_DIS1_S1_MASK) <<
  646. (32 - GEN8_EU_DIS0_S1_SHIFT));
  647. eu_disable[2] = (I915_READ(GEN8_EU_DISABLE1) >> GEN8_EU_DIS1_S2_SHIFT) |
  648. ((I915_READ(GEN8_EU_DISABLE2) & GEN8_EU_DIS2_S2_MASK) <<
  649. (32 - GEN8_EU_DIS1_S2_SHIFT));
  650. info = (struct intel_device_info *)&dev_priv->info;
  651. info->slice_total = hweight32(s_enable);
  652. /*
  653. * The subslice disable field is global, i.e. it applies
  654. * to each of the enabled slices.
  655. */
  656. info->subslice_per_slice = ss_max - hweight32(ss_disable);
  657. info->subslice_total = info->slice_total * info->subslice_per_slice;
  658. /*
  659. * Iterate through enabled slices and subslices to
  660. * count the total enabled EU.
  661. */
  662. for (s = 0; s < s_max; s++) {
  663. if (!(s_enable & (0x1 << s)))
  664. /* skip disabled slice */
  665. continue;
  666. for (ss = 0; ss < ss_max; ss++) {
  667. u32 n_disabled;
  668. if (ss_disable & (0x1 << ss))
  669. /* skip disabled subslice */
  670. continue;
  671. n_disabled = hweight8(eu_disable[s] >> (ss * eu_max));
  672. /*
  673. * Record which subslices have 7 EUs.
  674. */
  675. if (eu_max - n_disabled == 7)
  676. info->subslice_7eu[s] |= 1 << ss;
  677. info->eu_total += eu_max - n_disabled;
  678. }
  679. }
  680. /*
  681. * BDW is expected to always have a uniform distribution of EU across
  682. * subslices with the exception that any one EU in any one subslice may
  683. * be fused off for die recovery.
  684. */
  685. info->eu_per_subslice = info->subslice_total ?
  686. DIV_ROUND_UP(info->eu_total, info->subslice_total) : 0;
  687. /*
  688. * BDW supports slice power gating on devices with more than
  689. * one slice.
  690. */
  691. info->has_slice_pg = (info->slice_total > 1);
  692. info->has_subslice_pg = 0;
  693. info->has_eu_pg = 0;
  694. }
  695. /*
  696. * Determine various intel_device_info fields at runtime.
  697. *
  698. * Use it when either:
  699. * - it's judged too laborious to fill n static structures with the limit
  700. * when a simple if statement does the job,
  701. * - run-time checks (eg read fuse/strap registers) are needed.
  702. *
  703. * This function needs to be called:
  704. * - after the MMIO has been setup as we are reading registers,
  705. * - after the PCH has been detected,
  706. * - before the first usage of the fields it can tweak.
  707. */
  708. static void intel_device_info_runtime_init(struct drm_device *dev)
  709. {
  710. struct drm_i915_private *dev_priv = dev->dev_private;
  711. struct intel_device_info *info;
  712. enum pipe pipe;
  713. info = (struct intel_device_info *)&dev_priv->info;
  714. /*
  715. * Skylake and Broxton currently don't expose the topmost plane as its
  716. * use is exclusive with the legacy cursor and we only want to expose
  717. * one of those, not both. Until we can safely expose the topmost plane
  718. * as a DRM_PLANE_TYPE_CURSOR with all the features exposed/supported,
  719. * we don't expose the topmost plane at all to prevent ABI breakage
  720. * down the line.
  721. */
  722. if (IS_BROXTON(dev)) {
  723. info->num_sprites[PIPE_A] = 2;
  724. info->num_sprites[PIPE_B] = 2;
  725. info->num_sprites[PIPE_C] = 1;
  726. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  727. for_each_pipe(dev_priv, pipe)
  728. info->num_sprites[pipe] = 2;
  729. else
  730. for_each_pipe(dev_priv, pipe)
  731. info->num_sprites[pipe] = 1;
  732. if (i915.disable_display) {
  733. DRM_INFO("Display disabled (module parameter)\n");
  734. info->num_pipes = 0;
  735. } else if (info->num_pipes > 0 &&
  736. (INTEL_INFO(dev)->gen == 7 || INTEL_INFO(dev)->gen == 8) &&
  737. HAS_PCH_SPLIT(dev)) {
  738. u32 fuse_strap = I915_READ(FUSE_STRAP);
  739. u32 sfuse_strap = I915_READ(SFUSE_STRAP);
  740. /*
  741. * SFUSE_STRAP is supposed to have a bit signalling the display
  742. * is fused off. Unfortunately it seems that, at least in
  743. * certain cases, fused off display means that PCH display
  744. * reads don't land anywhere. In that case, we read 0s.
  745. *
  746. * On CPT/PPT, we can detect this case as SFUSE_STRAP_FUSE_LOCK
  747. * should be set when taking over after the firmware.
  748. */
  749. if (fuse_strap & ILK_INTERNAL_DISPLAY_DISABLE ||
  750. sfuse_strap & SFUSE_STRAP_DISPLAY_DISABLED ||
  751. (dev_priv->pch_type == PCH_CPT &&
  752. !(sfuse_strap & SFUSE_STRAP_FUSE_LOCK))) {
  753. DRM_INFO("Display fused off, disabling\n");
  754. info->num_pipes = 0;
  755. } else if (fuse_strap & IVB_PIPE_C_DISABLE) {
  756. DRM_INFO("PipeC fused off\n");
  757. info->num_pipes -= 1;
  758. }
  759. } else if (info->num_pipes > 0 && INTEL_INFO(dev)->gen == 9) {
  760. u32 dfsm = I915_READ(SKL_DFSM);
  761. u8 disabled_mask = 0;
  762. bool invalid;
  763. int num_bits;
  764. if (dfsm & SKL_DFSM_PIPE_A_DISABLE)
  765. disabled_mask |= BIT(PIPE_A);
  766. if (dfsm & SKL_DFSM_PIPE_B_DISABLE)
  767. disabled_mask |= BIT(PIPE_B);
  768. if (dfsm & SKL_DFSM_PIPE_C_DISABLE)
  769. disabled_mask |= BIT(PIPE_C);
  770. num_bits = hweight8(disabled_mask);
  771. switch (disabled_mask) {
  772. case BIT(PIPE_A):
  773. case BIT(PIPE_B):
  774. case BIT(PIPE_A) | BIT(PIPE_B):
  775. case BIT(PIPE_A) | BIT(PIPE_C):
  776. invalid = true;
  777. break;
  778. default:
  779. invalid = false;
  780. }
  781. if (num_bits > info->num_pipes || invalid)
  782. DRM_ERROR("invalid pipe fuse configuration: 0x%x\n",
  783. disabled_mask);
  784. else
  785. info->num_pipes -= num_bits;
  786. }
  787. /* Initialize slice/subslice/EU info */
  788. if (IS_CHERRYVIEW(dev))
  789. cherryview_sseu_info_init(dev);
  790. else if (IS_BROADWELL(dev))
  791. broadwell_sseu_info_init(dev);
  792. else if (INTEL_INFO(dev)->gen >= 9)
  793. gen9_sseu_info_init(dev);
  794. /* Snooping is broken on BXT A stepping. */
  795. info->has_snoop = !info->has_llc;
  796. info->has_snoop &= !IS_BXT_REVID(dev, 0, BXT_REVID_A1);
  797. DRM_DEBUG_DRIVER("slice total: %u\n", info->slice_total);
  798. DRM_DEBUG_DRIVER("subslice total: %u\n", info->subslice_total);
  799. DRM_DEBUG_DRIVER("subslice per slice: %u\n", info->subslice_per_slice);
  800. DRM_DEBUG_DRIVER("EU total: %u\n", info->eu_total);
  801. DRM_DEBUG_DRIVER("EU per subslice: %u\n", info->eu_per_subslice);
  802. DRM_DEBUG_DRIVER("has slice power gating: %s\n",
  803. info->has_slice_pg ? "y" : "n");
  804. DRM_DEBUG_DRIVER("has subslice power gating: %s\n",
  805. info->has_subslice_pg ? "y" : "n");
  806. DRM_DEBUG_DRIVER("has EU power gating: %s\n",
  807. info->has_eu_pg ? "y" : "n");
  808. }
  809. static void intel_init_dpio(struct drm_i915_private *dev_priv)
  810. {
  811. /*
  812. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  813. * CHV x1 PHY (DP/HDMI D)
  814. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  815. */
  816. if (IS_CHERRYVIEW(dev_priv)) {
  817. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  818. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  819. } else if (IS_VALLEYVIEW(dev_priv)) {
  820. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  821. }
  822. }
  823. static int i915_workqueues_init(struct drm_i915_private *dev_priv)
  824. {
  825. /*
  826. * The i915 workqueue is primarily used for batched retirement of
  827. * requests (and thus managing bo) once the task has been completed
  828. * by the GPU. i915_gem_retire_requests() is called directly when we
  829. * need high-priority retirement, such as waiting for an explicit
  830. * bo.
  831. *
  832. * It is also used for periodic low-priority events, such as
  833. * idle-timers and recording error state.
  834. *
  835. * All tasks on the workqueue are expected to acquire the dev mutex
  836. * so there is no point in running more than one instance of the
  837. * workqueue at any time. Use an ordered one.
  838. */
  839. dev_priv->wq = alloc_ordered_workqueue("i915", 0);
  840. if (dev_priv->wq == NULL)
  841. goto out_err;
  842. dev_priv->hotplug.dp_wq = alloc_ordered_workqueue("i915-dp", 0);
  843. if (dev_priv->hotplug.dp_wq == NULL)
  844. goto out_free_wq;
  845. dev_priv->gpu_error.hangcheck_wq =
  846. alloc_ordered_workqueue("i915-hangcheck", 0);
  847. if (dev_priv->gpu_error.hangcheck_wq == NULL)
  848. goto out_free_dp_wq;
  849. return 0;
  850. out_free_dp_wq:
  851. destroy_workqueue(dev_priv->hotplug.dp_wq);
  852. out_free_wq:
  853. destroy_workqueue(dev_priv->wq);
  854. out_err:
  855. DRM_ERROR("Failed to allocate workqueues.\n");
  856. return -ENOMEM;
  857. }
  858. static void i915_workqueues_cleanup(struct drm_i915_private *dev_priv)
  859. {
  860. destroy_workqueue(dev_priv->gpu_error.hangcheck_wq);
  861. destroy_workqueue(dev_priv->hotplug.dp_wq);
  862. destroy_workqueue(dev_priv->wq);
  863. }
  864. /**
  865. * i915_driver_init_early - setup state not requiring device access
  866. * @dev_priv: device private
  867. *
  868. * Initialize everything that is a "SW-only" state, that is state not
  869. * requiring accessing the device or exposing the driver via kernel internal
  870. * or userspace interfaces. Example steps belonging here: lock initialization,
  871. * system memory allocation, setting up device specific attributes and
  872. * function hooks not requiring accessing the device.
  873. */
  874. static int i915_driver_init_early(struct drm_i915_private *dev_priv,
  875. struct drm_device *dev,
  876. struct intel_device_info *info)
  877. {
  878. struct intel_device_info *device_info;
  879. int ret = 0;
  880. if (i915_inject_load_failure())
  881. return -ENODEV;
  882. /* Setup the write-once "constant" device info */
  883. device_info = (struct intel_device_info *)&dev_priv->info;
  884. memcpy(device_info, info, sizeof(dev_priv->info));
  885. device_info->device_id = dev->pdev->device;
  886. spin_lock_init(&dev_priv->irq_lock);
  887. spin_lock_init(&dev_priv->gpu_error.lock);
  888. mutex_init(&dev_priv->backlight_lock);
  889. spin_lock_init(&dev_priv->uncore.lock);
  890. spin_lock_init(&dev_priv->mm.object_stat_lock);
  891. spin_lock_init(&dev_priv->mmio_flip_lock);
  892. mutex_init(&dev_priv->sb_lock);
  893. mutex_init(&dev_priv->modeset_restore_lock);
  894. mutex_init(&dev_priv->av_mutex);
  895. mutex_init(&dev_priv->wm.wm_mutex);
  896. mutex_init(&dev_priv->pps_mutex);
  897. ret = i915_workqueues_init(dev_priv);
  898. if (ret < 0)
  899. return ret;
  900. /* This must be called before any calls to HAS_PCH_* */
  901. intel_detect_pch(dev);
  902. intel_pm_setup(dev);
  903. intel_init_dpio(dev_priv);
  904. intel_power_domains_init(dev_priv);
  905. intel_irq_init(dev_priv);
  906. intel_init_display_hooks(dev_priv);
  907. intel_init_clock_gating_hooks(dev_priv);
  908. intel_init_audio_hooks(dev_priv);
  909. i915_gem_load_init(dev);
  910. intel_display_crc_init(dev);
  911. i915_dump_device_info(dev_priv);
  912. /* Not all pre-production machines fall into this category, only the
  913. * very first ones. Almost everything should work, except for maybe
  914. * suspend/resume. And we don't implement workarounds that affect only
  915. * pre-production machines. */
  916. if (IS_HSW_EARLY_SDV(dev))
  917. DRM_INFO("This is an early pre-production Haswell machine. "
  918. "It may not be fully functional.\n");
  919. return 0;
  920. }
  921. /**
  922. * i915_driver_cleanup_early - cleanup the setup done in i915_driver_init_early()
  923. * @dev_priv: device private
  924. */
  925. static void i915_driver_cleanup_early(struct drm_i915_private *dev_priv)
  926. {
  927. i915_gem_load_cleanup(dev_priv->dev);
  928. i915_workqueues_cleanup(dev_priv);
  929. }
  930. static int i915_mmio_setup(struct drm_device *dev)
  931. {
  932. struct drm_i915_private *dev_priv = to_i915(dev);
  933. int mmio_bar;
  934. int mmio_size;
  935. mmio_bar = IS_GEN2(dev) ? 1 : 0;
  936. /*
  937. * Before gen4, the registers and the GTT are behind different BARs.
  938. * However, from gen4 onwards, the registers and the GTT are shared
  939. * in the same BAR, so we want to restrict this ioremap from
  940. * clobbering the GTT which we want ioremap_wc instead. Fortunately,
  941. * the register BAR remains the same size for all the earlier
  942. * generations up to Ironlake.
  943. */
  944. if (INTEL_INFO(dev)->gen < 5)
  945. mmio_size = 512 * 1024;
  946. else
  947. mmio_size = 2 * 1024 * 1024;
  948. dev_priv->regs = pci_iomap(dev->pdev, mmio_bar, mmio_size);
  949. if (dev_priv->regs == NULL) {
  950. DRM_ERROR("failed to map registers\n");
  951. return -EIO;
  952. }
  953. /* Try to make sure MCHBAR is enabled before poking at it */
  954. intel_setup_mchbar(dev);
  955. return 0;
  956. }
  957. static void i915_mmio_cleanup(struct drm_device *dev)
  958. {
  959. struct drm_i915_private *dev_priv = to_i915(dev);
  960. intel_teardown_mchbar(dev);
  961. pci_iounmap(dev->pdev, dev_priv->regs);
  962. }
  963. /**
  964. * i915_driver_init_mmio - setup device MMIO
  965. * @dev_priv: device private
  966. *
  967. * Setup minimal device state necessary for MMIO accesses later in the
  968. * initialization sequence. The setup here should avoid any other device-wide
  969. * side effects or exposing the driver via kernel internal or user space
  970. * interfaces.
  971. */
  972. static int i915_driver_init_mmio(struct drm_i915_private *dev_priv)
  973. {
  974. struct drm_device *dev = dev_priv->dev;
  975. int ret;
  976. if (i915_inject_load_failure())
  977. return -ENODEV;
  978. if (i915_get_bridge_dev(dev))
  979. return -EIO;
  980. ret = i915_mmio_setup(dev);
  981. if (ret < 0)
  982. goto put_bridge;
  983. intel_uncore_init(dev);
  984. return 0;
  985. put_bridge:
  986. pci_dev_put(dev_priv->bridge_dev);
  987. return ret;
  988. }
  989. /**
  990. * i915_driver_cleanup_mmio - cleanup the setup done in i915_driver_init_mmio()
  991. * @dev_priv: device private
  992. */
  993. static void i915_driver_cleanup_mmio(struct drm_i915_private *dev_priv)
  994. {
  995. struct drm_device *dev = dev_priv->dev;
  996. intel_uncore_fini(dev);
  997. i915_mmio_cleanup(dev);
  998. pci_dev_put(dev_priv->bridge_dev);
  999. }
  1000. /**
  1001. * i915_driver_init_hw - setup state requiring device access
  1002. * @dev_priv: device private
  1003. *
  1004. * Setup state that requires accessing the device, but doesn't require
  1005. * exposing the driver via kernel internal or userspace interfaces.
  1006. */
  1007. static int i915_driver_init_hw(struct drm_i915_private *dev_priv)
  1008. {
  1009. struct drm_device *dev = dev_priv->dev;
  1010. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1011. uint32_t aperture_size;
  1012. int ret;
  1013. if (i915_inject_load_failure())
  1014. return -ENODEV;
  1015. intel_device_info_runtime_init(dev);
  1016. ret = i915_ggtt_init_hw(dev);
  1017. if (ret)
  1018. return ret;
  1019. ret = i915_ggtt_enable_hw(dev);
  1020. if (ret) {
  1021. DRM_ERROR("failed to enable GGTT\n");
  1022. goto out_ggtt;
  1023. }
  1024. /* WARNING: Apparently we must kick fbdev drivers before vgacon,
  1025. * otherwise the vga fbdev driver falls over. */
  1026. ret = i915_kick_out_firmware_fb(dev_priv);
  1027. if (ret) {
  1028. DRM_ERROR("failed to remove conflicting framebuffer drivers\n");
  1029. goto out_ggtt;
  1030. }
  1031. ret = i915_kick_out_vgacon(dev_priv);
  1032. if (ret) {
  1033. DRM_ERROR("failed to remove conflicting VGA console\n");
  1034. goto out_ggtt;
  1035. }
  1036. pci_set_master(dev->pdev);
  1037. /* overlay on gen2 is broken and can't address above 1G */
  1038. if (IS_GEN2(dev))
  1039. dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(30));
  1040. /* 965GM sometimes incorrectly writes to hardware status page (HWS)
  1041. * using 32bit addressing, overwriting memory if HWS is located
  1042. * above 4GB.
  1043. *
  1044. * The documentation also mentions an issue with undefined
  1045. * behaviour if any general state is accessed within a page above 4GB,
  1046. * which also needs to be handled carefully.
  1047. */
  1048. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1049. dma_set_coherent_mask(&dev->pdev->dev, DMA_BIT_MASK(32));
  1050. aperture_size = ggtt->mappable_end;
  1051. ggtt->mappable =
  1052. io_mapping_create_wc(ggtt->mappable_base,
  1053. aperture_size);
  1054. if (!ggtt->mappable) {
  1055. ret = -EIO;
  1056. goto out_ggtt;
  1057. }
  1058. ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base,
  1059. aperture_size);
  1060. pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY,
  1061. PM_QOS_DEFAULT_VALUE);
  1062. intel_uncore_sanitize(dev);
  1063. intel_opregion_setup(dev);
  1064. i915_gem_load_init_fences(dev_priv);
  1065. /* On the 945G/GM, the chipset reports the MSI capability on the
  1066. * integrated graphics even though the support isn't actually there
  1067. * according to the published specs. It doesn't appear to function
  1068. * correctly in testing on 945G.
  1069. * This may be a side effect of MSI having been made available for PEG
  1070. * and the registers being closely associated.
  1071. *
  1072. * According to chipset errata, on the 965GM, MSI interrupts may
  1073. * be lost or delayed, but we use them anyways to avoid
  1074. * stuck interrupts on some machines.
  1075. */
  1076. if (!IS_I945G(dev) && !IS_I945GM(dev)) {
  1077. if (pci_enable_msi(dev->pdev) < 0)
  1078. DRM_DEBUG_DRIVER("can't enable MSI");
  1079. }
  1080. return 0;
  1081. out_ggtt:
  1082. i915_ggtt_cleanup_hw(dev);
  1083. return ret;
  1084. }
  1085. /**
  1086. * i915_driver_cleanup_hw - cleanup the setup done in i915_driver_init_hw()
  1087. * @dev_priv: device private
  1088. */
  1089. static void i915_driver_cleanup_hw(struct drm_i915_private *dev_priv)
  1090. {
  1091. struct drm_device *dev = dev_priv->dev;
  1092. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1093. if (dev->pdev->msi_enabled)
  1094. pci_disable_msi(dev->pdev);
  1095. pm_qos_remove_request(&dev_priv->pm_qos);
  1096. arch_phys_wc_del(ggtt->mtrr);
  1097. io_mapping_free(ggtt->mappable);
  1098. i915_ggtt_cleanup_hw(dev);
  1099. }
  1100. /**
  1101. * i915_driver_register - register the driver with the rest of the system
  1102. * @dev_priv: device private
  1103. *
  1104. * Perform any steps necessary to make the driver available via kernel
  1105. * internal or userspace interfaces.
  1106. */
  1107. static void i915_driver_register(struct drm_i915_private *dev_priv)
  1108. {
  1109. struct drm_device *dev = dev_priv->dev;
  1110. i915_gem_shrinker_init(dev_priv);
  1111. /*
  1112. * Notify a valid surface after modesetting,
  1113. * when running inside a VM.
  1114. */
  1115. if (intel_vgpu_active(dev))
  1116. I915_WRITE(vgtif_reg(display_ready), VGT_DRV_DISPLAY_READY);
  1117. i915_setup_sysfs(dev);
  1118. if (INTEL_INFO(dev_priv)->num_pipes) {
  1119. /* Must be done after probing outputs */
  1120. intel_opregion_init(dev);
  1121. acpi_video_register();
  1122. }
  1123. if (IS_GEN5(dev_priv))
  1124. intel_gpu_ips_init(dev_priv);
  1125. i915_audio_component_init(dev_priv);
  1126. }
  1127. /**
  1128. * i915_driver_unregister - cleanup the registration done in i915_driver_regiser()
  1129. * @dev_priv: device private
  1130. */
  1131. static void i915_driver_unregister(struct drm_i915_private *dev_priv)
  1132. {
  1133. i915_audio_component_cleanup(dev_priv);
  1134. intel_gpu_ips_teardown();
  1135. acpi_video_unregister();
  1136. intel_opregion_fini(dev_priv->dev);
  1137. i915_teardown_sysfs(dev_priv->dev);
  1138. i915_gem_shrinker_cleanup(dev_priv);
  1139. }
  1140. /**
  1141. * i915_driver_load - setup chip and create an initial config
  1142. * @dev: DRM device
  1143. * @flags: startup flags
  1144. *
  1145. * The driver load routine has to do several things:
  1146. * - drive output discovery via intel_modeset_init()
  1147. * - initialize the memory manager
  1148. * - allocate initial config memory
  1149. * - setup the DRM framebuffer with the allocated memory
  1150. */
  1151. int i915_driver_load(struct drm_device *dev, unsigned long flags)
  1152. {
  1153. struct drm_i915_private *dev_priv;
  1154. int ret = 0;
  1155. dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
  1156. if (dev_priv == NULL)
  1157. return -ENOMEM;
  1158. dev->dev_private = dev_priv;
  1159. /* Must be set before calling __i915_printk */
  1160. dev_priv->dev = dev;
  1161. ret = i915_driver_init_early(dev_priv, dev,
  1162. (struct intel_device_info *)flags);
  1163. if (ret < 0)
  1164. goto out_free_priv;
  1165. intel_runtime_pm_get(dev_priv);
  1166. ret = i915_driver_init_mmio(dev_priv);
  1167. if (ret < 0)
  1168. goto out_runtime_pm_put;
  1169. ret = i915_driver_init_hw(dev_priv);
  1170. if (ret < 0)
  1171. goto out_cleanup_mmio;
  1172. /*
  1173. * TODO: move the vblank init and parts of modeset init steps into one
  1174. * of the i915_driver_init_/i915_driver_register functions according
  1175. * to the role/effect of the given init step.
  1176. */
  1177. if (INTEL_INFO(dev)->num_pipes) {
  1178. ret = drm_vblank_init(dev, INTEL_INFO(dev)->num_pipes);
  1179. if (ret)
  1180. goto out_cleanup_hw;
  1181. }
  1182. ret = i915_load_modeset_init(dev);
  1183. if (ret < 0)
  1184. goto out_cleanup_vblank;
  1185. i915_driver_register(dev_priv);
  1186. intel_runtime_pm_enable(dev_priv);
  1187. intel_runtime_pm_put(dev_priv);
  1188. return 0;
  1189. out_cleanup_vblank:
  1190. drm_vblank_cleanup(dev);
  1191. out_cleanup_hw:
  1192. i915_driver_cleanup_hw(dev_priv);
  1193. out_cleanup_mmio:
  1194. i915_driver_cleanup_mmio(dev_priv);
  1195. out_runtime_pm_put:
  1196. intel_runtime_pm_put(dev_priv);
  1197. i915_driver_cleanup_early(dev_priv);
  1198. out_free_priv:
  1199. i915_load_error(dev_priv, "Device initialization failed (%d)\n", ret);
  1200. kfree(dev_priv);
  1201. return ret;
  1202. }
  1203. int i915_driver_unload(struct drm_device *dev)
  1204. {
  1205. struct drm_i915_private *dev_priv = dev->dev_private;
  1206. int ret;
  1207. intel_fbdev_fini(dev);
  1208. ret = i915_gem_suspend(dev);
  1209. if (ret) {
  1210. DRM_ERROR("failed to idle hardware: %d\n", ret);
  1211. return ret;
  1212. }
  1213. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  1214. i915_driver_unregister(dev_priv);
  1215. drm_vblank_cleanup(dev);
  1216. intel_modeset_cleanup(dev);
  1217. /*
  1218. * free the memory space allocated for the child device
  1219. * config parsed from VBT
  1220. */
  1221. if (dev_priv->vbt.child_dev && dev_priv->vbt.child_dev_num) {
  1222. kfree(dev_priv->vbt.child_dev);
  1223. dev_priv->vbt.child_dev = NULL;
  1224. dev_priv->vbt.child_dev_num = 0;
  1225. }
  1226. kfree(dev_priv->vbt.sdvo_lvds_vbt_mode);
  1227. dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
  1228. kfree(dev_priv->vbt.lfp_lvds_vbt_mode);
  1229. dev_priv->vbt.lfp_lvds_vbt_mode = NULL;
  1230. vga_switcheroo_unregister_client(dev->pdev);
  1231. vga_client_register(dev->pdev, NULL, NULL, NULL);
  1232. intel_csr_ucode_fini(dev_priv);
  1233. /* Free error state after interrupts are fully disabled. */
  1234. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  1235. i915_destroy_error_state(dev);
  1236. /* Flush any outstanding unpin_work. */
  1237. flush_workqueue(dev_priv->wq);
  1238. intel_guc_ucode_fini(dev);
  1239. mutex_lock(&dev->struct_mutex);
  1240. i915_gem_cleanup_engines(dev);
  1241. i915_gem_context_fini(dev);
  1242. mutex_unlock(&dev->struct_mutex);
  1243. intel_fbc_cleanup_cfb(dev_priv);
  1244. intel_power_domains_fini(dev_priv);
  1245. i915_driver_cleanup_hw(dev_priv);
  1246. i915_driver_cleanup_mmio(dev_priv);
  1247. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  1248. i915_driver_cleanup_early(dev_priv);
  1249. kfree(dev_priv);
  1250. return 0;
  1251. }
  1252. int i915_driver_open(struct drm_device *dev, struct drm_file *file)
  1253. {
  1254. int ret;
  1255. ret = i915_gem_open(dev, file);
  1256. if (ret)
  1257. return ret;
  1258. return 0;
  1259. }
  1260. /**
  1261. * i915_driver_lastclose - clean up after all DRM clients have exited
  1262. * @dev: DRM device
  1263. *
  1264. * Take care of cleaning up after all DRM clients have exited. In the
  1265. * mode setting case, we want to restore the kernel's initial mode (just
  1266. * in case the last client left us in a bad state).
  1267. *
  1268. * Additionally, in the non-mode setting case, we'll tear down the GTT
  1269. * and DMA structures, since the kernel won't be using them, and clea
  1270. * up any GEM state.
  1271. */
  1272. void i915_driver_lastclose(struct drm_device *dev)
  1273. {
  1274. intel_fbdev_restore_mode(dev);
  1275. vga_switcheroo_process_delayed_switch();
  1276. }
  1277. void i915_driver_preclose(struct drm_device *dev, struct drm_file *file)
  1278. {
  1279. mutex_lock(&dev->struct_mutex);
  1280. i915_gem_context_close(dev, file);
  1281. i915_gem_release(dev, file);
  1282. mutex_unlock(&dev->struct_mutex);
  1283. }
  1284. void i915_driver_postclose(struct drm_device *dev, struct drm_file *file)
  1285. {
  1286. struct drm_i915_file_private *file_priv = file->driver_priv;
  1287. kfree(file_priv);
  1288. }
  1289. static int
  1290. i915_gem_reject_pin_ioctl(struct drm_device *dev, void *data,
  1291. struct drm_file *file)
  1292. {
  1293. return -ENODEV;
  1294. }
  1295. const struct drm_ioctl_desc i915_ioctls[] = {
  1296. DRM_IOCTL_DEF_DRV(I915_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1297. DRM_IOCTL_DEF_DRV(I915_FLUSH, drm_noop, DRM_AUTH),
  1298. DRM_IOCTL_DEF_DRV(I915_FLIP, drm_noop, DRM_AUTH),
  1299. DRM_IOCTL_DEF_DRV(I915_BATCHBUFFER, drm_noop, DRM_AUTH),
  1300. DRM_IOCTL_DEF_DRV(I915_IRQ_EMIT, drm_noop, DRM_AUTH),
  1301. DRM_IOCTL_DEF_DRV(I915_IRQ_WAIT, drm_noop, DRM_AUTH),
  1302. DRM_IOCTL_DEF_DRV(I915_GETPARAM, i915_getparam, DRM_AUTH|DRM_RENDER_ALLOW),
  1303. DRM_IOCTL_DEF_DRV(I915_SETPARAM, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1304. DRM_IOCTL_DEF_DRV(I915_ALLOC, drm_noop, DRM_AUTH),
  1305. DRM_IOCTL_DEF_DRV(I915_FREE, drm_noop, DRM_AUTH),
  1306. DRM_IOCTL_DEF_DRV(I915_INIT_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1307. DRM_IOCTL_DEF_DRV(I915_CMDBUFFER, drm_noop, DRM_AUTH),
  1308. DRM_IOCTL_DEF_DRV(I915_DESTROY_HEAP, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1309. DRM_IOCTL_DEF_DRV(I915_SET_VBLANK_PIPE, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1310. DRM_IOCTL_DEF_DRV(I915_GET_VBLANK_PIPE, drm_noop, DRM_AUTH),
  1311. DRM_IOCTL_DEF_DRV(I915_VBLANK_SWAP, drm_noop, DRM_AUTH),
  1312. DRM_IOCTL_DEF_DRV(I915_HWS_ADDR, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1313. DRM_IOCTL_DEF_DRV(I915_GEM_INIT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1314. DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER, i915_gem_execbuffer, DRM_AUTH),
  1315. DRM_IOCTL_DEF_DRV(I915_GEM_EXECBUFFER2, i915_gem_execbuffer2, DRM_AUTH|DRM_RENDER_ALLOW),
  1316. DRM_IOCTL_DEF_DRV(I915_GEM_PIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
  1317. DRM_IOCTL_DEF_DRV(I915_GEM_UNPIN, i915_gem_reject_pin_ioctl, DRM_AUTH|DRM_ROOT_ONLY),
  1318. DRM_IOCTL_DEF_DRV(I915_GEM_BUSY, i915_gem_busy_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1319. DRM_IOCTL_DEF_DRV(I915_GEM_SET_CACHING, i915_gem_set_caching_ioctl, DRM_RENDER_ALLOW),
  1320. DRM_IOCTL_DEF_DRV(I915_GEM_GET_CACHING, i915_gem_get_caching_ioctl, DRM_RENDER_ALLOW),
  1321. DRM_IOCTL_DEF_DRV(I915_GEM_THROTTLE, i915_gem_throttle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1322. DRM_IOCTL_DEF_DRV(I915_GEM_ENTERVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1323. DRM_IOCTL_DEF_DRV(I915_GEM_LEAVEVT, drm_noop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  1324. DRM_IOCTL_DEF_DRV(I915_GEM_CREATE, i915_gem_create_ioctl, DRM_RENDER_ALLOW),
  1325. DRM_IOCTL_DEF_DRV(I915_GEM_PREAD, i915_gem_pread_ioctl, DRM_RENDER_ALLOW),
  1326. DRM_IOCTL_DEF_DRV(I915_GEM_PWRITE, i915_gem_pwrite_ioctl, DRM_RENDER_ALLOW),
  1327. DRM_IOCTL_DEF_DRV(I915_GEM_MMAP, i915_gem_mmap_ioctl, DRM_RENDER_ALLOW),
  1328. DRM_IOCTL_DEF_DRV(I915_GEM_MMAP_GTT, i915_gem_mmap_gtt_ioctl, DRM_RENDER_ALLOW),
  1329. DRM_IOCTL_DEF_DRV(I915_GEM_SET_DOMAIN, i915_gem_set_domain_ioctl, DRM_RENDER_ALLOW),
  1330. DRM_IOCTL_DEF_DRV(I915_GEM_SW_FINISH, i915_gem_sw_finish_ioctl, DRM_RENDER_ALLOW),
  1331. DRM_IOCTL_DEF_DRV(I915_GEM_SET_TILING, i915_gem_set_tiling, DRM_RENDER_ALLOW),
  1332. DRM_IOCTL_DEF_DRV(I915_GEM_GET_TILING, i915_gem_get_tiling, DRM_RENDER_ALLOW),
  1333. DRM_IOCTL_DEF_DRV(I915_GEM_GET_APERTURE, i915_gem_get_aperture_ioctl, DRM_RENDER_ALLOW),
  1334. DRM_IOCTL_DEF_DRV(I915_GET_PIPE_FROM_CRTC_ID, intel_get_pipe_from_crtc_id, 0),
  1335. DRM_IOCTL_DEF_DRV(I915_GEM_MADVISE, i915_gem_madvise_ioctl, DRM_RENDER_ALLOW),
  1336. DRM_IOCTL_DEF_DRV(I915_OVERLAY_PUT_IMAGE, intel_overlay_put_image, DRM_MASTER|DRM_CONTROL_ALLOW),
  1337. DRM_IOCTL_DEF_DRV(I915_OVERLAY_ATTRS, intel_overlay_attrs, DRM_MASTER|DRM_CONTROL_ALLOW),
  1338. DRM_IOCTL_DEF_DRV(I915_SET_SPRITE_COLORKEY, intel_sprite_set_colorkey, DRM_MASTER|DRM_CONTROL_ALLOW),
  1339. DRM_IOCTL_DEF_DRV(I915_GET_SPRITE_COLORKEY, drm_noop, DRM_MASTER|DRM_CONTROL_ALLOW),
  1340. DRM_IOCTL_DEF_DRV(I915_GEM_WAIT, i915_gem_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  1341. DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_CREATE, i915_gem_context_create_ioctl, DRM_RENDER_ALLOW),
  1342. DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_DESTROY, i915_gem_context_destroy_ioctl, DRM_RENDER_ALLOW),
  1343. DRM_IOCTL_DEF_DRV(I915_REG_READ, i915_reg_read_ioctl, DRM_RENDER_ALLOW),
  1344. DRM_IOCTL_DEF_DRV(I915_GET_RESET_STATS, i915_get_reset_stats_ioctl, DRM_RENDER_ALLOW),
  1345. DRM_IOCTL_DEF_DRV(I915_GEM_USERPTR, i915_gem_userptr_ioctl, DRM_RENDER_ALLOW),
  1346. DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_GETPARAM, i915_gem_context_getparam_ioctl, DRM_RENDER_ALLOW),
  1347. DRM_IOCTL_DEF_DRV(I915_GEM_CONTEXT_SETPARAM, i915_gem_context_setparam_ioctl, DRM_RENDER_ALLOW),
  1348. };
  1349. int i915_max_ioctl = ARRAY_SIZE(i915_ioctls);