mock_gem_device.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/pm_domain.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/iommu.h>
  27. #include <drm/drm_managed.h>
  28. #include "display/intel_display_device.h"
  29. #include "gt/intel_gt.h"
  30. #include "gt/intel_gt_requests.h"
  31. #include "gt/mock_engine.h"
  32. #include "i915_driver.h"
  33. #include "intel_memory_region.h"
  34. #include "intel_region_ttm.h"
  35. #include "mock_request.h"
  36. #include "mock_gem_device.h"
  37. #include "mock_gtt.h"
  38. #include "mock_uncore.h"
  39. #include "mock_region.h"
  40. #include "gem/selftests/mock_context.h"
  41. #include "gem/selftests/mock_gem_object.h"
  42. void mock_device_flush(struct drm_i915_private *i915)
  43. {
  44. struct intel_gt *gt = to_gt(i915);
  45. struct intel_engine_cs *engine;
  46. enum intel_engine_id id;
  47. do {
  48. for_each_engine(engine, gt, id)
  49. mock_engine_flush(engine);
  50. } while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT,
  51. NULL));
  52. }
  53. static void mock_device_release(struct drm_device *dev)
  54. {
  55. struct drm_i915_private *i915 = to_i915(dev);
  56. if (!i915->do_release)
  57. goto out;
  58. mock_device_flush(i915);
  59. intel_gt_driver_remove(to_gt(i915));
  60. i915_gem_drain_workqueue(i915);
  61. mock_fini_ggtt(to_gt(i915)->ggtt);
  62. destroy_workqueue(i915->unordered_wq);
  63. destroy_workqueue(i915->wq);
  64. intel_region_ttm_device_fini(i915);
  65. intel_gt_driver_late_release_all(i915);
  66. intel_memory_regions_driver_release(i915);
  67. drm_mode_config_cleanup(&i915->drm);
  68. out:
  69. i915_params_free(&i915->params);
  70. }
  71. static const struct drm_driver mock_driver = {
  72. .name = "mock",
  73. .driver_features = DRIVER_GEM,
  74. .release = mock_device_release,
  75. };
  76. static void release_dev(struct device *dev)
  77. {
  78. struct pci_dev *pdev = to_pci_dev(dev);
  79. kfree(pdev);
  80. }
  81. static int pm_domain_resume(struct device *dev)
  82. {
  83. return pm_generic_runtime_resume(dev);
  84. }
  85. static int pm_domain_suspend(struct device *dev)
  86. {
  87. return pm_generic_runtime_suspend(dev);
  88. }
  89. static struct dev_pm_domain pm_domain = {
  90. .ops = {
  91. .runtime_suspend = pm_domain_suspend,
  92. .runtime_resume = pm_domain_resume,
  93. },
  94. };
  95. static void mock_gt_probe(struct drm_i915_private *i915)
  96. {
  97. i915->gt[0]->name = "Mock GT";
  98. }
  99. static const struct intel_device_info mock_info = {
  100. .__runtime.graphics.ip.ver = -1,
  101. .__runtime.page_sizes = (I915_GTT_PAGE_SIZE_4K |
  102. I915_GTT_PAGE_SIZE_64K |
  103. I915_GTT_PAGE_SIZE_2M),
  104. .memory_regions = BIT(INTEL_REGION_SMEM),
  105. .platform_engine_mask = BIT(0),
  106. /* simply use legacy cache level for mock device */
  107. .max_pat_index = 3,
  108. .cachelevel_to_pat = {
  109. [I915_CACHE_NONE] = 0,
  110. [I915_CACHE_LLC] = 1,
  111. [I915_CACHE_L3_LLC] = 2,
  112. [I915_CACHE_WT] = 3,
  113. },
  114. };
  115. struct drm_i915_private *mock_gem_device(void)
  116. {
  117. #if IS_ENABLED(CONFIG_IOMMU_API) && IS_ENABLED(CONFIG_INTEL_IOMMU)
  118. static struct dev_iommu fake_iommu = { .priv = (void *)-1 };
  119. #endif
  120. struct drm_i915_private *i915;
  121. struct intel_display *display;
  122. struct pci_dev *pdev;
  123. int ret;
  124. pdev = kzalloc_obj(*pdev);
  125. if (!pdev)
  126. return NULL;
  127. device_initialize(&pdev->dev);
  128. pdev->class = PCI_BASE_CLASS_DISPLAY << 16;
  129. pdev->dev.release = release_dev;
  130. dev_set_name(&pdev->dev, "mock");
  131. dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  132. #if IS_ENABLED(CONFIG_IOMMU_API) && IS_ENABLED(CONFIG_INTEL_IOMMU)
  133. /* HACK to disable iommu for the fake device; force identity mapping */
  134. pdev->dev.iommu = &fake_iommu;
  135. #endif
  136. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
  137. put_device(&pdev->dev);
  138. return NULL;
  139. }
  140. i915 = devm_drm_dev_alloc(&pdev->dev, &mock_driver,
  141. struct drm_i915_private, drm);
  142. if (IS_ERR(i915)) {
  143. pr_err("Failed to allocate mock GEM device: err=%ld\n", PTR_ERR(i915));
  144. devres_release_group(&pdev->dev, NULL);
  145. put_device(&pdev->dev);
  146. return NULL;
  147. }
  148. pci_set_drvdata(pdev, &i915->drm);
  149. /* Device parameters start as a copy of module parameters. */
  150. i915_params_copy(&i915->params, &i915_modparams);
  151. /* Set up device info and initial runtime info. */
  152. intel_device_info_driver_create(i915, pdev->device, &mock_info);
  153. /* FIXME: Can we run selftests using a mock device without display? */
  154. display = intel_display_device_probe(pdev, i915_driver_parent_interface());
  155. if (IS_ERR(display))
  156. goto err_device;
  157. i915->display = display;
  158. dev_pm_domain_set(&pdev->dev, &pm_domain);
  159. pm_runtime_enable(&pdev->dev);
  160. pm_runtime_dont_use_autosuspend(&pdev->dev);
  161. if (pm_runtime_enabled(&pdev->dev))
  162. WARN_ON(pm_runtime_get_sync(&pdev->dev));
  163. intel_runtime_pm_init_early(&i915->runtime_pm);
  164. /* wakeref tracking has significant overhead */
  165. i915->runtime_pm.no_wakeref_tracking = true;
  166. /* Using the global GTT may ask questions about KMS users, so prepare */
  167. drm_mode_config_init(&i915->drm);
  168. intel_memory_regions_hw_probe(i915);
  169. spin_lock_init(&i915->gpu_error.lock);
  170. i915_gem_init__mm(i915);
  171. intel_root_gt_init_early(i915);
  172. mock_uncore_init(&i915->uncore, i915);
  173. atomic_inc(&to_gt(i915)->wakeref.count); /* disable; no hw support */
  174. to_gt(i915)->awake = INTEL_WAKEREF_MOCK_GT;
  175. mock_gt_probe(i915);
  176. ret = intel_region_ttm_device_init(i915);
  177. if (ret)
  178. goto err_ttm;
  179. i915->wq = alloc_ordered_workqueue("mock", 0);
  180. if (!i915->wq)
  181. goto err_drv;
  182. i915->unordered_wq = alloc_workqueue("mock-unordered", 0, 0);
  183. if (!i915->unordered_wq)
  184. goto err_wq;
  185. mock_init_contexts(i915);
  186. /* allocate the ggtt */
  187. ret = intel_gt_assign_ggtt(to_gt(i915));
  188. if (ret)
  189. goto err_unlock;
  190. mock_init_ggtt(to_gt(i915));
  191. to_gt(i915)->vm = i915_vm_get(&to_gt(i915)->ggtt->vm);
  192. to_gt(i915)->info.engine_mask = BIT(0);
  193. to_gt(i915)->engine[RCS0] = mock_engine(i915, "mock", RCS0);
  194. if (!to_gt(i915)->engine[RCS0])
  195. goto err_unlock;
  196. if (mock_engine_init(to_gt(i915)->engine[RCS0]))
  197. goto err_context;
  198. __clear_bit(I915_WEDGED, &to_gt(i915)->reset.flags);
  199. intel_engines_driver_register(i915);
  200. i915->do_release = true;
  201. ida_init(&i915->selftest.mock_region_instances);
  202. return i915;
  203. err_context:
  204. intel_gt_driver_remove(to_gt(i915));
  205. err_unlock:
  206. destroy_workqueue(i915->unordered_wq);
  207. err_wq:
  208. destroy_workqueue(i915->wq);
  209. err_drv:
  210. intel_region_ttm_device_fini(i915);
  211. err_ttm:
  212. intel_gt_driver_late_release_all(i915);
  213. intel_memory_regions_driver_release(i915);
  214. drm_mode_config_cleanup(&i915->drm);
  215. err_device:
  216. mock_destroy_device(i915);
  217. return NULL;
  218. }
  219. void mock_destroy_device(struct drm_i915_private *i915)
  220. {
  221. struct device *dev = i915->drm.dev;
  222. intel_display_device_remove(i915->display);
  223. devres_release_group(dev, NULL);
  224. put_device(dev);
  225. }