Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
Daniel Vetter | be6a037 | 2015-03-18 10:46:04 +0100 | [diff] [blame] | 2 | * Copyright © 2008-2015 Intel Corporation |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 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 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 28 | #include <drm/drm_vma_manager.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 29 | #include <drm/drm_pci.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/i915_drm.h> |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 31 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 32 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 33 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 34 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 36 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 37 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 38 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 39 | #include <linux/dma-buf.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 40 | #include <linux/mman.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 41 | |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 42 | #include "i915_drv.h" |
| 43 | #include "i915_gem_clflush.h" |
| 44 | #include "i915_gemfs.h" |
| 45 | #include "i915_reset.h" |
| 46 | #include "i915_trace.h" |
| 47 | #include "i915_vgpu.h" |
| 48 | |
| 49 | #include "intel_drv.h" |
| 50 | #include "intel_frontbuffer.h" |
| 51 | #include "intel_mocs.h" |
| 52 | #include "intel_workarounds.h" |
| 53 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 54 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 55 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 56 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 57 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 58 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 59 | return false; |
| 60 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 61 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 62 | return true; |
| 63 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 64 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 67 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 68 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 69 | struct drm_mm_node *node, u32 size) |
| 70 | { |
| 71 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 72 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 73 | size, 0, I915_COLOR_UNEVICTABLE, |
| 74 | 0, ggtt->mappable_end, |
| 75 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void |
| 79 | remove_mappable_node(struct drm_mm_node *node) |
| 80 | { |
| 81 | drm_mm_remove_node(node); |
| 82 | } |
| 83 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 84 | /* some bookkeeping */ |
| 85 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, |
Chris Wilson | 3ef7f22 | 2016-10-18 13:02:48 +0100 | [diff] [blame] | 86 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 87 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 88 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 89 | dev_priv->mm.object_count++; |
| 90 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 91 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, |
Chris Wilson | 3ef7f22 | 2016-10-18 13:02:48 +0100 | [diff] [blame] | 95 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 96 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 97 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 98 | dev_priv->mm.object_count--; |
| 99 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 100 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Chris Wilson | 21dd373 | 2011-01-26 15:55:56 +0000 | [diff] [blame] | 103 | static int |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 104 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 105 | { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 106 | int ret; |
| 107 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 108 | might_sleep(); |
| 109 | |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 110 | /* |
| 111 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging |
| 112 | * userspace. If it takes that long something really bad is going on and |
| 113 | * we should simply try to bail out and fail as gracefully as possible. |
| 114 | */ |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 115 | ret = wait_event_interruptible_timeout(error->reset_queue, |
Chris Wilson | 8c185ec | 2017-03-16 17:13:02 +0000 | [diff] [blame] | 116 | !i915_reset_backoff(error), |
Chris Wilson | b52992c | 2016-10-28 13:58:24 +0100 | [diff] [blame] | 117 | I915_RESET_TIMEOUT); |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 118 | if (ret == 0) { |
| 119 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); |
| 120 | return -EIO; |
| 121 | } else if (ret < 0) { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 122 | return ret; |
Chris Wilson | d98c52cf | 2016-04-13 17:35:05 +0100 | [diff] [blame] | 123 | } else { |
| 124 | return 0; |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 125 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 128 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 129 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 130 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 131 | int ret; |
| 132 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 133 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 134 | if (ret) |
| 135 | return ret; |
| 136 | |
| 137 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 138 | if (ret) |
| 139 | return ret; |
| 140 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 141 | return 0; |
| 142 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 143 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 144 | static u32 __i915_gem_park(struct drm_i915_private *i915) |
| 145 | { |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 146 | intel_wakeref_t wakeref; |
| 147 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 148 | GEM_TRACE("\n"); |
| 149 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 150 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 151 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 152 | GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 153 | |
| 154 | if (!i915->gt.awake) |
| 155 | return I915_EPOCH_INVALID; |
| 156 | |
| 157 | GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); |
| 158 | |
| 159 | /* |
| 160 | * Be paranoid and flush a concurrent interrupt to make sure |
| 161 | * we don't reactivate any irq tasklets after parking. |
| 162 | * |
| 163 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 164 | * there may still be an in-flight interrupt even though the CSB |
| 165 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 166 | * is completed before we continue, but it doesn't prevent the HW from |
| 167 | * raising a spurious interrupt later. To complete the shield we should |
| 168 | * coordinate disabling the CS irq with flushing the interrupts. |
| 169 | */ |
| 170 | synchronize_irq(i915->drm.irq); |
| 171 | |
| 172 | intel_engines_park(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 173 | i915_timelines_park(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 174 | |
| 175 | i915_pmu_gt_parked(i915); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 176 | i915_vma_parked(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 177 | |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 178 | wakeref = fetch_and_zero(&i915->gt.awake); |
| 179 | GEM_BUG_ON(!wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 180 | |
| 181 | if (INTEL_GEN(i915) >= 6) |
| 182 | gen6_rps_idle(i915); |
| 183 | |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 184 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 185 | |
| 186 | return i915->gt.epoch; |
| 187 | } |
| 188 | |
| 189 | void i915_gem_park(struct drm_i915_private *i915) |
| 190 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 191 | GEM_TRACE("\n"); |
| 192 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 193 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 194 | GEM_BUG_ON(i915->gt.active_requests); |
| 195 | |
| 196 | if (!i915->gt.awake) |
| 197 | return; |
| 198 | |
| 199 | /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ |
| 200 | mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); |
| 201 | } |
| 202 | |
| 203 | void i915_gem_unpark(struct drm_i915_private *i915) |
| 204 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 205 | GEM_TRACE("\n"); |
| 206 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 207 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 208 | GEM_BUG_ON(!i915->gt.active_requests); |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 209 | assert_rpm_wakelock_held(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 210 | |
| 211 | if (i915->gt.awake) |
| 212 | return; |
| 213 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 214 | /* |
| 215 | * It seems that the DMC likes to transition between the DC states a lot |
| 216 | * when there are no connected displays (no active power domains) during |
| 217 | * command submission. |
| 218 | * |
| 219 | * This activity has negative impact on the performance of the chip with |
| 220 | * huge latencies observed in the interrupt handler and elsewhere. |
| 221 | * |
| 222 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 223 | * GT activity, preventing any DC state transitions. |
| 224 | */ |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 225 | i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 226 | GEM_BUG_ON(!i915->gt.awake); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 227 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 228 | if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ |
| 229 | i915->gt.epoch = 1; |
| 230 | |
| 231 | intel_enable_gt_powersave(i915); |
| 232 | i915_update_gfx_val(i915); |
| 233 | if (INTEL_GEN(i915) >= 6) |
| 234 | gen6_rps_busy(i915); |
| 235 | i915_pmu_gt_unparked(i915); |
| 236 | |
| 237 | intel_engines_unpark(i915); |
| 238 | |
| 239 | i915_queue_hangcheck(i915); |
| 240 | |
| 241 | queue_delayed_work(i915->wq, |
| 242 | &i915->gt.retire_work, |
| 243 | round_jiffies_up_relative(HZ)); |
| 244 | } |
| 245 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 246 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 247 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 248 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 249 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 250 | struct i915_ggtt *ggtt = &to_i915(dev)->ggtt; |
Joonas Lahtinen | 72e96d64 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 251 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 252 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 253 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 254 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 255 | mutex_lock(&ggtt->vm.mutex); |
| 256 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 257 | pinned = ggtt->vm.reserved; |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 258 | list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 259 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 260 | pinned += vma->node.size; |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 261 | |
| 262 | mutex_unlock(&ggtt->vm.mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 263 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 264 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 265 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 266 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 270 | static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 271 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 272 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 273 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 274 | struct sg_table *st; |
| 275 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 276 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 277 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 278 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 279 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 280 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 281 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 282 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 283 | /* Always aligning to the object size, allows a single allocation |
| 284 | * to handle all possible callers, and given typical object sizes, |
| 285 | * the alignment of the buddy allocation will naturally match. |
| 286 | */ |
| 287 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 288 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 289 | roundup_pow_of_two(obj->base.size)); |
| 290 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 291 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 292 | |
| 293 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 294 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 295 | struct page *page; |
| 296 | char *src; |
| 297 | |
| 298 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 299 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 300 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 301 | goto err_phys; |
| 302 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 303 | |
| 304 | src = kmap_atomic(page); |
| 305 | memcpy(vaddr, src, PAGE_SIZE); |
| 306 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 307 | kunmap_atomic(src); |
| 308 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 309 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 310 | vaddr += PAGE_SIZE; |
| 311 | } |
| 312 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 313 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 314 | |
| 315 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 316 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 317 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 318 | goto err_phys; |
| 319 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 320 | |
| 321 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 322 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 323 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 324 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | sg = st->sgl; |
| 328 | sg->offset = 0; |
| 329 | sg->length = obj->base.size; |
| 330 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 331 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 332 | sg_dma_len(sg) = obj->base.size; |
| 333 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 334 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 335 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 336 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 337 | |
| 338 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 339 | |
| 340 | err_phys: |
| 341 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 342 | |
| 343 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 346 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 347 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 348 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 349 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 350 | if (cpu_write_needs_clflush(obj)) |
| 351 | obj->cache_dirty = true; |
| 352 | } |
| 353 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 354 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 355 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 356 | struct sg_table *pages, |
| 357 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 358 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 359 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 360 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 361 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 362 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 363 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 364 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 365 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 366 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 367 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 368 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 369 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static void |
| 373 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 374 | struct sg_table *pages) |
| 375 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 376 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 377 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 378 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 379 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 380 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 381 | int i; |
| 382 | |
| 383 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 384 | struct page *page; |
| 385 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 386 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 387 | page = shmem_read_mapping_page(mapping, i); |
| 388 | if (IS_ERR(page)) |
| 389 | continue; |
| 390 | |
| 391 | dst = kmap_atomic(page); |
| 392 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 393 | memcpy(dst, vaddr, PAGE_SIZE); |
| 394 | kunmap_atomic(dst); |
| 395 | |
| 396 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 397 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 398 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 399 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 400 | vaddr += PAGE_SIZE; |
| 401 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 402 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 405 | sg_free_table(pages); |
| 406 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 407 | |
| 408 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static void |
| 412 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 413 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 414 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 418 | .get_pages = i915_gem_object_get_pages_phys, |
| 419 | .put_pages = i915_gem_object_put_pages_phys, |
| 420 | .release = i915_gem_object_release_phys, |
| 421 | }; |
| 422 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 423 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 424 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 425 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 426 | { |
| 427 | struct i915_vma *vma; |
| 428 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 429 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 430 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 431 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 432 | |
| 433 | /* Closed vma are removed from the obj->vma_list - but they may |
| 434 | * still have an active binding on the object. To remove those we |
| 435 | * must wait for all rendering to complete to the object (as unbinding |
| 436 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 437 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 438 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 439 | if (ret) |
| 440 | return ret; |
| 441 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 442 | spin_lock(&obj->vma.lock); |
| 443 | while (!ret && (vma = list_first_entry_or_null(&obj->vma.list, |
| 444 | struct i915_vma, |
| 445 | obj_link))) { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 446 | list_move_tail(&vma->obj_link, &still_in_list); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 447 | spin_unlock(&obj->vma.lock); |
| 448 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 449 | ret = i915_vma_unbind(vma); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 450 | |
| 451 | spin_lock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 452 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 453 | list_splice(&still_in_list, &obj->vma.list); |
| 454 | spin_unlock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 459 | static long |
| 460 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 461 | unsigned int flags, |
| 462 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 463 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 464 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 465 | struct i915_request *rq; |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 466 | |
| 467 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 468 | |
| 469 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 470 | return timeout; |
| 471 | |
| 472 | if (!dma_fence_is_i915(fence)) |
| 473 | return dma_fence_wait_timeout(fence, |
| 474 | flags & I915_WAIT_INTERRUPTIBLE, |
| 475 | timeout); |
| 476 | |
| 477 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 478 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 479 | goto out; |
| 480 | |
Chris Wilson | e9af4ea | 2018-01-18 13:16:09 +0000 | [diff] [blame] | 481 | /* |
| 482 | * This client is about to stall waiting for the GPU. In many cases |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 483 | * this is undesirable and limits the throughput of the system, as |
| 484 | * many clients cannot continue processing user input/output whilst |
| 485 | * blocked. RPS autotuning may take tens of milliseconds to respond |
| 486 | * to the GPU load and thus incurs additional latency for the client. |
| 487 | * We can circumvent that by promoting the GPU frequency to maximum |
| 488 | * before we wait. This makes the GPU throttle up much more quickly |
| 489 | * (good for benchmarks and user experience, e.g. window animations), |
| 490 | * but at a cost of spending more power processing the workload |
| 491 | * (bad for battery). Not all clients even want their results |
| 492 | * immediately and for them we should just let the GPU select its own |
| 493 | * frequency to maximise efficiency. To prevent a single client from |
| 494 | * forcing the clocks too high for the whole system, we only allow |
| 495 | * each client to waitboost once in a busy period. |
| 496 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 497 | if (rps_client && !i915_request_started(rq)) { |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 498 | if (INTEL_GEN(rq->i915) >= 6) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 499 | gen6_rps_boost(rq, rps_client); |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 502 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 503 | |
| 504 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 505 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 506 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 507 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 508 | return timeout; |
| 509 | } |
| 510 | |
| 511 | static long |
| 512 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 513 | unsigned int flags, |
| 514 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 515 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 516 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 517 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 518 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 519 | bool prune_fences = false; |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 520 | |
| 521 | if (flags & I915_WAIT_ALL) { |
| 522 | struct dma_fence **shared; |
| 523 | unsigned int count, i; |
| 524 | int ret; |
| 525 | |
| 526 | ret = reservation_object_get_fences_rcu(resv, |
| 527 | &excl, &count, &shared); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
| 531 | for (i = 0; i < count; i++) { |
| 532 | timeout = i915_gem_object_wait_fence(shared[i], |
| 533 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 534 | rps_client); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 535 | if (timeout < 0) |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 536 | break; |
| 537 | |
| 538 | dma_fence_put(shared[i]); |
| 539 | } |
| 540 | |
| 541 | for (; i < count; i++) |
| 542 | dma_fence_put(shared[i]); |
| 543 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 544 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 545 | /* |
| 546 | * If both shared fences and an exclusive fence exist, |
| 547 | * then by construction the shared fences must be later |
| 548 | * than the exclusive fence. If we successfully wait for |
| 549 | * all the shared fences, we know that the exclusive fence |
| 550 | * must all be signaled. If all the shared fences are |
| 551 | * signaled, we can prune the array and recover the |
| 552 | * floating references on the fences/requests. |
| 553 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 554 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 555 | } else { |
| 556 | excl = reservation_object_get_excl_rcu(resv); |
| 557 | } |
| 558 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 559 | if (excl && timeout >= 0) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 560 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
| 561 | rps_client); |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 562 | |
| 563 | dma_fence_put(excl); |
| 564 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 565 | /* |
| 566 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 567 | * signaled and that the reservation object has not been changed (i.e. |
| 568 | * no new fences have been added). |
| 569 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 570 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 571 | if (reservation_object_trylock(resv)) { |
| 572 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 573 | reservation_object_add_excl_fence(resv, NULL); |
| 574 | reservation_object_unlock(resv); |
| 575 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 578 | return timeout; |
| 579 | } |
| 580 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 581 | static void __fence_set_priority(struct dma_fence *fence, |
| 582 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 583 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 584 | struct i915_request *rq; |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 585 | struct intel_engine_cs *engine; |
| 586 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 587 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 588 | return; |
| 589 | |
| 590 | rq = to_request(fence); |
| 591 | engine = rq->engine; |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 592 | |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 593 | local_bh_disable(); |
| 594 | rcu_read_lock(); /* RCU serialisation for set-wedged protection */ |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 595 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 596 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 597 | rcu_read_unlock(); |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 598 | local_bh_enable(); /* kick the tasklets if queues were reprioritised */ |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 601 | static void fence_set_priority(struct dma_fence *fence, |
| 602 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 603 | { |
| 604 | /* Recurse once into a fence-array */ |
| 605 | if (dma_fence_is_array(fence)) { |
| 606 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 607 | int i; |
| 608 | |
| 609 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 610 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 611 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 612 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | int |
| 617 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 618 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 619 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 620 | { |
| 621 | struct dma_fence *excl; |
| 622 | |
| 623 | if (flags & I915_WAIT_ALL) { |
| 624 | struct dma_fence **shared; |
| 625 | unsigned int count, i; |
| 626 | int ret; |
| 627 | |
| 628 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 629 | &excl, &count, &shared); |
| 630 | if (ret) |
| 631 | return ret; |
| 632 | |
| 633 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 634 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 635 | dma_fence_put(shared[i]); |
| 636 | } |
| 637 | |
| 638 | kfree(shared); |
| 639 | } else { |
| 640 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 641 | } |
| 642 | |
| 643 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 644 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f5 | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 645 | dma_fence_put(excl); |
| 646 | } |
| 647 | return 0; |
| 648 | } |
| 649 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 650 | /** |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 651 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 652 | * @obj: i915 gem object |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 653 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 654 | * @timeout: how long to wait |
Chris Wilson | a0a8b1c | 2017-11-09 14:06:44 +0000 | [diff] [blame] | 655 | * @rps_client: client (user process) to charge for any waitboosting |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 656 | */ |
| 657 | int |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 658 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 659 | unsigned int flags, |
| 660 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 661 | struct intel_rps_client *rps_client) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 662 | { |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 663 | might_sleep(); |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 664 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 665 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 666 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 667 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 668 | rps_client); |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 669 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 673 | { |
| 674 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 675 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 676 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 679 | static int |
| 680 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 681 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 682 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 683 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 684 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 685 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 686 | |
| 687 | /* We manually control the domain here and pretend that it |
| 688 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 689 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 690 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 691 | if (copy_from_user(vaddr, user_data, args->size)) |
| 692 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 693 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 694 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 695 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 696 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 697 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 698 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 701 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 702 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 703 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 707 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 708 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 709 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 712 | static int |
| 713 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 714 | struct drm_i915_private *dev_priv, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 715 | u64 size, |
| 716 | u32 *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 717 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 718 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 719 | int ret; |
| 720 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 721 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 722 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 723 | if (size == 0) |
| 724 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 725 | |
| 726 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 727 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 728 | if (IS_ERR(obj)) |
| 729 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 730 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 731 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 732 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 733 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 734 | if (ret) |
| 735 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 736 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 737 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 738 | return 0; |
| 739 | } |
| 740 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 741 | int |
| 742 | i915_gem_dumb_create(struct drm_file *file, |
| 743 | struct drm_device *dev, |
| 744 | struct drm_mode_create_dumb *args) |
| 745 | { |
| 746 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 747 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 748 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 749 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 750 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 751 | } |
| 752 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 753 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 754 | { |
| 755 | return !(obj->cache_level == I915_CACHE_NONE || |
| 756 | obj->cache_level == I915_CACHE_WT); |
| 757 | } |
| 758 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 759 | /** |
| 760 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 761 | * @dev: drm device pointer |
| 762 | * @data: ioctl data blob |
| 763 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 764 | */ |
| 765 | int |
| 766 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 767 | struct drm_file *file) |
| 768 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 769 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 770 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 771 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 772 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 773 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 774 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 775 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 776 | } |
| 777 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 778 | static inline enum fb_op_origin |
| 779 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 780 | { |
| 781 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 782 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 783 | } |
| 784 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 785 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 786 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 787 | intel_wakeref_t wakeref; |
| 788 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 789 | /* |
| 790 | * No actual flushing is required for the GTT write domain for reads |
| 791 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 792 | * as far as we know, so there's no chipset flush. It also doesn't |
| 793 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 794 | * |
| 795 | * However, we do have to enforce the order so that all writes through |
| 796 | * the GTT land before any writes to the device, such as updates to |
| 797 | * the GATT itself. |
| 798 | * |
| 799 | * We also have to wait a bit for the writes to land from the GTT. |
| 800 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 801 | * timing. This issue has only been observed when switching quickly |
| 802 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 803 | * and it appears to only affect discrete GTT blocks (i.e. on LLC |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 804 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 805 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 806 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 807 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 808 | wmb(); |
| 809 | |
| 810 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 811 | return; |
| 812 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 813 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 814 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 815 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 816 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 817 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 818 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 819 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 820 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 821 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static void |
| 825 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 826 | { |
| 827 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 828 | struct i915_vma *vma; |
| 829 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 830 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 831 | return; |
| 832 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 833 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 834 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 835 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 836 | |
| 837 | intel_fb_obj_flush(obj, |
| 838 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 839 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 840 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 841 | if (vma->iomap) |
| 842 | continue; |
| 843 | |
| 844 | i915_vma_unset_ggtt_write(vma); |
| 845 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 846 | break; |
| 847 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 848 | case I915_GEM_DOMAIN_WC: |
| 849 | wmb(); |
| 850 | break; |
| 851 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 852 | case I915_GEM_DOMAIN_CPU: |
| 853 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 854 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 855 | |
| 856 | case I915_GEM_DOMAIN_RENDER: |
| 857 | if (gpu_write_needs_clflush(obj)) |
| 858 | obj->cache_dirty = true; |
| 859 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 862 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 865 | /* |
| 866 | * Pins the specified object's pages and synchronizes the object with |
| 867 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 868 | * flush the object from the CPU cache. |
| 869 | */ |
| 870 | int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj, |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 871 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 872 | { |
| 873 | int ret; |
| 874 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 875 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 876 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 877 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 878 | if (!i915_gem_object_has_struct_page(obj)) |
| 879 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 880 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 881 | ret = i915_gem_object_wait(obj, |
| 882 | I915_WAIT_INTERRUPTIBLE | |
| 883 | I915_WAIT_LOCKED, |
| 884 | MAX_SCHEDULE_TIMEOUT, |
| 885 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 886 | if (ret) |
| 887 | return ret; |
| 888 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 889 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 890 | if (ret) |
| 891 | return ret; |
| 892 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 893 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 894 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 895 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 896 | if (ret) |
| 897 | goto err_unpin; |
| 898 | else |
| 899 | goto out; |
| 900 | } |
| 901 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 902 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 903 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 904 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 905 | * read domain and manually flush cachelines (if required). This |
| 906 | * optimizes for the case when the gpu will dirty the data |
| 907 | * anyway again before the next pread happens. |
| 908 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 909 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 910 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 911 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 912 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 913 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 914 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 915 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 916 | |
| 917 | err_unpin: |
| 918 | i915_gem_object_unpin_pages(obj); |
| 919 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 923 | unsigned int *needs_clflush) |
| 924 | { |
| 925 | int ret; |
| 926 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 927 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 928 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 929 | *needs_clflush = 0; |
| 930 | if (!i915_gem_object_has_struct_page(obj)) |
| 931 | return -ENODEV; |
| 932 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 933 | ret = i915_gem_object_wait(obj, |
| 934 | I915_WAIT_INTERRUPTIBLE | |
| 935 | I915_WAIT_LOCKED | |
| 936 | I915_WAIT_ALL, |
| 937 | MAX_SCHEDULE_TIMEOUT, |
| 938 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 939 | if (ret) |
| 940 | return ret; |
| 941 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 942 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 943 | if (ret) |
| 944 | return ret; |
| 945 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 946 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 947 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 948 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 949 | if (ret) |
| 950 | goto err_unpin; |
| 951 | else |
| 952 | goto out; |
| 953 | } |
| 954 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 955 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 956 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 957 | /* If we're not in the cpu write domain, set ourself into the |
| 958 | * gtt write domain and manually flush cachelines (as required). |
| 959 | * This optimizes for the case when the gpu will use the data |
| 960 | * right away and we therefore have to clflush anyway. |
| 961 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 962 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 963 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 964 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 965 | /* |
| 966 | * Same trick applies to invalidate partially written |
| 967 | * cachelines read before writing. |
| 968 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 969 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 970 | *needs_clflush |= CLFLUSH_BEFORE; |
| 971 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 972 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 973 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 974 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 975 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 976 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 977 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 978 | |
| 979 | err_unpin: |
| 980 | i915_gem_object_unpin_pages(obj); |
| 981 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 984 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 985 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 986 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 987 | { |
| 988 | char *vaddr; |
| 989 | int ret; |
| 990 | |
| 991 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 992 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 993 | if (needs_clflush) |
| 994 | drm_clflush_virt_range(vaddr + offset, len); |
| 995 | |
| 996 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 997 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 998 | kunmap(page); |
| 999 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1000 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | static int |
| 1004 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 1005 | struct drm_i915_gem_pread *args) |
| 1006 | { |
| 1007 | char __user *user_data; |
| 1008 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1009 | unsigned int needs_clflush; |
| 1010 | unsigned int idx, offset; |
| 1011 | int ret; |
| 1012 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1013 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 1014 | if (ret) |
| 1015 | return ret; |
| 1016 | |
| 1017 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 1018 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 1019 | if (ret) |
| 1020 | return ret; |
| 1021 | |
| 1022 | remain = args->size; |
| 1023 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1024 | offset = offset_in_page(args->offset); |
| 1025 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1026 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1027 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1028 | |
| 1029 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1030 | needs_clflush); |
| 1031 | if (ret) |
| 1032 | break; |
| 1033 | |
| 1034 | remain -= length; |
| 1035 | user_data += length; |
| 1036 | offset = 0; |
| 1037 | } |
| 1038 | |
| 1039 | i915_gem_obj_finish_shmem_access(obj); |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
| 1043 | static inline bool |
| 1044 | gtt_user_read(struct io_mapping *mapping, |
| 1045 | loff_t base, int offset, |
| 1046 | char __user *user_data, int length) |
| 1047 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1048 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1049 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1050 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1051 | /* We can use the cpu mem copy function because this is X86. */ |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1052 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1053 | unwritten = __copy_to_user_inatomic(user_data, |
| 1054 | (void __force *)vaddr + offset, |
| 1055 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1056 | io_mapping_unmap_atomic(vaddr); |
| 1057 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1058 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1059 | unwritten = copy_to_user(user_data, |
| 1060 | (void __force *)vaddr + offset, |
| 1061 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1062 | io_mapping_unmap(vaddr); |
| 1063 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1064 | return unwritten; |
| 1065 | } |
| 1066 | |
| 1067 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1068 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1069 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1070 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1071 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1072 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1073 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1074 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1075 | struct i915_vma *vma; |
| 1076 | void __user *user_data; |
| 1077 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1078 | int ret; |
| 1079 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1080 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1081 | if (ret) |
| 1082 | return ret; |
| 1083 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1084 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1085 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1086 | PIN_MAPPABLE | |
| 1087 | PIN_NONFAULT | |
| 1088 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1089 | if (!IS_ERR(vma)) { |
| 1090 | node.start = i915_ggtt_offset(vma); |
| 1091 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1092 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1093 | if (ret) { |
| 1094 | i915_vma_unpin(vma); |
| 1095 | vma = ERR_PTR(ret); |
| 1096 | } |
| 1097 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1098 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1099 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1100 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1101 | goto out_unlock; |
| 1102 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1106 | if (ret) |
| 1107 | goto out_unpin; |
| 1108 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1109 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1110 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1111 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1112 | remain = args->size; |
| 1113 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1114 | |
| 1115 | while (remain > 0) { |
| 1116 | /* Operation in this page |
| 1117 | * |
| 1118 | * page_base = page offset within aperture |
| 1119 | * page_offset = offset within page |
| 1120 | * page_length = bytes to copy for this page |
| 1121 | */ |
| 1122 | u32 page_base = node.start; |
| 1123 | unsigned page_offset = offset_in_page(offset); |
| 1124 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1125 | page_length = remain < page_length ? remain : page_length; |
| 1126 | if (node.allocated) { |
| 1127 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1128 | ggtt->vm.insert_page(&ggtt->vm, |
| 1129 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1130 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1131 | wmb(); |
| 1132 | } else { |
| 1133 | page_base += offset & PAGE_MASK; |
| 1134 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1135 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1136 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1137 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1138 | ret = -EFAULT; |
| 1139 | break; |
| 1140 | } |
| 1141 | |
| 1142 | remain -= page_length; |
| 1143 | user_data += page_length; |
| 1144 | offset += page_length; |
| 1145 | } |
| 1146 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1147 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1148 | out_unpin: |
| 1149 | if (node.allocated) { |
| 1150 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1151 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1152 | remove_mappable_node(&node); |
| 1153 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1154 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1155 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1156 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1157 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1158 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1159 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1160 | return ret; |
| 1161 | } |
| 1162 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1163 | /** |
| 1164 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1165 | * @dev: drm device pointer |
| 1166 | * @data: ioctl data blob |
| 1167 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1168 | * |
| 1169 | * On error, the contents of *data are undefined. |
| 1170 | */ |
| 1171 | int |
| 1172 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1173 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1174 | { |
| 1175 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1176 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1177 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1178 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1179 | if (args->size == 0) |
| 1180 | return 0; |
| 1181 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1182 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1183 | args->size)) |
| 1184 | return -EFAULT; |
| 1185 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1186 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1187 | if (!obj) |
| 1188 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1189 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1190 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1191 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1192 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1193 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1196 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1197 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1198 | ret = i915_gem_object_wait(obj, |
| 1199 | I915_WAIT_INTERRUPTIBLE, |
| 1200 | MAX_SCHEDULE_TIMEOUT, |
| 1201 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1202 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1203 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1204 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1205 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1206 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1207 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1208 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1209 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1210 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1211 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1212 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1213 | i915_gem_object_unpin_pages(obj); |
| 1214 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1215 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1216 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1219 | /* This is the fast write path which cannot handle |
| 1220 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1221 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1222 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1223 | static inline bool |
| 1224 | ggtt_write(struct io_mapping *mapping, |
| 1225 | loff_t base, int offset, |
| 1226 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1227 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1228 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1229 | unsigned long unwritten; |
| 1230 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1231 | /* We can use the cpu mem copy function because this is X86. */ |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1232 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1233 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1234 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1235 | io_mapping_unmap_atomic(vaddr); |
| 1236 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1237 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1238 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1239 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1240 | io_mapping_unmap(vaddr); |
| 1241 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1242 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1243 | return unwritten; |
| 1244 | } |
| 1245 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1246 | /** |
| 1247 | * This is the fast pwrite path, where we copy the data directly from the |
| 1248 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1249 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1250 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1251 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1252 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1253 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1254 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1255 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1256 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1257 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1258 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1259 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1260 | struct i915_vma *vma; |
| 1261 | u64 remain, offset; |
| 1262 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1263 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1264 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1265 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1266 | if (ret) |
| 1267 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1268 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1269 | if (i915_gem_object_has_struct_page(obj)) { |
| 1270 | /* |
| 1271 | * Avoid waking the device up if we can fallback, as |
| 1272 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1273 | * depending on PCI sleeps and our own resume time). |
| 1274 | * This easily dwarfs any performance advantage from |
| 1275 | * using the cache bypass of indirect GGTT access. |
| 1276 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1277 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1278 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1279 | ret = -EFAULT; |
| 1280 | goto out_unlock; |
| 1281 | } |
| 1282 | } else { |
| 1283 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1284 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1287 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1288 | PIN_MAPPABLE | |
| 1289 | PIN_NONFAULT | |
| 1290 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1291 | if (!IS_ERR(vma)) { |
| 1292 | node.start = i915_ggtt_offset(vma); |
| 1293 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1294 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1295 | if (ret) { |
| 1296 | i915_vma_unpin(vma); |
| 1297 | vma = ERR_PTR(ret); |
| 1298 | } |
| 1299 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1300 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1301 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1302 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1303 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1304 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1305 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1306 | |
| 1307 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1308 | if (ret) |
| 1309 | goto out_unpin; |
| 1310 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1311 | mutex_unlock(&i915->drm.struct_mutex); |
| 1312 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1313 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1314 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1315 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1316 | offset = args->offset; |
| 1317 | remain = args->size; |
| 1318 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1319 | /* Operation in this page |
| 1320 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1321 | * page_base = page offset within aperture |
| 1322 | * page_offset = offset within page |
| 1323 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1324 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1325 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1326 | unsigned int page_offset = offset_in_page(offset); |
| 1327 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1328 | page_length = remain < page_length ? remain : page_length; |
| 1329 | if (node.allocated) { |
| 1330 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1331 | ggtt->vm.insert_page(&ggtt->vm, |
| 1332 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1333 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1334 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1335 | } else { |
| 1336 | page_base += offset & PAGE_MASK; |
| 1337 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1338 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1339 | * source page isn't available. Return the error and we'll |
| 1340 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1341 | * If the object is non-shmem backed, we retry again with the |
| 1342 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1343 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1344 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1345 | user_data, page_length)) { |
| 1346 | ret = -EFAULT; |
| 1347 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1348 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1349 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1350 | remain -= page_length; |
| 1351 | user_data += page_length; |
| 1352 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1353 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1354 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1355 | |
| 1356 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1357 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1358 | if (node.allocated) { |
| 1359 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1360 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1361 | remove_mappable_node(&node); |
| 1362 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1363 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1364 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1365 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1366 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1367 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1368 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1369 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1372 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1373 | * Flushes invalid cachelines before writing to the target if |
| 1374 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1375 | * writing if needs_clflush is set. |
| 1376 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1377 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1378 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1379 | bool needs_clflush_before, |
| 1380 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1381 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1382 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1383 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1384 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1385 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1386 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1387 | if (needs_clflush_before) |
| 1388 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1389 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1390 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1391 | if (!ret && needs_clflush_after) |
| 1392 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1393 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1394 | kunmap(page); |
| 1395 | |
| 1396 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static int |
| 1400 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1401 | const struct drm_i915_gem_pwrite *args) |
| 1402 | { |
| 1403 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1404 | void __user *user_data; |
| 1405 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1406 | unsigned int partial_cacheline_write; |
| 1407 | unsigned int needs_clflush; |
| 1408 | unsigned int offset, idx; |
| 1409 | int ret; |
| 1410 | |
| 1411 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1412 | if (ret) |
| 1413 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1414 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1415 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1416 | mutex_unlock(&i915->drm.struct_mutex); |
| 1417 | if (ret) |
| 1418 | return ret; |
| 1419 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1420 | /* If we don't overwrite a cacheline completely we need to be |
| 1421 | * careful to have up-to-date data by first clflushing. Don't |
| 1422 | * overcomplicate things and flush the entire patch. |
| 1423 | */ |
| 1424 | partial_cacheline_write = 0; |
| 1425 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1426 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1427 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1428 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1429 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1430 | offset = offset_in_page(args->offset); |
| 1431 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1432 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1433 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1434 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1435 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1436 | (offset | length) & partial_cacheline_write, |
| 1437 | needs_clflush & CLFLUSH_AFTER); |
| 1438 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1439 | break; |
| 1440 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1441 | remain -= length; |
| 1442 | user_data += length; |
| 1443 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1446 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1447 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1448 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1453 | * @dev: drm device |
| 1454 | * @data: ioctl data blob |
| 1455 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1456 | * |
| 1457 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1458 | */ |
| 1459 | int |
| 1460 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1461 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1462 | { |
| 1463 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1464 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1465 | int ret; |
| 1466 | |
| 1467 | if (args->size == 0) |
| 1468 | return 0; |
| 1469 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1470 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1471 | return -EFAULT; |
| 1472 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1473 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1474 | if (!obj) |
| 1475 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1476 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1477 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1478 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1479 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1480 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1483 | /* Writes not allowed into this read-only object */ |
| 1484 | if (i915_gem_object_is_readonly(obj)) { |
| 1485 | ret = -EINVAL; |
| 1486 | goto err; |
| 1487 | } |
| 1488 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1489 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1490 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1491 | ret = -ENODEV; |
| 1492 | if (obj->ops->pwrite) |
| 1493 | ret = obj->ops->pwrite(obj, args); |
| 1494 | if (ret != -ENODEV) |
| 1495 | goto err; |
| 1496 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1497 | ret = i915_gem_object_wait(obj, |
| 1498 | I915_WAIT_INTERRUPTIBLE | |
| 1499 | I915_WAIT_ALL, |
| 1500 | MAX_SCHEDULE_TIMEOUT, |
| 1501 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1502 | if (ret) |
| 1503 | goto err; |
| 1504 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1505 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1506 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1507 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1508 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1509 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1510 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1511 | * it would end up going through the fenced access, and we'll get |
| 1512 | * different detiling behavior between reading and writing. |
| 1513 | * pread/pwrite currently are reading and writing from the CPU |
| 1514 | * perspective, requiring manual detiling by the client. |
| 1515 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1516 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1517 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1518 | /* Note that the gtt paths might fail with non-page-backed user |
| 1519 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1520 | * textures). Fallback to the shmem path in that case. |
| 1521 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1522 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1523 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1524 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1525 | if (obj->phys_handle) |
| 1526 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1527 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1528 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1529 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1530 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1531 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1532 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1533 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1534 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1537 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1538 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1539 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1540 | struct list_head *list; |
| 1541 | struct i915_vma *vma; |
| 1542 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1543 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1544 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1545 | mutex_lock(&i915->ggtt.vm.mutex); |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1546 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1547 | if (!drm_mm_node_allocated(&vma->node)) |
| 1548 | continue; |
| 1549 | |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 1550 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1551 | } |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1552 | mutex_unlock(&i915->ggtt.vm.mutex); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1553 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1554 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1555 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1556 | list_move_tail(&obj->mm.link, list); |
| 1557 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1558 | } |
| 1559 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1560 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1561 | * Called when user space prepares to use an object with the CPU, either |
| 1562 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1563 | * @dev: drm device |
| 1564 | * @data: ioctl data blob |
| 1565 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1566 | */ |
| 1567 | int |
| 1568 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1569 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1570 | { |
| 1571 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1572 | struct drm_i915_gem_object *obj; |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1573 | u32 read_domains = args->read_domains; |
| 1574 | u32 write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1575 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1576 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1577 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1578 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1579 | return -EINVAL; |
| 1580 | |
| 1581 | /* Having something in the write domain implies it's in the read |
| 1582 | * domain, and only that read domain. Enforce that in the request. |
| 1583 | */ |
| 1584 | if (write_domain != 0 && read_domains != write_domain) |
| 1585 | return -EINVAL; |
| 1586 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1587 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1588 | if (!obj) |
| 1589 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1590 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1591 | /* Try to flush the object off the GPU without holding the lock. |
| 1592 | * We will repeat the flush holding the lock in the normal manner |
| 1593 | * to catch cases where we are gazumped. |
| 1594 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1595 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1596 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1597 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1598 | (write_domain ? I915_WAIT_ALL : 0), |
| 1599 | MAX_SCHEDULE_TIMEOUT, |
| 1600 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1601 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1602 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1603 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1604 | /* |
| 1605 | * Proxy objects do not control access to the backing storage, ergo |
| 1606 | * they cannot be used as a means to manipulate the cache domain |
| 1607 | * tracking for that backing storage. The proxy object is always |
| 1608 | * considered to be outside of any cache domain. |
| 1609 | */ |
| 1610 | if (i915_gem_object_is_proxy(obj)) { |
| 1611 | err = -ENXIO; |
| 1612 | goto out; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1617 | * direct access in memory with previous cached writes through |
| 1618 | * shmemfs and that our cache domain tracking remains valid. |
| 1619 | * For example, if the obj->filp was moved to swap without us |
| 1620 | * being notified and releasing the pages, we would mistakenly |
| 1621 | * continue to assume that the obj remained out of the CPU cached |
| 1622 | * domain. |
| 1623 | */ |
| 1624 | err = i915_gem_object_pin_pages(obj); |
| 1625 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1626 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1627 | |
| 1628 | err = i915_mutex_lock_interruptible(dev); |
| 1629 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1630 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1631 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1632 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1633 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1634 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1635 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1636 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1637 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1638 | |
| 1639 | /* And bump the LRU for this access */ |
| 1640 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1641 | |
| 1642 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1643 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1644 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1645 | intel_fb_obj_invalidate(obj, |
| 1646 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1647 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1648 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1649 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1650 | out: |
| 1651 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1652 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /** |
| 1656 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1657 | * @dev: drm device |
| 1658 | * @data: ioctl data blob |
| 1659 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1660 | */ |
| 1661 | int |
| 1662 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1663 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1664 | { |
| 1665 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1666 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1667 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1668 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1669 | if (!obj) |
| 1670 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1671 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1672 | /* |
| 1673 | * Proxy objects are barred from CPU access, so there is no |
| 1674 | * need to ban sw_finish as it is a nop. |
| 1675 | */ |
| 1676 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1677 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1678 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1679 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1680 | |
| 1681 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1684 | static inline bool |
| 1685 | __vma_matches(struct vm_area_struct *vma, struct file *filp, |
| 1686 | unsigned long addr, unsigned long size) |
| 1687 | { |
| 1688 | if (vma->vm_file != filp) |
| 1689 | return false; |
| 1690 | |
| 1691 | return vma->vm_start == addr && (vma->vm_end - vma->vm_start) == size; |
| 1692 | } |
| 1693 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1694 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1695 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1696 | * it is mapped to. |
| 1697 | * @dev: drm device |
| 1698 | * @data: ioctl data blob |
| 1699 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1700 | * |
| 1701 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1702 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1703 | * |
| 1704 | * IMPORTANT: |
| 1705 | * |
| 1706 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1707 | * mmap support, please don't implement mmap support like here. The modern way |
| 1708 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1709 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1710 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1711 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1712 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1713 | */ |
| 1714 | int |
| 1715 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1716 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1717 | { |
| 1718 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1719 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1720 | unsigned long addr; |
| 1721 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1722 | if (args->flags & ~(I915_MMAP_WC)) |
| 1723 | return -EINVAL; |
| 1724 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1725 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1726 | return -ENODEV; |
| 1727 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1728 | obj = i915_gem_object_lookup(file, args->handle); |
| 1729 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1730 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1731 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1732 | /* prime objects have no backing filp to GEM mmap |
| 1733 | * pages from. |
| 1734 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1735 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1736 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1737 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1740 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1741 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1742 | args->offset); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1743 | if (IS_ERR_VALUE(addr)) |
| 1744 | goto err; |
| 1745 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1746 | if (args->flags & I915_MMAP_WC) { |
| 1747 | struct mm_struct *mm = current->mm; |
| 1748 | struct vm_area_struct *vma; |
| 1749 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1750 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1751 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1752 | return -EINTR; |
| 1753 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1754 | vma = find_vma(mm, addr); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1755 | if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1756 | vma->vm_page_prot = |
| 1757 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1758 | else |
| 1759 | addr = -ENOMEM; |
| 1760 | up_write(&mm->mmap_sem); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1761 | if (IS_ERR_VALUE(addr)) |
| 1762 | goto err; |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1763 | |
| 1764 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1765 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1766 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1767 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1768 | |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1769 | args->addr_ptr = (u64)addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1770 | |
| 1771 | return 0; |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1772 | |
| 1773 | err: |
| 1774 | i915_gem_object_put(obj); |
| 1775 | |
| 1776 | return addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1779 | static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj) |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1780 | { |
Chris Wilson | 6649a0b6 | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1781 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1782 | } |
| 1783 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1784 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1785 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1786 | * |
| 1787 | * A history of the GTT mmap interface: |
| 1788 | * |
| 1789 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1790 | * aligned and suitable for fencing, and still fit into the available |
| 1791 | * mappable space left by the pinned display objects. A classic problem |
| 1792 | * we called the page-fault-of-doom where we would ping-pong between |
| 1793 | * two objects that could not fit inside the GTT and so the memcpy |
| 1794 | * would page one object in at the expense of the other between every |
| 1795 | * single byte. |
| 1796 | * |
| 1797 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1798 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1799 | * object is too large for the available space (or simply too large |
| 1800 | * for the mappable aperture!), a view is created instead and faulted |
| 1801 | * into userspace. (This view is aligned and sized appropriately for |
| 1802 | * fenced access.) |
| 1803 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1804 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1805 | * delayed writes via GTT before performing direct access via WC. |
| 1806 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1807 | * Restrictions: |
| 1808 | * |
| 1809 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1810 | * hangs on some architectures, corruption on others. An attempt to service |
| 1811 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1812 | * |
| 1813 | * * the object must be able to fit into RAM (physical memory, though no |
| 1814 | * limited to the mappable aperture). |
| 1815 | * |
| 1816 | * |
| 1817 | * Caveats: |
| 1818 | * |
| 1819 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1820 | * all data to system memory. Subsequent access will not be synchronized. |
| 1821 | * |
| 1822 | * * all mappings are revoked on runtime device suspend. |
| 1823 | * |
| 1824 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1825 | * (older machines require fence register for display and blitter access |
| 1826 | * as well). Contention of the fence registers will cause the previous users |
| 1827 | * to be unmapped and any new access will generate new page faults. |
| 1828 | * |
| 1829 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1830 | * rather than the expected SIGSEGV. |
| 1831 | */ |
| 1832 | int i915_gem_mmap_gtt_version(void) |
| 1833 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1834 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1835 | } |
| 1836 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1837 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1838 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1839 | pgoff_t page_offset, |
| 1840 | unsigned int chunk) |
| 1841 | { |
| 1842 | struct i915_ggtt_view view; |
| 1843 | |
| 1844 | if (i915_gem_object_is_tiled(obj)) |
| 1845 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1846 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1847 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1848 | view.partial.offset = rounddown(page_offset, chunk); |
| 1849 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1850 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1851 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1852 | |
| 1853 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1854 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1855 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1856 | |
| 1857 | return view; |
| 1858 | } |
| 1859 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1860 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1861 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1862 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1863 | * |
| 1864 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1865 | * from userspace. The fault handler takes care of binding the object to |
| 1866 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1867 | * only if needed based on whether the old reg is still valid or the object |
| 1868 | * is tiled) and inserting a new PTE into the faulting process. |
| 1869 | * |
| 1870 | * Note that the faulting process may involve evicting existing objects |
| 1871 | * from the GTT and/or fence registers to make room. So performance may |
| 1872 | * suffer if the GTT working set is large or there are few fence registers |
| 1873 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1874 | * |
| 1875 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1876 | * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version). |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1877 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1878 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1879 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1880 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1881 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1882 | struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1883 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d64 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1884 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1885 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1886 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1887 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1888 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1889 | pgoff_t page_offset; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1890 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1891 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1892 | /* Sanity check that we allow writing into this object */ |
| 1893 | if (i915_gem_object_is_readonly(obj) && write) |
| 1894 | return VM_FAULT_SIGBUS; |
| 1895 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1896 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1897 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1898 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1899 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1900 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1901 | /* Try to flush the object off the GPU first without holding the lock. |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1902 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1903 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1904 | * where we are gazumped. |
| 1905 | */ |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1906 | ret = i915_gem_object_wait(obj, |
| 1907 | I915_WAIT_INTERRUPTIBLE, |
| 1908 | MAX_SCHEDULE_TIMEOUT, |
| 1909 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1910 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1911 | goto err; |
| 1912 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1913 | ret = i915_gem_object_pin_pages(obj); |
| 1914 | if (ret) |
| 1915 | goto err; |
| 1916 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1917 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1918 | |
| 1919 | ret = i915_mutex_lock_interruptible(dev); |
| 1920 | if (ret) |
| 1921 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1922 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1923 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1924 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1925 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1926 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1929 | |
Chris Wilson | a61007a8 | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1930 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1931 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1932 | PIN_MAPPABLE | |
| 1933 | PIN_NONBLOCK | |
| 1934 | PIN_NONFAULT); |
Chris Wilson | a61007a8 | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1935 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a8 | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1936 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1937 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1938 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1939 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1940 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1941 | flags = PIN_MAPPABLE; |
| 1942 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1943 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1944 | |
| 1945 | /* |
| 1946 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1947 | * all hope that the hardware is able to track future writes. |
| 1948 | */ |
| 1949 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1950 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1951 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1952 | if (IS_ERR(vma) && !view.type) { |
| 1953 | flags = PIN_MAPPABLE; |
| 1954 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1955 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1956 | } |
Chris Wilson | a61007a8 | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1957 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1958 | if (IS_ERR(vma)) { |
| 1959 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1960 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1961 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1962 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1963 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1964 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1965 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1966 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1967 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1968 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1969 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1970 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1971 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1972 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1973 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1974 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1975 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1976 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1977 | if (ret) |
| 1978 | goto err_fence; |
Chris Wilson | a61007a8 | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1979 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1980 | /* Mark as being mmapped into userspace for later revocation */ |
| 1981 | assert_rpm_wakelock_held(dev_priv); |
| 1982 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1983 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1984 | GEM_BUG_ON(!obj->userfault_count); |
| 1985 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1986 | i915_vma_set_ggtt_write(vma); |
| 1987 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1988 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1989 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1990 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1991 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1992 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1993 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1994 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1995 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1996 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1997 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1998 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1999 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 2000 | /* |
| 2001 | * We eat errors when the gpu is terminally wedged to avoid |
| 2002 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 2003 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 2004 | * and so needs to be reported. |
| 2005 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2006 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) |
| 2007 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 2008 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 2009 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 2010 | /* |
| 2011 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 2012 | * handler to reset everything when re-faulting in |
| 2013 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2014 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 2015 | case 0: |
| 2016 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 2017 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 2018 | case -EBUSY: |
| 2019 | /* |
| 2020 | * EBUSY is ok: this just means that another thread |
| 2021 | * already did the job. |
| 2022 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2023 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2024 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2025 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2026 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2027 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2028 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2029 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2030 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2031 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2032 | } |
| 2033 | } |
| 2034 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2035 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2036 | { |
| 2037 | struct i915_vma *vma; |
| 2038 | |
| 2039 | GEM_BUG_ON(!obj->userfault_count); |
| 2040 | |
| 2041 | obj->userfault_count = 0; |
| 2042 | list_del(&obj->userfault_link); |
| 2043 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2044 | obj->base.dev->anon_inode->i_mapping); |
| 2045 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 2046 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2047 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2050 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2051 | * i915_gem_release_mmap - remove physical page mappings |
| 2052 | * @obj: obj in question |
| 2053 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2054 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2055 | * relinquish ownership of the pages back to the system. |
| 2056 | * |
| 2057 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2058 | * object through the GTT and then lose the fence register due to |
| 2059 | * resource pressure. Similarly if the object has been moved out of the |
| 2060 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2061 | * mapping will then trigger a page fault on the next user access, allowing |
| 2062 | * fixup by i915_gem_fault(). |
| 2063 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2064 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2065 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2066 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2067 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2068 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2069 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2070 | /* Serialisation between user GTT access and our code depends upon |
| 2071 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2072 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2073 | * |
| 2074 | * Note that RPM complicates somewhat by adding an additional |
| 2075 | * requirement that operations to the GGTT be made holding the RPM |
| 2076 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2077 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2078 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2079 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2080 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2081 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2082 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2083 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2084 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2085 | |
| 2086 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2087 | * memory transactions from userspace before we return. The TLB |
| 2088 | * flushing implied above by changing the PTE above *should* be |
| 2089 | * sufficient, an extra barrier here just provides us with a bit |
| 2090 | * of paranoid documentation about our requirement to serialise |
| 2091 | * memory writes before touching registers / GSM. |
| 2092 | */ |
| 2093 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2094 | |
| 2095 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2096 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2097 | } |
| 2098 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2099 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2100 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2101 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2102 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2103 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2104 | /* |
| 2105 | * Only called during RPM suspend. All users of the userfault_list |
| 2106 | * must be holding an RPM wakeref to ensure that this can not |
| 2107 | * run concurrently with themselves (and use the struct_mutex for |
| 2108 | * protection between themselves). |
| 2109 | */ |
| 2110 | |
| 2111 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2112 | &dev_priv->mm.userfault_list, userfault_link) |
| 2113 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2114 | |
| 2115 | /* The fence will be lost when the device powers down. If any were |
| 2116 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2117 | * down! All other fences will be reacquired by the user upon waking. |
| 2118 | */ |
| 2119 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2120 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2121 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2122 | /* Ideally we want to assert that the fence register is not |
| 2123 | * live at this point (i.e. that no piece of code will be |
| 2124 | * trying to write through fence + GTT, as that both violates |
| 2125 | * our tracking of activity and associated locking/barriers, |
| 2126 | * but also is illegal given that the hw is powered down). |
| 2127 | * |
| 2128 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2129 | * That is not sufficient, and we need a more fine-grained |
| 2130 | * tool if we want to have a sanity check here. |
| 2131 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2132 | |
| 2133 | if (!reg->vma) |
| 2134 | continue; |
| 2135 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2136 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2137 | reg->dirty = true; |
| 2138 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2139 | } |
| 2140 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2141 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2142 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2143 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2144 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2145 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2146 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2147 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2148 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2149 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2150 | /* Attempt to reap some mmap space from dead objects */ |
| 2151 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2152 | err = i915_gem_wait_for_idle(dev_priv, |
| 2153 | I915_WAIT_INTERRUPTIBLE, |
| 2154 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2155 | if (err) |
| 2156 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2157 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2158 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2159 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2160 | if (!err) |
| 2161 | break; |
| 2162 | |
| 2163 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2164 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2165 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2169 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2170 | drm_gem_free_mmap_offset(&obj->base); |
| 2171 | } |
| 2172 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2173 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2174 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2175 | struct drm_device *dev, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 2176 | u32 handle, |
| 2177 | u64 *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2178 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2179 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2180 | int ret; |
| 2181 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2182 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2183 | if (!obj) |
| 2184 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2185 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2186 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2187 | if (ret == 0) |
| 2188 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2189 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2190 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2191 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2192 | } |
| 2193 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2194 | /** |
| 2195 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2196 | * @dev: DRM device |
| 2197 | * @data: GTT mapping ioctl data |
| 2198 | * @file: GEM object info |
| 2199 | * |
| 2200 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2201 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2202 | * up so we can get faults in the handler above. |
| 2203 | * |
| 2204 | * The fault handler will take care of binding the object into the GTT |
| 2205 | * (since it may have been evicted to make room for something), allocating |
| 2206 | * a fence register, and mapping the appropriate aperture address into |
| 2207 | * userspace. |
| 2208 | */ |
| 2209 | int |
| 2210 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2211 | struct drm_file *file) |
| 2212 | { |
| 2213 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2214 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2215 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2216 | } |
| 2217 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2218 | /* Immediately discard the backing storage */ |
| 2219 | static void |
| 2220 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2221 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2222 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2223 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2224 | if (obj->base.filp == NULL) |
| 2225 | return; |
| 2226 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2227 | /* Our goal here is to return as much of the memory as |
| 2228 | * is possible back to the system as we are called from OOM. |
| 2229 | * To do this we must instruct the shmfs to drop all of its |
| 2230 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2231 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2232 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2233 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2234 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2235 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2236 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2237 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2238 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2239 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2240 | struct address_space *mapping; |
| 2241 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2242 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2243 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2244 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2245 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2246 | case I915_MADV_DONTNEED: |
| 2247 | i915_gem_object_truncate(obj); |
| 2248 | case __I915_MADV_PURGED: |
| 2249 | return; |
| 2250 | } |
| 2251 | |
| 2252 | if (obj->base.filp == NULL) |
| 2253 | return; |
| 2254 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2255 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2256 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2257 | } |
| 2258 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2259 | /* |
| 2260 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2261 | * ref count of those pages. |
| 2262 | */ |
| 2263 | static void check_release_pagevec(struct pagevec *pvec) |
| 2264 | { |
| 2265 | check_move_unevictable_pages(pvec); |
| 2266 | __pagevec_release(pvec); |
| 2267 | cond_resched(); |
| 2268 | } |
| 2269 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2270 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2271 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2272 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2273 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2274 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2275 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2276 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2277 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2278 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2279 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2280 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2281 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2282 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2283 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2284 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2285 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2286 | |
| 2287 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2288 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2289 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2290 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2291 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2292 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2293 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2294 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2295 | if (!pagevec_add(&pvec, page)) |
| 2296 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2297 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2298 | if (pagevec_count(&pvec)) |
| 2299 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2300 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2301 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2302 | sg_free_table(pages); |
| 2303 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2304 | } |
| 2305 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2306 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2307 | { |
| 2308 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2309 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2310 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2311 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2312 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2313 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2314 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2315 | } |
| 2316 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2317 | static struct sg_table * |
| 2318 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2319 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2320 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2321 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2322 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2323 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2324 | if (IS_ERR_OR_NULL(pages)) |
| 2325 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2326 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2327 | spin_lock(&i915->mm.obj_lock); |
| 2328 | list_del(&obj->mm.link); |
| 2329 | spin_unlock(&i915->mm.obj_lock); |
| 2330 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2331 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2332 | void *ptr; |
| 2333 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2334 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2335 | if (is_vmalloc_addr(ptr)) |
| 2336 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2337 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2338 | kunmap(kmap_to_page(ptr)); |
| 2339 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2340 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2341 | } |
| 2342 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2343 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2344 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2345 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2346 | return pages; |
| 2347 | } |
| 2348 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2349 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2350 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2351 | { |
| 2352 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2353 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2354 | |
| 2355 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2356 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2357 | |
| 2358 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2359 | |
| 2360 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2361 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2362 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2363 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2364 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2365 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2366 | |
| 2367 | /* |
| 2368 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2369 | * array, hence protect them from being reaped by removing them from gtt |
| 2370 | * lists early. |
| 2371 | */ |
| 2372 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2373 | |
| 2374 | /* |
| 2375 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2376 | * NULL pages. In the future, when we have more asynchronous |
| 2377 | * get_pages backends we should be better able to handle the |
| 2378 | * cancellation of the async task in a more uniform manner. |
| 2379 | */ |
| 2380 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2381 | pages = ERR_PTR(-EINVAL); |
| 2382 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2383 | if (!IS_ERR(pages)) |
| 2384 | obj->ops->put_pages(obj, pages); |
| 2385 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2386 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2387 | unlock: |
| 2388 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2389 | |
| 2390 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2391 | } |
| 2392 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2393 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2394 | { |
| 2395 | struct sg_table new_st; |
| 2396 | struct scatterlist *sg, *new_sg; |
| 2397 | unsigned int i; |
| 2398 | |
| 2399 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2400 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2401 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2402 | if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN)) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2403 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2404 | |
| 2405 | new_sg = new_st.sgl; |
| 2406 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2407 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2408 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2409 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2410 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2411 | new_sg = sg_next(new_sg); |
| 2412 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2413 | GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */ |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2414 | |
| 2415 | sg_free_table(orig_st); |
| 2416 | |
| 2417 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2418 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2421 | static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2422 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2423 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2424 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2425 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2426 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2427 | struct sg_table *st; |
| 2428 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2429 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2430 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2431 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2432 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2433 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2434 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2435 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2436 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2437 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2438 | /* |
| 2439 | * Assert that the object is not currently in any GPU domain. As it |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2440 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2441 | * a GPU cache |
| 2442 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2443 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2444 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2445 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2446 | /* |
| 2447 | * If there's no chance of allocating enough pages for the whole |
| 2448 | * object, bail early. |
| 2449 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2450 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2451 | return -ENOMEM; |
| 2452 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2453 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2454 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2455 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2456 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2457 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2458 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2459 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2460 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2461 | } |
| 2462 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2463 | /* |
| 2464 | * Get the list of pages out of our struct file. They'll be pinned |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2465 | * at this point until we release them. |
| 2466 | * |
| 2467 | * Fail silently without starting the shrinker |
| 2468 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2469 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2470 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2471 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2472 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2473 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2474 | sg = st->sgl; |
| 2475 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2476 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2477 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2478 | const unsigned int shrink[] = { |
| 2479 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2480 | 0, |
| 2481 | }, *s = shrink; |
| 2482 | gfp_t gfp = noreclaim; |
| 2483 | |
| 2484 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2485 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2486 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2487 | if (likely(!IS_ERR(page))) |
| 2488 | break; |
| 2489 | |
| 2490 | if (!*s) { |
| 2491 | ret = PTR_ERR(page); |
| 2492 | goto err_sg; |
| 2493 | } |
| 2494 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2495 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2496 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2497 | /* |
| 2498 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2499 | * our own buffer, now let the real VM do its job and |
| 2500 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2501 | * |
| 2502 | * However, since graphics tend to be disposable, |
| 2503 | * defer the oom here by reporting the ENOMEM back |
| 2504 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2505 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2506 | if (!*s) { |
| 2507 | /* reclaim and warn, but no oom */ |
| 2508 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2509 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2510 | /* |
| 2511 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2512 | * kswapd to reclaim our pages (direct reclaim |
| 2513 | * does not effectively begin pageout of our |
| 2514 | * buffers on its own). However, direct reclaim |
| 2515 | * only waits for kswapd when under allocation |
| 2516 | * congestion. So as a result __GFP_RECLAIM is |
| 2517 | * unreliable and fails to actually reclaim our |
| 2518 | * dirty pages -- unless you try over and over |
| 2519 | * again with !__GFP_NORETRY. However, we still |
| 2520 | * want to fail this allocation rather than |
| 2521 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2522 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2523 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2524 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2525 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2526 | } while (1); |
| 2527 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2528 | if (!i || |
| 2529 | sg->length >= max_segment || |
| 2530 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2531 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2532 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2533 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2534 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2535 | st->nents++; |
| 2536 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2537 | } else { |
| 2538 | sg->length += PAGE_SIZE; |
| 2539 | } |
| 2540 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2541 | |
| 2542 | /* Check that the i965g/gm workaround works. */ |
| 2543 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2544 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2545 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2546 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2547 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2548 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2549 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2550 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2551 | i915_sg_trim(st); |
| 2552 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2553 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2554 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2555 | /* |
| 2556 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2557 | * it could not reserve enough large entries, asking |
| 2558 | * for PAGE_SIZE chunks instead may be helpful. |
| 2559 | */ |
| 2560 | if (max_segment > PAGE_SIZE) { |
| 2561 | for_each_sgt_page(page, sgt_iter, st) |
| 2562 | put_page(page); |
| 2563 | sg_free_table(st); |
| 2564 | |
| 2565 | max_segment = PAGE_SIZE; |
| 2566 | goto rebuild_st; |
| 2567 | } else { |
| 2568 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2569 | "Failed to DMA remap %lu pages\n", |
| 2570 | page_count); |
| 2571 | goto err_pages; |
| 2572 | } |
| 2573 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2574 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2575 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2576 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2577 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2578 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2579 | |
| 2580 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2581 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2582 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2583 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2584 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2585 | mapping_clear_unevictable(mapping); |
| 2586 | pagevec_init(&pvec); |
| 2587 | for_each_sgt_page(page, sgt_iter, st) { |
| 2588 | if (!pagevec_add(&pvec, page)) |
| 2589 | check_release_pagevec(&pvec); |
| 2590 | } |
| 2591 | if (pagevec_count(&pvec)) |
| 2592 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2593 | sg_free_table(st); |
| 2594 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2595 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2596 | /* |
| 2597 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2598 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2599 | * ENOMEM for a genuine allocation failure. |
| 2600 | * |
| 2601 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2602 | * space and so want to translate the error from shmemfs back to our |
| 2603 | * usual understanding of ENOMEM. |
| 2604 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2605 | if (ret == -ENOSPC) |
| 2606 | ret = -ENOMEM; |
| 2607 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2608 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2612 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2613 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2614 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2615 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2616 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2617 | int i; |
| 2618 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2619 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2620 | |
| 2621 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2622 | obj->mm.get_page.sg_idx = 0; |
| 2623 | |
| 2624 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2625 | |
| 2626 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2627 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2628 | GEM_BUG_ON(obj->mm.quirked); |
| 2629 | __i915_gem_object_pin_pages(obj); |
| 2630 | obj->mm.quirked = true; |
| 2631 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2632 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2633 | GEM_BUG_ON(!sg_page_sizes); |
| 2634 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2635 | |
| 2636 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2637 | * Calculate the supported page-sizes which fit into the given |
| 2638 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2639 | * to use opportunistically when later inserting into the GTT. For |
| 2640 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2641 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2642 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2643 | */ |
| 2644 | obj->mm.page_sizes.sg = 0; |
| 2645 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2646 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2647 | obj->mm.page_sizes.sg |= BIT(i); |
| 2648 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2649 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2650 | |
| 2651 | spin_lock(&i915->mm.obj_lock); |
| 2652 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2653 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2657 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2658 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2659 | |
| 2660 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2661 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2662 | return -EFAULT; |
| 2663 | } |
| 2664 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2665 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2666 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2667 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2668 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2669 | } |
| 2670 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2671 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2672 | * and pinned into our object. i915_gem_object_pin_pages() may be called |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2673 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2674 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2675 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2676 | * or as the object is itself released. |
| 2677 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2678 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2679 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2680 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2681 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2682 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2683 | if (err) |
| 2684 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2685 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2686 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2687 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2688 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2689 | err = ____i915_gem_object_get_pages(obj); |
| 2690 | if (err) |
| 2691 | goto unlock; |
| 2692 | |
| 2693 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2694 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2695 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2696 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2697 | unlock: |
| 2698 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2699 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2700 | } |
| 2701 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2702 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2703 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2704 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2705 | { |
| 2706 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2707 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2708 | struct sgt_iter sgt_iter; |
| 2709 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2710 | struct page *stack_pages[32]; |
| 2711 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2712 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2713 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2714 | void *addr; |
| 2715 | |
| 2716 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2717 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2718 | return kmap(sg_page(sgt->sgl)); |
| 2719 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2720 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2721 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2722 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2723 | if (!pages) |
| 2724 | return NULL; |
| 2725 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2726 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2727 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2728 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2729 | |
| 2730 | /* Check that we have the expected number of pages */ |
| 2731 | GEM_BUG_ON(i != n_pages); |
| 2732 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2733 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2734 | default: |
| 2735 | MISSING_CASE(type); |
| 2736 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2737 | case I915_MAP_WB: |
| 2738 | pgprot = PAGE_KERNEL; |
| 2739 | break; |
| 2740 | case I915_MAP_WC: |
| 2741 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2742 | break; |
| 2743 | } |
| 2744 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2745 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2746 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2747 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2748 | |
| 2749 | return addr; |
| 2750 | } |
| 2751 | |
| 2752 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2753 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2754 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2755 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2756 | enum i915_map_type has_type; |
| 2757 | bool pinned; |
| 2758 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2759 | int ret; |
| 2760 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2761 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2762 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2763 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2764 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2765 | if (ret) |
| 2766 | return ERR_PTR(ret); |
| 2767 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2768 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2769 | type &= ~I915_MAP_OVERRIDE; |
| 2770 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2771 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2772 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2773 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2774 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2775 | ret = ____i915_gem_object_get_pages(obj); |
| 2776 | if (ret) |
| 2777 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2778 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2779 | smp_mb__before_atomic(); |
| 2780 | } |
| 2781 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2782 | pinned = false; |
| 2783 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2784 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2785 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2786 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2787 | if (ptr && has_type != type) { |
| 2788 | if (pinned) { |
| 2789 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2790 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2791 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2792 | |
| 2793 | if (is_vmalloc_addr(ptr)) |
| 2794 | vunmap(ptr); |
| 2795 | else |
| 2796 | kunmap(kmap_to_page(ptr)); |
| 2797 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2798 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2799 | } |
| 2800 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2801 | if (!ptr) { |
| 2802 | ptr = i915_gem_object_map(obj, type); |
| 2803 | if (!ptr) { |
| 2804 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2805 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2806 | } |
| 2807 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2808 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2809 | } |
| 2810 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2811 | out_unlock: |
| 2812 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2813 | return ptr; |
| 2814 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2815 | err_unpin: |
| 2816 | atomic_dec(&obj->mm.pages_pin_count); |
| 2817 | err_unlock: |
| 2818 | ptr = ERR_PTR(ret); |
| 2819 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2822 | static int |
| 2823 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2824 | const struct drm_i915_gem_pwrite *arg) |
| 2825 | { |
| 2826 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2827 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2828 | u64 remain, offset; |
| 2829 | unsigned int pg; |
| 2830 | |
| 2831 | /* Before we instantiate/pin the backing store for our use, we |
| 2832 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2833 | * the pagecache. We avoid the penalty of instantiating all the |
| 2834 | * pages, important if the user is just writing to a few and never |
| 2835 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2836 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2837 | * or clearing-before-use) before it is overwritten. |
| 2838 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2839 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2840 | return -ENODEV; |
| 2841 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2842 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2843 | return -EFAULT; |
| 2844 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2845 | /* Before the pages are instantiated the object is treated as being |
| 2846 | * in the CPU domain. The pages will be clflushed as required before |
| 2847 | * use, and we can freely write into the pages directly. If userspace |
| 2848 | * races pwrite with any other operation; corruption will ensue - |
| 2849 | * that is userspace's prerogative! |
| 2850 | */ |
| 2851 | |
| 2852 | remain = arg->size; |
| 2853 | offset = arg->offset; |
| 2854 | pg = offset_in_page(offset); |
| 2855 | |
| 2856 | do { |
| 2857 | unsigned int len, unwritten; |
| 2858 | struct page *page; |
| 2859 | void *data, *vaddr; |
| 2860 | int err; |
| 2861 | |
| 2862 | len = PAGE_SIZE - pg; |
| 2863 | if (len > remain) |
| 2864 | len = remain; |
| 2865 | |
| 2866 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2867 | offset, len, 0, |
| 2868 | &page, &data); |
| 2869 | if (err < 0) |
| 2870 | return err; |
| 2871 | |
| 2872 | vaddr = kmap(page); |
| 2873 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2874 | kunmap(page); |
| 2875 | |
| 2876 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2877 | offset, len, len - unwritten, |
| 2878 | page, data); |
| 2879 | if (err < 0) |
| 2880 | return err; |
| 2881 | |
| 2882 | if (unwritten) |
| 2883 | return -EFAULT; |
| 2884 | |
| 2885 | remain -= len; |
| 2886 | user_data += len; |
| 2887 | offset += len; |
| 2888 | pg = 0; |
| 2889 | } while (remain); |
| 2890 | |
| 2891 | return 0; |
| 2892 | } |
| 2893 | |
Chris Wilson | 8547444 | 2019-01-29 18:54:50 +0000 | [diff] [blame] | 2894 | static bool match_ring(struct i915_request *rq) |
| 2895 | { |
| 2896 | struct drm_i915_private *dev_priv = rq->i915; |
| 2897 | u32 ring = I915_READ(RING_START(rq->engine->mmio_base)); |
| 2898 | |
| 2899 | return ring == i915_ggtt_offset(rq->ring->vma); |
| 2900 | } |
| 2901 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2902 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2903 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2904 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2905 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2906 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2907 | |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2908 | /* |
| 2909 | * We are called by the error capture, reset and to dump engine |
| 2910 | * state at random points in time. In particular, note that neither is |
| 2911 | * crucially ordered with an interrupt. After a hang, the GPU is dead |
| 2912 | * and we assume that no more writes can happen (we waited long enough |
| 2913 | * for all writes that were in transaction to be flushed) - adding an |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2914 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2915 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2916 | * At all other times, we must assume the GPU is still running, but |
| 2917 | * we only care about the snapshot of this moment. |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2918 | */ |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2919 | spin_lock_irqsave(&engine->timeline.lock, flags); |
| 2920 | list_for_each_entry(request, &engine->timeline.requests, link) { |
Chris Wilson | 5013eb8 | 2019-01-28 18:18:11 +0000 | [diff] [blame] | 2921 | if (i915_request_completed(request)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2922 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2923 | |
Chris Wilson | 8547444 | 2019-01-29 18:54:50 +0000 | [diff] [blame] | 2924 | if (!i915_request_started(request)) |
| 2925 | break; |
| 2926 | |
| 2927 | /* More than one preemptible request may match! */ |
| 2928 | if (!match_ring(request)) |
| 2929 | break; |
| 2930 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2931 | active = request; |
| 2932 | break; |
| 2933 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2934 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2935 | |
| 2936 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2937 | } |
| 2938 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2939 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2940 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2941 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2942 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2943 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 2944 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2945 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2946 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2947 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2948 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2949 | mutex_unlock(&dev->struct_mutex); |
| 2950 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2951 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2952 | /* |
| 2953 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2954 | * We do not need to do this test under locking as in the worst-case |
| 2955 | * we queue the retire worker once too often. |
| 2956 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2957 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2958 | queue_delayed_work(dev_priv->wq, |
| 2959 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2960 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2961 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2962 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2963 | static void shrink_caches(struct drm_i915_private *i915) |
| 2964 | { |
| 2965 | /* |
| 2966 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 2967 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 2968 | * with the aim of reducing fragmentation. |
| 2969 | */ |
| 2970 | kmem_cache_shrink(i915->priorities); |
| 2971 | kmem_cache_shrink(i915->dependencies); |
| 2972 | kmem_cache_shrink(i915->requests); |
| 2973 | kmem_cache_shrink(i915->luts); |
| 2974 | kmem_cache_shrink(i915->vmas); |
| 2975 | kmem_cache_shrink(i915->objects); |
| 2976 | } |
| 2977 | |
| 2978 | struct sleep_rcu_work { |
| 2979 | union { |
| 2980 | struct rcu_head rcu; |
| 2981 | struct work_struct work; |
| 2982 | }; |
| 2983 | struct drm_i915_private *i915; |
| 2984 | unsigned int epoch; |
| 2985 | }; |
| 2986 | |
| 2987 | static inline bool |
| 2988 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 2989 | { |
| 2990 | /* |
| 2991 | * There is a small chance that the epoch wrapped since we started |
| 2992 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 2993 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 2994 | */ |
| 2995 | return epoch == READ_ONCE(i915->gt.epoch); |
| 2996 | } |
| 2997 | |
| 2998 | static void __sleep_work(struct work_struct *work) |
| 2999 | { |
| 3000 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 3001 | struct drm_i915_private *i915 = s->i915; |
| 3002 | unsigned int epoch = s->epoch; |
| 3003 | |
| 3004 | kfree(s); |
| 3005 | if (same_epoch(i915, epoch)) |
| 3006 | shrink_caches(i915); |
| 3007 | } |
| 3008 | |
| 3009 | static void __sleep_rcu(struct rcu_head *rcu) |
| 3010 | { |
| 3011 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 3012 | struct drm_i915_private *i915 = s->i915; |
| 3013 | |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3014 | destroy_rcu_head(&s->rcu); |
| 3015 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3016 | if (same_epoch(i915, s->epoch)) { |
| 3017 | INIT_WORK(&s->work, __sleep_work); |
| 3018 | queue_work(i915->wq, &s->work); |
| 3019 | } else { |
| 3020 | kfree(s); |
| 3021 | } |
| 3022 | } |
| 3023 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3024 | static inline bool |
| 3025 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 3026 | { |
| 3027 | return (READ_ONCE(i915->gt.active_requests) || |
| 3028 | work_pending(&i915->gt.idle_work.work)); |
| 3029 | } |
| 3030 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3031 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
| 3032 | { |
| 3033 | struct intel_engine_cs *engine; |
| 3034 | enum intel_engine_id id; |
| 3035 | |
| 3036 | if (i915_terminally_wedged(&i915->gpu_error)) |
| 3037 | return; |
| 3038 | |
| 3039 | GEM_BUG_ON(i915->gt.active_requests); |
| 3040 | for_each_engine(engine, i915, id) { |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3041 | GEM_BUG_ON(__i915_active_request_peek(&engine->timeline.last_request)); |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3042 | GEM_BUG_ON(engine->last_retired_context != |
| 3043 | to_intel_context(i915->kernel_context, engine)); |
| 3044 | } |
| 3045 | } |
| 3046 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3047 | static void |
| 3048 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3049 | { |
| 3050 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3051 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3052 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3053 | bool rearm_hangcheck; |
| 3054 | |
| 3055 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3056 | return; |
| 3057 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 3058 | if (READ_ONCE(dev_priv->gt.active_requests)) |
| 3059 | return; |
| 3060 | |
| 3061 | /* |
| 3062 | * Flush out the last user context, leaving only the pinned |
| 3063 | * kernel context resident. When we are idling on the kernel_context, |
| 3064 | * no more new requests (with a context switch) are emitted and we |
| 3065 | * can finally rest. A consequence is that the idle work handler is |
| 3066 | * always called at least twice before idling (and if the system is |
| 3067 | * idle that implies a round trip through the retire worker). |
| 3068 | */ |
| 3069 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 3070 | i915_gem_switch_to_kernel_context(dev_priv); |
| 3071 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 3072 | |
| 3073 | GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n", |
| 3074 | READ_ONCE(dev_priv->gt.active_requests)); |
| 3075 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3076 | /* |
| 3077 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3078 | * new request is submitted. As we don't trust the hardware, we |
| 3079 | * continue on if the wait times out. This is necessary to allow |
| 3080 | * the machine to suspend even if the hardware dies, and we will |
| 3081 | * try to recover in resume (after depriving the hardware of power, |
| 3082 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3083 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3084 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3085 | intel_engines_are_idle(dev_priv), |
| 3086 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3087 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3088 | |
| 3089 | rearm_hangcheck = |
| 3090 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3091 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3092 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3093 | /* Currently busy, come back later */ |
| 3094 | mod_delayed_work(dev_priv->wq, |
| 3095 | &dev_priv->gt.idle_work, |
| 3096 | msecs_to_jiffies(50)); |
| 3097 | goto out_rearm; |
| 3098 | } |
| 3099 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3100 | /* |
| 3101 | * New request retired after this work handler started, extend active |
| 3102 | * period until next instance of the work. |
| 3103 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3104 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3105 | goto out_unlock; |
| 3106 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 3107 | epoch = __i915_gem_park(dev_priv); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3108 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3109 | assert_kernel_context_is_current(dev_priv); |
| 3110 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3111 | rearm_hangcheck = false; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3112 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3113 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3114 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3115 | out_rearm: |
| 3116 | if (rearm_hangcheck) { |
| 3117 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3118 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3119 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3120 | |
| 3121 | /* |
| 3122 | * When we are idle, it is an opportune time to reap our caches. |
| 3123 | * However, we have many objects that utilise RCU and the ordered |
| 3124 | * i915->wq that this work is executing on. To try and flush any |
| 3125 | * pending frees now we are idle, we first wait for an RCU grace |
| 3126 | * period, and then queue a task (that will run last on the wq) to |
| 3127 | * shrink and re-optimize the caches. |
| 3128 | */ |
| 3129 | if (same_epoch(dev_priv, epoch)) { |
| 3130 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3131 | if (s) { |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3132 | init_rcu_head(&s->rcu); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3133 | s->i915 = dev_priv; |
| 3134 | s->epoch = epoch; |
| 3135 | call_rcu(&s->rcu, __sleep_rcu); |
| 3136 | } |
| 3137 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3140 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3141 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3142 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3143 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3144 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3145 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3146 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3147 | mutex_lock(&i915->drm.struct_mutex); |
| 3148 | |
| 3149 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3150 | struct i915_gem_context *ctx = lut->ctx; |
| 3151 | struct i915_vma *vma; |
| 3152 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3153 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3154 | if (ctx->file_priv != fpriv) |
| 3155 | continue; |
| 3156 | |
| 3157 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3158 | GEM_BUG_ON(vma->obj != obj); |
| 3159 | |
| 3160 | /* We allow the process to have multiple handles to the same |
| 3161 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3162 | */ |
| 3163 | GEM_BUG_ON(!vma->open_count); |
| 3164 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3165 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3166 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3167 | list_del(&lut->obj_link); |
| 3168 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3169 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3170 | kmem_cache_free(i915->luts, lut); |
| 3171 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3172 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3173 | |
| 3174 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3175 | } |
| 3176 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3177 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3178 | { |
| 3179 | if (timeout_ns < 0) |
| 3180 | return MAX_SCHEDULE_TIMEOUT; |
| 3181 | |
| 3182 | if (timeout_ns == 0) |
| 3183 | return 0; |
| 3184 | |
| 3185 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3186 | } |
| 3187 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3188 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3189 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3190 | * @dev: drm device pointer |
| 3191 | * @data: ioctl data blob |
| 3192 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3193 | * |
| 3194 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3195 | * the timeout parameter. |
| 3196 | * -ETIME: object is still busy after timeout |
| 3197 | * -ERESTARTSYS: signal interrupted the wait |
| 3198 | * -ENONENT: object doesn't exist |
| 3199 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3200 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3201 | * -ENOMEM: damn |
| 3202 | * -ENODEV: Internal IRQ fail |
| 3203 | * -E?: The add request failed |
| 3204 | * |
| 3205 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3206 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3207 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3208 | * without holding struct_mutex the object may become re-busied before this |
| 3209 | * function completes. A similar but shorter * race condition exists in the busy |
| 3210 | * ioctl |
| 3211 | */ |
| 3212 | int |
| 3213 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3214 | { |
| 3215 | struct drm_i915_gem_wait *args = data; |
| 3216 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3217 | ktime_t start; |
| 3218 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3219 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3220 | if (args->flags != 0) |
| 3221 | return -EINVAL; |
| 3222 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3223 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3224 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3225 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3226 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3227 | start = ktime_get(); |
| 3228 | |
| 3229 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 3230 | I915_WAIT_INTERRUPTIBLE | |
| 3231 | I915_WAIT_PRIORITY | |
| 3232 | I915_WAIT_ALL, |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3233 | to_wait_timeout(args->timeout_ns), |
| 3234 | to_rps_client(file)); |
| 3235 | |
| 3236 | if (args->timeout_ns > 0) { |
| 3237 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3238 | if (args->timeout_ns < 0) |
| 3239 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3240 | |
| 3241 | /* |
| 3242 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3243 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3244 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3245 | * |
| 3246 | * This is a regression from the timespec->ktime conversion. |
| 3247 | */ |
| 3248 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3249 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3250 | |
| 3251 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3252 | if (ret == -ETIME && args->timeout_ns) |
| 3253 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3254 | } |
| 3255 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3256 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3257 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3260 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3261 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3262 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3263 | dev_err(i915->drm.dev, |
| 3264 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3265 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3266 | i915_gem_set_wedged(i915); |
| 3267 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | return 0; |
| 3271 | } |
| 3272 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3273 | static long |
| 3274 | wait_for_timelines(struct drm_i915_private *i915, |
| 3275 | unsigned int flags, long timeout) |
| 3276 | { |
| 3277 | struct i915_gt_timelines *gt = &i915->gt.timelines; |
| 3278 | struct i915_timeline *tl; |
| 3279 | |
| 3280 | if (!READ_ONCE(i915->gt.active_requests)) |
| 3281 | return timeout; |
| 3282 | |
| 3283 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3284 | list_for_each_entry(tl, >->active_list, link) { |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3285 | struct i915_request *rq; |
| 3286 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3287 | rq = i915_active_request_get_unlocked(&tl->last_request); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3288 | if (!rq) |
| 3289 | continue; |
| 3290 | |
| 3291 | mutex_unlock(>->mutex); |
| 3292 | |
| 3293 | /* |
| 3294 | * "Race-to-idle". |
| 3295 | * |
| 3296 | * Switching to the kernel context is often used a synchronous |
| 3297 | * step prior to idling, e.g. in suspend for flushing all |
| 3298 | * current operations to memory before sleeping. These we |
| 3299 | * want to complete as quickly as possible to avoid prolonged |
| 3300 | * stalls, so allow the gpu to boost to maximum clocks. |
| 3301 | */ |
| 3302 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
| 3303 | gen6_rps_boost(rq, NULL); |
| 3304 | |
| 3305 | timeout = i915_request_wait(rq, flags, timeout); |
| 3306 | i915_request_put(rq); |
| 3307 | if (timeout < 0) |
| 3308 | return timeout; |
| 3309 | |
| 3310 | /* restart after reacquiring the lock */ |
| 3311 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3312 | tl = list_entry(>->active_list, typeof(*tl), link); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3313 | } |
| 3314 | mutex_unlock(>->mutex); |
| 3315 | |
| 3316 | return timeout; |
| 3317 | } |
| 3318 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3319 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 3320 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3321 | { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3322 | GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", |
| 3323 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
| 3324 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3325 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3326 | /* If the device is asleep, we have no requests outstanding */ |
| 3327 | if (!READ_ONCE(i915->gt.awake)) |
| 3328 | return 0; |
| 3329 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3330 | timeout = wait_for_timelines(i915, flags, timeout); |
| 3331 | if (timeout < 0) |
| 3332 | return timeout; |
| 3333 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3334 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3335 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3336 | |
| 3337 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3338 | |
Chris Wilson | c1e63f6 | 2018-08-08 11:50:59 +0100 | [diff] [blame] | 3339 | if (GEM_SHOW_DEBUG() && !timeout) { |
| 3340 | /* Presume that timeout was non-zero to begin with! */ |
| 3341 | dev_warn(&i915->drm.pdev->dev, |
| 3342 | "Missed idle-completion interrupt!\n"); |
| 3343 | GEM_TRACE_DUMP(); |
| 3344 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3345 | |
| 3346 | err = wait_for_engines(i915); |
| 3347 | if (err) |
| 3348 | return err; |
| 3349 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3350 | i915_retire_requests(i915); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3351 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3352 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3353 | |
| 3354 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3355 | } |
| 3356 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3357 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3358 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3359 | /* |
| 3360 | * We manually flush the CPU domain so that we can override and |
| 3361 | * force the flush for the display, and perform it asyncrhonously. |
| 3362 | */ |
| 3363 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3364 | if (obj->cache_dirty) |
| 3365 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3366 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3370 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3371 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3372 | return; |
| 3373 | |
| 3374 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3375 | __i915_gem_object_flush_for_display(obj); |
| 3376 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3377 | } |
| 3378 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3379 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3380 | * Moves a single object to the WC read, and possibly write domain. |
| 3381 | * @obj: object to act on |
| 3382 | * @write: ask for write access or read only |
| 3383 | * |
| 3384 | * This function returns when the move is complete, including waiting on |
| 3385 | * flushes to occur. |
| 3386 | */ |
| 3387 | int |
| 3388 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3389 | { |
| 3390 | int ret; |
| 3391 | |
| 3392 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3393 | |
| 3394 | ret = i915_gem_object_wait(obj, |
| 3395 | I915_WAIT_INTERRUPTIBLE | |
| 3396 | I915_WAIT_LOCKED | |
| 3397 | (write ? I915_WAIT_ALL : 0), |
| 3398 | MAX_SCHEDULE_TIMEOUT, |
| 3399 | NULL); |
| 3400 | if (ret) |
| 3401 | return ret; |
| 3402 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3403 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3404 | return 0; |
| 3405 | |
| 3406 | /* Flush and acquire obj->pages so that we are coherent through |
| 3407 | * direct access in memory with previous cached writes through |
| 3408 | * shmemfs and that our cache domain tracking remains valid. |
| 3409 | * For example, if the obj->filp was moved to swap without us |
| 3410 | * being notified and releasing the pages, we would mistakenly |
| 3411 | * continue to assume that the obj remained out of the CPU cached |
| 3412 | * domain. |
| 3413 | */ |
| 3414 | ret = i915_gem_object_pin_pages(obj); |
| 3415 | if (ret) |
| 3416 | return ret; |
| 3417 | |
| 3418 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3419 | |
| 3420 | /* Serialise direct access to this object with the barriers for |
| 3421 | * coherent writes from the GPU, by effectively invalidating the |
| 3422 | * WC domain upon first access. |
| 3423 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3424 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3425 | mb(); |
| 3426 | |
| 3427 | /* It should now be out of any other write domains, and we can update |
| 3428 | * the domain values for our changes. |
| 3429 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3430 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3431 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3432 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3433 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3434 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3435 | obj->mm.dirty = true; |
| 3436 | } |
| 3437 | |
| 3438 | i915_gem_object_unpin_pages(obj); |
| 3439 | return 0; |
| 3440 | } |
| 3441 | |
| 3442 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3443 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3444 | * @obj: object to act on |
| 3445 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3446 | * |
| 3447 | * This function returns when the move is complete, including waiting on |
| 3448 | * flushes to occur. |
| 3449 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3450 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3451 | i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3452 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3453 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3454 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3455 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3456 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3457 | ret = i915_gem_object_wait(obj, |
| 3458 | I915_WAIT_INTERRUPTIBLE | |
| 3459 | I915_WAIT_LOCKED | |
| 3460 | (write ? I915_WAIT_ALL : 0), |
| 3461 | MAX_SCHEDULE_TIMEOUT, |
| 3462 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3463 | if (ret) |
| 3464 | return ret; |
| 3465 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3466 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3467 | return 0; |
| 3468 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3469 | /* Flush and acquire obj->pages so that we are coherent through |
| 3470 | * direct access in memory with previous cached writes through |
| 3471 | * shmemfs and that our cache domain tracking remains valid. |
| 3472 | * For example, if the obj->filp was moved to swap without us |
| 3473 | * being notified and releasing the pages, we would mistakenly |
| 3474 | * continue to assume that the obj remained out of the CPU cached |
| 3475 | * domain. |
| 3476 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3477 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3478 | if (ret) |
| 3479 | return ret; |
| 3480 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3481 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3482 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3483 | /* Serialise direct access to this object with the barriers for |
| 3484 | * coherent writes from the GPU, by effectively invalidating the |
| 3485 | * GTT domain upon first access. |
| 3486 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3487 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3488 | mb(); |
| 3489 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3490 | /* It should now be out of any other write domains, and we can update |
| 3491 | * the domain values for our changes. |
| 3492 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3493 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3494 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3495 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3496 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3497 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3498 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3499 | } |
| 3500 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3501 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3502 | return 0; |
| 3503 | } |
| 3504 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3505 | /** |
| 3506 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3507 | * @obj: object to act on |
| 3508 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3509 | * |
| 3510 | * After this function returns, the object will be in the new cache-level |
| 3511 | * across all GTT and the contents of the backing storage will be coherent, |
| 3512 | * with respect to the new cache-level. In order to keep the backing storage |
| 3513 | * coherent for all users, we only allow a single cache level to be set |
| 3514 | * globally on the object and prevent it from being changed whilst the |
| 3515 | * hardware is reading from the object. That is if the object is currently |
| 3516 | * on the scanout it will be set to uncached (or equivalent display |
| 3517 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3518 | * that all direct access to the scanout remains coherent. |
| 3519 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3520 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3521 | enum i915_cache_level cache_level) |
| 3522 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3523 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3524 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3525 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3526 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3527 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3528 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3529 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3530 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3531 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3532 | * be invalid given the new cache-level. This is principally to |
| 3533 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3534 | * reading an invalid PTE on older architectures. |
| 3535 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3536 | restart: |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3537 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3538 | if (!drm_mm_node_allocated(&vma->node)) |
| 3539 | continue; |
| 3540 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3541 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3542 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3543 | return -EBUSY; |
| 3544 | } |
| 3545 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3546 | if (!i915_vma_is_closed(vma) && |
| 3547 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3548 | continue; |
| 3549 | |
| 3550 | ret = i915_vma_unbind(vma); |
| 3551 | if (ret) |
| 3552 | return ret; |
| 3553 | |
| 3554 | /* As unbinding may affect other elements in the |
| 3555 | * obj->vma_list (due to side-effects from retiring |
| 3556 | * an active vma), play safe and restart the iterator. |
| 3557 | */ |
| 3558 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3559 | } |
| 3560 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3561 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3562 | * cache-level on the PTE. We could simply unbind them all and |
| 3563 | * rebind with the correct cache-level on next use. However since |
| 3564 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3565 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3566 | * state and so involves less work. |
| 3567 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3568 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3569 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3570 | * If we wait upon the object, we know that all the bound |
| 3571 | * VMA are no longer active. |
| 3572 | */ |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3573 | ret = i915_gem_object_wait(obj, |
| 3574 | I915_WAIT_INTERRUPTIBLE | |
| 3575 | I915_WAIT_LOCKED | |
| 3576 | I915_WAIT_ALL, |
| 3577 | MAX_SCHEDULE_TIMEOUT, |
| 3578 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3579 | if (ret) |
| 3580 | return ret; |
| 3581 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3582 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3583 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3584 | /* Access to snoopable pages through the GTT is |
| 3585 | * incoherent and on some machines causes a hard |
| 3586 | * lockup. Relinquish the CPU mmaping to force |
| 3587 | * userspace to refault in the pages and we can |
| 3588 | * then double check if the GTT mapping is still |
| 3589 | * valid for that pointer access. |
| 3590 | */ |
| 3591 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3592 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3593 | /* As we no longer need a fence for GTT access, |
| 3594 | * we can relinquish it now (and so prevent having |
| 3595 | * to steal a fence from someone else on the next |
| 3596 | * fence request). Note GPU activity would have |
| 3597 | * dropped the fence as all snoopable access is |
| 3598 | * supposed to be linear. |
| 3599 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3600 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3601 | ret = i915_vma_put_fence(vma); |
| 3602 | if (ret) |
| 3603 | return ret; |
| 3604 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3605 | } else { |
| 3606 | /* We either have incoherent backing store and |
| 3607 | * so no GTT access or the architecture is fully |
| 3608 | * coherent. In such cases, existing GTT mmaps |
| 3609 | * ignore the cache bit in the PTE and we can |
| 3610 | * rewrite it without confusing the GPU or having |
| 3611 | * to force userspace to fault back in its mmaps. |
| 3612 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3613 | } |
| 3614 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3615 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3616 | if (!drm_mm_node_allocated(&vma->node)) |
| 3617 | continue; |
| 3618 | |
| 3619 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3620 | if (ret) |
| 3621 | return ret; |
| 3622 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3623 | } |
| 3624 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3625 | list_for_each_entry(vma, &obj->vma.list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3626 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3627 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3628 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3629 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3630 | return 0; |
| 3631 | } |
| 3632 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3633 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3634 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3635 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3636 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3637 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3638 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3639 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3640 | rcu_read_lock(); |
| 3641 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3642 | if (!obj) { |
| 3643 | err = -ENOENT; |
| 3644 | goto out; |
| 3645 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3646 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3647 | switch (obj->cache_level) { |
| 3648 | case I915_CACHE_LLC: |
| 3649 | case I915_CACHE_L3_LLC: |
| 3650 | args->caching = I915_CACHING_CACHED; |
| 3651 | break; |
| 3652 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3653 | case I915_CACHE_WT: |
| 3654 | args->caching = I915_CACHING_DISPLAY; |
| 3655 | break; |
| 3656 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3657 | default: |
| 3658 | args->caching = I915_CACHING_NONE; |
| 3659 | break; |
| 3660 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3661 | out: |
| 3662 | rcu_read_unlock(); |
| 3663 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3664 | } |
| 3665 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3666 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3667 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3668 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3669 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3670 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3671 | struct drm_i915_gem_object *obj; |
| 3672 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3673 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3674 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3675 | switch (args->caching) { |
| 3676 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3677 | level = I915_CACHE_NONE; |
| 3678 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3679 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3680 | /* |
| 3681 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3682 | * snooped mapping may leave stale data in a corresponding CPU |
| 3683 | * cacheline, whereas normally such cachelines would get |
| 3684 | * invalidated. |
| 3685 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3686 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3687 | return -ENODEV; |
| 3688 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3689 | level = I915_CACHE_LLC; |
| 3690 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3691 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3692 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3693 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3694 | default: |
| 3695 | return -EINVAL; |
| 3696 | } |
| 3697 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3698 | obj = i915_gem_object_lookup(file, args->handle); |
| 3699 | if (!obj) |
| 3700 | return -ENOENT; |
| 3701 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3702 | /* |
| 3703 | * The caching mode of proxy object is handled by its generator, and |
| 3704 | * not allowed to be changed by userspace. |
| 3705 | */ |
| 3706 | if (i915_gem_object_is_proxy(obj)) { |
| 3707 | ret = -ENXIO; |
| 3708 | goto out; |
| 3709 | } |
| 3710 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3711 | if (obj->cache_level == level) |
| 3712 | goto out; |
| 3713 | |
| 3714 | ret = i915_gem_object_wait(obj, |
| 3715 | I915_WAIT_INTERRUPTIBLE, |
| 3716 | MAX_SCHEDULE_TIMEOUT, |
| 3717 | to_rps_client(file)); |
| 3718 | if (ret) |
| 3719 | goto out; |
| 3720 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3721 | ret = i915_mutex_lock_interruptible(dev); |
| 3722 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3723 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3724 | |
| 3725 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3726 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3727 | |
| 3728 | out: |
| 3729 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3730 | return ret; |
| 3731 | } |
| 3732 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3733 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 3734 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 3735 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 3736 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 3737 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3738 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3739 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3740 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3741 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3742 | const struct i915_ggtt_view *view, |
| 3743 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3744 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3745 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3746 | int ret; |
| 3747 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3748 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3749 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3750 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3751 | * display coherency whilst setting up the cache domains. |
| 3752 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3753 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3754 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3755 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3756 | * a result, we make sure that the pinning that is about to occur is |
| 3757 | * done with uncached PTEs. This is lowest common denominator for all |
| 3758 | * chipsets. |
| 3759 | * |
| 3760 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3761 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3762 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3763 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3764 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3765 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3766 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3767 | if (ret) { |
| 3768 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3769 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3770 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3771 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3772 | /* As the user may map the buffer once pinned in the display plane |
| 3773 | * (e.g. libkms for the bootup splash), we have to ensure that we |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3774 | * always use map_and_fenceable for all scanout buffers. However, |
| 3775 | * it may simply be too big to fit into mappable, in which case |
| 3776 | * put it anyway and hope that userspace can cope (but always first |
| 3777 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3778 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3779 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3780 | if ((flags & PIN_MAPPABLE) == 0 && |
| 3781 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3782 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3783 | flags | |
| 3784 | PIN_MAPPABLE | |
| 3785 | PIN_NONBLOCK); |
| 3786 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3787 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3788 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3789 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3790 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 3791 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 3792 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3793 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3794 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3795 | /* It should now be out of any other write domains, and we can update |
| 3796 | * the domain values for our changes. |
| 3797 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3798 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3799 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3800 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3801 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3802 | err_unpin_global: |
| 3803 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3804 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3808 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3809 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 3810 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3811 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3812 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 3813 | return; |
| 3814 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3815 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 3816 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3817 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3818 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 3819 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3820 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3821 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3822 | } |
| 3823 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3824 | /** |
| 3825 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3826 | * @obj: object to act on |
| 3827 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3828 | * |
| 3829 | * This function returns when the move is complete, including waiting on |
| 3830 | * flushes to occur. |
| 3831 | */ |
Chris Wilson | dabdfe02 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3832 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3833 | i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3834 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3835 | int ret; |
| 3836 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3837 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3838 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3839 | ret = i915_gem_object_wait(obj, |
| 3840 | I915_WAIT_INTERRUPTIBLE | |
| 3841 | I915_WAIT_LOCKED | |
| 3842 | (write ? I915_WAIT_ALL : 0), |
| 3843 | MAX_SCHEDULE_TIMEOUT, |
| 3844 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3845 | if (ret) |
| 3846 | return ret; |
| 3847 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3848 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3849 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3850 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3851 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 3852 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3853 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | /* It should now be out of any other write domains, and we can update |
| 3857 | * the domain values for our changes. |
| 3858 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3859 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3860 | |
| 3861 | /* If we're writing through the CPU, then the GPU read domains will |
| 3862 | * need to be invalidated at next use. |
| 3863 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3864 | if (write) |
| 3865 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3866 | |
| 3867 | return 0; |
| 3868 | } |
| 3869 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3870 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3871 | * emitted over 20 msec ago. |
| 3872 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3873 | * Note that if we were to use the current jiffies each time around the loop, |
| 3874 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3875 | * render a frame was over 20ms. |
| 3876 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3877 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3878 | * relatively low latency when blocking on a particular request to finish. |
| 3879 | */ |
| 3880 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3881 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3882 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3883 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3884 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 3885 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3886 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3887 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3888 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 3889 | /* ABI: return -EIO if already wedged */ |
| 3890 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 3891 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3892 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3893 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3894 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3895 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3896 | break; |
| 3897 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3898 | if (target) { |
| 3899 | list_del(&target->client_link); |
| 3900 | target->file_priv = NULL; |
| 3901 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 3902 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3903 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3904 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3905 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3906 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3907 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3908 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3909 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3910 | return 0; |
| 3911 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3912 | ret = i915_request_wait(target, |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3913 | I915_WAIT_INTERRUPTIBLE, |
| 3914 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3915 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3916 | |
Chris Wilson | e95433c7 | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3917 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3918 | } |
| 3919 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3920 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3921 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 3922 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 3923 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 3924 | u64 alignment, |
| 3925 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3926 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3927 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 3928 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3929 | struct i915_vma *vma; |
| 3930 | int ret; |
Joonas Lahtinen | 72e96d64 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 3931 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3932 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3933 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 3934 | if (flags & PIN_MAPPABLE && |
| 3935 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3936 | /* If the required space is larger than the available |
| 3937 | * aperture, we will not able to find a slot for the |
| 3938 | * object and unbinding the object now will be in |
| 3939 | * vain. Worse, doing so may cause us to ping-pong |
| 3940 | * the object in and out of the Global GTT and |
| 3941 | * waste a lot of cycles under the mutex. |
| 3942 | */ |
| 3943 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 3944 | return ERR_PTR(-E2BIG); |
| 3945 | |
| 3946 | /* If NONBLOCK is set the caller is optimistically |
| 3947 | * trying to cache the full object within the mappable |
| 3948 | * aperture, and *must* have a fallback in place for |
| 3949 | * situations where we cannot bind the object. We |
| 3950 | * can be a little more lax here and use the fallback |
| 3951 | * more often to avoid costly migrations of ourselves |
| 3952 | * and other objects within the aperture. |
| 3953 | * |
| 3954 | * Half-the-aperture is used as a simple heuristic. |
| 3955 | * More interesting would to do search for a free |
| 3956 | * block prior to making the commitment to unbind. |
| 3957 | * That caters for the self-harm case, and with a |
| 3958 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 3959 | * we could try to minimise harm to others. |
| 3960 | */ |
| 3961 | if (flags & PIN_NONBLOCK && |
| 3962 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 3963 | return ERR_PTR(-ENOSPC); |
| 3964 | } |
| 3965 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 3966 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 3967 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3968 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3969 | |
| 3970 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3971 | if (flags & PIN_NONBLOCK) { |
| 3972 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 3973 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3974 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3975 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 3976 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3977 | return ERR_PTR(-ENOSPC); |
| 3978 | } |
| 3979 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3980 | WARN(i915_vma_is_pinned(vma), |
| 3981 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3982 | " offset=%08x, req.alignment=%llx," |
| 3983 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 3984 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3985 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3986 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3987 | ret = i915_vma_unbind(vma); |
| 3988 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3989 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3990 | } |
| 3991 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3992 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 3993 | if (ret) |
| 3994 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3995 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3996 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3997 | } |
| 3998 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3999 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4000 | { |
| 4001 | /* Note that we could alias engines in the execbuf API, but |
| 4002 | * that would be very unwise as it prevents userspace from |
| 4003 | * fine control over engine selection. Ahem. |
| 4004 | * |
| 4005 | * This should be something like EXEC_MAX_ENGINE instead of |
| 4006 | * I915_NUM_ENGINES. |
| 4007 | */ |
| 4008 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 4009 | return 0x10000 << id; |
| 4010 | } |
| 4011 | |
| 4012 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 4013 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 4014 | /* The uABI guarantees an active writer is also amongst the read |
| 4015 | * engines. This would be true if we accessed the activity tracking |
| 4016 | * under the lock, but as we perform the lookup of the object and |
| 4017 | * its activity locklessly we can not guarantee that the last_write |
| 4018 | * being active implies that we have set the same engine flag from |
| 4019 | * last_read - hence we always set both read and write busy for |
| 4020 | * last_write. |
| 4021 | */ |
| 4022 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4023 | } |
| 4024 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4025 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4026 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4027 | unsigned int (*flag)(unsigned int id)) |
| 4028 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4029 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4030 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4031 | /* We have to check the current hw status of the fence as the uABI |
| 4032 | * guarantees forward progress. We could rely on the idle worker |
| 4033 | * to eventually flush us, but to minimise latency just ask the |
| 4034 | * hardware. |
| 4035 | * |
| 4036 | * Note we only report on the status of native fences. |
| 4037 | */ |
| 4038 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4039 | return 0; |
| 4040 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4041 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4042 | rq = container_of(fence, struct i915_request, fence); |
| 4043 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4044 | return 0; |
| 4045 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4046 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4047 | } |
| 4048 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4049 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4050 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4051 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4052 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4053 | } |
| 4054 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4055 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4056 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4057 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4058 | if (!fence) |
| 4059 | return 0; |
| 4060 | |
| 4061 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4062 | } |
| 4063 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4064 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4065 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4066 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4067 | { |
| 4068 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4069 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4070 | struct reservation_object_list *list; |
| 4071 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4072 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4073 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4074 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4075 | rcu_read_lock(); |
| 4076 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4077 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4078 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4079 | |
| 4080 | /* A discrepancy here is that we do not report the status of |
| 4081 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4082 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4083 | * This also means that wait-ioctl may report an object as busy, |
| 4084 | * where busy-ioctl considers it idle. |
| 4085 | * |
| 4086 | * We trade the ability to warn of foreign fences to report on which |
| 4087 | * i915 engines are active for the object. |
| 4088 | * |
| 4089 | * Alternatively, we can trade that extra information on read/write |
| 4090 | * activity with |
| 4091 | * args->busy = |
| 4092 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4093 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4094 | * |
| 4095 | */ |
| 4096 | retry: |
| 4097 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4098 | |
| 4099 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4100 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4101 | |
| 4102 | /* Translate shared fences to READ set of engines */ |
| 4103 | list = rcu_dereference(obj->resv->fence); |
| 4104 | if (list) { |
| 4105 | unsigned int shared_count = list->shared_count, i; |
| 4106 | |
| 4107 | for (i = 0; i < shared_count; ++i) { |
| 4108 | struct dma_fence *fence = |
| 4109 | rcu_dereference(list->shared[i]); |
| 4110 | |
| 4111 | args->busy |= busy_check_reader(fence); |
| 4112 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4113 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4114 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4115 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4116 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4117 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4118 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4119 | out: |
| 4120 | rcu_read_unlock(); |
| 4121 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | int |
| 4125 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4126 | struct drm_file *file_priv) |
| 4127 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4128 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4129 | } |
| 4130 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4131 | int |
| 4132 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4133 | struct drm_file *file_priv) |
| 4134 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4135 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4136 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4137 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4138 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4139 | |
| 4140 | switch (args->madv) { |
| 4141 | case I915_MADV_DONTNEED: |
| 4142 | case I915_MADV_WILLNEED: |
| 4143 | break; |
| 4144 | default: |
| 4145 | return -EINVAL; |
| 4146 | } |
| 4147 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4148 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4149 | if (!obj) |
| 4150 | return -ENOENT; |
| 4151 | |
| 4152 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4153 | if (err) |
| 4154 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4155 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4156 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4157 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4158 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4159 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4160 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4161 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4162 | obj->mm.quirked = false; |
| 4163 | } |
| 4164 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4165 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4166 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4167 | obj->mm.quirked = true; |
| 4168 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4169 | } |
| 4170 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4171 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4172 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4173 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4174 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4175 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4176 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4177 | i915_gem_object_truncate(obj); |
| 4178 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4179 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4180 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4181 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4182 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4183 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4184 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4185 | } |
| 4186 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4187 | static void |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4188 | frontbuffer_retire(struct i915_active_request *active, |
| 4189 | struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4190 | { |
| 4191 | struct drm_i915_gem_object *obj = |
| 4192 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4193 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4194 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4197 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4198 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4199 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4200 | mutex_init(&obj->mm.lock); |
| 4201 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4202 | spin_lock_init(&obj->vma.lock); |
| 4203 | INIT_LIST_HEAD(&obj->vma.list); |
| 4204 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4205 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4206 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4207 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4208 | init_rcu_head(&obj->rcu); |
| 4209 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4210 | obj->ops = ops; |
| 4211 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4212 | reservation_object_init(&obj->__builtin_resv); |
| 4213 | obj->resv = &obj->__builtin_resv; |
| 4214 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4215 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4216 | i915_active_request_init(&obj->frontbuffer_write, |
| 4217 | NULL, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4218 | |
| 4219 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4220 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4221 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4222 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4223 | i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4224 | } |
| 4225 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4226 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4227 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4228 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4229 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4230 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4231 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4232 | |
| 4233 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4234 | }; |
| 4235 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4236 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4237 | struct drm_gem_object *obj, |
| 4238 | size_t size) |
| 4239 | { |
| 4240 | struct drm_i915_private *i915 = to_i915(dev); |
| 4241 | unsigned long flags = VM_NORESERVE; |
| 4242 | struct file *filp; |
| 4243 | |
| 4244 | drm_gem_private_object_init(dev, obj, size); |
| 4245 | |
| 4246 | if (i915->mm.gemfs) |
| 4247 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4248 | flags); |
| 4249 | else |
| 4250 | filp = shmem_file_setup("i915", size, flags); |
| 4251 | |
| 4252 | if (IS_ERR(filp)) |
| 4253 | return PTR_ERR(filp); |
| 4254 | |
| 4255 | obj->filp = filp; |
| 4256 | |
| 4257 | return 0; |
| 4258 | } |
| 4259 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4260 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4261 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4262 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4263 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4264 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4265 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4266 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4267 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4268 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4269 | /* There is a prevalence of the assumption that we fit the object's |
| 4270 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4271 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4272 | * such a local variable, please consider fixing! |
| 4273 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4274 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4275 | return ERR_PTR(-E2BIG); |
| 4276 | |
| 4277 | if (overflows_type(size, obj->base.size)) |
| 4278 | return ERR_PTR(-E2BIG); |
| 4279 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4280 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4281 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4282 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4283 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4284 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4285 | if (ret) |
| 4286 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4287 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4288 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4289 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4290 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4291 | mask &= ~__GFP_HIGHMEM; |
| 4292 | mask |= __GFP_DMA32; |
| 4293 | } |
| 4294 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4295 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4296 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4297 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4298 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4299 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4300 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4301 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4302 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4303 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4304 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4305 | /* On some devices, we can have the GPU use the LLC (the CPU |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4306 | * cache) for about a 10% performance improvement |
| 4307 | * compared to uncached. Graphics requests other than |
| 4308 | * display scanout are coherent with the CPU in |
| 4309 | * accessing this cache. This means in this mode we |
| 4310 | * don't need to clflush on the CPU side, and on the |
| 4311 | * GPU side we only need to flush internal caches to |
| 4312 | * get data visible to the CPU. |
| 4313 | * |
| 4314 | * However, we maintain the display planes as UC, and so |
| 4315 | * need to rebind when first used as such. |
| 4316 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4317 | cache_level = I915_CACHE_LLC; |
| 4318 | else |
| 4319 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4320 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4321 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4322 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4323 | trace_i915_gem_object_create(obj); |
| 4324 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4325 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4326 | |
| 4327 | fail: |
| 4328 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4329 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4332 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4333 | { |
| 4334 | /* If we are the last user of the backing storage (be it shmemfs |
| 4335 | * pages or stolen etc), we know that the pages are going to be |
| 4336 | * immediately released. In this case, we can then skip copying |
| 4337 | * back the contents from the GPU. |
| 4338 | */ |
| 4339 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4340 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4341 | return false; |
| 4342 | |
| 4343 | if (obj->base.filp == NULL) |
| 4344 | return true; |
| 4345 | |
| 4346 | /* At first glance, this looks racy, but then again so would be |
| 4347 | * userspace racing mmap against close. However, the first external |
| 4348 | * reference to the filp can only be obtained through the |
| 4349 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4350 | * acquiring such a reference whilst we are in the middle of |
| 4351 | * freeing the object. |
| 4352 | */ |
| 4353 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4354 | } |
| 4355 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4356 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4357 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4358 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4359 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4360 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4361 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4362 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4363 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4364 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4365 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4366 | trace_i915_gem_object_destroy(obj); |
| 4367 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4368 | mutex_lock(&i915->drm.struct_mutex); |
| 4369 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4370 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4371 | list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4372 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4373 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4374 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4375 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4376 | GEM_BUG_ON(!list_empty(&obj->vma.list)); |
| 4377 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4378 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4379 | /* This serializes freeing with the shrinker. Since the free |
| 4380 | * is delayed, first by RCU then by the workqueue, we want the |
| 4381 | * shrinker to be able to free pages of unreferenced objects, |
| 4382 | * or else we may oom whilst there are plenty of deferred |
| 4383 | * freed objects. |
| 4384 | */ |
| 4385 | if (i915_gem_object_has_pages(obj)) { |
| 4386 | spin_lock(&i915->mm.obj_lock); |
| 4387 | list_del_init(&obj->mm.link); |
| 4388 | spin_unlock(&i915->mm.obj_lock); |
| 4389 | } |
| 4390 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4391 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4392 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4393 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4394 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4395 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4396 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4397 | |
| 4398 | if (obj->ops->release) |
| 4399 | obj->ops->release(obj); |
| 4400 | |
| 4401 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4402 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4403 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4404 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4405 | |
| 4406 | if (obj->base.import_attach) |
| 4407 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4408 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4409 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4410 | drm_gem_object_release(&obj->base); |
| 4411 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4412 | |
| 4413 | kfree(obj->bit_17); |
| 4414 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4415 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4416 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4417 | atomic_dec(&i915->mm.free_count); |
| 4418 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4419 | if (on) |
| 4420 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4421 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4422 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4426 | { |
| 4427 | struct llist_node *freed; |
| 4428 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4429 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4430 | freed = NULL; |
| 4431 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4432 | /* Only one consumer of llist_del_first() allowed */ |
| 4433 | spin_lock(&i915->mm.free_lock); |
| 4434 | freed = llist_del_first(&i915->mm.free_list); |
| 4435 | spin_unlock(&i915->mm.free_lock); |
| 4436 | } |
| 4437 | if (unlikely(freed)) { |
| 4438 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4439 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4440 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4441 | } |
| 4442 | |
| 4443 | static void __i915_gem_free_work(struct work_struct *work) |
| 4444 | { |
| 4445 | struct drm_i915_private *i915 = |
| 4446 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4447 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4448 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4449 | /* |
| 4450 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4451 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4452 | * However, the object may also be bound into the global GTT (e.g. |
| 4453 | * older GPUs without per-process support, or for direct access through |
| 4454 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4455 | * unbound now. |
| 4456 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4457 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4458 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4459 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4460 | spin_unlock(&i915->mm.free_lock); |
| 4461 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4462 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4463 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4464 | return; |
| 4465 | |
| 4466 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4467 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4468 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4472 | { |
| 4473 | struct drm_i915_gem_object *obj = |
| 4474 | container_of(head, typeof(*obj), rcu); |
| 4475 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4476 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4477 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4478 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4479 | * it like a rcu_head from this point forwards. And we expect all |
| 4480 | * objects to be freed via this path. |
| 4481 | */ |
| 4482 | destroy_rcu_head(&obj->rcu); |
| 4483 | |
| 4484 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4485 | * Since we require blocking on struct_mutex to unbind the freed |
| 4486 | * object from the GPU before releasing resources back to the |
| 4487 | * system, we can not do that directly from the RCU callback (which may |
| 4488 | * be a softirq context), but must instead then defer that work onto a |
| 4489 | * kthread. We use the RCU callback rather than move the freed object |
| 4490 | * directly onto the work queue so that we can mix between using the |
| 4491 | * worker and performing frees directly from subsequent allocations for |
| 4492 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4493 | */ |
| 4494 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4495 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4496 | } |
| 4497 | |
| 4498 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4499 | { |
| 4500 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4501 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4502 | if (obj->mm.quirked) |
| 4503 | __i915_gem_object_unpin_pages(obj); |
| 4504 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4505 | if (discard_backing_storage(obj)) |
| 4506 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4507 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4508 | /* |
| 4509 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4510 | * read-side critical sections are complete, e.g. |
| 4511 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4512 | * lookup see i915_gem_object_lookup_rcu(). |
| 4513 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4514 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4515 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4516 | } |
| 4517 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4518 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4519 | { |
| 4520 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4521 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4522 | if (!i915_gem_object_has_active_reference(obj) && |
| 4523 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4524 | i915_gem_object_set_active_reference(obj); |
| 4525 | else |
| 4526 | i915_gem_object_put(obj); |
| 4527 | } |
| 4528 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4529 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4530 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4531 | intel_wakeref_t wakeref; |
| 4532 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4533 | GEM_TRACE("\n"); |
| 4534 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4535 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4536 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 4537 | |
| 4538 | /* |
| 4539 | * As we have just resumed the machine and woken the device up from |
| 4540 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4541 | * back to defaults, recovering from whatever wedged state we left it |
| 4542 | * in and so worth trying to use the device once more. |
| 4543 | */ |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4544 | if (i915_terminally_wedged(&i915->gpu_error)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4545 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4546 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4547 | /* |
| 4548 | * If we inherit context state from the BIOS or earlier occupants |
| 4549 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4550 | * try to take over. The only way to remove the earlier state |
| 4551 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4552 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4553 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4554 | */ |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 4555 | intel_engines_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4556 | |
| 4557 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4558 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4559 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4560 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4561 | i915_gem_contexts_lost(i915); |
| 4562 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4565 | int i915_gem_suspend(struct drm_i915_private *i915) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4566 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4567 | intel_wakeref_t wakeref; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4568 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4569 | |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 4570 | GEM_TRACE("\n"); |
| 4571 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4572 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4573 | intel_suspend_gt_powersave(i915); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4574 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4575 | flush_workqueue(i915->wq); |
| 4576 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4577 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4578 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4579 | /* |
| 4580 | * We have to flush all the executing contexts to main memory so |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4581 | * that they can saved in the hibernation image. To ensure the last |
| 4582 | * context image is coherent, we have to switch away from it. That |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4583 | * leaves the i915->kernel_context still active when |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4584 | * we actually suspend, and its image in memory may not match the GPU |
| 4585 | * state. Fortunately, the kernel_context is disposable and we do |
| 4586 | * not rely on its state. |
| 4587 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4588 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 4589 | ret = i915_gem_switch_to_kernel_context(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4590 | if (ret) |
| 4591 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4592 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4593 | ret = i915_gem_wait_for_idle(i915, |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4594 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 4595 | I915_WAIT_LOCKED | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4596 | I915_WAIT_FOR_IDLE_BOOST, |
| 4597 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4598 | if (ret && ret != -EIO) |
| 4599 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4600 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4601 | assert_kernel_context_is_current(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4602 | } |
Chris Wilson | 01f8f33 | 2018-07-17 09:41:21 +0100 | [diff] [blame] | 4603 | i915_retire_requests(i915); /* ensure we flush after wedging */ |
| 4604 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4605 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4606 | i915_reset_flush(i915); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4607 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4608 | drain_delayed_work(&i915->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4609 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4610 | /* |
| 4611 | * As the idle_work is rearming if it detects a race, play safe and |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4612 | * repeat the flush until it is definitely idle. |
| 4613 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4614 | drain_delayed_work(&i915->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4615 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4616 | intel_uc_suspend(i915); |
| 4617 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4618 | /* |
| 4619 | * Assert that we successfully flushed all the work and |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4620 | * reset the GPU back to its idle, low power state. |
| 4621 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4622 | WARN_ON(i915->gt.awake); |
| 4623 | if (WARN_ON(!intel_engines_are_idle(i915))) |
| 4624 | i915_gem_set_wedged(i915); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4625 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4626 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4627 | return 0; |
| 4628 | |
| 4629 | err_unlock: |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4630 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4631 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4632 | return ret; |
| 4633 | } |
| 4634 | |
| 4635 | void i915_gem_suspend_late(struct drm_i915_private *i915) |
| 4636 | { |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4637 | struct drm_i915_gem_object *obj; |
| 4638 | struct list_head *phases[] = { |
| 4639 | &i915->mm.unbound_list, |
| 4640 | &i915->mm.bound_list, |
| 4641 | NULL |
| 4642 | }, **phase; |
| 4643 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4644 | /* |
| 4645 | * Neither the BIOS, ourselves or any other kernel |
| 4646 | * expects the system to be in execlists mode on startup, |
| 4647 | * so we need to reset the GPU back to legacy mode. And the only |
| 4648 | * known way to disable logical contexts is through a GPU reset. |
| 4649 | * |
| 4650 | * So in order to leave the system in a known default configuration, |
| 4651 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4652 | * clean up the GEM state tracking, flushing off the requests and |
| 4653 | * leaving the system in a known idle state. |
| 4654 | * |
| 4655 | * Note that is of the upmost importance that the GPU is idle and |
| 4656 | * all stray writes are flushed *before* we dismantle the backing |
| 4657 | * storage for the pinned objects. |
| 4658 | * |
| 4659 | * However, since we are uncertain that resetting the GPU on older |
| 4660 | * machines is a good idea, we don't - just in case it leaves the |
| 4661 | * machine in an unusable condition. |
| 4662 | */ |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4663 | |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4664 | mutex_lock(&i915->drm.struct_mutex); |
| 4665 | for (phase = phases; *phase; phase++) { |
| 4666 | list_for_each_entry(obj, *phase, mm.link) |
| 4667 | WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); |
| 4668 | } |
| 4669 | mutex_unlock(&i915->drm.struct_mutex); |
| 4670 | |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4671 | intel_uc_sanitize(i915); |
| 4672 | i915_gem_sanitize(i915); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4673 | } |
| 4674 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4675 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4676 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4677 | GEM_TRACE("\n"); |
| 4678 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4679 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4680 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4681 | mutex_lock(&i915->drm.struct_mutex); |
| 4682 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4683 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4684 | i915_gem_restore_gtt_mappings(i915); |
| 4685 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4686 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4687 | /* |
| 4688 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4689 | * guarantee that the context image is complete. So let's just reset |
| 4690 | * it and start again. |
| 4691 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4692 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4693 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4694 | if (i915_gem_init_hw(i915)) |
| 4695 | goto err_wedged; |
| 4696 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 4697 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 4698 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4699 | /* Always reload a context for powersaving. */ |
| 4700 | if (i915_gem_switch_to_kernel_context(i915)) |
| 4701 | goto err_wedged; |
| 4702 | |
| 4703 | out_unlock: |
| 4704 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 4705 | mutex_unlock(&i915->drm.struct_mutex); |
| 4706 | return; |
| 4707 | |
| 4708 | err_wedged: |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4709 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 4710 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 4711 | i915_gem_set_wedged(i915); |
| 4712 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4713 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4714 | } |
| 4715 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4716 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4717 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4718 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4719 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4720 | return; |
| 4721 | |
| 4722 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4723 | DISP_TILE_SURFACE_SWIZZLING); |
| 4724 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4725 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4726 | return; |
| 4727 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4728 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4729 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4730 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4731 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4732 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4733 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4734 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4735 | else |
| 4736 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4737 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4738 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4739 | static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4740 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4741 | I915_WRITE(RING_CTL(base), 0); |
| 4742 | I915_WRITE(RING_HEAD(base), 0); |
| 4743 | I915_WRITE(RING_TAIL(base), 0); |
| 4744 | I915_WRITE(RING_START(base), 0); |
| 4745 | } |
| 4746 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4747 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4748 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4749 | if (IS_I830(dev_priv)) { |
| 4750 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4751 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4752 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4753 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4754 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4755 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4756 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4757 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4758 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4759 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4760 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4761 | } |
| 4762 | } |
| 4763 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4764 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4765 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4766 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4767 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4768 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4769 | int err; |
| 4770 | |
| 4771 | for_each_engine(engine, i915, id) { |
| 4772 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4773 | if (err) { |
| 4774 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 4775 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4776 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4777 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | return 0; |
| 4781 | } |
| 4782 | |
| 4783 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4784 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4785 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4786 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4787 | dev_priv->gt.last_init_time = ktime_get(); |
| 4788 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4789 | /* Double layer security blanket, see i915_gem_init() */ |
| 4790 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 4791 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4792 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4793 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4794 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4795 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4796 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4797 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4798 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4799 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4800 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4801 | /* ...and determine whether they are sticking. */ |
| 4802 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 4803 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4804 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4805 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4806 | /* |
| 4807 | * At least 830 can leave some of the unused rings |
| 4808 | * "active" (ie. head != tail) after resume which |
| 4809 | * will prevent c3 entry. Makes sure all unused rings |
| 4810 | * are totally idle. |
| 4811 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4812 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4813 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4814 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4815 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 4816 | ret = -EIO; |
| 4817 | goto out; |
| 4818 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4819 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4820 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4821 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4822 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4823 | goto out; |
| 4824 | } |
| 4825 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 4826 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 4827 | if (ret) { |
| 4828 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 4829 | goto out; |
| 4830 | } |
| 4831 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4832 | /* We can't enable contexts until all firmware is loaded */ |
| 4833 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4834 | if (ret) { |
| 4835 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4836 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4837 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4838 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4839 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 4840 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 4841 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 4842 | ret = __i915_gem_restart_engines(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4843 | if (ret) |
| 4844 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4845 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4846 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4847 | |
| 4848 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4849 | |
| 4850 | cleanup_uc: |
| 4851 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4852 | out: |
| 4853 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 4854 | |
| 4855 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4856 | } |
| 4857 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4858 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 4859 | { |
| 4860 | struct i915_gem_context *ctx; |
| 4861 | struct intel_engine_cs *engine; |
| 4862 | enum intel_engine_id id; |
| 4863 | int err; |
| 4864 | |
| 4865 | /* |
| 4866 | * As we reset the gpu during very early sanitisation, the current |
| 4867 | * register state on the GPU should reflect its defaults values. |
| 4868 | * We load a context onto the hw (with restore-inhibit), then switch |
| 4869 | * over to a second context to save that default register state. We |
| 4870 | * can then prime every new context with that state so they all start |
| 4871 | * from the same default HW values. |
| 4872 | */ |
| 4873 | |
| 4874 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 4875 | if (IS_ERR(ctx)) |
| 4876 | return PTR_ERR(ctx); |
| 4877 | |
| 4878 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4879 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4880 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4881 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4882 | if (IS_ERR(rq)) { |
| 4883 | err = PTR_ERR(rq); |
| 4884 | goto out_ctx; |
| 4885 | } |
| 4886 | |
Chris Wilson | 3fef5cda | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 4887 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4888 | if (engine->init_context) |
| 4889 | err = engine->init_context(rq); |
| 4890 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 4891 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4892 | if (err) |
| 4893 | goto err_active; |
| 4894 | } |
| 4895 | |
| 4896 | err = i915_gem_switch_to_kernel_context(i915); |
| 4897 | if (err) |
| 4898 | goto err_active; |
| 4899 | |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4900 | if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) { |
| 4901 | i915_gem_set_wedged(i915); |
| 4902 | err = -EIO; /* Caller will declare us wedged */ |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4903 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4904 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4905 | |
| 4906 | assert_kernel_context_is_current(i915); |
| 4907 | |
Chris Wilson | 8e1cb32 | 2018-09-20 17:13:43 +0100 | [diff] [blame] | 4908 | /* |
| 4909 | * Immediately park the GPU so that we enable powersaving and |
| 4910 | * treat it as idle. The next time we issue a request, we will |
| 4911 | * unpark and start using the engine->pinned_default_state, otherwise |
| 4912 | * it is in limbo and an early reset may fail. |
| 4913 | */ |
| 4914 | __i915_gem_park(i915); |
| 4915 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4916 | for_each_engine(engine, i915, id) { |
| 4917 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4918 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4919 | |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4920 | GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count); |
| 4921 | |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 4922 | state = to_intel_context(ctx, engine)->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4923 | if (!state) |
| 4924 | continue; |
| 4925 | |
| 4926 | /* |
| 4927 | * As we will hold a reference to the logical state, it will |
| 4928 | * not be torn down with the context, and importantly the |
| 4929 | * object will hold onto its vma (making it possible for a |
| 4930 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 4931 | * from the GTT to prevent such accidents and reclaim the |
| 4932 | * space. |
| 4933 | */ |
| 4934 | err = i915_vma_unbind(state); |
| 4935 | if (err) |
| 4936 | goto err_active; |
| 4937 | |
| 4938 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 4939 | if (err) |
| 4940 | goto err_active; |
| 4941 | |
| 4942 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4943 | |
| 4944 | /* Check we can acquire the image of the context state */ |
| 4945 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4946 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4947 | if (IS_ERR(vaddr)) { |
| 4948 | err = PTR_ERR(vaddr); |
| 4949 | goto err_active; |
| 4950 | } |
| 4951 | |
| 4952 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 4956 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 4957 | |
| 4958 | /* |
| 4959 | * Make sure that classes with multiple engine instances all |
| 4960 | * share the same basic configuration. |
| 4961 | */ |
| 4962 | for_each_engine(engine, i915, id) { |
| 4963 | unsigned int bit = BIT(engine->uabi_class); |
| 4964 | unsigned int expected = engine->default_state ? bit : 0; |
| 4965 | |
| 4966 | if ((found & bit) != expected) { |
| 4967 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 4968 | engine->uabi_class, engine->name); |
| 4969 | } |
| 4970 | } |
| 4971 | } |
| 4972 | |
| 4973 | out_ctx: |
| 4974 | i915_gem_context_set_closed(ctx); |
| 4975 | i915_gem_context_put(ctx); |
| 4976 | return err; |
| 4977 | |
| 4978 | err_active: |
| 4979 | /* |
| 4980 | * If we have to abandon now, we expect the engines to be idle |
| 4981 | * and ready to be torn-down. First try to flush any remaining |
| 4982 | * request, ensure we are pointing at the kernel context and |
| 4983 | * then remove it. |
| 4984 | */ |
| 4985 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 4986 | goto out_ctx; |
| 4987 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4988 | if (WARN_ON(i915_gem_wait_for_idle(i915, |
| 4989 | I915_WAIT_LOCKED, |
| 4990 | MAX_SCHEDULE_TIMEOUT))) |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4991 | goto out_ctx; |
| 4992 | |
| 4993 | i915_gem_contexts_lost(i915); |
| 4994 | goto out_ctx; |
| 4995 | } |
| 4996 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4997 | static int |
| 4998 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 4999 | { |
| 5000 | struct drm_i915_gem_object *obj; |
| 5001 | struct i915_vma *vma; |
| 5002 | int ret; |
| 5003 | |
| 5004 | obj = i915_gem_object_create_stolen(i915, size); |
| 5005 | if (!obj) |
| 5006 | obj = i915_gem_object_create_internal(i915, size); |
| 5007 | if (IS_ERR(obj)) { |
| 5008 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 5009 | return PTR_ERR(obj); |
| 5010 | } |
| 5011 | |
| 5012 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 5013 | if (IS_ERR(vma)) { |
| 5014 | ret = PTR_ERR(vma); |
| 5015 | goto err_unref; |
| 5016 | } |
| 5017 | |
| 5018 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 5019 | if (ret) |
| 5020 | goto err_unref; |
| 5021 | |
| 5022 | i915->gt.scratch = vma; |
| 5023 | return 0; |
| 5024 | |
| 5025 | err_unref: |
| 5026 | i915_gem_object_put(obj); |
| 5027 | return ret; |
| 5028 | } |
| 5029 | |
| 5030 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 5031 | { |
| 5032 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 5033 | } |
| 5034 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5035 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5036 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5037 | int ret; |
| 5038 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 5039 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 5040 | if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv)) |
Matthew Auld | da9fe3f3 | 2017-10-06 23:18:31 +0100 | [diff] [blame] | 5041 | mkwrite_device_info(dev_priv)->page_sizes = |
| 5042 | I915_GTT_PAGE_SIZE_4K; |
| 5043 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 5044 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 5045 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5046 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 5047 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5048 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5049 | } else { |
| 5050 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5051 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5052 | } |
| 5053 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5054 | i915_timelines_init(dev_priv); |
| 5055 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5056 | ret = i915_gem_init_userptr(dev_priv); |
| 5057 | if (ret) |
| 5058 | return ret; |
| 5059 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5060 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 5061 | if (ret) |
| 5062 | return ret; |
| 5063 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5064 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 5065 | if (ret) |
| 5066 | goto err_uc_misc; |
| 5067 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5068 | /* This is just a security blanket to placate dragons. |
| 5069 | * On some systems, we very sporadically observe that the first TLBs |
| 5070 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5071 | * we hold the forcewake during initialisation these problems |
| 5072 | * just magically go away. |
| 5073 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5074 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5075 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5076 | |
Chris Wilson | f6b9d5ca | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5077 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5078 | if (ret) { |
| 5079 | GEM_BUG_ON(ret == -EIO); |
| 5080 | goto err_unlock; |
| 5081 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5082 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5083 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5084 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5085 | if (ret) { |
| 5086 | GEM_BUG_ON(ret == -EIO); |
| 5087 | goto err_ggtt; |
| 5088 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5089 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5090 | ret = i915_gem_contexts_init(dev_priv); |
| 5091 | if (ret) { |
| 5092 | GEM_BUG_ON(ret == -EIO); |
| 5093 | goto err_scratch; |
| 5094 | } |
| 5095 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5096 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5097 | if (ret) { |
| 5098 | GEM_BUG_ON(ret == -EIO); |
| 5099 | goto err_context; |
| 5100 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5101 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5102 | intel_init_gt_powersave(dev_priv); |
| 5103 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5104 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5105 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5106 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5107 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5108 | ret = i915_gem_init_hw(dev_priv); |
| 5109 | if (ret) |
| 5110 | goto err_uc_init; |
| 5111 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5112 | /* |
| 5113 | * Despite its name intel_init_clock_gating applies both display |
| 5114 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5115 | * GT power context workaround. Worse, sometimes it includes a context |
| 5116 | * register workaround which we need to apply before we record the |
| 5117 | * default HW state for all contexts. |
| 5118 | * |
| 5119 | * FIXME: break up the workarounds and apply them at the right time! |
| 5120 | */ |
| 5121 | intel_init_clock_gating(dev_priv); |
| 5122 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5123 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5124 | if (ret) |
| 5125 | goto err_init_hw; |
| 5126 | |
| 5127 | if (i915_inject_load_failure()) { |
| 5128 | ret = -ENODEV; |
| 5129 | goto err_init_hw; |
| 5130 | } |
| 5131 | |
| 5132 | if (i915_inject_load_failure()) { |
| 5133 | ret = -EIO; |
| 5134 | goto err_init_hw; |
| 5135 | } |
| 5136 | |
| 5137 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5138 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5139 | |
| 5140 | return 0; |
| 5141 | |
| 5142 | /* |
| 5143 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5144 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5145 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5146 | * driver doesn't explode during runtime. |
| 5147 | */ |
| 5148 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5149 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5150 | |
| 5151 | WARN_ON(i915_gem_suspend(dev_priv)); |
| 5152 | i915_gem_suspend_late(dev_priv); |
| 5153 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 5154 | i915_gem_drain_workqueue(dev_priv); |
| 5155 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5156 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5157 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5158 | err_uc_init: |
| 5159 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5160 | err_pm: |
| 5161 | if (ret != -EIO) { |
| 5162 | intel_cleanup_gt_powersave(dev_priv); |
| 5163 | i915_gem_cleanup_engines(dev_priv); |
| 5164 | } |
| 5165 | err_context: |
| 5166 | if (ret != -EIO) |
| 5167 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5168 | err_scratch: |
| 5169 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5170 | err_ggtt: |
| 5171 | err_unlock: |
| 5172 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5173 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5174 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5175 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5176 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5177 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5178 | if (ret != -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5179 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5180 | i915_timelines_fini(dev_priv); |
| 5181 | } |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5182 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5183 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5184 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5185 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5186 | /* |
| 5187 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5188 | * wedged. But we only want to do this where the GPU is angry, |
| 5189 | * for all other failure, such as an allocation failure, bail. |
| 5190 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5191 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 5192 | i915_load_error(dev_priv, |
| 5193 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5194 | i915_gem_set_wedged(dev_priv); |
| 5195 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5196 | |
| 5197 | /* Minimal basic recovery for KMS */ |
| 5198 | ret = i915_ggtt_enable_hw(dev_priv); |
| 5199 | i915_gem_restore_gtt_mappings(dev_priv); |
| 5200 | i915_gem_restore_fences(dev_priv); |
| 5201 | intel_init_clock_gating(dev_priv); |
| 5202 | |
| 5203 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5204 | } |
| 5205 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5206 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5207 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5208 | } |
| 5209 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5210 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 5211 | { |
| 5212 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5213 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5214 | |
| 5215 | /* Flush any outstanding unpin_work. */ |
| 5216 | i915_gem_drain_workqueue(dev_priv); |
| 5217 | |
| 5218 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5219 | intel_uc_fini_hw(dev_priv); |
| 5220 | intel_uc_fini(dev_priv); |
| 5221 | i915_gem_cleanup_engines(dev_priv); |
| 5222 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5223 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5224 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5225 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5226 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 5227 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5228 | intel_cleanup_gt_powersave(dev_priv); |
| 5229 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5230 | intel_uc_fini_misc(dev_priv); |
| 5231 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5232 | i915_timelines_fini(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5233 | |
| 5234 | i915_gem_drain_freed_objects(dev_priv); |
| 5235 | |
| 5236 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 5237 | } |
| 5238 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5239 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5240 | { |
| 5241 | i915_gem_sanitize(i915); |
| 5242 | } |
| 5243 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5244 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5245 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5246 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5247 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5248 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5249 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5250 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5251 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5252 | } |
| 5253 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5254 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5255 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5256 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5257 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5258 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5259 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5260 | !IS_CHERRYVIEW(dev_priv)) |
| 5261 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5262 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5263 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5264 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5265 | dev_priv->num_fence_regs = 16; |
| 5266 | else |
| 5267 | dev_priv->num_fence_regs = 8; |
| 5268 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5269 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5270 | dev_priv->num_fence_regs = |
| 5271 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5272 | |
| 5273 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5274 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5275 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5276 | |
| 5277 | fence->i915 = dev_priv; |
| 5278 | fence->id = i; |
| 5279 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5280 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5281 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5282 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5283 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5284 | } |
| 5285 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5286 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5287 | { |
| 5288 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5289 | spin_lock_init(&i915->mm.obj_lock); |
| 5290 | spin_lock_init(&i915->mm.free_lock); |
| 5291 | |
| 5292 | init_llist_head(&i915->mm.free_list); |
| 5293 | |
| 5294 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5295 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5296 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5297 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5298 | |
| 5299 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5300 | } |
| 5301 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5302 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5303 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5304 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5305 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5306 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5307 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5308 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5309 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5310 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5311 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5312 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5313 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5314 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5315 | if (!dev_priv->luts) |
| 5316 | goto err_vmas; |
| 5317 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5318 | dev_priv->requests = KMEM_CACHE(i915_request, |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5319 | SLAB_HWCACHE_ALIGN | |
| 5320 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5321 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5322 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5323 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5324 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5325 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5326 | SLAB_HWCACHE_ALIGN | |
| 5327 | SLAB_RECLAIM_ACCOUNT); |
| 5328 | if (!dev_priv->dependencies) |
| 5329 | goto err_requests; |
| 5330 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5331 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5332 | if (!dev_priv->priorities) |
| 5333 | goto err_dependencies; |
| 5334 | |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5335 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 5336 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5337 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5338 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5339 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5340 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5341 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5342 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5343 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5344 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5345 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 5346 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5347 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5348 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5349 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5350 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5351 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5352 | err = i915_gemfs_init(dev_priv); |
| 5353 | if (err) |
| 5354 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5355 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5356 | return 0; |
| 5357 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5358 | err_dependencies: |
| 5359 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5360 | err_requests: |
| 5361 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5362 | err_luts: |
| 5363 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5364 | err_vmas: |
| 5365 | kmem_cache_destroy(dev_priv->vmas); |
| 5366 | err_objects: |
| 5367 | kmem_cache_destroy(dev_priv->objects); |
| 5368 | err_out: |
| 5369 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5370 | } |
Dave Airlie | 71acb5eb | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5371 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5372 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5373 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5374 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5375 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5376 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5377 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5378 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5379 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5380 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5381 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5382 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5383 | kmem_cache_destroy(dev_priv->vmas); |
| 5384 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5385 | |
| 5386 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5387 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5388 | |
| 5389 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5390 | } |
| 5391 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5392 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5393 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5394 | /* Discard all purgeable objects, let userspace recover those as |
| 5395 | * required after resuming. |
| 5396 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5397 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5398 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5399 | return 0; |
| 5400 | } |
| 5401 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5402 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5403 | { |
| 5404 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5405 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5406 | &i915->mm.unbound_list, |
| 5407 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5408 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5409 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5410 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5411 | /* |
| 5412 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5413 | * |
| 5414 | * We need to update the domain tracking to reflect that the CPU |
| 5415 | * will be accessing all the pages to create and restore from the |
| 5416 | * hibernation, and so upon restoration those pages will be in the |
| 5417 | * CPU domain. |
| 5418 | * |
| 5419 | * To make sure the hibernation image contains the latest state, |
| 5420 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5421 | * |
| 5422 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5423 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5424 | */ |
| 5425 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5426 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 5427 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5428 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5429 | mutex_lock(&i915->drm.struct_mutex); |
| 5430 | for (phase = phases; *phase; phase++) { |
| 5431 | list_for_each_entry(obj, *phase, mm.link) |
| 5432 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5433 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5434 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5435 | |
| 5436 | return 0; |
| 5437 | } |
| 5438 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5439 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5440 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5441 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5442 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5443 | |
| 5444 | /* Clean up our request list when the client is going away, so that |
| 5445 | * later retire_requests won't dereference our soon-to-be-gone |
| 5446 | * file_priv. |
| 5447 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5448 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5449 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5450 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5451 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5452 | } |
| 5453 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5454 | int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file) |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5455 | { |
| 5456 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b888 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5457 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5458 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5459 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5460 | |
| 5461 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5462 | if (!file_priv) |
| 5463 | return -ENOMEM; |
| 5464 | |
| 5465 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5466 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5467 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5468 | |
| 5469 | spin_lock_init(&file_priv->mm.lock); |
| 5470 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5471 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5472 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 5473 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5474 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5475 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b888 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5476 | if (ret) |
| 5477 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5478 | |
Ben Widawsky | e422b888 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5479 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5480 | } |
| 5481 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5482 | /** |
| 5483 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5484 | * @old: current GEM buffer for the frontbuffer slots |
| 5485 | * @new: new GEM buffer for the frontbuffer slots |
| 5486 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5487 | * |
| 5488 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5489 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5490 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5491 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5492 | struct drm_i915_gem_object *new, |
| 5493 | unsigned frontbuffer_bits) |
| 5494 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5495 | /* Control of individual bits within the mask are guarded by |
| 5496 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5497 | * manipulation of individual bits. But since the bitfield as a whole |
| 5498 | * is updated using RMW, we need to use atomics in order to update |
| 5499 | * the bits. |
| 5500 | */ |
| 5501 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 5502 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5503 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5504 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5505 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5506 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5510 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5511 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5512 | } |
| 5513 | } |
| 5514 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5515 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5516 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5517 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5518 | const void *data, size_t size) |
| 5519 | { |
| 5520 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5521 | struct file *file; |
| 5522 | size_t offset; |
| 5523 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5524 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5525 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5526 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5527 | return obj; |
| 5528 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5529 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5530 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5531 | file = obj->base.filp; |
| 5532 | offset = 0; |
| 5533 | do { |
| 5534 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5535 | struct page *page; |
| 5536 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5537 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5538 | err = pagecache_write_begin(file, file->f_mapping, |
| 5539 | offset, len, 0, |
| 5540 | &page, &pgdata); |
| 5541 | if (err < 0) |
| 5542 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5543 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5544 | vaddr = kmap(page); |
| 5545 | memcpy(vaddr, data, len); |
| 5546 | kunmap(page); |
| 5547 | |
| 5548 | err = pagecache_write_end(file, file->f_mapping, |
| 5549 | offset, len, len, |
| 5550 | page, pgdata); |
| 5551 | if (err < 0) |
| 5552 | goto fail; |
| 5553 | |
| 5554 | size -= len; |
| 5555 | data += len; |
| 5556 | offset += len; |
| 5557 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5558 | |
| 5559 | return obj; |
| 5560 | |
| 5561 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5562 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5563 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5564 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5565 | |
| 5566 | struct scatterlist * |
| 5567 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5568 | unsigned int n, |
| 5569 | unsigned int *offset) |
| 5570 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5571 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5572 | struct scatterlist *sg; |
| 5573 | unsigned int idx, count; |
| 5574 | |
| 5575 | might_sleep(); |
| 5576 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5577 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5578 | |
| 5579 | /* As we iterate forward through the sg, we record each entry in a |
| 5580 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5581 | * this index previously, we will have an entry for it. |
| 5582 | * |
| 5583 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5584 | * sequential page access (where each new request is consecutive |
| 5585 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5586 | * i.e. O(1) with a large constant! |
| 5587 | */ |
| 5588 | if (n < READ_ONCE(iter->sg_idx)) |
| 5589 | goto lookup; |
| 5590 | |
| 5591 | mutex_lock(&iter->lock); |
| 5592 | |
| 5593 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5594 | * (or the subsequent) sg are fast - comparing against the last |
| 5595 | * sg is faster than going through the radixtree. |
| 5596 | */ |
| 5597 | |
| 5598 | sg = iter->sg_pos; |
| 5599 | idx = iter->sg_idx; |
| 5600 | count = __sg_page_count(sg); |
| 5601 | |
| 5602 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5603 | void *entry; |
| 5604 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5605 | int ret; |
| 5606 | |
| 5607 | /* If we cannot allocate and insert this entry, or the |
| 5608 | * individual pages from this range, cancel updating the |
| 5609 | * sg_idx so that on this lookup we are forced to linearly |
| 5610 | * scan onwards, but on future lookups we will try the |
| 5611 | * insertion again (in which case we need to be careful of |
| 5612 | * the error return reporting that we have already inserted |
| 5613 | * this index). |
| 5614 | */ |
| 5615 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5616 | if (ret && ret != -EEXIST) |
| 5617 | goto scan; |
| 5618 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5619 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5620 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5621 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5622 | if (ret && ret != -EEXIST) |
| 5623 | goto scan; |
| 5624 | } |
| 5625 | |
| 5626 | idx += count; |
| 5627 | sg = ____sg_next(sg); |
| 5628 | count = __sg_page_count(sg); |
| 5629 | } |
| 5630 | |
| 5631 | scan: |
| 5632 | iter->sg_pos = sg; |
| 5633 | iter->sg_idx = idx; |
| 5634 | |
| 5635 | mutex_unlock(&iter->lock); |
| 5636 | |
| 5637 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5638 | goto lookup; |
| 5639 | |
| 5640 | /* In case we failed to insert the entry into the radixtree, we need |
| 5641 | * to look beyond the current sg. |
| 5642 | */ |
| 5643 | while (idx + count <= n) { |
| 5644 | idx += count; |
| 5645 | sg = ____sg_next(sg); |
| 5646 | count = __sg_page_count(sg); |
| 5647 | } |
| 5648 | |
| 5649 | *offset = n - idx; |
| 5650 | return sg; |
| 5651 | |
| 5652 | lookup: |
| 5653 | rcu_read_lock(); |
| 5654 | |
| 5655 | sg = radix_tree_lookup(&iter->radix, n); |
| 5656 | GEM_BUG_ON(!sg); |
| 5657 | |
| 5658 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5659 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5660 | * to the start of that range. We will return the pointer to |
| 5661 | * the base page and the offset of this page within the |
| 5662 | * sg entry's range. |
| 5663 | */ |
| 5664 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5665 | if (unlikely(xa_is_value(sg))) { |
| 5666 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5667 | |
| 5668 | sg = radix_tree_lookup(&iter->radix, base); |
| 5669 | GEM_BUG_ON(!sg); |
| 5670 | |
| 5671 | *offset = n - base; |
| 5672 | } |
| 5673 | |
| 5674 | rcu_read_unlock(); |
| 5675 | |
| 5676 | return sg; |
| 5677 | } |
| 5678 | |
| 5679 | struct page * |
| 5680 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5681 | { |
| 5682 | struct scatterlist *sg; |
| 5683 | unsigned int offset; |
| 5684 | |
| 5685 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5686 | |
| 5687 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5688 | return nth_page(sg_page(sg), offset); |
| 5689 | } |
| 5690 | |
| 5691 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5692 | struct page * |
| 5693 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5694 | unsigned int n) |
| 5695 | { |
| 5696 | struct page *page; |
| 5697 | |
| 5698 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5699 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5700 | set_page_dirty(page); |
| 5701 | |
| 5702 | return page; |
| 5703 | } |
| 5704 | |
| 5705 | dma_addr_t |
| 5706 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5707 | unsigned long n) |
| 5708 | { |
| 5709 | struct scatterlist *sg; |
| 5710 | unsigned int offset; |
| 5711 | |
| 5712 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5713 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5714 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5715 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5716 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5717 | { |
| 5718 | struct sg_table *pages; |
| 5719 | int err; |
| 5720 | |
| 5721 | if (align > obj->base.size) |
| 5722 | return -EINVAL; |
| 5723 | |
| 5724 | if (obj->ops == &i915_gem_phys_ops) |
| 5725 | return 0; |
| 5726 | |
| 5727 | if (obj->ops != &i915_gem_object_ops) |
| 5728 | return -EINVAL; |
| 5729 | |
| 5730 | err = i915_gem_object_unbind(obj); |
| 5731 | if (err) |
| 5732 | return err; |
| 5733 | |
| 5734 | mutex_lock(&obj->mm.lock); |
| 5735 | |
| 5736 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5737 | err = -EFAULT; |
| 5738 | goto err_unlock; |
| 5739 | } |
| 5740 | |
| 5741 | if (obj->mm.quirked) { |
| 5742 | err = -EFAULT; |
| 5743 | goto err_unlock; |
| 5744 | } |
| 5745 | |
| 5746 | if (obj->mm.mapping) { |
| 5747 | err = -EBUSY; |
| 5748 | goto err_unlock; |
| 5749 | } |
| 5750 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5751 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5752 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5753 | obj->ops = &i915_gem_phys_ops; |
| 5754 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5755 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5756 | if (err) |
| 5757 | goto err_xfer; |
| 5758 | |
| 5759 | /* Perma-pin (until release) the physical set of pages */ |
| 5760 | __i915_gem_object_pin_pages(obj); |
| 5761 | |
| 5762 | if (!IS_ERR_OR_NULL(pages)) |
| 5763 | i915_gem_object_ops.put_pages(obj, pages); |
| 5764 | mutex_unlock(&obj->mm.lock); |
| 5765 | return 0; |
| 5766 | |
| 5767 | err_xfer: |
| 5768 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5769 | if (!IS_ERR_OR_NULL(pages)) { |
| 5770 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 5771 | |
| 5772 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 5773 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5774 | err_unlock: |
| 5775 | mutex_unlock(&obj->mm.lock); |
| 5776 | return err; |
| 5777 | } |
| 5778 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5779 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5780 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5781 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5782 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5783 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5784 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5785 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 5786 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5787 | #endif |