i915_dma.c 40 KB

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