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

source code of linux/drivers/gpu/drm/i915/selftests/mock_gem_device.c