]>
git.ipfire.org Git - thirdparty/kernel/linux.git/blob - drivers/gpu/drm/i915/selftests/mock_gem_device.c
2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #include <linux/pm_runtime.h>
27 #include "mock_engine.h"
28 #include "mock_context.h"
29 #include "mock_request.h"
30 #include "mock_gem_device.h"
31 #include "mock_gem_object.h"
34 void mock_device_flush(struct drm_i915_private
*i915
)
36 struct intel_engine_cs
*engine
;
37 enum intel_engine_id id
;
39 lockdep_assert_held(&i915
->drm
.struct_mutex
);
41 for_each_engine(engine
, i915
, id
)
42 mock_engine_flush(engine
);
44 i915_gem_retire_requests(i915
);
47 static void mock_device_release(struct drm_device
*dev
)
49 struct drm_i915_private
*i915
= to_i915(dev
);
50 struct intel_engine_cs
*engine
;
51 enum intel_engine_id id
;
53 mutex_lock(&i915
->drm
.struct_mutex
);
54 mock_device_flush(i915
);
55 mutex_unlock(&i915
->drm
.struct_mutex
);
57 cancel_delayed_work_sync(&i915
->gt
.retire_work
);
58 cancel_delayed_work_sync(&i915
->gt
.idle_work
);
60 mutex_lock(&i915
->drm
.struct_mutex
);
61 for_each_engine(engine
, i915
, id
)
62 mock_engine_free(engine
);
63 i915_gem_context_fini(i915
);
64 mutex_unlock(&i915
->drm
.struct_mutex
);
66 drain_workqueue(i915
->wq
);
67 i915_gem_drain_freed_objects(i915
);
69 mutex_lock(&i915
->drm
.struct_mutex
);
71 i915_gem_timeline_fini(&i915
->gt
.global_timeline
);
72 mutex_unlock(&i915
->drm
.struct_mutex
);
74 destroy_workqueue(i915
->wq
);
76 kmem_cache_destroy(i915
->dependencies
);
77 kmem_cache_destroy(i915
->requests
);
78 kmem_cache_destroy(i915
->vmas
);
79 kmem_cache_destroy(i915
->objects
);
81 drm_dev_fini(&i915
->drm
);
82 put_device(&i915
->drm
.pdev
->dev
);
85 static struct drm_driver mock_driver
= {
87 .driver_features
= DRIVER_GEM
,
88 .release
= mock_device_release
,
90 .gem_close_object
= i915_gem_close_object
,
91 .gem_free_object_unlocked
= i915_gem_free_object
,
94 static void release_dev(struct device
*dev
)
96 struct pci_dev
*pdev
= to_pci_dev(dev
);
101 static void mock_retire_work_handler(struct work_struct
*work
)
105 static void mock_idle_work_handler(struct work_struct
*work
)
109 struct drm_i915_private
*mock_gem_device(void)
111 struct drm_i915_private
*i915
;
112 struct intel_engine_cs
*engine
;
113 enum intel_engine_id id
;
114 struct pci_dev
*pdev
;
117 pdev
= kzalloc(sizeof(*pdev
) + sizeof(*i915
), GFP_KERNEL
);
121 device_initialize(&pdev
->dev
);
122 pdev
->dev
.release
= release_dev
;
123 dev_set_name(&pdev
->dev
, "mock");
124 dma_coerce_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
126 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
127 pm_runtime_get_sync(&pdev
->dev
);
129 i915
= (struct drm_i915_private
*)(pdev
+ 1);
130 pci_set_drvdata(pdev
, i915
);
132 err
= drm_dev_init(&i915
->drm
, &mock_driver
, &pdev
->dev
);
134 pr_err("Failed to initialise mock GEM device: err=%d\n", err
);
137 i915
->drm
.pdev
= pdev
;
138 i915
->drm
.dev_private
= i915
;
140 /* Using the global GTT may ask questions about KMS users, so prepare */
141 drm_mode_config_init(&i915
->drm
);
143 mkwrite_device_info(i915
)->gen
= -1;
145 spin_lock_init(&i915
->mm
.object_stat_lock
);
147 init_waitqueue_head(&i915
->gpu_error
.wait_queue
);
148 init_waitqueue_head(&i915
->gpu_error
.reset_queue
);
150 i915
->wq
= alloc_ordered_workqueue("mock", 0);
154 INIT_WORK(&i915
->mm
.free_work
, __i915_gem_free_work
);
155 init_llist_head(&i915
->mm
.free_list
);
156 INIT_LIST_HEAD(&i915
->mm
.unbound_list
);
157 INIT_LIST_HEAD(&i915
->mm
.bound_list
);
159 ida_init(&i915
->context_hw_ida
);
161 INIT_DELAYED_WORK(&i915
->gt
.retire_work
, mock_retire_work_handler
);
162 INIT_DELAYED_WORK(&i915
->gt
.idle_work
, mock_idle_work_handler
);
164 i915
->gt
.awake
= true;
166 i915
->objects
= KMEM_CACHE(mock_object
, SLAB_HWCACHE_ALIGN
);
170 i915
->vmas
= KMEM_CACHE(i915_vma
, SLAB_HWCACHE_ALIGN
);
174 i915
->requests
= KMEM_CACHE(mock_request
,
176 SLAB_RECLAIM_ACCOUNT
|
177 SLAB_TYPESAFE_BY_RCU
);
181 i915
->dependencies
= KMEM_CACHE(i915_dependency
,
183 SLAB_RECLAIM_ACCOUNT
);
184 if (!i915
->dependencies
)
187 mutex_lock(&i915
->drm
.struct_mutex
);
188 INIT_LIST_HEAD(&i915
->gt
.timelines
);
189 err
= i915_gem_timeline_init__global(i915
);
191 mutex_unlock(&i915
->drm
.struct_mutex
);
192 goto err_dependencies
;
195 mock_init_ggtt(i915
);
196 mutex_unlock(&i915
->drm
.struct_mutex
);
198 mkwrite_device_info(i915
)->ring_mask
= BIT(0);
199 i915
->engine
[RCS
] = mock_engine(i915
, "mock");
200 if (!i915
->engine
[RCS
])
201 goto err_dependencies
;
203 i915
->kernel_context
= mock_context(i915
, NULL
);
204 if (!i915
->kernel_context
)
210 for_each_engine(engine
, i915
, id
)
211 mock_engine_free(engine
);
213 kmem_cache_destroy(i915
->dependencies
);
215 kmem_cache_destroy(i915
->requests
);
217 kmem_cache_destroy(i915
->vmas
);
219 kmem_cache_destroy(i915
->objects
);
221 destroy_workqueue(i915
->wq
);
223 put_device(&pdev
->dev
);