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"
33 #include "mock_uncore.h"
35 void mock_device_flush(struct drm_i915_private *i915)
37 struct intel_engine_cs *engine;
38 enum intel_engine_id id;
40 lockdep_assert_held(&i915->drm.struct_mutex);
42 for_each_engine(engine, i915, id)
43 mock_engine_flush(engine);
45 i915_gem_retire_requests(i915);
48 static void mock_device_release(struct drm_device *dev)
50 struct drm_i915_private *i915 = to_i915(dev);
51 struct intel_engine_cs *engine;
52 enum intel_engine_id id;
54 mutex_lock(&i915->drm.struct_mutex);
55 mock_device_flush(i915);
56 mutex_unlock(&i915->drm.struct_mutex);
58 cancel_delayed_work_sync(&i915->gt.retire_work);
59 cancel_delayed_work_sync(&i915->gt.idle_work);
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);
67 drain_workqueue(i915->wq);
68 i915_gem_drain_freed_objects(i915);
70 mutex_lock(&i915->drm.struct_mutex);
72 i915_gem_timeline_fini(&i915->gt.global_timeline);
73 mutex_unlock(&i915->drm.struct_mutex);
75 destroy_workqueue(i915->wq);
77 kmem_cache_destroy(i915->priorities);
78 kmem_cache_destroy(i915->dependencies);
79 kmem_cache_destroy(i915->requests);
80 kmem_cache_destroy(i915->vmas);
81 kmem_cache_destroy(i915->objects);
83 drm_dev_fini(&i915->drm);
84 put_device(&i915->drm.pdev->dev);
87 static struct drm_driver mock_driver = {
89 .driver_features = DRIVER_GEM,
90 .release = mock_device_release,
92 .gem_close_object = i915_gem_close_object,
93 .gem_free_object_unlocked = i915_gem_free_object,
96 static void release_dev(struct device *dev)
98 struct pci_dev *pdev = to_pci_dev(dev);
103 static void mock_retire_work_handler(struct work_struct *work)
107 static void mock_idle_work_handler(struct work_struct *work)
111 struct drm_i915_private *mock_gem_device(void)
113 struct drm_i915_private *i915;
114 struct intel_engine_cs *engine;
115 enum intel_engine_id id;
116 struct pci_dev *pdev;
119 pdev = kzalloc(sizeof(*pdev) + sizeof(*i915), GFP_KERNEL);
123 device_initialize(&pdev->dev);
124 pdev->class = PCI_BASE_CLASS_DISPLAY << 16;
125 pdev->dev.release = release_dev;
126 dev_set_name(&pdev->dev, "mock");
127 dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
129 pm_runtime_dont_use_autosuspend(&pdev->dev);
130 pm_runtime_get_sync(&pdev->dev);
132 i915 = (struct drm_i915_private *)(pdev + 1);
133 pci_set_drvdata(pdev, i915);
135 err = drm_dev_init(&i915->drm, &mock_driver, &pdev->dev);
137 pr_err("Failed to initialise mock GEM device: err=%d\n", err);
140 i915->drm.pdev = pdev;
141 i915->drm.dev_private = i915;
143 /* Using the global GTT may ask questions about KMS users, so prepare */
144 drm_mode_config_init(&i915->drm);
146 mkwrite_device_info(i915)->gen = -1;
148 spin_lock_init(&i915->mm.object_stat_lock);
149 mock_uncore_init(i915);
151 init_waitqueue_head(&i915->gpu_error.wait_queue);
152 init_waitqueue_head(&i915->gpu_error.reset_queue);
154 i915->wq = alloc_ordered_workqueue("mock", 0);
158 INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
159 init_llist_head(&i915->mm.free_list);
160 INIT_LIST_HEAD(&i915->mm.unbound_list);
161 INIT_LIST_HEAD(&i915->mm.bound_list);
163 ida_init(&i915->context_hw_ida);
165 INIT_DELAYED_WORK(&i915->gt.retire_work, mock_retire_work_handler);
166 INIT_DELAYED_WORK(&i915->gt.idle_work, mock_idle_work_handler);
168 i915->gt.awake = true;
170 i915->objects = KMEM_CACHE(mock_object, SLAB_HWCACHE_ALIGN);
174 i915->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
178 i915->requests = KMEM_CACHE(mock_request,
180 SLAB_RECLAIM_ACCOUNT |
181 SLAB_TYPESAFE_BY_RCU);
185 i915->dependencies = KMEM_CACHE(i915_dependency,
187 SLAB_RECLAIM_ACCOUNT);
188 if (!i915->dependencies)
191 i915->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
192 if (!i915->priorities)
193 goto err_dependencies;
195 mutex_lock(&i915->drm.struct_mutex);
196 INIT_LIST_HEAD(&i915->gt.timelines);
197 err = i915_gem_timeline_init__global(i915);
199 mutex_unlock(&i915->drm.struct_mutex);
203 mock_init_ggtt(i915);
204 mutex_unlock(&i915->drm.struct_mutex);
206 mkwrite_device_info(i915)->ring_mask = BIT(0);
207 i915->engine[RCS] = mock_engine(i915, "mock");
208 if (!i915->engine[RCS])
209 goto err_dependencies;
211 i915->kernel_context = mock_context(i915, NULL);
212 if (!i915->kernel_context)
218 for_each_engine(engine, i915, id)
219 mock_engine_free(engine);
221 kmem_cache_destroy(i915->priorities);
223 kmem_cache_destroy(i915->dependencies);
225 kmem_cache_destroy(i915->requests);
227 kmem_cache_destroy(i915->vmas);
229 kmem_cache_destroy(i915->objects);
231 destroy_workqueue(i915->wq);
233 put_device(&pdev->dev);