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