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/intel_gt.h"
29 #include "gt/intel_gt_requests.h"
30 #include "gt/mock_engine.h"
31 
32 #include "mock_request.h"
33 #include "mock_gem_device.h"
34 #include "mock_gtt.h"
35 #include "mock_uncore.h"
36 
37 #include "gem/selftests/mock_context.h"
38 #include "gem/selftests/mock_gem_object.h"
39 
40 void mock_device_flush(struct drm_i915_private *i915)
41 {
42 	struct intel_engine_cs *engine;
43 	enum intel_engine_id id;
44 
45 	do {
46 		for_each_engine(engine, i915, id)
47 			mock_engine_flush(engine);
48 	} while (intel_gt_retire_requests_timeout(&i915->gt,
49 						  MAX_SCHEDULE_TIMEOUT));
50 }
51 
52 static void mock_device_release(struct drm_device *dev)
53 {
54 	struct drm_i915_private *i915 = to_i915(dev);
55 	struct intel_engine_cs *engine;
56 	enum intel_engine_id id;
57 
58 	mock_device_flush(i915);
59 
60 	i915_gem_drain_workqueue(i915);
61 
62 	for_each_engine(engine, i915, id)
63 		mock_engine_free(engine);
64 	i915_gem_driver_release__contexts(i915);
65 
66 	intel_timelines_fini(i915);
67 
68 	drain_workqueue(i915->wq);
69 	i915_gem_drain_freed_objects(i915);
70 
71 	mock_fini_ggtt(&i915->ggtt);
72 	destroy_workqueue(i915->wq);
73 
74 	i915_gemfs_fini(i915);
75 
76 	drm_mode_config_cleanup(&i915->drm);
77 
78 	drm_dev_fini(&i915->drm);
79 	put_device(&i915->drm.pdev->dev);
80 }
81 
82 static struct drm_driver mock_driver = {
83 	.name = "mock",
84 	.driver_features = DRIVER_GEM,
85 	.release = mock_device_release,
86 
87 	.gem_close_object = i915_gem_close_object,
88 	.gem_free_object_unlocked = i915_gem_free_object,
89 };
90 
91 static void release_dev(struct device *dev)
92 {
93 	struct pci_dev *pdev = to_pci_dev(dev);
94 
95 	kfree(pdev);
96 }
97 
98 static int pm_domain_resume(struct device *dev)
99 {
100 	return pm_generic_runtime_resume(dev);
101 }
102 
103 static int pm_domain_suspend(struct device *dev)
104 {
105 	return pm_generic_runtime_suspend(dev);
106 }
107 
108 static struct dev_pm_domain pm_domain = {
109 	.ops = {
110 		.runtime_suspend = pm_domain_suspend,
111 		.runtime_resume = pm_domain_resume,
112 	},
113 };
114 
115 struct drm_i915_private *mock_gem_device(void)
116 {
117 	struct drm_i915_private *i915;
118 	struct pci_dev *pdev;
119 	int err;
120 
121 	pdev = kzalloc(sizeof(*pdev) + sizeof(*i915), GFP_KERNEL);
122 	if (!pdev)
123 		goto err;
124 
125 	device_initialize(&pdev->dev);
126 	pdev->class = PCI_BASE_CLASS_DISPLAY << 16;
127 	pdev->dev.release = release_dev;
128 	dev_set_name(&pdev->dev, "mock");
129 	dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
130 
131 #if IS_ENABLED(CONFIG_IOMMU_API) && defined(CONFIG_INTEL_IOMMU)
132 	/* hack to disable iommu for the fake device; force identity mapping */
133 	pdev->dev.archdata.iommu = (void *)-1;
134 #endif
135 
136 	i915 = (struct drm_i915_private *)(pdev + 1);
137 	pci_set_drvdata(pdev, i915);
138 
139 	dev_pm_domain_set(&pdev->dev, &pm_domain);
140 	pm_runtime_enable(&pdev->dev);
141 	pm_runtime_dont_use_autosuspend(&pdev->dev);
142 	if (pm_runtime_enabled(&pdev->dev))
143 		WARN_ON(pm_runtime_get_sync(&pdev->dev));
144 
145 	err = drm_dev_init(&i915->drm, &mock_driver, &pdev->dev);
146 	if (err) {
147 		pr_err("Failed to initialise mock GEM device: err=%d\n", err);
148 		goto put_device;
149 	}
150 	i915->drm.pdev = pdev;
151 	i915->drm.dev_private = i915;
152 
153 	intel_runtime_pm_init_early(&i915->runtime_pm);
154 
155 	/* Using the global GTT may ask questions about KMS users, so prepare */
156 	drm_mode_config_init(&i915->drm);
157 
158 	mkwrite_device_info(i915)->gen = -1;
159 
160 	mkwrite_device_info(i915)->page_sizes =
161 		I915_GTT_PAGE_SIZE_4K |
162 		I915_GTT_PAGE_SIZE_64K |
163 		I915_GTT_PAGE_SIZE_2M;
164 
165 	mock_uncore_init(&i915->uncore);
166 	i915_gem_init__mm(i915);
167 	intel_gt_init_early(&i915->gt, i915);
168 	atomic_inc(&i915->gt.wakeref.count); /* disable; no hw support */
169 	i915->gt.awake = -ENODEV;
170 
171 	i915->wq = alloc_ordered_workqueue("mock", 0);
172 	if (!i915->wq)
173 		goto err_drv;
174 
175 	mock_init_contexts(i915);
176 
177 	intel_timelines_init(i915);
178 
179 	mock_init_ggtt(i915, &i915->ggtt);
180 
181 	mkwrite_device_info(i915)->engine_mask = BIT(0);
182 
183 	i915->engine[RCS0] = mock_engine(i915, "mock", RCS0);
184 	if (!i915->engine[RCS0])
185 		goto err_unlock;
186 
187 	i915->kernel_context = mock_context(i915, NULL);
188 	if (!i915->kernel_context)
189 		goto err_engine;
190 
191 	if (mock_engine_init(i915->engine[RCS0]))
192 		goto err_context;
193 
194 	intel_engines_driver_register(i915);
195 
196 	WARN_ON(i915_gemfs_init(i915));
197 
198 	return i915;
199 
200 err_context:
201 	i915_gem_driver_release__contexts(i915);
202 err_engine:
203 	mock_engine_free(i915->engine[RCS0]);
204 err_unlock:
205 	intel_timelines_fini(i915);
206 	destroy_workqueue(i915->wq);
207 err_drv:
208 	drm_mode_config_cleanup(&i915->drm);
209 	drm_dev_fini(&i915->drm);
210 put_device:
211 	put_device(&pdev->dev);
212 err:
213 	return NULL;
214 }
215