2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #include <linux/pm_domain.h>
26 #include <linux/pm_runtime.h>
28 #include "gt/mock_engine.h"
30 #include "mock_context.h"
31 #include "mock_request.h"
32 #include "mock_gem_device.h"
33 #include "mock_gem_object.h"
35 #include "mock_uncore.h"
37 void mock_device_flush(struct drm_i915_private *i915)
39 struct intel_engine_cs *engine;
40 enum intel_engine_id id;
42 lockdep_assert_held(&i915->drm.struct_mutex);
45 for_each_engine(engine, i915, id)
46 mock_engine_flush(engine);
47 } while (i915_retire_requests(i915));
50 static void mock_device_release(struct drm_device *dev)
52 struct drm_i915_private *i915 = to_i915(dev);
53 struct intel_engine_cs *engine;
54 enum intel_engine_id id;
56 mutex_lock(&i915->drm.struct_mutex);
57 mock_device_flush(i915);
58 i915_gem_contexts_lost(i915);
59 mutex_unlock(&i915->drm.struct_mutex);
61 flush_work(&i915->gem.idle_work);
62 i915_gem_drain_workqueue(i915);
64 mutex_lock(&i915->drm.struct_mutex);
65 for_each_engine(engine, i915, id)
66 mock_engine_free(engine);
67 i915_gem_contexts_fini(i915);
68 mutex_unlock(&i915->drm.struct_mutex);
70 i915_timelines_fini(i915);
72 drain_workqueue(i915->wq);
73 i915_gem_drain_freed_objects(i915);
75 mutex_lock(&i915->drm.struct_mutex);
76 mock_fini_ggtt(&i915->ggtt);
77 mutex_unlock(&i915->drm.struct_mutex);
79 destroy_workqueue(i915->wq);
81 i915_gemfs_fini(i915);
83 drm_mode_config_cleanup(&i915->drm);
85 drm_dev_fini(&i915->drm);
86 put_device(&i915->drm.pdev->dev);
89 static struct drm_driver mock_driver = {
91 .driver_features = DRIVER_GEM,
92 .release = mock_device_release,
94 .gem_close_object = i915_gem_close_object,
95 .gem_free_object_unlocked = i915_gem_free_object,
98 static void release_dev(struct device *dev)
100 struct pci_dev *pdev = to_pci_dev(dev);
105 static void mock_retire_work_handler(struct work_struct *work)
109 static void mock_idle_work_handler(struct work_struct *work)
113 static int pm_domain_resume(struct device *dev)
115 return pm_generic_runtime_resume(dev);
118 static int pm_domain_suspend(struct device *dev)
120 return pm_generic_runtime_suspend(dev);
123 static struct dev_pm_domain pm_domain = {
125 .runtime_suspend = pm_domain_suspend,
126 .runtime_resume = pm_domain_resume,
130 struct drm_i915_private *mock_gem_device(void)
132 struct drm_i915_private *i915;
133 struct pci_dev *pdev;
136 pdev = kzalloc(sizeof(*pdev) + sizeof(*i915), GFP_KERNEL);
140 device_initialize(&pdev->dev);
141 pdev->class = PCI_BASE_CLASS_DISPLAY << 16;
142 pdev->dev.release = release_dev;
143 dev_set_name(&pdev->dev, "mock");
144 dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
146 #if IS_ENABLED(CONFIG_IOMMU_API) && defined(CONFIG_INTEL_IOMMU)
147 /* hack to disable iommu for the fake device; force identity mapping */
148 pdev->dev.archdata.iommu = (void *)-1;
151 i915 = (struct drm_i915_private *)(pdev + 1);
152 pci_set_drvdata(pdev, i915);
154 intel_runtime_pm_init_early(i915);
156 dev_pm_domain_set(&pdev->dev, &pm_domain);
157 pm_runtime_enable(&pdev->dev);
158 pm_runtime_dont_use_autosuspend(&pdev->dev);
159 if (pm_runtime_enabled(&pdev->dev))
160 WARN_ON(pm_runtime_get_sync(&pdev->dev));
162 err = drm_dev_init(&i915->drm, &mock_driver, &pdev->dev);
164 pr_err("Failed to initialise mock GEM device: err=%d\n", err);
167 i915->drm.pdev = pdev;
168 i915->drm.dev_private = i915;
170 /* Using the global GTT may ask questions about KMS users, so prepare */
171 drm_mode_config_init(&i915->drm);
173 mkwrite_device_info(i915)->gen = -1;
175 mkwrite_device_info(i915)->page_sizes =
176 I915_GTT_PAGE_SIZE_4K |
177 I915_GTT_PAGE_SIZE_64K |
178 I915_GTT_PAGE_SIZE_2M;
180 mock_uncore_init(&i915->uncore);
181 i915_gem_init__mm(i915);
182 intel_gt_pm_init(i915);
183 atomic_inc(&i915->gt.wakeref.count); /* disable; no hw support */
185 init_waitqueue_head(&i915->gpu_error.wait_queue);
186 init_waitqueue_head(&i915->gpu_error.reset_queue);
187 init_srcu_struct(&i915->gpu_error.reset_backoff_srcu);
188 mutex_init(&i915->gpu_error.wedge_mutex);
190 i915->wq = alloc_ordered_workqueue("mock", 0);
194 mock_init_contexts(i915);
196 INIT_DELAYED_WORK(&i915->gem.retire_work, mock_retire_work_handler);
197 INIT_WORK(&i915->gem.idle_work, mock_idle_work_handler);
199 i915->gt.awake = true;
201 i915_timelines_init(i915);
203 INIT_LIST_HEAD(&i915->gt.active_rings);
204 INIT_LIST_HEAD(&i915->gt.closed_vma);
206 mutex_lock(&i915->drm.struct_mutex);
208 mock_init_ggtt(i915, &i915->ggtt);
210 mkwrite_device_info(i915)->engine_mask = BIT(0);
212 i915->engine[RCS0] = mock_engine(i915, "mock", RCS0);
213 if (!i915->engine[RCS0])
216 i915->kernel_context = mock_context(i915, NULL);
217 if (!i915->kernel_context)
220 if (mock_engine_init(i915->engine[RCS0]))
223 mutex_unlock(&i915->drm.struct_mutex);
225 WARN_ON(i915_gemfs_init(i915));
230 i915_gem_contexts_fini(i915);
232 mock_engine_free(i915->engine[RCS0]);
234 mutex_unlock(&i915->drm.struct_mutex);
235 i915_timelines_fini(i915);
236 destroy_workqueue(i915->wq);
238 drm_mode_config_cleanup(&i915->drm);
239 drm_dev_fini(&i915->drm);
241 put_device(&pdev->dev);