]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/i915/selftests/mock_gem_device.c
drm/i915: Create a kmem_cache to allocate struct i915_priolist from
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / i915 / selftests / mock_gem_device.c
CommitLineData
66d9cb5d
CW
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_runtime.h>
26
0daf0113
CW
27#include "mock_engine.h"
28#include "mock_context.h"
29#include "mock_request.h"
66d9cb5d
CW
30#include "mock_gem_device.h"
31#include "mock_gem_object.h"
3b5bb0a3 32#include "mock_gtt.h"
0757ac8f 33#include "mock_uncore.h"
66d9cb5d 34
0daf0113
CW
35void mock_device_flush(struct drm_i915_private *i915)
36{
37 struct intel_engine_cs *engine;
38 enum intel_engine_id id;
39
40 lockdep_assert_held(&i915->drm.struct_mutex);
41
42 for_each_engine(engine, i915, id)
43 mock_engine_flush(engine);
44
45 i915_gem_retire_requests(i915);
46}
47
66d9cb5d
CW
48static void mock_device_release(struct drm_device *dev)
49{
50 struct drm_i915_private *i915 = to_i915(dev);
0daf0113
CW
51 struct intel_engine_cs *engine;
52 enum intel_engine_id id;
53
54 mutex_lock(&i915->drm.struct_mutex);
55 mock_device_flush(i915);
56 mutex_unlock(&i915->drm.struct_mutex);
66d9cb5d 57
0daf0113
CW
58 cancel_delayed_work_sync(&i915->gt.retire_work);
59 cancel_delayed_work_sync(&i915->gt.idle_work);
60
61 mutex_lock(&i915->drm.struct_mutex);
62 for_each_engine(engine, i915, id)
63 mock_engine_free(engine);
64 i915_gem_context_fini(i915);
65 mutex_unlock(&i915->drm.struct_mutex);
66
67 drain_workqueue(i915->wq);
66d9cb5d
CW
68 i915_gem_drain_freed_objects(i915);
69
3b5bb0a3
CW
70 mutex_lock(&i915->drm.struct_mutex);
71 mock_fini_ggtt(i915);
72 i915_gem_timeline_fini(&i915->gt.global_timeline);
73 mutex_unlock(&i915->drm.struct_mutex);
74
0daf0113
CW
75 destroy_workqueue(i915->wq);
76
c5cf9a91 77 kmem_cache_destroy(i915->priorities);
0daf0113
CW
78 kmem_cache_destroy(i915->dependencies);
79 kmem_cache_destroy(i915->requests);
3b5bb0a3 80 kmem_cache_destroy(i915->vmas);
66d9cb5d
CW
81 kmem_cache_destroy(i915->objects);
82
83 drm_dev_fini(&i915->drm);
84 put_device(&i915->drm.pdev->dev);
85}
86
87static struct drm_driver mock_driver = {
88 .name = "mock",
89 .driver_features = DRIVER_GEM,
90 .release = mock_device_release,
91
92 .gem_close_object = i915_gem_close_object,
93 .gem_free_object_unlocked = i915_gem_free_object,
94};
95
96static void release_dev(struct device *dev)
97{
98 struct pci_dev *pdev = to_pci_dev(dev);
99
100 kfree(pdev);
101}
102
0daf0113
CW
103static void mock_retire_work_handler(struct work_struct *work)
104{
105}
106
107static void mock_idle_work_handler(struct work_struct *work)
108{
109}
110
66d9cb5d
CW
111struct drm_i915_private *mock_gem_device(void)
112{
113 struct drm_i915_private *i915;
0daf0113
CW
114 struct intel_engine_cs *engine;
115 enum intel_engine_id id;
66d9cb5d
CW
116 struct pci_dev *pdev;
117 int err;
118
119 pdev = kzalloc(sizeof(*pdev) + sizeof(*i915), GFP_KERNEL);
120 if (!pdev)
121 goto err;
122
123 device_initialize(&pdev->dev);
124 pdev->dev.release = release_dev;
125 dev_set_name(&pdev->dev, "mock");
126 dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
127
128 pm_runtime_dont_use_autosuspend(&pdev->dev);
129 pm_runtime_get_sync(&pdev->dev);
130
131 i915 = (struct drm_i915_private *)(pdev + 1);
132 pci_set_drvdata(pdev, i915);
133
134 err = drm_dev_init(&i915->drm, &mock_driver, &pdev->dev);
135 if (err) {
136 pr_err("Failed to initialise mock GEM device: err=%d\n", err);
137 goto put_device;
138 }
139 i915->drm.pdev = pdev;
140 i915->drm.dev_private = i915;
141
3b5bb0a3
CW
142 /* Using the global GTT may ask questions about KMS users, so prepare */
143 drm_mode_config_init(&i915->drm);
144
66d9cb5d
CW
145 mkwrite_device_info(i915)->gen = -1;
146
147 spin_lock_init(&i915->mm.object_stat_lock);
0757ac8f 148 mock_uncore_init(i915);
66d9cb5d 149
0daf0113
CW
150 init_waitqueue_head(&i915->gpu_error.wait_queue);
151 init_waitqueue_head(&i915->gpu_error.reset_queue);
152
153 i915->wq = alloc_ordered_workqueue("mock", 0);
154 if (!i915->wq)
155 goto put_device;
156
66d9cb5d
CW
157 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
158 init_llist_head(&i915->mm.free_list);
3b5bb0a3
CW
159 INIT_LIST_HEAD(&i915->mm.unbound_list);
160 INIT_LIST_HEAD(&i915->mm.bound_list);
66d9cb5d 161
0daf0113
CW
162 ida_init(&i915->context_hw_ida);
163
164 INIT_DELAYED_WORK(&i915->gt.retire_work, mock_retire_work_handler);
165 INIT_DELAYED_WORK(&i915->gt.idle_work, mock_idle_work_handler);
166
167 i915->gt.awake = true;
168
66d9cb5d
CW
169 i915->objects = KMEM_CACHE(mock_object, SLAB_HWCACHE_ALIGN);
170 if (!i915->objects)
0daf0113 171 goto err_wq;
66d9cb5d 172
3b5bb0a3
CW
173 i915->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
174 if (!i915->vmas)
175 goto err_objects;
176
0daf0113
CW
177 i915->requests = KMEM_CACHE(mock_request,
178 SLAB_HWCACHE_ALIGN |
179 SLAB_RECLAIM_ACCOUNT |
180 SLAB_DESTROY_BY_RCU);
181 if (!i915->requests)
182 goto err_vmas;
183
184 i915->dependencies = KMEM_CACHE(i915_dependency,
185 SLAB_HWCACHE_ALIGN |
186 SLAB_RECLAIM_ACCOUNT);
187 if (!i915->dependencies)
188 goto err_requests;
189
c5cf9a91
CW
190 i915->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
191 if (!i915->priorities)
192 goto err_dependencies;
193
3b5bb0a3
CW
194 mutex_lock(&i915->drm.struct_mutex);
195 INIT_LIST_HEAD(&i915->gt.timelines);
196 err = i915_gem_timeline_init__global(i915);
197 if (err) {
198 mutex_unlock(&i915->drm.struct_mutex);
c5cf9a91 199 goto err_priorities;
3b5bb0a3
CW
200 }
201
202 mock_init_ggtt(i915);
203 mutex_unlock(&i915->drm.struct_mutex);
204
0daf0113
CW
205 mkwrite_device_info(i915)->ring_mask = BIT(0);
206 i915->engine[RCS] = mock_engine(i915, "mock");
207 if (!i915->engine[RCS])
208 goto err_dependencies;
209
210 i915->kernel_context = mock_context(i915, NULL);
211 if (!i915->kernel_context)
212 goto err_engine;
213
66d9cb5d
CW
214 return i915;
215
0daf0113
CW
216err_engine:
217 for_each_engine(engine, i915, id)
218 mock_engine_free(engine);
c5cf9a91
CW
219err_priorities:
220 kmem_cache_destroy(i915->priorities);
0daf0113
CW
221err_dependencies:
222 kmem_cache_destroy(i915->dependencies);
223err_requests:
224 kmem_cache_destroy(i915->requests);
3b5bb0a3
CW
225err_vmas:
226 kmem_cache_destroy(i915->vmas);
227err_objects:
228 kmem_cache_destroy(i915->objects);
0daf0113
CW
229err_wq:
230 destroy_workqueue(i915->wq);
66d9cb5d
CW
231put_device:
232 put_device(&pdev->dev);
233err:
234 return NULL;
235}