xref: /openbmc/linux/drivers/gpu/drm/i915/selftests/mock_gem_device.c (revision 530e7a660fb795452357b36cce26b839a9a187a9)
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 
28 #include "mock_engine.h"
29 #include "mock_context.h"
30 #include "mock_request.h"
31 #include "mock_gem_device.h"
32 #include "mock_gem_object.h"
33 #include "mock_gtt.h"
34 #include "mock_uncore.h"
35 
36 void mock_device_flush(struct drm_i915_private *i915)
37 {
38 	struct intel_engine_cs *engine;
39 	enum intel_engine_id id;
40 
41 	lockdep_assert_held(&i915->drm.struct_mutex);
42 
43 	for_each_engine(engine, i915, id)
44 		mock_engine_flush(engine);
45 
46 	i915_retire_requests(i915);
47 	GEM_BUG_ON(i915->gt.active_requests);
48 }
49 
50 static void mock_device_release(struct drm_device *dev)
51 {
52 	struct drm_i915_private *i915 = to_i915(dev);
53 	struct intel_engine_cs *engine;
54 	enum intel_engine_id id;
55 
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);
60 
61 	cancel_delayed_work_sync(&i915->gt.retire_work);
62 	cancel_delayed_work_sync(&i915->gt.idle_work);
63 	i915_gem_drain_workqueue(i915);
64 
65 	mutex_lock(&i915->drm.struct_mutex);
66 	for_each_engine(engine, i915, id)
67 		mock_engine_free(engine);
68 	i915_gem_contexts_fini(i915);
69 	mutex_unlock(&i915->drm.struct_mutex);
70 
71 	drain_workqueue(i915->wq);
72 	i915_gem_drain_freed_objects(i915);
73 
74 	mutex_lock(&i915->drm.struct_mutex);
75 	mock_fini_ggtt(i915);
76 	mutex_unlock(&i915->drm.struct_mutex);
77 	WARN_ON(!list_empty(&i915->gt.timelines));
78 
79 	destroy_workqueue(i915->wq);
80 
81 	kmem_cache_destroy(i915->priorities);
82 	kmem_cache_destroy(i915->dependencies);
83 	kmem_cache_destroy(i915->requests);
84 	kmem_cache_destroy(i915->vmas);
85 	kmem_cache_destroy(i915->objects);
86 
87 	i915_gemfs_fini(i915);
88 
89 	drm_mode_config_cleanup(&i915->drm);
90 
91 	drm_dev_fini(&i915->drm);
92 	put_device(&i915->drm.pdev->dev);
93 }
94 
95 static struct drm_driver mock_driver = {
96 	.name = "mock",
97 	.driver_features = DRIVER_GEM,
98 	.release = mock_device_release,
99 
100 	.gem_close_object = i915_gem_close_object,
101 	.gem_free_object_unlocked = i915_gem_free_object,
102 };
103 
104 static void release_dev(struct device *dev)
105 {
106 	struct pci_dev *pdev = to_pci_dev(dev);
107 
108 	kfree(pdev);
109 }
110 
111 static void mock_retire_work_handler(struct work_struct *work)
112 {
113 }
114 
115 static void mock_idle_work_handler(struct work_struct *work)
116 {
117 }
118 
119 static int pm_domain_resume(struct device *dev)
120 {
121 	return pm_generic_runtime_resume(dev);
122 }
123 
124 static int pm_domain_suspend(struct device *dev)
125 {
126 	return pm_generic_runtime_suspend(dev);
127 }
128 
129 static struct dev_pm_domain pm_domain = {
130 	.ops = {
131 		.runtime_suspend = pm_domain_suspend,
132 		.runtime_resume = pm_domain_resume,
133 	},
134 };
135 
136 struct drm_i915_private *mock_gem_device(void)
137 {
138 	struct drm_i915_private *i915;
139 	struct pci_dev *pdev;
140 	int err;
141 
142 	pdev = kzalloc(sizeof(*pdev) + sizeof(*i915), GFP_KERNEL);
143 	if (!pdev)
144 		goto err;
145 
146 	device_initialize(&pdev->dev);
147 	pdev->class = PCI_BASE_CLASS_DISPLAY << 16;
148 	pdev->dev.release = release_dev;
149 	dev_set_name(&pdev->dev, "mock");
150 	dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
151 
152 #if IS_ENABLED(CONFIG_IOMMU_API) && defined(CONFIG_INTEL_IOMMU)
153 	/* hack to disable iommu for the fake device; force identity mapping */
154 	pdev->dev.archdata.iommu = (void *)-1;
155 #endif
156 
157 	dev_pm_domain_set(&pdev->dev, &pm_domain);
158 	pm_runtime_enable(&pdev->dev);
159 	pm_runtime_dont_use_autosuspend(&pdev->dev);
160 	WARN_ON(pm_runtime_get_sync(&pdev->dev));
161 
162 	i915 = (struct drm_i915_private *)(pdev + 1);
163 	pci_set_drvdata(pdev, i915);
164 
165 	err = drm_dev_init(&i915->drm, &mock_driver, &pdev->dev);
166 	if (err) {
167 		pr_err("Failed to initialise mock GEM device: err=%d\n", err);
168 		goto put_device;
169 	}
170 	i915->drm.pdev = pdev;
171 	i915->drm.dev_private = i915;
172 
173 	/* Using the global GTT may ask questions about KMS users, so prepare */
174 	drm_mode_config_init(&i915->drm);
175 
176 	mkwrite_device_info(i915)->gen = -1;
177 
178 	mkwrite_device_info(i915)->page_sizes =
179 		I915_GTT_PAGE_SIZE_4K |
180 		I915_GTT_PAGE_SIZE_64K |
181 		I915_GTT_PAGE_SIZE_2M;
182 
183 	mock_uncore_init(i915);
184 	i915_gem_init__mm(i915);
185 
186 	init_waitqueue_head(&i915->gpu_error.wait_queue);
187 	init_waitqueue_head(&i915->gpu_error.reset_queue);
188 
189 	i915->wq = alloc_ordered_workqueue("mock", 0);
190 	if (!i915->wq)
191 		goto err_drv;
192 
193 	mock_init_contexts(i915);
194 
195 	INIT_DELAYED_WORK(&i915->gt.retire_work, mock_retire_work_handler);
196 	INIT_DELAYED_WORK(&i915->gt.idle_work, mock_idle_work_handler);
197 
198 	i915->gt.awake = true;
199 
200 	i915->objects = KMEM_CACHE(mock_object, SLAB_HWCACHE_ALIGN);
201 	if (!i915->objects)
202 		goto err_wq;
203 
204 	i915->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
205 	if (!i915->vmas)
206 		goto err_objects;
207 
208 	i915->requests = KMEM_CACHE(mock_request,
209 				    SLAB_HWCACHE_ALIGN |
210 				    SLAB_RECLAIM_ACCOUNT |
211 				    SLAB_TYPESAFE_BY_RCU);
212 	if (!i915->requests)
213 		goto err_vmas;
214 
215 	i915->dependencies = KMEM_CACHE(i915_dependency,
216 					SLAB_HWCACHE_ALIGN |
217 					SLAB_RECLAIM_ACCOUNT);
218 	if (!i915->dependencies)
219 		goto err_requests;
220 
221 	i915->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
222 	if (!i915->priorities)
223 		goto err_dependencies;
224 
225 	INIT_LIST_HEAD(&i915->gt.timelines);
226 	INIT_LIST_HEAD(&i915->gt.active_rings);
227 	INIT_LIST_HEAD(&i915->gt.closed_vma);
228 
229 	mutex_lock(&i915->drm.struct_mutex);
230 
231 	mock_init_ggtt(i915);
232 
233 	mkwrite_device_info(i915)->ring_mask = BIT(0);
234 	i915->kernel_context = mock_context(i915, NULL);
235 	if (!i915->kernel_context)
236 		goto err_unlock;
237 
238 	i915->engine[RCS] = mock_engine(i915, "mock", RCS);
239 	if (!i915->engine[RCS])
240 		goto err_context;
241 
242 	mutex_unlock(&i915->drm.struct_mutex);
243 
244 	WARN_ON(i915_gemfs_init(i915));
245 
246 	return i915;
247 
248 err_context:
249 	i915_gem_contexts_fini(i915);
250 err_unlock:
251 	mutex_unlock(&i915->drm.struct_mutex);
252 	kmem_cache_destroy(i915->priorities);
253 err_dependencies:
254 	kmem_cache_destroy(i915->dependencies);
255 err_requests:
256 	kmem_cache_destroy(i915->requests);
257 err_vmas:
258 	kmem_cache_destroy(i915->vmas);
259 err_objects:
260 	kmem_cache_destroy(i915->objects);
261 err_wq:
262 	destroy_workqueue(i915->wq);
263 err_drv:
264 	drm_mode_config_cleanup(&i915->drm);
265 	drm_dev_fini(&i915->drm);
266 put_device:
267 	put_device(&pdev->dev);
268 err:
269 	return NULL;
270 }
271