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 "gt/mock_engine.h"
29 
30 #include "mock_context.h"
31 #include "mock_request.h"
32 #include "mock_gem_device.h"
33 #include "mock_gem_object.h"
34 #include "mock_gtt.h"
35 #include "mock_uncore.h"
36 
37 void mock_device_flush(struct drm_i915_private *i915)
38 {
39 	struct intel_engine_cs *engine;
40 	enum intel_engine_id id;
41 
42 	lockdep_assert_held(&i915->drm.struct_mutex);
43 
44 	do {
45 		for_each_engine(engine, i915, id)
46 			mock_engine_flush(engine);
47 	} while (i915_retire_requests(i915));
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 	flush_work(&i915->gem.idle_work);
62 	i915_gem_drain_workqueue(i915);
63 
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);
69 
70 	i915_timelines_fini(i915);
71 
72 	drain_workqueue(i915->wq);
73 	i915_gem_drain_freed_objects(i915);
74 
75 	mutex_lock(&i915->drm.struct_mutex);
76 	mock_fini_ggtt(&i915->ggtt);
77 	mutex_unlock(&i915->drm.struct_mutex);
78 
79 	destroy_workqueue(i915->wq);
80 
81 	i915_gemfs_fini(i915);
82 
83 	drm_mode_config_cleanup(&i915->drm);
84 
85 	drm_dev_fini(&i915->drm);
86 	put_device(&i915->drm.pdev->dev);
87 }
88 
89 static struct drm_driver mock_driver = {
90 	.name = "mock",
91 	.driver_features = DRIVER_GEM,
92 	.release = mock_device_release,
93 
94 	.gem_close_object = i915_gem_close_object,
95 	.gem_free_object_unlocked = i915_gem_free_object,
96 };
97 
98 static void release_dev(struct device *dev)
99 {
100 	struct pci_dev *pdev = to_pci_dev(dev);
101 
102 	kfree(pdev);
103 }
104 
105 static void mock_retire_work_handler(struct work_struct *work)
106 {
107 }
108 
109 static void mock_idle_work_handler(struct work_struct *work)
110 {
111 }
112 
113 static int pm_domain_resume(struct device *dev)
114 {
115 	return pm_generic_runtime_resume(dev);
116 }
117 
118 static int pm_domain_suspend(struct device *dev)
119 {
120 	return pm_generic_runtime_suspend(dev);
121 }
122 
123 static struct dev_pm_domain pm_domain = {
124 	.ops = {
125 		.runtime_suspend = pm_domain_suspend,
126 		.runtime_resume = pm_domain_resume,
127 	},
128 };
129 
130 struct drm_i915_private *mock_gem_device(void)
131 {
132 	struct drm_i915_private *i915;
133 	struct pci_dev *pdev;
134 	int err;
135 
136 	pdev = kzalloc(sizeof(*pdev) + sizeof(*i915), GFP_KERNEL);
137 	if (!pdev)
138 		goto err;
139 
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));
145 
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;
149 #endif
150 
151 	i915 = (struct drm_i915_private *)(pdev + 1);
152 	pci_set_drvdata(pdev, i915);
153 
154 	intel_runtime_pm_init_early(i915);
155 
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));
161 
162 	err = drm_dev_init(&i915->drm, &mock_driver, &pdev->dev);
163 	if (err) {
164 		pr_err("Failed to initialise mock GEM device: err=%d\n", err);
165 		goto put_device;
166 	}
167 	i915->drm.pdev = pdev;
168 	i915->drm.dev_private = i915;
169 
170 	/* Using the global GTT may ask questions about KMS users, so prepare */
171 	drm_mode_config_init(&i915->drm);
172 
173 	mkwrite_device_info(i915)->gen = -1;
174 
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;
179 
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 */
184 
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);
189 
190 	i915->wq = alloc_ordered_workqueue("mock", 0);
191 	if (!i915->wq)
192 		goto err_drv;
193 
194 	mock_init_contexts(i915);
195 
196 	INIT_DELAYED_WORK(&i915->gem.retire_work, mock_retire_work_handler);
197 	INIT_WORK(&i915->gem.idle_work, mock_idle_work_handler);
198 
199 	i915->gt.awake = true;
200 
201 	i915_timelines_init(i915);
202 
203 	INIT_LIST_HEAD(&i915->gt.active_rings);
204 	INIT_LIST_HEAD(&i915->gt.closed_vma);
205 
206 	mutex_lock(&i915->drm.struct_mutex);
207 
208 	mock_init_ggtt(i915, &i915->ggtt);
209 
210 	mkwrite_device_info(i915)->engine_mask = BIT(0);
211 
212 	i915->engine[RCS0] = mock_engine(i915, "mock", RCS0);
213 	if (!i915->engine[RCS0])
214 		goto err_unlock;
215 
216 	i915->kernel_context = mock_context(i915, NULL);
217 	if (!i915->kernel_context)
218 		goto err_engine;
219 
220 	if (mock_engine_init(i915->engine[RCS0]))
221 		goto err_context;
222 
223 	mutex_unlock(&i915->drm.struct_mutex);
224 
225 	WARN_ON(i915_gemfs_init(i915));
226 
227 	return i915;
228 
229 err_context:
230 	i915_gem_contexts_fini(i915);
231 err_engine:
232 	mock_engine_free(i915->engine[RCS0]);
233 err_unlock:
234 	mutex_unlock(&i915->drm.struct_mutex);
235 	i915_timelines_fini(i915);
236 	destroy_workqueue(i915->wq);
237 err_drv:
238 	drm_mode_config_cleanup(&i915->drm);
239 	drm_dev_fini(&i915->drm);
240 put_device:
241 	put_device(&pdev->dev);
242 err:
243 	return NULL;
244 }
245