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