mock_gem_device.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "gt/intel_gt.h"
  29. #include "gt/intel_gt_requests.h"
  30. #include "gt/mock_engine.h"
  31. #include "intel_memory_region.h"
  32. #include "intel_region_ttm.h"
  33. #include "mock_request.h"
  34. #include "mock_gem_device.h"
  35. #include "mock_gtt.h"
  36. #include "mock_uncore.h"
  37. #include "mock_region.h"
  38. #include "gem/selftests/mock_context.h"
  39. #include "gem/selftests/mock_gem_object.h"
  40. void mock_device_flush(struct drm_i915_private *i915)
  41. {
  42. struct intel_gt *gt = to_gt(i915);
  43. struct intel_engine_cs *engine;
  44. enum intel_engine_id id;
  45. do {
  46. for_each_engine(engine, gt, id)
  47. mock_engine_flush(engine);
  48. } while (intel_gt_retire_requests_timeout(gt, MAX_SCHEDULE_TIMEOUT,
  49. NULL));
  50. }
  51. static void mock_device_release(struct drm_device *dev)
  52. {
  53. struct drm_i915_private *i915 = to_i915(dev);
  54. if (!i915->do_release)
  55. goto out;
  56. mock_device_flush(i915);
  57. intel_gt_driver_remove(to_gt(i915));
  58. i915_gem_drain_workqueue(i915);
  59. mock_fini_ggtt(to_gt(i915)->ggtt);
  60. destroy_workqueue(i915->unordered_wq);
  61. destroy_workqueue(i915->wq);
  62. intel_region_ttm_device_fini(i915);
  63. intel_gt_driver_late_release_all(i915);
  64. intel_memory_regions_driver_release(i915);
  65. drm_mode_config_cleanup(&i915->drm);
  66. out:
  67. i915_params_free(&i915->params);
  68. }
  69. static const struct drm_driver mock_driver = {
  70. .name = "mock",
  71. .driver_features = DRIVER_GEM,
  72. .release = mock_device_release,
  73. };
  74. static void release_dev(struct device *dev)
  75. {
  76. struct pci_dev *pdev = to_pci_dev(dev);
  77. kfree(pdev);
  78. }
  79. static int pm_domain_resume(struct device *dev)
  80. {
  81. return pm_generic_runtime_resume(dev);
  82. }
  83. static int pm_domain_suspend(struct device *dev)
  84. {
  85. return pm_generic_runtime_suspend(dev);
  86. }
  87. static struct dev_pm_domain pm_domain = {
  88. .ops = {
  89. .runtime_suspend = pm_domain_suspend,
  90. .runtime_resume = pm_domain_resume,
  91. },
  92. };
  93. static void mock_gt_probe(struct drm_i915_private *i915)
  94. {
  95. i915->gt[0]->name = "Mock GT";
  96. }
  97. static const struct intel_device_info mock_info = {
  98. .__runtime.graphics.ip.ver = -1,
  99. .__runtime.page_sizes = (I915_GTT_PAGE_SIZE_4K |
  100. I915_GTT_PAGE_SIZE_64K |
  101. I915_GTT_PAGE_SIZE_2M),
  102. .memory_regions = BIT(INTEL_REGION_SMEM),
  103. .platform_engine_mask = BIT(0),
  104. /* simply use legacy cache level for mock device */
  105. .max_pat_index = 3,
  106. .cachelevel_to_pat = {
  107. [I915_CACHE_NONE] = 0,
  108. [I915_CACHE_LLC] = 1,
  109. [I915_CACHE_L3_LLC] = 2,
  110. [I915_CACHE_WT] = 3,
  111. },
  112. };
  113. struct drm_i915_private *mock_gem_device(void)
  114. {
  115. #if IS_ENABLED(CONFIG_IOMMU_API) && defined(CONFIG_INTEL_IOMMU)
  116. static struct dev_iommu fake_iommu = { .priv = (void *)-1 };
  117. #endif
  118. struct drm_i915_private *i915;
  119. struct pci_dev *pdev;
  120. int ret;
  121. pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
  122. if (!pdev)
  123. return NULL;
  124. device_initialize(&pdev->dev);
  125. pdev->class = PCI_BASE_CLASS_DISPLAY << 16;
  126. pdev->dev.release = release_dev;
  127. dev_set_name(&pdev->dev, "mock");
  128. dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  129. #if IS_ENABLED(CONFIG_IOMMU_API) && defined(CONFIG_INTEL_IOMMU)
  130. /* HACK to disable iommu for the fake device; force identity mapping */
  131. pdev->dev.iommu = &fake_iommu;
  132. #endif
  133. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
  134. put_device(&pdev->dev);
  135. return NULL;
  136. }
  137. i915 = devm_drm_dev_alloc(&pdev->dev, &mock_driver,
  138. struct drm_i915_private, drm);
  139. if (IS_ERR(i915)) {
  140. pr_err("Failed to allocate mock GEM device: err=%ld\n", PTR_ERR(i915));
  141. devres_release_group(&pdev->dev, NULL);
  142. put_device(&pdev->dev);
  143. return NULL;
  144. }
  145. pci_set_drvdata(pdev, &i915->drm);
  146. /* Device parameters start as a copy of module parameters. */
  147. i915_params_copy(&i915->params, &i915_modparams);
  148. /* Set up device info and initial runtime info. */
  149. intel_device_info_driver_create(i915, pdev->device, &mock_info);
  150. intel_display_device_probe(i915);
  151. dev_pm_domain_set(&pdev->dev, &pm_domain);
  152. pm_runtime_enable(&pdev->dev);
  153. pm_runtime_dont_use_autosuspend(&pdev->dev);
  154. if (pm_runtime_enabled(&pdev->dev))
  155. WARN_ON(pm_runtime_get_sync(&pdev->dev));
  156. intel_runtime_pm_init_early(&i915->runtime_pm);
  157. /* wakeref tracking has significant overhead */
  158. i915->runtime_pm.no_wakeref_tracking = true;
  159. /* Using the global GTT may ask questions about KMS users, so prepare */
  160. drm_mode_config_init(&i915->drm);
  161. intel_memory_regions_hw_probe(i915);
  162. spin_lock_init(&i915->gpu_error.lock);
  163. i915_gem_init__mm(i915);
  164. intel_root_gt_init_early(i915);
  165. mock_uncore_init(&i915->uncore, i915);
  166. atomic_inc(&to_gt(i915)->wakeref.count); /* disable; no hw support */
  167. to_gt(i915)->awake = -ENODEV;
  168. mock_gt_probe(i915);
  169. ret = intel_region_ttm_device_init(i915);
  170. if (ret)
  171. goto err_ttm;
  172. i915->wq = alloc_ordered_workqueue("mock", 0);
  173. if (!i915->wq)
  174. goto err_drv;
  175. i915->unordered_wq = alloc_workqueue("mock-unordered", 0, 0);
  176. if (!i915->unordered_wq)
  177. goto err_wq;
  178. mock_init_contexts(i915);
  179. /* allocate the ggtt */
  180. ret = intel_gt_assign_ggtt(to_gt(i915));
  181. if (ret)
  182. goto err_unlock;
  183. mock_init_ggtt(to_gt(i915));
  184. to_gt(i915)->vm = i915_vm_get(&to_gt(i915)->ggtt->vm);
  185. to_gt(i915)->info.engine_mask = BIT(0);
  186. to_gt(i915)->engine[RCS0] = mock_engine(i915, "mock", RCS0);
  187. if (!to_gt(i915)->engine[RCS0])
  188. goto err_unlock;
  189. if (mock_engine_init(to_gt(i915)->engine[RCS0]))
  190. goto err_context;
  191. __clear_bit(I915_WEDGED, &to_gt(i915)->reset.flags);
  192. intel_engines_driver_register(i915);
  193. i915->do_release = true;
  194. ida_init(&i915->selftest.mock_region_instances);
  195. return i915;
  196. err_context:
  197. intel_gt_driver_remove(to_gt(i915));
  198. err_unlock:
  199. destroy_workqueue(i915->unordered_wq);
  200. err_wq:
  201. destroy_workqueue(i915->wq);
  202. err_drv:
  203. intel_region_ttm_device_fini(i915);
  204. err_ttm:
  205. intel_gt_driver_late_release_all(i915);
  206. intel_memory_regions_driver_release(i915);
  207. drm_mode_config_cleanup(&i915->drm);
  208. mock_destroy_device(i915);
  209. return NULL;
  210. }
  211. void mock_destroy_device(struct drm_i915_private *i915)
  212. {
  213. struct device *dev = i915->drm.dev;
  214. devres_release_group(dev, NULL);
  215. put_device(dev);
  216. }