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> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/i915_drm.h> |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 30 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 31 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 32 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 33 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 35 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 36 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 37 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 38 | #include <linux/dma-buf.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 39 | |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 40 | #include "i915_drv.h" |
| 41 | #include "i915_gem_clflush.h" |
| 42 | #include "i915_gemfs.h" |
| 43 | #include "i915_reset.h" |
| 44 | #include "i915_trace.h" |
| 45 | #include "i915_vgpu.h" |
| 46 | |
| 47 | #include "intel_drv.h" |
| 48 | #include "intel_frontbuffer.h" |
| 49 | #include "intel_mocs.h" |
| 50 | #include "intel_workarounds.h" |
| 51 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 52 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 53 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 54 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 55 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 56 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 57 | return false; |
| 58 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 59 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 60 | return true; |
| 61 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 62 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 65 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 66 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 67 | struct drm_mm_node *node, u32 size) |
| 68 | { |
| 69 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 70 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 71 | size, 0, I915_COLOR_UNEVICTABLE, |
| 72 | 0, ggtt->mappable_end, |
| 73 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void |
| 77 | remove_mappable_node(struct drm_mm_node *node) |
| 78 | { |
| 79 | drm_mm_remove_node(node); |
| 80 | } |
| 81 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 82 | /* some bookkeeping */ |
| 83 | 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] | 84 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 85 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 86 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 87 | dev_priv->mm.object_count++; |
| 88 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 89 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | 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] | 93 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 94 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 95 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 96 | dev_priv->mm.object_count--; |
| 97 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 98 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Chris Wilson | 21dd373 | 2011-01-26 15:55:56 +0000 | [diff] [blame] | 101 | static int |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 102 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 103 | { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 104 | int ret; |
| 105 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 106 | might_sleep(); |
| 107 | |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 108 | /* |
| 109 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging |
| 110 | * userspace. If it takes that long something really bad is going on and |
| 111 | * we should simply try to bail out and fail as gracefully as possible. |
| 112 | */ |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 113 | ret = wait_event_interruptible_timeout(error->reset_queue, |
Chris Wilson | 8c185ec | 2017-03-16 17:13:02 +0000 | [diff] [blame] | 114 | !i915_reset_backoff(error), |
Chris Wilson | b52992c | 2016-10-28 13:58:24 +0100 | [diff] [blame] | 115 | I915_RESET_TIMEOUT); |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 116 | if (ret == 0) { |
| 117 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); |
| 118 | return -EIO; |
| 119 | } else if (ret < 0) { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 120 | return ret; |
Chris Wilson | d98c52c | 2016-04-13 17:35:05 +0100 | [diff] [blame] | 121 | } else { |
| 122 | return 0; |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 123 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 126 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 127 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 128 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 129 | int ret; |
| 130 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 131 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 132 | if (ret) |
| 133 | return ret; |
| 134 | |
| 135 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 136 | if (ret) |
| 137 | return ret; |
| 138 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 139 | return 0; |
| 140 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 141 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 142 | static u32 __i915_gem_park(struct drm_i915_private *i915) |
| 143 | { |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 144 | intel_wakeref_t wakeref; |
| 145 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 146 | GEM_TRACE("\n"); |
| 147 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 148 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 149 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 150 | GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 151 | |
| 152 | if (!i915->gt.awake) |
| 153 | return I915_EPOCH_INVALID; |
| 154 | |
| 155 | GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); |
| 156 | |
| 157 | /* |
| 158 | * Be paranoid and flush a concurrent interrupt to make sure |
| 159 | * we don't reactivate any irq tasklets after parking. |
| 160 | * |
| 161 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 162 | * there may still be an in-flight interrupt even though the CSB |
| 163 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 164 | * is completed before we continue, but it doesn't prevent the HW from |
| 165 | * raising a spurious interrupt later. To complete the shield we should |
| 166 | * coordinate disabling the CS irq with flushing the interrupts. |
| 167 | */ |
| 168 | synchronize_irq(i915->drm.irq); |
| 169 | |
| 170 | intel_engines_park(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 171 | i915_timelines_park(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 172 | |
| 173 | i915_pmu_gt_parked(i915); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 174 | i915_vma_parked(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 175 | |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 176 | wakeref = fetch_and_zero(&i915->gt.awake); |
| 177 | GEM_BUG_ON(!wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 178 | |
| 179 | if (INTEL_GEN(i915) >= 6) |
| 180 | gen6_rps_idle(i915); |
| 181 | |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 182 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 183 | |
| 184 | return i915->gt.epoch; |
| 185 | } |
| 186 | |
| 187 | void i915_gem_park(struct drm_i915_private *i915) |
| 188 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 189 | GEM_TRACE("\n"); |
| 190 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 191 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 192 | GEM_BUG_ON(i915->gt.active_requests); |
| 193 | |
| 194 | if (!i915->gt.awake) |
| 195 | return; |
| 196 | |
| 197 | /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ |
| 198 | mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); |
| 199 | } |
| 200 | |
| 201 | void i915_gem_unpark(struct drm_i915_private *i915) |
| 202 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 203 | GEM_TRACE("\n"); |
| 204 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 205 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 206 | GEM_BUG_ON(!i915->gt.active_requests); |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 207 | assert_rpm_wakelock_held(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 208 | |
| 209 | if (i915->gt.awake) |
| 210 | return; |
| 211 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 212 | /* |
| 213 | * It seems that the DMC likes to transition between the DC states a lot |
| 214 | * when there are no connected displays (no active power domains) during |
| 215 | * command submission. |
| 216 | * |
| 217 | * This activity has negative impact on the performance of the chip with |
| 218 | * huge latencies observed in the interrupt handler and elsewhere. |
| 219 | * |
| 220 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 221 | * GT activity, preventing any DC state transitions. |
| 222 | */ |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 223 | i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 224 | GEM_BUG_ON(!i915->gt.awake); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 225 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 226 | if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ |
| 227 | i915->gt.epoch = 1; |
| 228 | |
| 229 | intel_enable_gt_powersave(i915); |
| 230 | i915_update_gfx_val(i915); |
| 231 | if (INTEL_GEN(i915) >= 6) |
| 232 | gen6_rps_busy(i915); |
| 233 | i915_pmu_gt_unparked(i915); |
| 234 | |
| 235 | intel_engines_unpark(i915); |
| 236 | |
| 237 | i915_queue_hangcheck(i915); |
| 238 | |
| 239 | queue_delayed_work(i915->wq, |
| 240 | &i915->gt.retire_work, |
| 241 | round_jiffies_up_relative(HZ)); |
| 242 | } |
| 243 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 244 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 245 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 246 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 247 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame^] | 248 | struct i915_ggtt *ggtt = &to_i915(dev)->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 249 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 250 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 251 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 252 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame^] | 253 | mutex_lock(&ggtt->vm.mutex); |
| 254 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 255 | pinned = ggtt->vm.reserved; |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 256 | list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 257 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 258 | pinned += vma->node.size; |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame^] | 259 | |
| 260 | mutex_unlock(&ggtt->vm.mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 261 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 262 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 263 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 264 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 268 | 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] | 269 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 270 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 271 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 272 | struct sg_table *st; |
| 273 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 274 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 275 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 276 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 277 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 278 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 279 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 280 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 281 | /* Always aligning to the object size, allows a single allocation |
| 282 | * to handle all possible callers, and given typical object sizes, |
| 283 | * the alignment of the buddy allocation will naturally match. |
| 284 | */ |
| 285 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 286 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 287 | roundup_pow_of_two(obj->base.size)); |
| 288 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 289 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 290 | |
| 291 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 292 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 293 | struct page *page; |
| 294 | char *src; |
| 295 | |
| 296 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 297 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 298 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 299 | goto err_phys; |
| 300 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 301 | |
| 302 | src = kmap_atomic(page); |
| 303 | memcpy(vaddr, src, PAGE_SIZE); |
| 304 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 305 | kunmap_atomic(src); |
| 306 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 307 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 308 | vaddr += PAGE_SIZE; |
| 309 | } |
| 310 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 311 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 312 | |
| 313 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 314 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 315 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 316 | goto err_phys; |
| 317 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 318 | |
| 319 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 320 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 321 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 322 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | sg = st->sgl; |
| 326 | sg->offset = 0; |
| 327 | sg->length = obj->base.size; |
| 328 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 329 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 330 | sg_dma_len(sg) = obj->base.size; |
| 331 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 332 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 333 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 334 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 335 | |
| 336 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 337 | |
| 338 | err_phys: |
| 339 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 340 | |
| 341 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 344 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 345 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 346 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 347 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 348 | if (cpu_write_needs_clflush(obj)) |
| 349 | obj->cache_dirty = true; |
| 350 | } |
| 351 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 352 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 353 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 354 | struct sg_table *pages, |
| 355 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 356 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 357 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
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 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 360 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 361 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 362 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 363 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 364 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 365 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 366 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 367 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void |
| 371 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 372 | struct sg_table *pages) |
| 373 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 374 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 375 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 376 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 377 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 378 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 379 | int i; |
| 380 | |
| 381 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 382 | struct page *page; |
| 383 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 384 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 385 | page = shmem_read_mapping_page(mapping, i); |
| 386 | if (IS_ERR(page)) |
| 387 | continue; |
| 388 | |
| 389 | dst = kmap_atomic(page); |
| 390 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 391 | memcpy(dst, vaddr, PAGE_SIZE); |
| 392 | kunmap_atomic(dst); |
| 393 | |
| 394 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 395 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 396 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 397 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 398 | vaddr += PAGE_SIZE; |
| 399 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 400 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 403 | sg_free_table(pages); |
| 404 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 405 | |
| 406 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static void |
| 410 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 411 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 412 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 416 | .get_pages = i915_gem_object_get_pages_phys, |
| 417 | .put_pages = i915_gem_object_put_pages_phys, |
| 418 | .release = i915_gem_object_release_phys, |
| 419 | }; |
| 420 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 421 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 422 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 423 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 424 | { |
| 425 | struct i915_vma *vma; |
| 426 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 427 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 428 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 429 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 430 | |
| 431 | /* Closed vma are removed from the obj->vma_list - but they may |
| 432 | * still have an active binding on the object. To remove those we |
| 433 | * must wait for all rendering to complete to the object (as unbinding |
| 434 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 435 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 436 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 437 | if (ret) |
| 438 | return ret; |
| 439 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 440 | while ((vma = list_first_entry_or_null(&obj->vma_list, |
| 441 | struct i915_vma, |
| 442 | obj_link))) { |
| 443 | list_move_tail(&vma->obj_link, &still_in_list); |
| 444 | ret = i915_vma_unbind(vma); |
| 445 | if (ret) |
| 446 | break; |
| 447 | } |
| 448 | list_splice(&still_in_list, &obj->vma_list); |
| 449 | |
| 450 | return ret; |
| 451 | } |
| 452 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 453 | static long |
| 454 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 455 | unsigned int flags, |
| 456 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 457 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 458 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 459 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 460 | |
| 461 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 462 | |
| 463 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 464 | return timeout; |
| 465 | |
| 466 | if (!dma_fence_is_i915(fence)) |
| 467 | return dma_fence_wait_timeout(fence, |
| 468 | flags & I915_WAIT_INTERRUPTIBLE, |
| 469 | timeout); |
| 470 | |
| 471 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 472 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 473 | goto out; |
| 474 | |
Chris Wilson | e9af4ea | 2018-01-18 13:16:09 +0000 | [diff] [blame] | 475 | /* |
| 476 | * This client is about to stall waiting for the GPU. In many cases |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 477 | * this is undesirable and limits the throughput of the system, as |
| 478 | * many clients cannot continue processing user input/output whilst |
| 479 | * blocked. RPS autotuning may take tens of milliseconds to respond |
| 480 | * to the GPU load and thus incurs additional latency for the client. |
| 481 | * We can circumvent that by promoting the GPU frequency to maximum |
| 482 | * before we wait. This makes the GPU throttle up much more quickly |
| 483 | * (good for benchmarks and user experience, e.g. window animations), |
| 484 | * but at a cost of spending more power processing the workload |
| 485 | * (bad for battery). Not all clients even want their results |
| 486 | * immediately and for them we should just let the GPU select its own |
| 487 | * frequency to maximise efficiency. To prevent a single client from |
| 488 | * forcing the clocks too high for the whole system, we only allow |
| 489 | * each client to waitboost once in a busy period. |
| 490 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 491 | if (rps_client && !i915_request_started(rq)) { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 492 | if (INTEL_GEN(rq->i915) >= 6) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 493 | gen6_rps_boost(rq, rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 494 | } |
| 495 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 496 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 497 | |
| 498 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 499 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 500 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 501 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 502 | return timeout; |
| 503 | } |
| 504 | |
| 505 | static long |
| 506 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 507 | unsigned int flags, |
| 508 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 509 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 510 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 511 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 512 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 513 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 514 | |
| 515 | if (flags & I915_WAIT_ALL) { |
| 516 | struct dma_fence **shared; |
| 517 | unsigned int count, i; |
| 518 | int ret; |
| 519 | |
| 520 | ret = reservation_object_get_fences_rcu(resv, |
| 521 | &excl, &count, &shared); |
| 522 | if (ret) |
| 523 | return ret; |
| 524 | |
| 525 | for (i = 0; i < count; i++) { |
| 526 | timeout = i915_gem_object_wait_fence(shared[i], |
| 527 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 528 | rps_client); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 529 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 530 | break; |
| 531 | |
| 532 | dma_fence_put(shared[i]); |
| 533 | } |
| 534 | |
| 535 | for (; i < count; i++) |
| 536 | dma_fence_put(shared[i]); |
| 537 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 538 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 539 | /* |
| 540 | * If both shared fences and an exclusive fence exist, |
| 541 | * then by construction the shared fences must be later |
| 542 | * than the exclusive fence. If we successfully wait for |
| 543 | * all the shared fences, we know that the exclusive fence |
| 544 | * must all be signaled. If all the shared fences are |
| 545 | * signaled, we can prune the array and recover the |
| 546 | * floating references on the fences/requests. |
| 547 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 548 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 549 | } else { |
| 550 | excl = reservation_object_get_excl_rcu(resv); |
| 551 | } |
| 552 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 553 | if (excl && timeout >= 0) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 554 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
| 555 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 556 | |
| 557 | dma_fence_put(excl); |
| 558 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 559 | /* |
| 560 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 561 | * signaled and that the reservation object has not been changed (i.e. |
| 562 | * no new fences have been added). |
| 563 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 564 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 565 | if (reservation_object_trylock(resv)) { |
| 566 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 567 | reservation_object_add_excl_fence(resv, NULL); |
| 568 | reservation_object_unlock(resv); |
| 569 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 572 | return timeout; |
| 573 | } |
| 574 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 575 | static void __fence_set_priority(struct dma_fence *fence, |
| 576 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 577 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 578 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 579 | struct intel_engine_cs *engine; |
| 580 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 581 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 582 | return; |
| 583 | |
| 584 | rq = to_request(fence); |
| 585 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 586 | |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 587 | local_bh_disable(); |
| 588 | rcu_read_lock(); /* RCU serialisation for set-wedged protection */ |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 589 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 590 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 591 | rcu_read_unlock(); |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 592 | local_bh_enable(); /* kick the tasklets if queues were reprioritised */ |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 595 | static void fence_set_priority(struct dma_fence *fence, |
| 596 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 597 | { |
| 598 | /* Recurse once into a fence-array */ |
| 599 | if (dma_fence_is_array(fence)) { |
| 600 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 601 | int i; |
| 602 | |
| 603 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 604 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 605 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 606 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | int |
| 611 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 612 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 613 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 614 | { |
| 615 | struct dma_fence *excl; |
| 616 | |
| 617 | if (flags & I915_WAIT_ALL) { |
| 618 | struct dma_fence **shared; |
| 619 | unsigned int count, i; |
| 620 | int ret; |
| 621 | |
| 622 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 623 | &excl, &count, &shared); |
| 624 | if (ret) |
| 625 | return ret; |
| 626 | |
| 627 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 628 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 629 | dma_fence_put(shared[i]); |
| 630 | } |
| 631 | |
| 632 | kfree(shared); |
| 633 | } else { |
| 634 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 635 | } |
| 636 | |
| 637 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 638 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 639 | dma_fence_put(excl); |
| 640 | } |
| 641 | return 0; |
| 642 | } |
| 643 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 644 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 645 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 646 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 647 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 648 | * @timeout: how long to wait |
Chris Wilson | a0a8b1c | 2017-11-09 14:06:44 +0000 | [diff] [blame] | 649 | * @rps_client: client (user process) to charge for any waitboosting |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 650 | */ |
| 651 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 652 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 653 | unsigned int flags, |
| 654 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 655 | struct intel_rps_client *rps_client) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 656 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 657 | might_sleep(); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 658 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 659 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 660 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 661 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 662 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 663 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 667 | { |
| 668 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 669 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 670 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 673 | static int |
| 674 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 675 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 676 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 677 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 678 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 679 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 680 | |
| 681 | /* We manually control the domain here and pretend that it |
| 682 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 683 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 684 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 685 | if (copy_from_user(vaddr, user_data, args->size)) |
| 686 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 687 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 688 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 689 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 690 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 691 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 692 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 695 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 696 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 697 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 701 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 702 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 703 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 706 | static int |
| 707 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 708 | struct drm_i915_private *dev_priv, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 709 | u64 size, |
| 710 | u32 *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 711 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 712 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 713 | int ret; |
| 714 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 715 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 716 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 717 | if (size == 0) |
| 718 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 719 | |
| 720 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 721 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 722 | if (IS_ERR(obj)) |
| 723 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 724 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 725 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 726 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 727 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 728 | if (ret) |
| 729 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 730 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 731 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 735 | int |
| 736 | i915_gem_dumb_create(struct drm_file *file, |
| 737 | struct drm_device *dev, |
| 738 | struct drm_mode_create_dumb *args) |
| 739 | { |
| 740 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 741 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 742 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 743 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 744 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 745 | } |
| 746 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 747 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 748 | { |
| 749 | return !(obj->cache_level == I915_CACHE_NONE || |
| 750 | obj->cache_level == I915_CACHE_WT); |
| 751 | } |
| 752 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 753 | /** |
| 754 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 755 | * @dev: drm device pointer |
| 756 | * @data: ioctl data blob |
| 757 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 758 | */ |
| 759 | int |
| 760 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 761 | struct drm_file *file) |
| 762 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 763 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 764 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 765 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 766 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 767 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 768 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 769 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 770 | } |
| 771 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 772 | static inline enum fb_op_origin |
| 773 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 774 | { |
| 775 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 776 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 777 | } |
| 778 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 779 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 780 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 781 | intel_wakeref_t wakeref; |
| 782 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 783 | /* |
| 784 | * No actual flushing is required for the GTT write domain for reads |
| 785 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 786 | * as far as we know, so there's no chipset flush. It also doesn't |
| 787 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 788 | * |
| 789 | * However, we do have to enforce the order so that all writes through |
| 790 | * the GTT land before any writes to the device, such as updates to |
| 791 | * the GATT itself. |
| 792 | * |
| 793 | * We also have to wait a bit for the writes to land from the GTT. |
| 794 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 795 | * timing. This issue has only been observed when switching quickly |
| 796 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 797 | * 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] | 798 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 799 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 800 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 801 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 802 | wmb(); |
| 803 | |
| 804 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 805 | return; |
| 806 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 807 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 808 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 809 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 810 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 811 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 812 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 813 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 814 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 815 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static void |
| 819 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 820 | { |
| 821 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 822 | struct i915_vma *vma; |
| 823 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 824 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 825 | return; |
| 826 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 827 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 828 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 829 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 830 | |
| 831 | intel_fb_obj_flush(obj, |
| 832 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 833 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 834 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 835 | if (vma->iomap) |
| 836 | continue; |
| 837 | |
| 838 | i915_vma_unset_ggtt_write(vma); |
| 839 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 840 | break; |
| 841 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 842 | case I915_GEM_DOMAIN_WC: |
| 843 | wmb(); |
| 844 | break; |
| 845 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 846 | case I915_GEM_DOMAIN_CPU: |
| 847 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 848 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 849 | |
| 850 | case I915_GEM_DOMAIN_RENDER: |
| 851 | if (gpu_write_needs_clflush(obj)) |
| 852 | obj->cache_dirty = true; |
| 853 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 854 | } |
| 855 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 856 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 859 | /* |
| 860 | * Pins the specified object's pages and synchronizes the object with |
| 861 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 862 | * flush the object from the CPU cache. |
| 863 | */ |
| 864 | 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] | 865 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 866 | { |
| 867 | int ret; |
| 868 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 869 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 870 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 871 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 872 | if (!i915_gem_object_has_struct_page(obj)) |
| 873 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 874 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 875 | ret = i915_gem_object_wait(obj, |
| 876 | I915_WAIT_INTERRUPTIBLE | |
| 877 | I915_WAIT_LOCKED, |
| 878 | MAX_SCHEDULE_TIMEOUT, |
| 879 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 880 | if (ret) |
| 881 | return ret; |
| 882 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 883 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 884 | if (ret) |
| 885 | return ret; |
| 886 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 887 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 888 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 889 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 890 | if (ret) |
| 891 | goto err_unpin; |
| 892 | else |
| 893 | goto out; |
| 894 | } |
| 895 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 896 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 897 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 898 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 899 | * read domain and manually flush cachelines (if required). This |
| 900 | * optimizes for the case when the gpu will dirty the data |
| 901 | * anyway again before the next pread happens. |
| 902 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 903 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 904 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 905 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 906 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 907 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 908 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 909 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 910 | |
| 911 | err_unpin: |
| 912 | i915_gem_object_unpin_pages(obj); |
| 913 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 917 | unsigned int *needs_clflush) |
| 918 | { |
| 919 | int ret; |
| 920 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 921 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 922 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 923 | *needs_clflush = 0; |
| 924 | if (!i915_gem_object_has_struct_page(obj)) |
| 925 | return -ENODEV; |
| 926 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 927 | ret = i915_gem_object_wait(obj, |
| 928 | I915_WAIT_INTERRUPTIBLE | |
| 929 | I915_WAIT_LOCKED | |
| 930 | I915_WAIT_ALL, |
| 931 | MAX_SCHEDULE_TIMEOUT, |
| 932 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 933 | if (ret) |
| 934 | return ret; |
| 935 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 936 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 937 | if (ret) |
| 938 | return ret; |
| 939 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 940 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 941 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 942 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 943 | if (ret) |
| 944 | goto err_unpin; |
| 945 | else |
| 946 | goto out; |
| 947 | } |
| 948 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 949 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 950 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 951 | /* If we're not in the cpu write domain, set ourself into the |
| 952 | * gtt write domain and manually flush cachelines (as required). |
| 953 | * This optimizes for the case when the gpu will use the data |
| 954 | * right away and we therefore have to clflush anyway. |
| 955 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 956 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 957 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 958 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 959 | /* |
| 960 | * Same trick applies to invalidate partially written |
| 961 | * cachelines read before writing. |
| 962 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 963 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 964 | *needs_clflush |= CLFLUSH_BEFORE; |
| 965 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 966 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 967 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 968 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 969 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 970 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 971 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 972 | |
| 973 | err_unpin: |
| 974 | i915_gem_object_unpin_pages(obj); |
| 975 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 978 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 979 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 980 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 981 | { |
| 982 | char *vaddr; |
| 983 | int ret; |
| 984 | |
| 985 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 986 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 987 | if (needs_clflush) |
| 988 | drm_clflush_virt_range(vaddr + offset, len); |
| 989 | |
| 990 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 991 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 992 | kunmap(page); |
| 993 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 994 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | static int |
| 998 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 999 | struct drm_i915_gem_pread *args) |
| 1000 | { |
| 1001 | char __user *user_data; |
| 1002 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1003 | unsigned int needs_clflush; |
| 1004 | unsigned int idx, offset; |
| 1005 | int ret; |
| 1006 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1007 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 1008 | if (ret) |
| 1009 | return ret; |
| 1010 | |
| 1011 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 1012 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 1013 | if (ret) |
| 1014 | return ret; |
| 1015 | |
| 1016 | remain = args->size; |
| 1017 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1018 | offset = offset_in_page(args->offset); |
| 1019 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1020 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1021 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1022 | |
| 1023 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1024 | needs_clflush); |
| 1025 | if (ret) |
| 1026 | break; |
| 1027 | |
| 1028 | remain -= length; |
| 1029 | user_data += length; |
| 1030 | offset = 0; |
| 1031 | } |
| 1032 | |
| 1033 | i915_gem_obj_finish_shmem_access(obj); |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
| 1037 | static inline bool |
| 1038 | gtt_user_read(struct io_mapping *mapping, |
| 1039 | loff_t base, int offset, |
| 1040 | char __user *user_data, int length) |
| 1041 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1042 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1043 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1044 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1045 | /* 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] | 1046 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1047 | unwritten = __copy_to_user_inatomic(user_data, |
| 1048 | (void __force *)vaddr + offset, |
| 1049 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1050 | io_mapping_unmap_atomic(vaddr); |
| 1051 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1052 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1053 | unwritten = copy_to_user(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(vaddr); |
| 1057 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1058 | return unwritten; |
| 1059 | } |
| 1060 | |
| 1061 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1062 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1063 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1064 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1065 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1066 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1067 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1068 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1069 | struct i915_vma *vma; |
| 1070 | void __user *user_data; |
| 1071 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1072 | int ret; |
| 1073 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1074 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1075 | if (ret) |
| 1076 | return ret; |
| 1077 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1078 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1079 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1080 | PIN_MAPPABLE | |
| 1081 | PIN_NONFAULT | |
| 1082 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1083 | if (!IS_ERR(vma)) { |
| 1084 | node.start = i915_ggtt_offset(vma); |
| 1085 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1086 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1087 | if (ret) { |
| 1088 | i915_vma_unpin(vma); |
| 1089 | vma = ERR_PTR(ret); |
| 1090 | } |
| 1091 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1092 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1093 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1094 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1095 | goto out_unlock; |
| 1096 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1100 | if (ret) |
| 1101 | goto out_unpin; |
| 1102 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1103 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1104 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1105 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1106 | remain = args->size; |
| 1107 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1108 | |
| 1109 | while (remain > 0) { |
| 1110 | /* Operation in this page |
| 1111 | * |
| 1112 | * page_base = page offset within aperture |
| 1113 | * page_offset = offset within page |
| 1114 | * page_length = bytes to copy for this page |
| 1115 | */ |
| 1116 | u32 page_base = node.start; |
| 1117 | unsigned page_offset = offset_in_page(offset); |
| 1118 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1119 | page_length = remain < page_length ? remain : page_length; |
| 1120 | if (node.allocated) { |
| 1121 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1122 | ggtt->vm.insert_page(&ggtt->vm, |
| 1123 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1124 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1125 | wmb(); |
| 1126 | } else { |
| 1127 | page_base += offset & PAGE_MASK; |
| 1128 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1129 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1130 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1131 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1132 | ret = -EFAULT; |
| 1133 | break; |
| 1134 | } |
| 1135 | |
| 1136 | remain -= page_length; |
| 1137 | user_data += page_length; |
| 1138 | offset += page_length; |
| 1139 | } |
| 1140 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1141 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1142 | out_unpin: |
| 1143 | if (node.allocated) { |
| 1144 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1145 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1146 | remove_mappable_node(&node); |
| 1147 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1148 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1149 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1150 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1151 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1152 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1153 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1154 | return ret; |
| 1155 | } |
| 1156 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1157 | /** |
| 1158 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1159 | * @dev: drm device pointer |
| 1160 | * @data: ioctl data blob |
| 1161 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1162 | * |
| 1163 | * On error, the contents of *data are undefined. |
| 1164 | */ |
| 1165 | int |
| 1166 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1167 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1168 | { |
| 1169 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1170 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1171 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1172 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1173 | if (args->size == 0) |
| 1174 | return 0; |
| 1175 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1176 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1177 | args->size)) |
| 1178 | return -EFAULT; |
| 1179 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1180 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1181 | if (!obj) |
| 1182 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1183 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1184 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1185 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1186 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1187 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1190 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1191 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1192 | ret = i915_gem_object_wait(obj, |
| 1193 | I915_WAIT_INTERRUPTIBLE, |
| 1194 | MAX_SCHEDULE_TIMEOUT, |
| 1195 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1196 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1197 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1198 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1199 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1200 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1201 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1202 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1203 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1204 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1205 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1206 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1207 | i915_gem_object_unpin_pages(obj); |
| 1208 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1209 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1210 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1213 | /* This is the fast write path which cannot handle |
| 1214 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1215 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1216 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1217 | static inline bool |
| 1218 | ggtt_write(struct io_mapping *mapping, |
| 1219 | loff_t base, int offset, |
| 1220 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1221 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1222 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1223 | unsigned long unwritten; |
| 1224 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1225 | /* 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] | 1226 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1227 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1228 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1229 | io_mapping_unmap_atomic(vaddr); |
| 1230 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1231 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1232 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1233 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1234 | io_mapping_unmap(vaddr); |
| 1235 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1236 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1237 | return unwritten; |
| 1238 | } |
| 1239 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1240 | /** |
| 1241 | * This is the fast pwrite path, where we copy the data directly from the |
| 1242 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1243 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1244 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1245 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1246 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1247 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1248 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1249 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1250 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1251 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1252 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1253 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1254 | struct i915_vma *vma; |
| 1255 | u64 remain, offset; |
| 1256 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1257 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1258 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1259 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1260 | if (ret) |
| 1261 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1262 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1263 | if (i915_gem_object_has_struct_page(obj)) { |
| 1264 | /* |
| 1265 | * Avoid waking the device up if we can fallback, as |
| 1266 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1267 | * depending on PCI sleeps and our own resume time). |
| 1268 | * This easily dwarfs any performance advantage from |
| 1269 | * using the cache bypass of indirect GGTT access. |
| 1270 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1271 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1272 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1273 | ret = -EFAULT; |
| 1274 | goto out_unlock; |
| 1275 | } |
| 1276 | } else { |
| 1277 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1278 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1279 | } |
| 1280 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1281 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1282 | PIN_MAPPABLE | |
| 1283 | PIN_NONFAULT | |
| 1284 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1285 | if (!IS_ERR(vma)) { |
| 1286 | node.start = i915_ggtt_offset(vma); |
| 1287 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1288 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1289 | if (ret) { |
| 1290 | i915_vma_unpin(vma); |
| 1291 | vma = ERR_PTR(ret); |
| 1292 | } |
| 1293 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1294 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1295 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1296 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1297 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1298 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1299 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1300 | |
| 1301 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1302 | if (ret) |
| 1303 | goto out_unpin; |
| 1304 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1305 | mutex_unlock(&i915->drm.struct_mutex); |
| 1306 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1307 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1308 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1309 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1310 | offset = args->offset; |
| 1311 | remain = args->size; |
| 1312 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1313 | /* Operation in this page |
| 1314 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1315 | * page_base = page offset within aperture |
| 1316 | * page_offset = offset within page |
| 1317 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1318 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1319 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1320 | unsigned int page_offset = offset_in_page(offset); |
| 1321 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1322 | page_length = remain < page_length ? remain : page_length; |
| 1323 | if (node.allocated) { |
| 1324 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1325 | ggtt->vm.insert_page(&ggtt->vm, |
| 1326 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1327 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1328 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1329 | } else { |
| 1330 | page_base += offset & PAGE_MASK; |
| 1331 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1332 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1333 | * source page isn't available. Return the error and we'll |
| 1334 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1335 | * If the object is non-shmem backed, we retry again with the |
| 1336 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1337 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1338 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1339 | user_data, page_length)) { |
| 1340 | ret = -EFAULT; |
| 1341 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1342 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1343 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1344 | remain -= page_length; |
| 1345 | user_data += page_length; |
| 1346 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1347 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1348 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1349 | |
| 1350 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1351 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1352 | if (node.allocated) { |
| 1353 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1354 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1355 | remove_mappable_node(&node); |
| 1356 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1357 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1358 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1359 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1360 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1361 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1362 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1363 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1366 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1367 | * Flushes invalid cachelines before writing to the target if |
| 1368 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1369 | * writing if needs_clflush is set. |
| 1370 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1371 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1372 | 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] | 1373 | bool needs_clflush_before, |
| 1374 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1375 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1376 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1377 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1378 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1379 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1380 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1381 | if (needs_clflush_before) |
| 1382 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1383 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1384 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1385 | if (!ret && needs_clflush_after) |
| 1386 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1387 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1388 | kunmap(page); |
| 1389 | |
| 1390 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | static int |
| 1394 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1395 | const struct drm_i915_gem_pwrite *args) |
| 1396 | { |
| 1397 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1398 | void __user *user_data; |
| 1399 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1400 | unsigned int partial_cacheline_write; |
| 1401 | unsigned int needs_clflush; |
| 1402 | unsigned int offset, idx; |
| 1403 | int ret; |
| 1404 | |
| 1405 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1406 | if (ret) |
| 1407 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1408 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1409 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1410 | mutex_unlock(&i915->drm.struct_mutex); |
| 1411 | if (ret) |
| 1412 | return ret; |
| 1413 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1414 | /* If we don't overwrite a cacheline completely we need to be |
| 1415 | * careful to have up-to-date data by first clflushing. Don't |
| 1416 | * overcomplicate things and flush the entire patch. |
| 1417 | */ |
| 1418 | partial_cacheline_write = 0; |
| 1419 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1420 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1421 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1422 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1423 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1424 | offset = offset_in_page(args->offset); |
| 1425 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1426 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1427 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1428 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1429 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1430 | (offset | length) & partial_cacheline_write, |
| 1431 | needs_clflush & CLFLUSH_AFTER); |
| 1432 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1433 | break; |
| 1434 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1435 | remain -= length; |
| 1436 | user_data += length; |
| 1437 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1440 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1441 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1442 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | /** |
| 1446 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1447 | * @dev: drm device |
| 1448 | * @data: ioctl data blob |
| 1449 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1450 | * |
| 1451 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1452 | */ |
| 1453 | int |
| 1454 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1455 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1456 | { |
| 1457 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1458 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1459 | int ret; |
| 1460 | |
| 1461 | if (args->size == 0) |
| 1462 | return 0; |
| 1463 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1464 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1465 | return -EFAULT; |
| 1466 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1467 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1468 | if (!obj) |
| 1469 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1470 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1471 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1472 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1473 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1474 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1475 | } |
| 1476 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1477 | /* Writes not allowed into this read-only object */ |
| 1478 | if (i915_gem_object_is_readonly(obj)) { |
| 1479 | ret = -EINVAL; |
| 1480 | goto err; |
| 1481 | } |
| 1482 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1483 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1484 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1485 | ret = -ENODEV; |
| 1486 | if (obj->ops->pwrite) |
| 1487 | ret = obj->ops->pwrite(obj, args); |
| 1488 | if (ret != -ENODEV) |
| 1489 | goto err; |
| 1490 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1491 | ret = i915_gem_object_wait(obj, |
| 1492 | I915_WAIT_INTERRUPTIBLE | |
| 1493 | I915_WAIT_ALL, |
| 1494 | MAX_SCHEDULE_TIMEOUT, |
| 1495 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1496 | if (ret) |
| 1497 | goto err; |
| 1498 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1499 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1500 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1501 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1502 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1503 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1504 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1505 | * it would end up going through the fenced access, and we'll get |
| 1506 | * different detiling behavior between reading and writing. |
| 1507 | * pread/pwrite currently are reading and writing from the CPU |
| 1508 | * perspective, requiring manual detiling by the client. |
| 1509 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1510 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1511 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1512 | /* Note that the gtt paths might fail with non-page-backed user |
| 1513 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1514 | * textures). Fallback to the shmem path in that case. |
| 1515 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1516 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1517 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1518 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1519 | if (obj->phys_handle) |
| 1520 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1521 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1522 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1523 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1524 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1525 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1526 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1527 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1528 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1531 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1532 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame^] | 1533 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1534 | struct list_head *list; |
| 1535 | struct i915_vma *vma; |
| 1536 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1537 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1538 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame^] | 1539 | mutex_lock(&i915->ggtt.vm.mutex); |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1540 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1541 | if (!drm_mm_node_allocated(&vma->node)) |
| 1542 | continue; |
| 1543 | |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 1544 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1545 | } |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame^] | 1546 | mutex_unlock(&i915->ggtt.vm.mutex); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1547 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1548 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1549 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1550 | list_move_tail(&obj->mm.link, list); |
| 1551 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1552 | } |
| 1553 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1554 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1555 | * Called when user space prepares to use an object with the CPU, either |
| 1556 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1557 | * @dev: drm device |
| 1558 | * @data: ioctl data blob |
| 1559 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1560 | */ |
| 1561 | int |
| 1562 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1563 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1564 | { |
| 1565 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1566 | struct drm_i915_gem_object *obj; |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1567 | u32 read_domains = args->read_domains; |
| 1568 | u32 write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1569 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1570 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1571 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1572 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1573 | return -EINVAL; |
| 1574 | |
| 1575 | /* Having something in the write domain implies it's in the read |
| 1576 | * domain, and only that read domain. Enforce that in the request. |
| 1577 | */ |
| 1578 | if (write_domain != 0 && read_domains != write_domain) |
| 1579 | return -EINVAL; |
| 1580 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1581 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1582 | if (!obj) |
| 1583 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1584 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1585 | /* Try to flush the object off the GPU without holding the lock. |
| 1586 | * We will repeat the flush holding the lock in the normal manner |
| 1587 | * to catch cases where we are gazumped. |
| 1588 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1589 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1590 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1591 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1592 | (write_domain ? I915_WAIT_ALL : 0), |
| 1593 | MAX_SCHEDULE_TIMEOUT, |
| 1594 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1595 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1596 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1597 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1598 | /* |
| 1599 | * Proxy objects do not control access to the backing storage, ergo |
| 1600 | * they cannot be used as a means to manipulate the cache domain |
| 1601 | * tracking for that backing storage. The proxy object is always |
| 1602 | * considered to be outside of any cache domain. |
| 1603 | */ |
| 1604 | if (i915_gem_object_is_proxy(obj)) { |
| 1605 | err = -ENXIO; |
| 1606 | goto out; |
| 1607 | } |
| 1608 | |
| 1609 | /* |
| 1610 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1611 | * direct access in memory with previous cached writes through |
| 1612 | * shmemfs and that our cache domain tracking remains valid. |
| 1613 | * For example, if the obj->filp was moved to swap without us |
| 1614 | * being notified and releasing the pages, we would mistakenly |
| 1615 | * continue to assume that the obj remained out of the CPU cached |
| 1616 | * domain. |
| 1617 | */ |
| 1618 | err = i915_gem_object_pin_pages(obj); |
| 1619 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1620 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1621 | |
| 1622 | err = i915_mutex_lock_interruptible(dev); |
| 1623 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1624 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1625 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1626 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1627 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1628 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1629 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1630 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1631 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* And bump the LRU for this access */ |
| 1634 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1635 | |
| 1636 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1637 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1638 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1639 | intel_fb_obj_invalidate(obj, |
| 1640 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1641 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1642 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1643 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1644 | out: |
| 1645 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1646 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /** |
| 1650 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1651 | * @dev: drm device |
| 1652 | * @data: ioctl data blob |
| 1653 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1654 | */ |
| 1655 | int |
| 1656 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1657 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1658 | { |
| 1659 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1660 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1661 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1662 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1663 | if (!obj) |
| 1664 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1665 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1666 | /* |
| 1667 | * Proxy objects are barred from CPU access, so there is no |
| 1668 | * need to ban sw_finish as it is a nop. |
| 1669 | */ |
| 1670 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1671 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1672 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1673 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1674 | |
| 1675 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1679 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1680 | * it is mapped to. |
| 1681 | * @dev: drm device |
| 1682 | * @data: ioctl data blob |
| 1683 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1684 | * |
| 1685 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1686 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1687 | * |
| 1688 | * IMPORTANT: |
| 1689 | * |
| 1690 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1691 | * mmap support, please don't implement mmap support like here. The modern way |
| 1692 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1693 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1694 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1695 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1696 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1697 | */ |
| 1698 | int |
| 1699 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1700 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1701 | { |
| 1702 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1703 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1704 | unsigned long addr; |
| 1705 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1706 | if (args->flags & ~(I915_MMAP_WC)) |
| 1707 | return -EINVAL; |
| 1708 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1709 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1710 | return -ENODEV; |
| 1711 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1712 | obj = i915_gem_object_lookup(file, args->handle); |
| 1713 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1714 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1715 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1716 | /* prime objects have no backing filp to GEM mmap |
| 1717 | * pages from. |
| 1718 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1719 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1720 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1721 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1724 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1725 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1726 | args->offset); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1727 | if (args->flags & I915_MMAP_WC) { |
| 1728 | struct mm_struct *mm = current->mm; |
| 1729 | struct vm_area_struct *vma; |
| 1730 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1731 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1732 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1733 | return -EINTR; |
| 1734 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1735 | vma = find_vma(mm, addr); |
| 1736 | if (vma) |
| 1737 | vma->vm_page_prot = |
| 1738 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1739 | else |
| 1740 | addr = -ENOMEM; |
| 1741 | up_write(&mm->mmap_sem); |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1742 | |
| 1743 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1744 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1745 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1746 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1747 | if (IS_ERR((void *)addr)) |
| 1748 | return addr; |
| 1749 | |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1750 | args->addr_ptr = (u64)addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1751 | |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1755 | 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] | 1756 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1757 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1758 | } |
| 1759 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1760 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1761 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1762 | * |
| 1763 | * A history of the GTT mmap interface: |
| 1764 | * |
| 1765 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1766 | * aligned and suitable for fencing, and still fit into the available |
| 1767 | * mappable space left by the pinned display objects. A classic problem |
| 1768 | * we called the page-fault-of-doom where we would ping-pong between |
| 1769 | * two objects that could not fit inside the GTT and so the memcpy |
| 1770 | * would page one object in at the expense of the other between every |
| 1771 | * single byte. |
| 1772 | * |
| 1773 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1774 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1775 | * object is too large for the available space (or simply too large |
| 1776 | * for the mappable aperture!), a view is created instead and faulted |
| 1777 | * into userspace. (This view is aligned and sized appropriately for |
| 1778 | * fenced access.) |
| 1779 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1780 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1781 | * delayed writes via GTT before performing direct access via WC. |
| 1782 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1783 | * Restrictions: |
| 1784 | * |
| 1785 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1786 | * hangs on some architectures, corruption on others. An attempt to service |
| 1787 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1788 | * |
| 1789 | * * the object must be able to fit into RAM (physical memory, though no |
| 1790 | * limited to the mappable aperture). |
| 1791 | * |
| 1792 | * |
| 1793 | * Caveats: |
| 1794 | * |
| 1795 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1796 | * all data to system memory. Subsequent access will not be synchronized. |
| 1797 | * |
| 1798 | * * all mappings are revoked on runtime device suspend. |
| 1799 | * |
| 1800 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1801 | * (older machines require fence register for display and blitter access |
| 1802 | * as well). Contention of the fence registers will cause the previous users |
| 1803 | * to be unmapped and any new access will generate new page faults. |
| 1804 | * |
| 1805 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1806 | * rather than the expected SIGSEGV. |
| 1807 | */ |
| 1808 | int i915_gem_mmap_gtt_version(void) |
| 1809 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1810 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1811 | } |
| 1812 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1813 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1814 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1815 | pgoff_t page_offset, |
| 1816 | unsigned int chunk) |
| 1817 | { |
| 1818 | struct i915_ggtt_view view; |
| 1819 | |
| 1820 | if (i915_gem_object_is_tiled(obj)) |
| 1821 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1822 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1823 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1824 | view.partial.offset = rounddown(page_offset, chunk); |
| 1825 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1826 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1827 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1828 | |
| 1829 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1830 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1831 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1832 | |
| 1833 | return view; |
| 1834 | } |
| 1835 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1836 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1837 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1838 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1839 | * |
| 1840 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1841 | * from userspace. The fault handler takes care of binding the object to |
| 1842 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1843 | * only if needed based on whether the old reg is still valid or the object |
| 1844 | * is tiled) and inserting a new PTE into the faulting process. |
| 1845 | * |
| 1846 | * Note that the faulting process may involve evicting existing objects |
| 1847 | * from the GTT and/or fence registers to make room. So performance may |
| 1848 | * suffer if the GTT working set is large or there are few fence registers |
| 1849 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1850 | * |
| 1851 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1852 | * 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] | 1853 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1854 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1855 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1856 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1857 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1858 | 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] | 1859 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1860 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1861 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1862 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1863 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1864 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1865 | pgoff_t page_offset; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1866 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1867 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1868 | /* Sanity check that we allow writing into this object */ |
| 1869 | if (i915_gem_object_is_readonly(obj) && write) |
| 1870 | return VM_FAULT_SIGBUS; |
| 1871 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1872 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1873 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1874 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1875 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1876 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1877 | /* 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] | 1878 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1879 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1880 | * where we are gazumped. |
| 1881 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1882 | ret = i915_gem_object_wait(obj, |
| 1883 | I915_WAIT_INTERRUPTIBLE, |
| 1884 | MAX_SCHEDULE_TIMEOUT, |
| 1885 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1886 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1887 | goto err; |
| 1888 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1889 | ret = i915_gem_object_pin_pages(obj); |
| 1890 | if (ret) |
| 1891 | goto err; |
| 1892 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1893 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1894 | |
| 1895 | ret = i915_mutex_lock_interruptible(dev); |
| 1896 | if (ret) |
| 1897 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1898 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1899 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1900 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1901 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1902 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1905 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1906 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1907 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1908 | PIN_MAPPABLE | |
| 1909 | PIN_NONBLOCK | |
| 1910 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1911 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1912 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1913 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1914 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1915 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1916 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1917 | flags = PIN_MAPPABLE; |
| 1918 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1919 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1920 | |
| 1921 | /* |
| 1922 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1923 | * all hope that the hardware is able to track future writes. |
| 1924 | */ |
| 1925 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1926 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1927 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1928 | if (IS_ERR(vma) && !view.type) { |
| 1929 | flags = PIN_MAPPABLE; |
| 1930 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1931 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1932 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1933 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1934 | if (IS_ERR(vma)) { |
| 1935 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1936 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1937 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1938 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1939 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1940 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1941 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1942 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1943 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1944 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1945 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1946 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1947 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1948 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1949 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1950 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1951 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1952 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1953 | if (ret) |
| 1954 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1955 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1956 | /* Mark as being mmapped into userspace for later revocation */ |
| 1957 | assert_rpm_wakelock_held(dev_priv); |
| 1958 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1959 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1960 | GEM_BUG_ON(!obj->userfault_count); |
| 1961 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1962 | i915_vma_set_ggtt_write(vma); |
| 1963 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1964 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1965 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1966 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1967 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1968 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1969 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1970 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1971 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1972 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1973 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1974 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1975 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1976 | /* |
| 1977 | * We eat errors when the gpu is terminally wedged to avoid |
| 1978 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1979 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1980 | * and so needs to be reported. |
| 1981 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1982 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) |
| 1983 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1984 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1985 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1986 | /* |
| 1987 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1988 | * handler to reset everything when re-faulting in |
| 1989 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1990 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1991 | case 0: |
| 1992 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1993 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1994 | case -EBUSY: |
| 1995 | /* |
| 1996 | * EBUSY is ok: this just means that another thread |
| 1997 | * already did the job. |
| 1998 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1999 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2000 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2001 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2002 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2003 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2004 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2005 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2006 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2007 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2011 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2012 | { |
| 2013 | struct i915_vma *vma; |
| 2014 | |
| 2015 | GEM_BUG_ON(!obj->userfault_count); |
| 2016 | |
| 2017 | obj->userfault_count = 0; |
| 2018 | list_del(&obj->userfault_link); |
| 2019 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2020 | obj->base.dev->anon_inode->i_mapping); |
| 2021 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 2022 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2023 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2026 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2027 | * i915_gem_release_mmap - remove physical page mappings |
| 2028 | * @obj: obj in question |
| 2029 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2030 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2031 | * relinquish ownership of the pages back to the system. |
| 2032 | * |
| 2033 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2034 | * object through the GTT and then lose the fence register due to |
| 2035 | * resource pressure. Similarly if the object has been moved out of the |
| 2036 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2037 | * mapping will then trigger a page fault on the next user access, allowing |
| 2038 | * fixup by i915_gem_fault(). |
| 2039 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2040 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2041 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2042 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2043 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2044 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2045 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2046 | /* Serialisation between user GTT access and our code depends upon |
| 2047 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2048 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2049 | * |
| 2050 | * Note that RPM complicates somewhat by adding an additional |
| 2051 | * requirement that operations to the GGTT be made holding the RPM |
| 2052 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2053 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2054 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2055 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2056 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2057 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2058 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2059 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2060 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2061 | |
| 2062 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2063 | * memory transactions from userspace before we return. The TLB |
| 2064 | * flushing implied above by changing the PTE above *should* be |
| 2065 | * sufficient, an extra barrier here just provides us with a bit |
| 2066 | * of paranoid documentation about our requirement to serialise |
| 2067 | * memory writes before touching registers / GSM. |
| 2068 | */ |
| 2069 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2070 | |
| 2071 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2072 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2073 | } |
| 2074 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2075 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2076 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2077 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2078 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2079 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2080 | /* |
| 2081 | * Only called during RPM suspend. All users of the userfault_list |
| 2082 | * must be holding an RPM wakeref to ensure that this can not |
| 2083 | * run concurrently with themselves (and use the struct_mutex for |
| 2084 | * protection between themselves). |
| 2085 | */ |
| 2086 | |
| 2087 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2088 | &dev_priv->mm.userfault_list, userfault_link) |
| 2089 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* The fence will be lost when the device powers down. If any were |
| 2092 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2093 | * down! All other fences will be reacquired by the user upon waking. |
| 2094 | */ |
| 2095 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2096 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2097 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2098 | /* Ideally we want to assert that the fence register is not |
| 2099 | * live at this point (i.e. that no piece of code will be |
| 2100 | * trying to write through fence + GTT, as that both violates |
| 2101 | * our tracking of activity and associated locking/barriers, |
| 2102 | * but also is illegal given that the hw is powered down). |
| 2103 | * |
| 2104 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2105 | * That is not sufficient, and we need a more fine-grained |
| 2106 | * tool if we want to have a sanity check here. |
| 2107 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2108 | |
| 2109 | if (!reg->vma) |
| 2110 | continue; |
| 2111 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2112 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2113 | reg->dirty = true; |
| 2114 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2115 | } |
| 2116 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2117 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2118 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2119 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2120 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2121 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2122 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2123 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2124 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2125 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2126 | /* Attempt to reap some mmap space from dead objects */ |
| 2127 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2128 | err = i915_gem_wait_for_idle(dev_priv, |
| 2129 | I915_WAIT_INTERRUPTIBLE, |
| 2130 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2131 | if (err) |
| 2132 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2133 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2134 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2135 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2136 | if (!err) |
| 2137 | break; |
| 2138 | |
| 2139 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2140 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2141 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2145 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2146 | drm_gem_free_mmap_offset(&obj->base); |
| 2147 | } |
| 2148 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2149 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2150 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2151 | struct drm_device *dev, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 2152 | u32 handle, |
| 2153 | u64 *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2154 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2155 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2156 | int ret; |
| 2157 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2158 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2159 | if (!obj) |
| 2160 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2161 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2162 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2163 | if (ret == 0) |
| 2164 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2165 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2166 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2167 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2168 | } |
| 2169 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2170 | /** |
| 2171 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2172 | * @dev: DRM device |
| 2173 | * @data: GTT mapping ioctl data |
| 2174 | * @file: GEM object info |
| 2175 | * |
| 2176 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2177 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2178 | * up so we can get faults in the handler above. |
| 2179 | * |
| 2180 | * The fault handler will take care of binding the object into the GTT |
| 2181 | * (since it may have been evicted to make room for something), allocating |
| 2182 | * a fence register, and mapping the appropriate aperture address into |
| 2183 | * userspace. |
| 2184 | */ |
| 2185 | int |
| 2186 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2187 | struct drm_file *file) |
| 2188 | { |
| 2189 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2190 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2191 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2192 | } |
| 2193 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2194 | /* Immediately discard the backing storage */ |
| 2195 | static void |
| 2196 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2197 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2198 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2199 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2200 | if (obj->base.filp == NULL) |
| 2201 | return; |
| 2202 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2203 | /* Our goal here is to return as much of the memory as |
| 2204 | * is possible back to the system as we are called from OOM. |
| 2205 | * To do this we must instruct the shmfs to drop all of its |
| 2206 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2207 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2208 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2209 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2210 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2211 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2212 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2213 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2214 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2215 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2216 | struct address_space *mapping; |
| 2217 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2218 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2219 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2220 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2221 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2222 | case I915_MADV_DONTNEED: |
| 2223 | i915_gem_object_truncate(obj); |
| 2224 | case __I915_MADV_PURGED: |
| 2225 | return; |
| 2226 | } |
| 2227 | |
| 2228 | if (obj->base.filp == NULL) |
| 2229 | return; |
| 2230 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2231 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2232 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2233 | } |
| 2234 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2235 | /* |
| 2236 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2237 | * ref count of those pages. |
| 2238 | */ |
| 2239 | static void check_release_pagevec(struct pagevec *pvec) |
| 2240 | { |
| 2241 | check_move_unevictable_pages(pvec); |
| 2242 | __pagevec_release(pvec); |
| 2243 | cond_resched(); |
| 2244 | } |
| 2245 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2246 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2247 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2248 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2249 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2250 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2251 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2252 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2253 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2254 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2255 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2256 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2257 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2258 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2259 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2260 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2261 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2262 | |
| 2263 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2264 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2265 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2266 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2267 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2268 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2269 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2270 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2271 | if (!pagevec_add(&pvec, page)) |
| 2272 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2273 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2274 | if (pagevec_count(&pvec)) |
| 2275 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2276 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2277 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2278 | sg_free_table(pages); |
| 2279 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2282 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2283 | { |
| 2284 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2285 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2286 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2287 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2288 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2289 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2290 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2291 | } |
| 2292 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2293 | static struct sg_table * |
| 2294 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2295 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2296 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2297 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2298 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2299 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2300 | if (IS_ERR_OR_NULL(pages)) |
| 2301 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2302 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2303 | spin_lock(&i915->mm.obj_lock); |
| 2304 | list_del(&obj->mm.link); |
| 2305 | spin_unlock(&i915->mm.obj_lock); |
| 2306 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2307 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2308 | void *ptr; |
| 2309 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2310 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2311 | if (is_vmalloc_addr(ptr)) |
| 2312 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2313 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2314 | kunmap(kmap_to_page(ptr)); |
| 2315 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2316 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2319 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2320 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2321 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2322 | return pages; |
| 2323 | } |
| 2324 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2325 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2326 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2327 | { |
| 2328 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2329 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2330 | |
| 2331 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2332 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2333 | |
| 2334 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2335 | |
| 2336 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2337 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2338 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2339 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2340 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2341 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2342 | |
| 2343 | /* |
| 2344 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2345 | * array, hence protect them from being reaped by removing them from gtt |
| 2346 | * lists early. |
| 2347 | */ |
| 2348 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2349 | |
| 2350 | /* |
| 2351 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2352 | * NULL pages. In the future, when we have more asynchronous |
| 2353 | * get_pages backends we should be better able to handle the |
| 2354 | * cancellation of the async task in a more uniform manner. |
| 2355 | */ |
| 2356 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2357 | pages = ERR_PTR(-EINVAL); |
| 2358 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2359 | if (!IS_ERR(pages)) |
| 2360 | obj->ops->put_pages(obj, pages); |
| 2361 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2362 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2363 | unlock: |
| 2364 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2365 | |
| 2366 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2367 | } |
| 2368 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2369 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2370 | { |
| 2371 | struct sg_table new_st; |
| 2372 | struct scatterlist *sg, *new_sg; |
| 2373 | unsigned int i; |
| 2374 | |
| 2375 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2376 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2377 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2378 | 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] | 2379 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2380 | |
| 2381 | new_sg = new_st.sgl; |
| 2382 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2383 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2384 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2385 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2386 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2387 | new_sg = sg_next(new_sg); |
| 2388 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2389 | 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] | 2390 | |
| 2391 | sg_free_table(orig_st); |
| 2392 | |
| 2393 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2394 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2397 | 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] | 2398 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2399 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2400 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2401 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2402 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2403 | struct sg_table *st; |
| 2404 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2405 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2406 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2407 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2408 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2409 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2410 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2411 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2412 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2413 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2414 | /* |
| 2415 | * 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] | 2416 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2417 | * a GPU cache |
| 2418 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2419 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2420 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2421 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2422 | /* |
| 2423 | * If there's no chance of allocating enough pages for the whole |
| 2424 | * object, bail early. |
| 2425 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2426 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2427 | return -ENOMEM; |
| 2428 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2429 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2430 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2431 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2432 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2433 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2434 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2435 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2436 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2437 | } |
| 2438 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2439 | /* |
| 2440 | * 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] | 2441 | * at this point until we release them. |
| 2442 | * |
| 2443 | * Fail silently without starting the shrinker |
| 2444 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2445 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2446 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2447 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2448 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2449 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2450 | sg = st->sgl; |
| 2451 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2452 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2453 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2454 | const unsigned int shrink[] = { |
| 2455 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2456 | 0, |
| 2457 | }, *s = shrink; |
| 2458 | gfp_t gfp = noreclaim; |
| 2459 | |
| 2460 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2461 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2462 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2463 | if (likely(!IS_ERR(page))) |
| 2464 | break; |
| 2465 | |
| 2466 | if (!*s) { |
| 2467 | ret = PTR_ERR(page); |
| 2468 | goto err_sg; |
| 2469 | } |
| 2470 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2471 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2472 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2473 | /* |
| 2474 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2475 | * our own buffer, now let the real VM do its job and |
| 2476 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2477 | * |
| 2478 | * However, since graphics tend to be disposable, |
| 2479 | * defer the oom here by reporting the ENOMEM back |
| 2480 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2481 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2482 | if (!*s) { |
| 2483 | /* reclaim and warn, but no oom */ |
| 2484 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2485 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2486 | /* |
| 2487 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2488 | * kswapd to reclaim our pages (direct reclaim |
| 2489 | * does not effectively begin pageout of our |
| 2490 | * buffers on its own). However, direct reclaim |
| 2491 | * only waits for kswapd when under allocation |
| 2492 | * congestion. So as a result __GFP_RECLAIM is |
| 2493 | * unreliable and fails to actually reclaim our |
| 2494 | * dirty pages -- unless you try over and over |
| 2495 | * again with !__GFP_NORETRY. However, we still |
| 2496 | * want to fail this allocation rather than |
| 2497 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2498 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2499 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2500 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2501 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2502 | } while (1); |
| 2503 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2504 | if (!i || |
| 2505 | sg->length >= max_segment || |
| 2506 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2507 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2508 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2509 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2510 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2511 | st->nents++; |
| 2512 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2513 | } else { |
| 2514 | sg->length += PAGE_SIZE; |
| 2515 | } |
| 2516 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2517 | |
| 2518 | /* Check that the i965g/gm workaround works. */ |
| 2519 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2520 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2521 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2522 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2523 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2524 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2525 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2526 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2527 | i915_sg_trim(st); |
| 2528 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2529 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2530 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2531 | /* |
| 2532 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2533 | * it could not reserve enough large entries, asking |
| 2534 | * for PAGE_SIZE chunks instead may be helpful. |
| 2535 | */ |
| 2536 | if (max_segment > PAGE_SIZE) { |
| 2537 | for_each_sgt_page(page, sgt_iter, st) |
| 2538 | put_page(page); |
| 2539 | sg_free_table(st); |
| 2540 | |
| 2541 | max_segment = PAGE_SIZE; |
| 2542 | goto rebuild_st; |
| 2543 | } else { |
| 2544 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2545 | "Failed to DMA remap %lu pages\n", |
| 2546 | page_count); |
| 2547 | goto err_pages; |
| 2548 | } |
| 2549 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2550 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2551 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2552 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2553 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2554 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2555 | |
| 2556 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2557 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2558 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2559 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2560 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2561 | mapping_clear_unevictable(mapping); |
| 2562 | pagevec_init(&pvec); |
| 2563 | for_each_sgt_page(page, sgt_iter, st) { |
| 2564 | if (!pagevec_add(&pvec, page)) |
| 2565 | check_release_pagevec(&pvec); |
| 2566 | } |
| 2567 | if (pagevec_count(&pvec)) |
| 2568 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2569 | sg_free_table(st); |
| 2570 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2571 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2572 | /* |
| 2573 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2574 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2575 | * ENOMEM for a genuine allocation failure. |
| 2576 | * |
| 2577 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2578 | * space and so want to translate the error from shmemfs back to our |
| 2579 | * usual understanding of ENOMEM. |
| 2580 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2581 | if (ret == -ENOSPC) |
| 2582 | ret = -ENOMEM; |
| 2583 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2584 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2588 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2589 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2590 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2591 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2592 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2593 | int i; |
| 2594 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2595 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2596 | |
| 2597 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2598 | obj->mm.get_page.sg_idx = 0; |
| 2599 | |
| 2600 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2601 | |
| 2602 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2603 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2604 | GEM_BUG_ON(obj->mm.quirked); |
| 2605 | __i915_gem_object_pin_pages(obj); |
| 2606 | obj->mm.quirked = true; |
| 2607 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2608 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2609 | GEM_BUG_ON(!sg_page_sizes); |
| 2610 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2611 | |
| 2612 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2613 | * Calculate the supported page-sizes which fit into the given |
| 2614 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2615 | * to use opportunistically when later inserting into the GTT. For |
| 2616 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2617 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2618 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2619 | */ |
| 2620 | obj->mm.page_sizes.sg = 0; |
| 2621 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2622 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2623 | obj->mm.page_sizes.sg |= BIT(i); |
| 2624 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2625 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2626 | |
| 2627 | spin_lock(&i915->mm.obj_lock); |
| 2628 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2629 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2633 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2634 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2635 | |
| 2636 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2637 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2638 | return -EFAULT; |
| 2639 | } |
| 2640 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2641 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2642 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2643 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2644 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2647 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2648 | * 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] | 2649 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2650 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2651 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2652 | * or as the object is itself released. |
| 2653 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2654 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2655 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2656 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2657 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2658 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2659 | if (err) |
| 2660 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2661 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2662 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2663 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2664 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2665 | err = ____i915_gem_object_get_pages(obj); |
| 2666 | if (err) |
| 2667 | goto unlock; |
| 2668 | |
| 2669 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2670 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2671 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2672 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2673 | unlock: |
| 2674 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2675 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2678 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2679 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2680 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2681 | { |
| 2682 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2683 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2684 | struct sgt_iter sgt_iter; |
| 2685 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2686 | struct page *stack_pages[32]; |
| 2687 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2688 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2689 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2690 | void *addr; |
| 2691 | |
| 2692 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2693 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2694 | return kmap(sg_page(sgt->sgl)); |
| 2695 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2696 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2697 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2698 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2699 | if (!pages) |
| 2700 | return NULL; |
| 2701 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2702 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2703 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2704 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2705 | |
| 2706 | /* Check that we have the expected number of pages */ |
| 2707 | GEM_BUG_ON(i != n_pages); |
| 2708 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2709 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2710 | default: |
| 2711 | MISSING_CASE(type); |
| 2712 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2713 | case I915_MAP_WB: |
| 2714 | pgprot = PAGE_KERNEL; |
| 2715 | break; |
| 2716 | case I915_MAP_WC: |
| 2717 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2718 | break; |
| 2719 | } |
| 2720 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2721 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2722 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2723 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2724 | |
| 2725 | return addr; |
| 2726 | } |
| 2727 | |
| 2728 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2729 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2730 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2731 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2732 | enum i915_map_type has_type; |
| 2733 | bool pinned; |
| 2734 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2735 | int ret; |
| 2736 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2737 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2738 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2739 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2740 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2741 | if (ret) |
| 2742 | return ERR_PTR(ret); |
| 2743 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2744 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2745 | type &= ~I915_MAP_OVERRIDE; |
| 2746 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2747 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2748 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2749 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2750 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2751 | ret = ____i915_gem_object_get_pages(obj); |
| 2752 | if (ret) |
| 2753 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2754 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2755 | smp_mb__before_atomic(); |
| 2756 | } |
| 2757 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2758 | pinned = false; |
| 2759 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2760 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2761 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2762 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2763 | if (ptr && has_type != type) { |
| 2764 | if (pinned) { |
| 2765 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2766 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2767 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2768 | |
| 2769 | if (is_vmalloc_addr(ptr)) |
| 2770 | vunmap(ptr); |
| 2771 | else |
| 2772 | kunmap(kmap_to_page(ptr)); |
| 2773 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2774 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2775 | } |
| 2776 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2777 | if (!ptr) { |
| 2778 | ptr = i915_gem_object_map(obj, type); |
| 2779 | if (!ptr) { |
| 2780 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2781 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2782 | } |
| 2783 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2784 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2785 | } |
| 2786 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2787 | out_unlock: |
| 2788 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2789 | return ptr; |
| 2790 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2791 | err_unpin: |
| 2792 | atomic_dec(&obj->mm.pages_pin_count); |
| 2793 | err_unlock: |
| 2794 | ptr = ERR_PTR(ret); |
| 2795 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2796 | } |
| 2797 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2798 | static int |
| 2799 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2800 | const struct drm_i915_gem_pwrite *arg) |
| 2801 | { |
| 2802 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2803 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2804 | u64 remain, offset; |
| 2805 | unsigned int pg; |
| 2806 | |
| 2807 | /* Before we instantiate/pin the backing store for our use, we |
| 2808 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2809 | * the pagecache. We avoid the penalty of instantiating all the |
| 2810 | * pages, important if the user is just writing to a few and never |
| 2811 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2812 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2813 | * or clearing-before-use) before it is overwritten. |
| 2814 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2815 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2816 | return -ENODEV; |
| 2817 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2818 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2819 | return -EFAULT; |
| 2820 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2821 | /* Before the pages are instantiated the object is treated as being |
| 2822 | * in the CPU domain. The pages will be clflushed as required before |
| 2823 | * use, and we can freely write into the pages directly. If userspace |
| 2824 | * races pwrite with any other operation; corruption will ensue - |
| 2825 | * that is userspace's prerogative! |
| 2826 | */ |
| 2827 | |
| 2828 | remain = arg->size; |
| 2829 | offset = arg->offset; |
| 2830 | pg = offset_in_page(offset); |
| 2831 | |
| 2832 | do { |
| 2833 | unsigned int len, unwritten; |
| 2834 | struct page *page; |
| 2835 | void *data, *vaddr; |
| 2836 | int err; |
| 2837 | |
| 2838 | len = PAGE_SIZE - pg; |
| 2839 | if (len > remain) |
| 2840 | len = remain; |
| 2841 | |
| 2842 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2843 | offset, len, 0, |
| 2844 | &page, &data); |
| 2845 | if (err < 0) |
| 2846 | return err; |
| 2847 | |
| 2848 | vaddr = kmap(page); |
| 2849 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2850 | kunmap(page); |
| 2851 | |
| 2852 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2853 | offset, len, len - unwritten, |
| 2854 | page, data); |
| 2855 | if (err < 0) |
| 2856 | return err; |
| 2857 | |
| 2858 | if (unwritten) |
| 2859 | return -EFAULT; |
| 2860 | |
| 2861 | remain -= len; |
| 2862 | user_data += len; |
| 2863 | offset += len; |
| 2864 | pg = 0; |
| 2865 | } while (remain); |
| 2866 | |
| 2867 | return 0; |
| 2868 | } |
| 2869 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2870 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2871 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2872 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2873 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2874 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2875 | |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2876 | /* |
| 2877 | * We are called by the error capture, reset and to dump engine |
| 2878 | * state at random points in time. In particular, note that neither is |
| 2879 | * crucially ordered with an interrupt. After a hang, the GPU is dead |
| 2880 | * and we assume that no more writes can happen (we waited long enough |
| 2881 | * for all writes that were in transaction to be flushed) - adding an |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2882 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2883 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2884 | * At all other times, we must assume the GPU is still running, but |
| 2885 | * we only care about the snapshot of this moment. |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2886 | */ |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2887 | spin_lock_irqsave(&engine->timeline.lock, flags); |
| 2888 | list_for_each_entry(request, &engine->timeline.requests, link) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2889 | if (__i915_request_completed(request, request->global_seqno)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2890 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2891 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2892 | active = request; |
| 2893 | break; |
| 2894 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2895 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2896 | |
| 2897 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2898 | } |
| 2899 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2900 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2901 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2902 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2903 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2904 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 2905 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2906 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2907 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2908 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2909 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2910 | mutex_unlock(&dev->struct_mutex); |
| 2911 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2912 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2913 | /* |
| 2914 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2915 | * We do not need to do this test under locking as in the worst-case |
| 2916 | * we queue the retire worker once too often. |
| 2917 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2918 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2919 | queue_delayed_work(dev_priv->wq, |
| 2920 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2921 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2922 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2923 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2924 | static void shrink_caches(struct drm_i915_private *i915) |
| 2925 | { |
| 2926 | /* |
| 2927 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 2928 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 2929 | * with the aim of reducing fragmentation. |
| 2930 | */ |
| 2931 | kmem_cache_shrink(i915->priorities); |
| 2932 | kmem_cache_shrink(i915->dependencies); |
| 2933 | kmem_cache_shrink(i915->requests); |
| 2934 | kmem_cache_shrink(i915->luts); |
| 2935 | kmem_cache_shrink(i915->vmas); |
| 2936 | kmem_cache_shrink(i915->objects); |
| 2937 | } |
| 2938 | |
| 2939 | struct sleep_rcu_work { |
| 2940 | union { |
| 2941 | struct rcu_head rcu; |
| 2942 | struct work_struct work; |
| 2943 | }; |
| 2944 | struct drm_i915_private *i915; |
| 2945 | unsigned int epoch; |
| 2946 | }; |
| 2947 | |
| 2948 | static inline bool |
| 2949 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 2950 | { |
| 2951 | /* |
| 2952 | * There is a small chance that the epoch wrapped since we started |
| 2953 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 2954 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 2955 | */ |
| 2956 | return epoch == READ_ONCE(i915->gt.epoch); |
| 2957 | } |
| 2958 | |
| 2959 | static void __sleep_work(struct work_struct *work) |
| 2960 | { |
| 2961 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 2962 | struct drm_i915_private *i915 = s->i915; |
| 2963 | unsigned int epoch = s->epoch; |
| 2964 | |
| 2965 | kfree(s); |
| 2966 | if (same_epoch(i915, epoch)) |
| 2967 | shrink_caches(i915); |
| 2968 | } |
| 2969 | |
| 2970 | static void __sleep_rcu(struct rcu_head *rcu) |
| 2971 | { |
| 2972 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 2973 | struct drm_i915_private *i915 = s->i915; |
| 2974 | |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 2975 | destroy_rcu_head(&s->rcu); |
| 2976 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2977 | if (same_epoch(i915, s->epoch)) { |
| 2978 | INIT_WORK(&s->work, __sleep_work); |
| 2979 | queue_work(i915->wq, &s->work); |
| 2980 | } else { |
| 2981 | kfree(s); |
| 2982 | } |
| 2983 | } |
| 2984 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 2985 | static inline bool |
| 2986 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 2987 | { |
| 2988 | return (READ_ONCE(i915->gt.active_requests) || |
| 2989 | work_pending(&i915->gt.idle_work.work)); |
| 2990 | } |
| 2991 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2992 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
| 2993 | { |
| 2994 | struct intel_engine_cs *engine; |
| 2995 | enum intel_engine_id id; |
| 2996 | |
| 2997 | if (i915_terminally_wedged(&i915->gpu_error)) |
| 2998 | return; |
| 2999 | |
| 3000 | GEM_BUG_ON(i915->gt.active_requests); |
| 3001 | for_each_engine(engine, i915, id) { |
| 3002 | GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request)); |
| 3003 | GEM_BUG_ON(engine->last_retired_context != |
| 3004 | to_intel_context(i915->kernel_context, engine)); |
| 3005 | } |
| 3006 | } |
| 3007 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3008 | static void |
| 3009 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3010 | { |
| 3011 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3012 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3013 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3014 | bool rearm_hangcheck; |
| 3015 | |
| 3016 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3017 | return; |
| 3018 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 3019 | if (READ_ONCE(dev_priv->gt.active_requests)) |
| 3020 | return; |
| 3021 | |
| 3022 | /* |
| 3023 | * Flush out the last user context, leaving only the pinned |
| 3024 | * kernel context resident. When we are idling on the kernel_context, |
| 3025 | * no more new requests (with a context switch) are emitted and we |
| 3026 | * can finally rest. A consequence is that the idle work handler is |
| 3027 | * always called at least twice before idling (and if the system is |
| 3028 | * idle that implies a round trip through the retire worker). |
| 3029 | */ |
| 3030 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 3031 | i915_gem_switch_to_kernel_context(dev_priv); |
| 3032 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 3033 | |
| 3034 | GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n", |
| 3035 | READ_ONCE(dev_priv->gt.active_requests)); |
| 3036 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3037 | /* |
| 3038 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3039 | * new request is submitted. As we don't trust the hardware, we |
| 3040 | * continue on if the wait times out. This is necessary to allow |
| 3041 | * the machine to suspend even if the hardware dies, and we will |
| 3042 | * try to recover in resume (after depriving the hardware of power, |
| 3043 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3044 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3045 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3046 | intel_engines_are_idle(dev_priv), |
| 3047 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3048 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3049 | |
| 3050 | rearm_hangcheck = |
| 3051 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3052 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3053 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3054 | /* Currently busy, come back later */ |
| 3055 | mod_delayed_work(dev_priv->wq, |
| 3056 | &dev_priv->gt.idle_work, |
| 3057 | msecs_to_jiffies(50)); |
| 3058 | goto out_rearm; |
| 3059 | } |
| 3060 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3061 | /* |
| 3062 | * New request retired after this work handler started, extend active |
| 3063 | * period until next instance of the work. |
| 3064 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3065 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3066 | goto out_unlock; |
| 3067 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 3068 | epoch = __i915_gem_park(dev_priv); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3069 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3070 | assert_kernel_context_is_current(dev_priv); |
| 3071 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3072 | rearm_hangcheck = false; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3073 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3074 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3075 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3076 | out_rearm: |
| 3077 | if (rearm_hangcheck) { |
| 3078 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3079 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3080 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3081 | |
| 3082 | /* |
| 3083 | * When we are idle, it is an opportune time to reap our caches. |
| 3084 | * However, we have many objects that utilise RCU and the ordered |
| 3085 | * i915->wq that this work is executing on. To try and flush any |
| 3086 | * pending frees now we are idle, we first wait for an RCU grace |
| 3087 | * period, and then queue a task (that will run last on the wq) to |
| 3088 | * shrink and re-optimize the caches. |
| 3089 | */ |
| 3090 | if (same_epoch(dev_priv, epoch)) { |
| 3091 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3092 | if (s) { |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3093 | init_rcu_head(&s->rcu); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3094 | s->i915 = dev_priv; |
| 3095 | s->epoch = epoch; |
| 3096 | call_rcu(&s->rcu, __sleep_rcu); |
| 3097 | } |
| 3098 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3099 | } |
| 3100 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3101 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3102 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3103 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3104 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3105 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3106 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3107 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3108 | mutex_lock(&i915->drm.struct_mutex); |
| 3109 | |
| 3110 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3111 | struct i915_gem_context *ctx = lut->ctx; |
| 3112 | struct i915_vma *vma; |
| 3113 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3114 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3115 | if (ctx->file_priv != fpriv) |
| 3116 | continue; |
| 3117 | |
| 3118 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3119 | GEM_BUG_ON(vma->obj != obj); |
| 3120 | |
| 3121 | /* We allow the process to have multiple handles to the same |
| 3122 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3123 | */ |
| 3124 | GEM_BUG_ON(!vma->open_count); |
| 3125 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3126 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3127 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3128 | list_del(&lut->obj_link); |
| 3129 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3130 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3131 | kmem_cache_free(i915->luts, lut); |
| 3132 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3133 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3134 | |
| 3135 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3136 | } |
| 3137 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3138 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3139 | { |
| 3140 | if (timeout_ns < 0) |
| 3141 | return MAX_SCHEDULE_TIMEOUT; |
| 3142 | |
| 3143 | if (timeout_ns == 0) |
| 3144 | return 0; |
| 3145 | |
| 3146 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3147 | } |
| 3148 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3149 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3150 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3151 | * @dev: drm device pointer |
| 3152 | * @data: ioctl data blob |
| 3153 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3154 | * |
| 3155 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3156 | * the timeout parameter. |
| 3157 | * -ETIME: object is still busy after timeout |
| 3158 | * -ERESTARTSYS: signal interrupted the wait |
| 3159 | * -ENONENT: object doesn't exist |
| 3160 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3161 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3162 | * -ENOMEM: damn |
| 3163 | * -ENODEV: Internal IRQ fail |
| 3164 | * -E?: The add request failed |
| 3165 | * |
| 3166 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3167 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3168 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3169 | * without holding struct_mutex the object may become re-busied before this |
| 3170 | * function completes. A similar but shorter * race condition exists in the busy |
| 3171 | * ioctl |
| 3172 | */ |
| 3173 | int |
| 3174 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3175 | { |
| 3176 | struct drm_i915_gem_wait *args = data; |
| 3177 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3178 | ktime_t start; |
| 3179 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3180 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3181 | if (args->flags != 0) |
| 3182 | return -EINVAL; |
| 3183 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3184 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3185 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3186 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3187 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3188 | start = ktime_get(); |
| 3189 | |
| 3190 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 3191 | I915_WAIT_INTERRUPTIBLE | |
| 3192 | I915_WAIT_PRIORITY | |
| 3193 | I915_WAIT_ALL, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3194 | to_wait_timeout(args->timeout_ns), |
| 3195 | to_rps_client(file)); |
| 3196 | |
| 3197 | if (args->timeout_ns > 0) { |
| 3198 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3199 | if (args->timeout_ns < 0) |
| 3200 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3201 | |
| 3202 | /* |
| 3203 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3204 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3205 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3206 | * |
| 3207 | * This is a regression from the timespec->ktime conversion. |
| 3208 | */ |
| 3209 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3210 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3211 | |
| 3212 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3213 | if (ret == -ETIME && args->timeout_ns) |
| 3214 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3215 | } |
| 3216 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3217 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3218 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3221 | static long wait_for_timeline(struct i915_timeline *tl, |
| 3222 | unsigned int flags, long timeout) |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3223 | { |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3224 | struct i915_request *rq; |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3225 | |
| 3226 | rq = i915_gem_active_get_unlocked(&tl->last_request); |
| 3227 | if (!rq) |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3228 | return timeout; |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3229 | |
| 3230 | /* |
| 3231 | * "Race-to-idle". |
| 3232 | * |
| 3233 | * Switching to the kernel context is often used a synchronous |
| 3234 | * step prior to idling, e.g. in suspend for flushing all |
| 3235 | * current operations to memory before sleeping. These we |
| 3236 | * want to complete as quickly as possible to avoid prolonged |
| 3237 | * stalls, so allow the gpu to boost to maximum clocks. |
| 3238 | */ |
| 3239 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
| 3240 | gen6_rps_boost(rq, NULL); |
| 3241 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3242 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3243 | i915_request_put(rq); |
| 3244 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3245 | return timeout; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3246 | } |
| 3247 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3248 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3249 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3250 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3251 | dev_err(i915->drm.dev, |
| 3252 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3253 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3254 | i915_gem_set_wedged(i915); |
| 3255 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | return 0; |
| 3259 | } |
| 3260 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3261 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 3262 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3263 | { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3264 | GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", |
| 3265 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
| 3266 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3267 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3268 | /* If the device is asleep, we have no requests outstanding */ |
| 3269 | if (!READ_ONCE(i915->gt.awake)) |
| 3270 | return 0; |
| 3271 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3272 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3273 | struct i915_timeline *tl; |
| 3274 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3275 | |
| 3276 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3277 | |
| 3278 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3279 | timeout = wait_for_timeline(tl, flags, timeout); |
| 3280 | if (timeout < 0) |
| 3281 | return timeout; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3282 | } |
Chris Wilson | c1e63f6 | 2018-08-08 11:50:59 +0100 | [diff] [blame] | 3283 | if (GEM_SHOW_DEBUG() && !timeout) { |
| 3284 | /* Presume that timeout was non-zero to begin with! */ |
| 3285 | dev_warn(&i915->drm.pdev->dev, |
| 3286 | "Missed idle-completion interrupt!\n"); |
| 3287 | GEM_TRACE_DUMP(); |
| 3288 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3289 | |
| 3290 | err = wait_for_engines(i915); |
| 3291 | if (err) |
| 3292 | return err; |
| 3293 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3294 | i915_retire_requests(i915); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3295 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3296 | } else { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3297 | struct intel_engine_cs *engine; |
| 3298 | enum intel_engine_id id; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3299 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3300 | for_each_engine(engine, i915, id) { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3301 | struct i915_timeline *tl = &engine->timeline; |
| 3302 | |
| 3303 | timeout = wait_for_timeline(tl, flags, timeout); |
| 3304 | if (timeout < 0) |
| 3305 | return timeout; |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3306 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3307 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3308 | |
| 3309 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3310 | } |
| 3311 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3312 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3313 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3314 | /* |
| 3315 | * We manually flush the CPU domain so that we can override and |
| 3316 | * force the flush for the display, and perform it asyncrhonously. |
| 3317 | */ |
| 3318 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3319 | if (obj->cache_dirty) |
| 3320 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3321 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3325 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3326 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3327 | return; |
| 3328 | |
| 3329 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3330 | __i915_gem_object_flush_for_display(obj); |
| 3331 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3332 | } |
| 3333 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3334 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3335 | * Moves a single object to the WC read, and possibly write domain. |
| 3336 | * @obj: object to act on |
| 3337 | * @write: ask for write access or read only |
| 3338 | * |
| 3339 | * This function returns when the move is complete, including waiting on |
| 3340 | * flushes to occur. |
| 3341 | */ |
| 3342 | int |
| 3343 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3344 | { |
| 3345 | int ret; |
| 3346 | |
| 3347 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3348 | |
| 3349 | ret = i915_gem_object_wait(obj, |
| 3350 | I915_WAIT_INTERRUPTIBLE | |
| 3351 | I915_WAIT_LOCKED | |
| 3352 | (write ? I915_WAIT_ALL : 0), |
| 3353 | MAX_SCHEDULE_TIMEOUT, |
| 3354 | NULL); |
| 3355 | if (ret) |
| 3356 | return ret; |
| 3357 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3358 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3359 | return 0; |
| 3360 | |
| 3361 | /* Flush and acquire obj->pages so that we are coherent through |
| 3362 | * direct access in memory with previous cached writes through |
| 3363 | * shmemfs and that our cache domain tracking remains valid. |
| 3364 | * For example, if the obj->filp was moved to swap without us |
| 3365 | * being notified and releasing the pages, we would mistakenly |
| 3366 | * continue to assume that the obj remained out of the CPU cached |
| 3367 | * domain. |
| 3368 | */ |
| 3369 | ret = i915_gem_object_pin_pages(obj); |
| 3370 | if (ret) |
| 3371 | return ret; |
| 3372 | |
| 3373 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3374 | |
| 3375 | /* Serialise direct access to this object with the barriers for |
| 3376 | * coherent writes from the GPU, by effectively invalidating the |
| 3377 | * WC domain upon first access. |
| 3378 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3379 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3380 | mb(); |
| 3381 | |
| 3382 | /* It should now be out of any other write domains, and we can update |
| 3383 | * the domain values for our changes. |
| 3384 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3385 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3386 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3387 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3388 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3389 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3390 | obj->mm.dirty = true; |
| 3391 | } |
| 3392 | |
| 3393 | i915_gem_object_unpin_pages(obj); |
| 3394 | return 0; |
| 3395 | } |
| 3396 | |
| 3397 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3398 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3399 | * @obj: object to act on |
| 3400 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3401 | * |
| 3402 | * This function returns when the move is complete, including waiting on |
| 3403 | * flushes to occur. |
| 3404 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3405 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3406 | 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] | 3407 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3408 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3409 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3410 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3411 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3412 | ret = i915_gem_object_wait(obj, |
| 3413 | I915_WAIT_INTERRUPTIBLE | |
| 3414 | I915_WAIT_LOCKED | |
| 3415 | (write ? I915_WAIT_ALL : 0), |
| 3416 | MAX_SCHEDULE_TIMEOUT, |
| 3417 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3418 | if (ret) |
| 3419 | return ret; |
| 3420 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3421 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3422 | return 0; |
| 3423 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3424 | /* Flush and acquire obj->pages so that we are coherent through |
| 3425 | * direct access in memory with previous cached writes through |
| 3426 | * shmemfs and that our cache domain tracking remains valid. |
| 3427 | * For example, if the obj->filp was moved to swap without us |
| 3428 | * being notified and releasing the pages, we would mistakenly |
| 3429 | * continue to assume that the obj remained out of the CPU cached |
| 3430 | * domain. |
| 3431 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3432 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3433 | if (ret) |
| 3434 | return ret; |
| 3435 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3436 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3437 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3438 | /* Serialise direct access to this object with the barriers for |
| 3439 | * coherent writes from the GPU, by effectively invalidating the |
| 3440 | * GTT domain upon first access. |
| 3441 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3442 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3443 | mb(); |
| 3444 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3445 | /* It should now be out of any other write domains, and we can update |
| 3446 | * the domain values for our changes. |
| 3447 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3448 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3449 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3450 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3451 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3452 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3453 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3454 | } |
| 3455 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3456 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3457 | return 0; |
| 3458 | } |
| 3459 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3460 | /** |
| 3461 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3462 | * @obj: object to act on |
| 3463 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3464 | * |
| 3465 | * After this function returns, the object will be in the new cache-level |
| 3466 | * across all GTT and the contents of the backing storage will be coherent, |
| 3467 | * with respect to the new cache-level. In order to keep the backing storage |
| 3468 | * coherent for all users, we only allow a single cache level to be set |
| 3469 | * globally on the object and prevent it from being changed whilst the |
| 3470 | * hardware is reading from the object. That is if the object is currently |
| 3471 | * on the scanout it will be set to uncached (or equivalent display |
| 3472 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3473 | * that all direct access to the scanout remains coherent. |
| 3474 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3475 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3476 | enum i915_cache_level cache_level) |
| 3477 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3478 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3479 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3480 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3481 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3482 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3483 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3484 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3485 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3486 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3487 | * be invalid given the new cache-level. This is principally to |
| 3488 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3489 | * reading an invalid PTE on older architectures. |
| 3490 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3491 | restart: |
| 3492 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3493 | if (!drm_mm_node_allocated(&vma->node)) |
| 3494 | continue; |
| 3495 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3496 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3497 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3498 | return -EBUSY; |
| 3499 | } |
| 3500 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3501 | if (!i915_vma_is_closed(vma) && |
| 3502 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3503 | continue; |
| 3504 | |
| 3505 | ret = i915_vma_unbind(vma); |
| 3506 | if (ret) |
| 3507 | return ret; |
| 3508 | |
| 3509 | /* As unbinding may affect other elements in the |
| 3510 | * obj->vma_list (due to side-effects from retiring |
| 3511 | * an active vma), play safe and restart the iterator. |
| 3512 | */ |
| 3513 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3514 | } |
| 3515 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3516 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3517 | * cache-level on the PTE. We could simply unbind them all and |
| 3518 | * rebind with the correct cache-level on next use. However since |
| 3519 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3520 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3521 | * state and so involves less work. |
| 3522 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3523 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3524 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3525 | * If we wait upon the object, we know that all the bound |
| 3526 | * VMA are no longer active. |
| 3527 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3528 | ret = i915_gem_object_wait(obj, |
| 3529 | I915_WAIT_INTERRUPTIBLE | |
| 3530 | I915_WAIT_LOCKED | |
| 3531 | I915_WAIT_ALL, |
| 3532 | MAX_SCHEDULE_TIMEOUT, |
| 3533 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3534 | if (ret) |
| 3535 | return ret; |
| 3536 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3537 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3538 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3539 | /* Access to snoopable pages through the GTT is |
| 3540 | * incoherent and on some machines causes a hard |
| 3541 | * lockup. Relinquish the CPU mmaping to force |
| 3542 | * userspace to refault in the pages and we can |
| 3543 | * then double check if the GTT mapping is still |
| 3544 | * valid for that pointer access. |
| 3545 | */ |
| 3546 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3547 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3548 | /* As we no longer need a fence for GTT access, |
| 3549 | * we can relinquish it now (and so prevent having |
| 3550 | * to steal a fence from someone else on the next |
| 3551 | * fence request). Note GPU activity would have |
| 3552 | * dropped the fence as all snoopable access is |
| 3553 | * supposed to be linear. |
| 3554 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3555 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3556 | ret = i915_vma_put_fence(vma); |
| 3557 | if (ret) |
| 3558 | return ret; |
| 3559 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3560 | } else { |
| 3561 | /* We either have incoherent backing store and |
| 3562 | * so no GTT access or the architecture is fully |
| 3563 | * coherent. In such cases, existing GTT mmaps |
| 3564 | * ignore the cache bit in the PTE and we can |
| 3565 | * rewrite it without confusing the GPU or having |
| 3566 | * to force userspace to fault back in its mmaps. |
| 3567 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3568 | } |
| 3569 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3570 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3571 | if (!drm_mm_node_allocated(&vma->node)) |
| 3572 | continue; |
| 3573 | |
| 3574 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3575 | if (ret) |
| 3576 | return ret; |
| 3577 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3578 | } |
| 3579 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3580 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3581 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3582 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3583 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3584 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3585 | return 0; |
| 3586 | } |
| 3587 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3588 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3589 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3590 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3591 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3592 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3593 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3594 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3595 | rcu_read_lock(); |
| 3596 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3597 | if (!obj) { |
| 3598 | err = -ENOENT; |
| 3599 | goto out; |
| 3600 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3601 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3602 | switch (obj->cache_level) { |
| 3603 | case I915_CACHE_LLC: |
| 3604 | case I915_CACHE_L3_LLC: |
| 3605 | args->caching = I915_CACHING_CACHED; |
| 3606 | break; |
| 3607 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3608 | case I915_CACHE_WT: |
| 3609 | args->caching = I915_CACHING_DISPLAY; |
| 3610 | break; |
| 3611 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3612 | default: |
| 3613 | args->caching = I915_CACHING_NONE; |
| 3614 | break; |
| 3615 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3616 | out: |
| 3617 | rcu_read_unlock(); |
| 3618 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3619 | } |
| 3620 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3621 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3622 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3623 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3624 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3625 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3626 | struct drm_i915_gem_object *obj; |
| 3627 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3628 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3629 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3630 | switch (args->caching) { |
| 3631 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3632 | level = I915_CACHE_NONE; |
| 3633 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3634 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3635 | /* |
| 3636 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3637 | * snooped mapping may leave stale data in a corresponding CPU |
| 3638 | * cacheline, whereas normally such cachelines would get |
| 3639 | * invalidated. |
| 3640 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3641 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3642 | return -ENODEV; |
| 3643 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3644 | level = I915_CACHE_LLC; |
| 3645 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3646 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3647 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3648 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3649 | default: |
| 3650 | return -EINVAL; |
| 3651 | } |
| 3652 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3653 | obj = i915_gem_object_lookup(file, args->handle); |
| 3654 | if (!obj) |
| 3655 | return -ENOENT; |
| 3656 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3657 | /* |
| 3658 | * The caching mode of proxy object is handled by its generator, and |
| 3659 | * not allowed to be changed by userspace. |
| 3660 | */ |
| 3661 | if (i915_gem_object_is_proxy(obj)) { |
| 3662 | ret = -ENXIO; |
| 3663 | goto out; |
| 3664 | } |
| 3665 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3666 | if (obj->cache_level == level) |
| 3667 | goto out; |
| 3668 | |
| 3669 | ret = i915_gem_object_wait(obj, |
| 3670 | I915_WAIT_INTERRUPTIBLE, |
| 3671 | MAX_SCHEDULE_TIMEOUT, |
| 3672 | to_rps_client(file)); |
| 3673 | if (ret) |
| 3674 | goto out; |
| 3675 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3676 | ret = i915_mutex_lock_interruptible(dev); |
| 3677 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3678 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3679 | |
| 3680 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3681 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3682 | |
| 3683 | out: |
| 3684 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3685 | return ret; |
| 3686 | } |
| 3687 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3688 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 3689 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 3690 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 3691 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 3692 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3693 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3694 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3695 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3696 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3697 | const struct i915_ggtt_view *view, |
| 3698 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3699 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3700 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3701 | int ret; |
| 3702 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3703 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3704 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3705 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3706 | * display coherency whilst setting up the cache domains. |
| 3707 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3708 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3709 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3710 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3711 | * a result, we make sure that the pinning that is about to occur is |
| 3712 | * done with uncached PTEs. This is lowest common denominator for all |
| 3713 | * chipsets. |
| 3714 | * |
| 3715 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3716 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3717 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3718 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3719 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3720 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3721 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3722 | if (ret) { |
| 3723 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3724 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3725 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3726 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3727 | /* As the user may map the buffer once pinned in the display plane |
| 3728 | * (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] | 3729 | * always use map_and_fenceable for all scanout buffers. However, |
| 3730 | * it may simply be too big to fit into mappable, in which case |
| 3731 | * put it anyway and hope that userspace can cope (but always first |
| 3732 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3733 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3734 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3735 | if ((flags & PIN_MAPPABLE) == 0 && |
| 3736 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3737 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3738 | flags | |
| 3739 | PIN_MAPPABLE | |
| 3740 | PIN_NONBLOCK); |
| 3741 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3742 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3743 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3744 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3745 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 3746 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 3747 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3748 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3749 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3750 | /* It should now be out of any other write domains, and we can update |
| 3751 | * the domain values for our changes. |
| 3752 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3753 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3754 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3755 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3756 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3757 | err_unpin_global: |
| 3758 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3759 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3763 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3764 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 3765 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3766 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3767 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 3768 | return; |
| 3769 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3770 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 3771 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3772 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3773 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 3774 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3775 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3776 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3777 | } |
| 3778 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3779 | /** |
| 3780 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3781 | * @obj: object to act on |
| 3782 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3783 | * |
| 3784 | * This function returns when the move is complete, including waiting on |
| 3785 | * flushes to occur. |
| 3786 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3787 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3788 | 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] | 3789 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3790 | int ret; |
| 3791 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3792 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3793 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3794 | ret = i915_gem_object_wait(obj, |
| 3795 | I915_WAIT_INTERRUPTIBLE | |
| 3796 | I915_WAIT_LOCKED | |
| 3797 | (write ? I915_WAIT_ALL : 0), |
| 3798 | MAX_SCHEDULE_TIMEOUT, |
| 3799 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3800 | if (ret) |
| 3801 | return ret; |
| 3802 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3803 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3804 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3805 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3806 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 3807 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3808 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | /* It should now be out of any other write domains, and we can update |
| 3812 | * the domain values for our changes. |
| 3813 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3814 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3815 | |
| 3816 | /* If we're writing through the CPU, then the GPU read domains will |
| 3817 | * need to be invalidated at next use. |
| 3818 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3819 | if (write) |
| 3820 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3821 | |
| 3822 | return 0; |
| 3823 | } |
| 3824 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3825 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3826 | * emitted over 20 msec ago. |
| 3827 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3828 | * Note that if we were to use the current jiffies each time around the loop, |
| 3829 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3830 | * render a frame was over 20ms. |
| 3831 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3832 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3833 | * relatively low latency when blocking on a particular request to finish. |
| 3834 | */ |
| 3835 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3836 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3837 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3838 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3839 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 3840 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3841 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3842 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3843 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 3844 | /* ABI: return -EIO if already wedged */ |
| 3845 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 3846 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3847 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3848 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3849 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3850 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3851 | break; |
| 3852 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3853 | if (target) { |
| 3854 | list_del(&target->client_link); |
| 3855 | target->file_priv = NULL; |
| 3856 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 3857 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3858 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3859 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3860 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3861 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3862 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3863 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3864 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3865 | return 0; |
| 3866 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3867 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3868 | I915_WAIT_INTERRUPTIBLE, |
| 3869 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3870 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3871 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3872 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3875 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3876 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 3877 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 3878 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 3879 | u64 alignment, |
| 3880 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3881 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3882 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 3883 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3884 | struct i915_vma *vma; |
| 3885 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 3886 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3887 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3888 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 3889 | if (flags & PIN_MAPPABLE && |
| 3890 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3891 | /* If the required space is larger than the available |
| 3892 | * aperture, we will not able to find a slot for the |
| 3893 | * object and unbinding the object now will be in |
| 3894 | * vain. Worse, doing so may cause us to ping-pong |
| 3895 | * the object in and out of the Global GTT and |
| 3896 | * waste a lot of cycles under the mutex. |
| 3897 | */ |
| 3898 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 3899 | return ERR_PTR(-E2BIG); |
| 3900 | |
| 3901 | /* If NONBLOCK is set the caller is optimistically |
| 3902 | * trying to cache the full object within the mappable |
| 3903 | * aperture, and *must* have a fallback in place for |
| 3904 | * situations where we cannot bind the object. We |
| 3905 | * can be a little more lax here and use the fallback |
| 3906 | * more often to avoid costly migrations of ourselves |
| 3907 | * and other objects within the aperture. |
| 3908 | * |
| 3909 | * Half-the-aperture is used as a simple heuristic. |
| 3910 | * More interesting would to do search for a free |
| 3911 | * block prior to making the commitment to unbind. |
| 3912 | * That caters for the self-harm case, and with a |
| 3913 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 3914 | * we could try to minimise harm to others. |
| 3915 | */ |
| 3916 | if (flags & PIN_NONBLOCK && |
| 3917 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 3918 | return ERR_PTR(-ENOSPC); |
| 3919 | } |
| 3920 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 3921 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 3922 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3923 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3924 | |
| 3925 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3926 | if (flags & PIN_NONBLOCK) { |
| 3927 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 3928 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3929 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3930 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 3931 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3932 | return ERR_PTR(-ENOSPC); |
| 3933 | } |
| 3934 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3935 | WARN(i915_vma_is_pinned(vma), |
| 3936 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3937 | " offset=%08x, req.alignment=%llx," |
| 3938 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 3939 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3940 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3941 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3942 | ret = i915_vma_unbind(vma); |
| 3943 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3944 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3945 | } |
| 3946 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3947 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 3948 | if (ret) |
| 3949 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3950 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3951 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3952 | } |
| 3953 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3954 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3955 | { |
| 3956 | /* Note that we could alias engines in the execbuf API, but |
| 3957 | * that would be very unwise as it prevents userspace from |
| 3958 | * fine control over engine selection. Ahem. |
| 3959 | * |
| 3960 | * This should be something like EXEC_MAX_ENGINE instead of |
| 3961 | * I915_NUM_ENGINES. |
| 3962 | */ |
| 3963 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 3964 | return 0x10000 << id; |
| 3965 | } |
| 3966 | |
| 3967 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 3968 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 3969 | /* The uABI guarantees an active writer is also amongst the read |
| 3970 | * engines. This would be true if we accessed the activity tracking |
| 3971 | * under the lock, but as we perform the lookup of the object and |
| 3972 | * its activity locklessly we can not guarantee that the last_write |
| 3973 | * being active implies that we have set the same engine flag from |
| 3974 | * last_read - hence we always set both read and write busy for |
| 3975 | * last_write. |
| 3976 | */ |
| 3977 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3978 | } |
| 3979 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3980 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3981 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3982 | unsigned int (*flag)(unsigned int id)) |
| 3983 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3984 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3985 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3986 | /* We have to check the current hw status of the fence as the uABI |
| 3987 | * guarantees forward progress. We could rely on the idle worker |
| 3988 | * to eventually flush us, but to minimise latency just ask the |
| 3989 | * hardware. |
| 3990 | * |
| 3991 | * Note we only report on the status of native fences. |
| 3992 | */ |
| 3993 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3994 | return 0; |
| 3995 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3996 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3997 | rq = container_of(fence, struct i915_request, fence); |
| 3998 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3999 | return 0; |
| 4000 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4001 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4002 | } |
| 4003 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4004 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4005 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4006 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4007 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4008 | } |
| 4009 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4010 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4011 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4012 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4013 | if (!fence) |
| 4014 | return 0; |
| 4015 | |
| 4016 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4017 | } |
| 4018 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4019 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4020 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4021 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4022 | { |
| 4023 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4024 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4025 | struct reservation_object_list *list; |
| 4026 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4027 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4028 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4029 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4030 | rcu_read_lock(); |
| 4031 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4032 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4033 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4034 | |
| 4035 | /* A discrepancy here is that we do not report the status of |
| 4036 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4037 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4038 | * This also means that wait-ioctl may report an object as busy, |
| 4039 | * where busy-ioctl considers it idle. |
| 4040 | * |
| 4041 | * We trade the ability to warn of foreign fences to report on which |
| 4042 | * i915 engines are active for the object. |
| 4043 | * |
| 4044 | * Alternatively, we can trade that extra information on read/write |
| 4045 | * activity with |
| 4046 | * args->busy = |
| 4047 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4048 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4049 | * |
| 4050 | */ |
| 4051 | retry: |
| 4052 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4053 | |
| 4054 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4055 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4056 | |
| 4057 | /* Translate shared fences to READ set of engines */ |
| 4058 | list = rcu_dereference(obj->resv->fence); |
| 4059 | if (list) { |
| 4060 | unsigned int shared_count = list->shared_count, i; |
| 4061 | |
| 4062 | for (i = 0; i < shared_count; ++i) { |
| 4063 | struct dma_fence *fence = |
| 4064 | rcu_dereference(list->shared[i]); |
| 4065 | |
| 4066 | args->busy |= busy_check_reader(fence); |
| 4067 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4068 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4069 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4070 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4071 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4072 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4073 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4074 | out: |
| 4075 | rcu_read_unlock(); |
| 4076 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | int |
| 4080 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4081 | struct drm_file *file_priv) |
| 4082 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4083 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4086 | int |
| 4087 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4088 | struct drm_file *file_priv) |
| 4089 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4090 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4091 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4092 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4093 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4094 | |
| 4095 | switch (args->madv) { |
| 4096 | case I915_MADV_DONTNEED: |
| 4097 | case I915_MADV_WILLNEED: |
| 4098 | break; |
| 4099 | default: |
| 4100 | return -EINVAL; |
| 4101 | } |
| 4102 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4103 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4104 | if (!obj) |
| 4105 | return -ENOENT; |
| 4106 | |
| 4107 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4108 | if (err) |
| 4109 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4110 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4111 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4112 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4113 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4114 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4115 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4116 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4117 | obj->mm.quirked = false; |
| 4118 | } |
| 4119 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4120 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4121 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4122 | obj->mm.quirked = true; |
| 4123 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4124 | } |
| 4125 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4126 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4127 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4128 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4129 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4130 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4131 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4132 | i915_gem_object_truncate(obj); |
| 4133 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4134 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4135 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4136 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4137 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4138 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4139 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4140 | } |
| 4141 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4142 | static void |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4143 | frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4144 | { |
| 4145 | struct drm_i915_gem_object *obj = |
| 4146 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4147 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4148 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4151 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4152 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4153 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4154 | mutex_init(&obj->mm.lock); |
| 4155 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4156 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4157 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4158 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4159 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4160 | init_rcu_head(&obj->rcu); |
| 4161 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4162 | obj->ops = ops; |
| 4163 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4164 | reservation_object_init(&obj->__builtin_resv); |
| 4165 | obj->resv = &obj->__builtin_resv; |
| 4166 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4167 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4168 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4169 | |
| 4170 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4171 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4172 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4173 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4174 | 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] | 4175 | } |
| 4176 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4177 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4178 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4179 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4180 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4181 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4182 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4183 | |
| 4184 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4185 | }; |
| 4186 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4187 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4188 | struct drm_gem_object *obj, |
| 4189 | size_t size) |
| 4190 | { |
| 4191 | struct drm_i915_private *i915 = to_i915(dev); |
| 4192 | unsigned long flags = VM_NORESERVE; |
| 4193 | struct file *filp; |
| 4194 | |
| 4195 | drm_gem_private_object_init(dev, obj, size); |
| 4196 | |
| 4197 | if (i915->mm.gemfs) |
| 4198 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4199 | flags); |
| 4200 | else |
| 4201 | filp = shmem_file_setup("i915", size, flags); |
| 4202 | |
| 4203 | if (IS_ERR(filp)) |
| 4204 | return PTR_ERR(filp); |
| 4205 | |
| 4206 | obj->filp = filp; |
| 4207 | |
| 4208 | return 0; |
| 4209 | } |
| 4210 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4211 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4212 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4213 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4214 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4215 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4216 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4217 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4218 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4219 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4220 | /* There is a prevalence of the assumption that we fit the object's |
| 4221 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4222 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4223 | * such a local variable, please consider fixing! |
| 4224 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4225 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4226 | return ERR_PTR(-E2BIG); |
| 4227 | |
| 4228 | if (overflows_type(size, obj->base.size)) |
| 4229 | return ERR_PTR(-E2BIG); |
| 4230 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4231 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4232 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4233 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4234 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4235 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4236 | if (ret) |
| 4237 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4238 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4239 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4240 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4241 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4242 | mask &= ~__GFP_HIGHMEM; |
| 4243 | mask |= __GFP_DMA32; |
| 4244 | } |
| 4245 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4246 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4247 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4248 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4249 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4250 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4251 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4252 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4253 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4254 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4255 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4256 | /* 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] | 4257 | * cache) for about a 10% performance improvement |
| 4258 | * compared to uncached. Graphics requests other than |
| 4259 | * display scanout are coherent with the CPU in |
| 4260 | * accessing this cache. This means in this mode we |
| 4261 | * don't need to clflush on the CPU side, and on the |
| 4262 | * GPU side we only need to flush internal caches to |
| 4263 | * get data visible to the CPU. |
| 4264 | * |
| 4265 | * However, we maintain the display planes as UC, and so |
| 4266 | * need to rebind when first used as such. |
| 4267 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4268 | cache_level = I915_CACHE_LLC; |
| 4269 | else |
| 4270 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4271 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4272 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4273 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4274 | trace_i915_gem_object_create(obj); |
| 4275 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4276 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4277 | |
| 4278 | fail: |
| 4279 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4280 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4281 | } |
| 4282 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4283 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4284 | { |
| 4285 | /* If we are the last user of the backing storage (be it shmemfs |
| 4286 | * pages or stolen etc), we know that the pages are going to be |
| 4287 | * immediately released. In this case, we can then skip copying |
| 4288 | * back the contents from the GPU. |
| 4289 | */ |
| 4290 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4291 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4292 | return false; |
| 4293 | |
| 4294 | if (obj->base.filp == NULL) |
| 4295 | return true; |
| 4296 | |
| 4297 | /* At first glance, this looks racy, but then again so would be |
| 4298 | * userspace racing mmap against close. However, the first external |
| 4299 | * reference to the filp can only be obtained through the |
| 4300 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4301 | * acquiring such a reference whilst we are in the middle of |
| 4302 | * freeing the object. |
| 4303 | */ |
| 4304 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4305 | } |
| 4306 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4307 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4308 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4309 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4310 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4311 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4312 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4313 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4314 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4315 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4316 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4317 | trace_i915_gem_object_destroy(obj); |
| 4318 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4319 | mutex_lock(&i915->drm.struct_mutex); |
| 4320 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4321 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
| 4322 | list_for_each_entry_safe(vma, vn, |
| 4323 | &obj->vma_list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4324 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4325 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4326 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4327 | } |
Chris Wilson | db6c2b4 | 2016-11-01 11:54:00 +0000 | [diff] [blame] | 4328 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
| 4329 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4330 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4331 | /* This serializes freeing with the shrinker. Since the free |
| 4332 | * is delayed, first by RCU then by the workqueue, we want the |
| 4333 | * shrinker to be able to free pages of unreferenced objects, |
| 4334 | * or else we may oom whilst there are plenty of deferred |
| 4335 | * freed objects. |
| 4336 | */ |
| 4337 | if (i915_gem_object_has_pages(obj)) { |
| 4338 | spin_lock(&i915->mm.obj_lock); |
| 4339 | list_del_init(&obj->mm.link); |
| 4340 | spin_unlock(&i915->mm.obj_lock); |
| 4341 | } |
| 4342 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4343 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4344 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4345 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4346 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4347 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4348 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4349 | |
| 4350 | if (obj->ops->release) |
| 4351 | obj->ops->release(obj); |
| 4352 | |
| 4353 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4354 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4355 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4356 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4357 | |
| 4358 | if (obj->base.import_attach) |
| 4359 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4360 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4361 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4362 | drm_gem_object_release(&obj->base); |
| 4363 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4364 | |
| 4365 | kfree(obj->bit_17); |
| 4366 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4367 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4368 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4369 | atomic_dec(&i915->mm.free_count); |
| 4370 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4371 | if (on) |
| 4372 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4373 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4374 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4378 | { |
| 4379 | struct llist_node *freed; |
| 4380 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4381 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4382 | freed = NULL; |
| 4383 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4384 | /* Only one consumer of llist_del_first() allowed */ |
| 4385 | spin_lock(&i915->mm.free_lock); |
| 4386 | freed = llist_del_first(&i915->mm.free_list); |
| 4387 | spin_unlock(&i915->mm.free_lock); |
| 4388 | } |
| 4389 | if (unlikely(freed)) { |
| 4390 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4391 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4392 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | static void __i915_gem_free_work(struct work_struct *work) |
| 4396 | { |
| 4397 | struct drm_i915_private *i915 = |
| 4398 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4399 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4400 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4401 | /* |
| 4402 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4403 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4404 | * However, the object may also be bound into the global GTT (e.g. |
| 4405 | * older GPUs without per-process support, or for direct access through |
| 4406 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4407 | * unbound now. |
| 4408 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4409 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4410 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4411 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4412 | spin_unlock(&i915->mm.free_lock); |
| 4413 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4414 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4415 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4416 | return; |
| 4417 | |
| 4418 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4419 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4420 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4424 | { |
| 4425 | struct drm_i915_gem_object *obj = |
| 4426 | container_of(head, typeof(*obj), rcu); |
| 4427 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4428 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4429 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4430 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4431 | * it like a rcu_head from this point forwards. And we expect all |
| 4432 | * objects to be freed via this path. |
| 4433 | */ |
| 4434 | destroy_rcu_head(&obj->rcu); |
| 4435 | |
| 4436 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4437 | * Since we require blocking on struct_mutex to unbind the freed |
| 4438 | * object from the GPU before releasing resources back to the |
| 4439 | * system, we can not do that directly from the RCU callback (which may |
| 4440 | * be a softirq context), but must instead then defer that work onto a |
| 4441 | * kthread. We use the RCU callback rather than move the freed object |
| 4442 | * directly onto the work queue so that we can mix between using the |
| 4443 | * worker and performing frees directly from subsequent allocations for |
| 4444 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4445 | */ |
| 4446 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4447 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4451 | { |
| 4452 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4453 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4454 | if (obj->mm.quirked) |
| 4455 | __i915_gem_object_unpin_pages(obj); |
| 4456 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4457 | if (discard_backing_storage(obj)) |
| 4458 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4459 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4460 | /* |
| 4461 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4462 | * read-side critical sections are complete, e.g. |
| 4463 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4464 | * lookup see i915_gem_object_lookup_rcu(). |
| 4465 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4466 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4467 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4468 | } |
| 4469 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4470 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4471 | { |
| 4472 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4473 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4474 | if (!i915_gem_object_has_active_reference(obj) && |
| 4475 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4476 | i915_gem_object_set_active_reference(obj); |
| 4477 | else |
| 4478 | i915_gem_object_put(obj); |
| 4479 | } |
| 4480 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4481 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4482 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4483 | intel_wakeref_t wakeref; |
| 4484 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4485 | GEM_TRACE("\n"); |
| 4486 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4487 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4488 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 4489 | |
| 4490 | /* |
| 4491 | * As we have just resumed the machine and woken the device up from |
| 4492 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4493 | * back to defaults, recovering from whatever wedged state we left it |
| 4494 | * in and so worth trying to use the device once more. |
| 4495 | */ |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4496 | if (i915_terminally_wedged(&i915->gpu_error)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4497 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4498 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4499 | /* |
| 4500 | * If we inherit context state from the BIOS or earlier occupants |
| 4501 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4502 | * try to take over. The only way to remove the earlier state |
| 4503 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4504 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4505 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4506 | */ |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 4507 | intel_engines_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4508 | |
| 4509 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4510 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4511 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4512 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4513 | i915_gem_contexts_lost(i915); |
| 4514 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4517 | int i915_gem_suspend(struct drm_i915_private *i915) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4518 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4519 | intel_wakeref_t wakeref; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4520 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4521 | |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 4522 | GEM_TRACE("\n"); |
| 4523 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4524 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4525 | intel_suspend_gt_powersave(i915); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4526 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4527 | flush_workqueue(i915->wq); |
| 4528 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4529 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4530 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4531 | /* |
| 4532 | * We have to flush all the executing contexts to main memory so |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4533 | * that they can saved in the hibernation image. To ensure the last |
| 4534 | * context image is coherent, we have to switch away from it. That |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4535 | * leaves the i915->kernel_context still active when |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4536 | * we actually suspend, and its image in memory may not match the GPU |
| 4537 | * state. Fortunately, the kernel_context is disposable and we do |
| 4538 | * not rely on its state. |
| 4539 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4540 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 4541 | ret = i915_gem_switch_to_kernel_context(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4542 | if (ret) |
| 4543 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4544 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4545 | ret = i915_gem_wait_for_idle(i915, |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4546 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 4547 | I915_WAIT_LOCKED | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4548 | I915_WAIT_FOR_IDLE_BOOST, |
| 4549 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4550 | if (ret && ret != -EIO) |
| 4551 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4552 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4553 | assert_kernel_context_is_current(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4554 | } |
Chris Wilson | 01f8f33 | 2018-07-17 09:41:21 +0100 | [diff] [blame] | 4555 | i915_retire_requests(i915); /* ensure we flush after wedging */ |
| 4556 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4557 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4558 | i915_reset_flush(i915); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4559 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4560 | drain_delayed_work(&i915->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4561 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4562 | /* |
| 4563 | * 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] | 4564 | * repeat the flush until it is definitely idle. |
| 4565 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4566 | drain_delayed_work(&i915->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4567 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4568 | intel_uc_suspend(i915); |
| 4569 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4570 | /* |
| 4571 | * Assert that we successfully flushed all the work and |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4572 | * reset the GPU back to its idle, low power state. |
| 4573 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4574 | WARN_ON(i915->gt.awake); |
| 4575 | if (WARN_ON(!intel_engines_are_idle(i915))) |
| 4576 | i915_gem_set_wedged(i915); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4577 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4578 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4579 | return 0; |
| 4580 | |
| 4581 | err_unlock: |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4582 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4583 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4584 | return ret; |
| 4585 | } |
| 4586 | |
| 4587 | void i915_gem_suspend_late(struct drm_i915_private *i915) |
| 4588 | { |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4589 | struct drm_i915_gem_object *obj; |
| 4590 | struct list_head *phases[] = { |
| 4591 | &i915->mm.unbound_list, |
| 4592 | &i915->mm.bound_list, |
| 4593 | NULL |
| 4594 | }, **phase; |
| 4595 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4596 | /* |
| 4597 | * Neither the BIOS, ourselves or any other kernel |
| 4598 | * expects the system to be in execlists mode on startup, |
| 4599 | * so we need to reset the GPU back to legacy mode. And the only |
| 4600 | * known way to disable logical contexts is through a GPU reset. |
| 4601 | * |
| 4602 | * So in order to leave the system in a known default configuration, |
| 4603 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4604 | * clean up the GEM state tracking, flushing off the requests and |
| 4605 | * leaving the system in a known idle state. |
| 4606 | * |
| 4607 | * Note that is of the upmost importance that the GPU is idle and |
| 4608 | * all stray writes are flushed *before* we dismantle the backing |
| 4609 | * storage for the pinned objects. |
| 4610 | * |
| 4611 | * However, since we are uncertain that resetting the GPU on older |
| 4612 | * machines is a good idea, we don't - just in case it leaves the |
| 4613 | * machine in an unusable condition. |
| 4614 | */ |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4615 | |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4616 | mutex_lock(&i915->drm.struct_mutex); |
| 4617 | for (phase = phases; *phase; phase++) { |
| 4618 | list_for_each_entry(obj, *phase, mm.link) |
| 4619 | WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); |
| 4620 | } |
| 4621 | mutex_unlock(&i915->drm.struct_mutex); |
| 4622 | |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4623 | intel_uc_sanitize(i915); |
| 4624 | i915_gem_sanitize(i915); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4625 | } |
| 4626 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4627 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4628 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4629 | GEM_TRACE("\n"); |
| 4630 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4631 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4632 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4633 | mutex_lock(&i915->drm.struct_mutex); |
| 4634 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4635 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4636 | i915_gem_restore_gtt_mappings(i915); |
| 4637 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4638 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4639 | /* |
| 4640 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4641 | * guarantee that the context image is complete. So let's just reset |
| 4642 | * it and start again. |
| 4643 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4644 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4645 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4646 | if (i915_gem_init_hw(i915)) |
| 4647 | goto err_wedged; |
| 4648 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 4649 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 4650 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4651 | /* Always reload a context for powersaving. */ |
| 4652 | if (i915_gem_switch_to_kernel_context(i915)) |
| 4653 | goto err_wedged; |
| 4654 | |
| 4655 | out_unlock: |
| 4656 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 4657 | mutex_unlock(&i915->drm.struct_mutex); |
| 4658 | return; |
| 4659 | |
| 4660 | err_wedged: |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4661 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 4662 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 4663 | i915_gem_set_wedged(i915); |
| 4664 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4665 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4666 | } |
| 4667 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4668 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4669 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4670 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4671 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4672 | return; |
| 4673 | |
| 4674 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4675 | DISP_TILE_SURFACE_SWIZZLING); |
| 4676 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4677 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4678 | return; |
| 4679 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4680 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4681 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4682 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4683 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4684 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4685 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4686 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4687 | else |
| 4688 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4689 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4690 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4691 | 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] | 4692 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4693 | I915_WRITE(RING_CTL(base), 0); |
| 4694 | I915_WRITE(RING_HEAD(base), 0); |
| 4695 | I915_WRITE(RING_TAIL(base), 0); |
| 4696 | I915_WRITE(RING_START(base), 0); |
| 4697 | } |
| 4698 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4699 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4700 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4701 | if (IS_I830(dev_priv)) { |
| 4702 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4703 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4704 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4705 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4706 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4707 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4708 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4709 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4710 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4711 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4712 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4713 | } |
| 4714 | } |
| 4715 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4716 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4717 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4718 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4719 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4720 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4721 | int err; |
| 4722 | |
| 4723 | for_each_engine(engine, i915, id) { |
| 4724 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4725 | if (err) { |
| 4726 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 4727 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4728 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4729 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | return 0; |
| 4733 | } |
| 4734 | |
| 4735 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4736 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4737 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4738 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4739 | dev_priv->gt.last_init_time = ktime_get(); |
| 4740 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4741 | /* Double layer security blanket, see i915_gem_init() */ |
| 4742 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 4743 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4744 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4745 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4746 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4747 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4748 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4749 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4750 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4751 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4752 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4753 | /* ...and determine whether they are sticking. */ |
| 4754 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 4755 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4756 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4757 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4758 | /* |
| 4759 | * At least 830 can leave some of the unused rings |
| 4760 | * "active" (ie. head != tail) after resume which |
| 4761 | * will prevent c3 entry. Makes sure all unused rings |
| 4762 | * are totally idle. |
| 4763 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4764 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4765 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4766 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4767 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 4768 | ret = -EIO; |
| 4769 | goto out; |
| 4770 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4771 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4772 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4773 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4774 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4775 | goto out; |
| 4776 | } |
| 4777 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 4778 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 4779 | if (ret) { |
| 4780 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 4781 | goto out; |
| 4782 | } |
| 4783 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4784 | /* We can't enable contexts until all firmware is loaded */ |
| 4785 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4786 | if (ret) { |
| 4787 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4788 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4789 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4790 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4791 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 4792 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 4793 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 4794 | ret = __i915_gem_restart_engines(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4795 | if (ret) |
| 4796 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4797 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4798 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4799 | |
| 4800 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4801 | |
| 4802 | cleanup_uc: |
| 4803 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4804 | out: |
| 4805 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 4806 | |
| 4807 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4808 | } |
| 4809 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4810 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 4811 | { |
| 4812 | struct i915_gem_context *ctx; |
| 4813 | struct intel_engine_cs *engine; |
| 4814 | enum intel_engine_id id; |
| 4815 | int err; |
| 4816 | |
| 4817 | /* |
| 4818 | * As we reset the gpu during very early sanitisation, the current |
| 4819 | * register state on the GPU should reflect its defaults values. |
| 4820 | * We load a context onto the hw (with restore-inhibit), then switch |
| 4821 | * over to a second context to save that default register state. We |
| 4822 | * can then prime every new context with that state so they all start |
| 4823 | * from the same default HW values. |
| 4824 | */ |
| 4825 | |
| 4826 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 4827 | if (IS_ERR(ctx)) |
| 4828 | return PTR_ERR(ctx); |
| 4829 | |
| 4830 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4831 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4832 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4833 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4834 | if (IS_ERR(rq)) { |
| 4835 | err = PTR_ERR(rq); |
| 4836 | goto out_ctx; |
| 4837 | } |
| 4838 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 4839 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4840 | if (engine->init_context) |
| 4841 | err = engine->init_context(rq); |
| 4842 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 4843 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4844 | if (err) |
| 4845 | goto err_active; |
| 4846 | } |
| 4847 | |
| 4848 | err = i915_gem_switch_to_kernel_context(i915); |
| 4849 | if (err) |
| 4850 | goto err_active; |
| 4851 | |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4852 | if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) { |
| 4853 | i915_gem_set_wedged(i915); |
| 4854 | err = -EIO; /* Caller will declare us wedged */ |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4855 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4856 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4857 | |
| 4858 | assert_kernel_context_is_current(i915); |
| 4859 | |
Chris Wilson | 8e1cb32 | 2018-09-20 17:13:43 +0100 | [diff] [blame] | 4860 | /* |
| 4861 | * Immediately park the GPU so that we enable powersaving and |
| 4862 | * treat it as idle. The next time we issue a request, we will |
| 4863 | * unpark and start using the engine->pinned_default_state, otherwise |
| 4864 | * it is in limbo and an early reset may fail. |
| 4865 | */ |
| 4866 | __i915_gem_park(i915); |
| 4867 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4868 | for_each_engine(engine, i915, id) { |
| 4869 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4870 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4871 | |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4872 | GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count); |
| 4873 | |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 4874 | state = to_intel_context(ctx, engine)->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4875 | if (!state) |
| 4876 | continue; |
| 4877 | |
| 4878 | /* |
| 4879 | * As we will hold a reference to the logical state, it will |
| 4880 | * not be torn down with the context, and importantly the |
| 4881 | * object will hold onto its vma (making it possible for a |
| 4882 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 4883 | * from the GTT to prevent such accidents and reclaim the |
| 4884 | * space. |
| 4885 | */ |
| 4886 | err = i915_vma_unbind(state); |
| 4887 | if (err) |
| 4888 | goto err_active; |
| 4889 | |
| 4890 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 4891 | if (err) |
| 4892 | goto err_active; |
| 4893 | |
| 4894 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4895 | |
| 4896 | /* Check we can acquire the image of the context state */ |
| 4897 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4898 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4899 | if (IS_ERR(vaddr)) { |
| 4900 | err = PTR_ERR(vaddr); |
| 4901 | goto err_active; |
| 4902 | } |
| 4903 | |
| 4904 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 4908 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 4909 | |
| 4910 | /* |
| 4911 | * Make sure that classes with multiple engine instances all |
| 4912 | * share the same basic configuration. |
| 4913 | */ |
| 4914 | for_each_engine(engine, i915, id) { |
| 4915 | unsigned int bit = BIT(engine->uabi_class); |
| 4916 | unsigned int expected = engine->default_state ? bit : 0; |
| 4917 | |
| 4918 | if ((found & bit) != expected) { |
| 4919 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 4920 | engine->uabi_class, engine->name); |
| 4921 | } |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | out_ctx: |
| 4926 | i915_gem_context_set_closed(ctx); |
| 4927 | i915_gem_context_put(ctx); |
| 4928 | return err; |
| 4929 | |
| 4930 | err_active: |
| 4931 | /* |
| 4932 | * If we have to abandon now, we expect the engines to be idle |
| 4933 | * and ready to be torn-down. First try to flush any remaining |
| 4934 | * request, ensure we are pointing at the kernel context and |
| 4935 | * then remove it. |
| 4936 | */ |
| 4937 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 4938 | goto out_ctx; |
| 4939 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4940 | if (WARN_ON(i915_gem_wait_for_idle(i915, |
| 4941 | I915_WAIT_LOCKED, |
| 4942 | MAX_SCHEDULE_TIMEOUT))) |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4943 | goto out_ctx; |
| 4944 | |
| 4945 | i915_gem_contexts_lost(i915); |
| 4946 | goto out_ctx; |
| 4947 | } |
| 4948 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4949 | static int |
| 4950 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 4951 | { |
| 4952 | struct drm_i915_gem_object *obj; |
| 4953 | struct i915_vma *vma; |
| 4954 | int ret; |
| 4955 | |
| 4956 | obj = i915_gem_object_create_stolen(i915, size); |
| 4957 | if (!obj) |
| 4958 | obj = i915_gem_object_create_internal(i915, size); |
| 4959 | if (IS_ERR(obj)) { |
| 4960 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 4961 | return PTR_ERR(obj); |
| 4962 | } |
| 4963 | |
| 4964 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 4965 | if (IS_ERR(vma)) { |
| 4966 | ret = PTR_ERR(vma); |
| 4967 | goto err_unref; |
| 4968 | } |
| 4969 | |
| 4970 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 4971 | if (ret) |
| 4972 | goto err_unref; |
| 4973 | |
| 4974 | i915->gt.scratch = vma; |
| 4975 | return 0; |
| 4976 | |
| 4977 | err_unref: |
| 4978 | i915_gem_object_put(obj); |
| 4979 | return ret; |
| 4980 | } |
| 4981 | |
| 4982 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 4983 | { |
| 4984 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 4985 | } |
| 4986 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4987 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4988 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4989 | int ret; |
| 4990 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 4991 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 4992 | 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] | 4993 | mkwrite_device_info(dev_priv)->page_sizes = |
| 4994 | I915_GTT_PAGE_SIZE_4K; |
| 4995 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 4996 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4997 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4998 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 4999 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5000 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5001 | } else { |
| 5002 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5003 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5004 | } |
| 5005 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5006 | ret = i915_gem_init_userptr(dev_priv); |
| 5007 | if (ret) |
| 5008 | return ret; |
| 5009 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5010 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 5011 | if (ret) |
| 5012 | return ret; |
| 5013 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5014 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 5015 | if (ret) |
| 5016 | goto err_uc_misc; |
| 5017 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5018 | /* This is just a security blanket to placate dragons. |
| 5019 | * On some systems, we very sporadically observe that the first TLBs |
| 5020 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5021 | * we hold the forcewake during initialisation these problems |
| 5022 | * just magically go away. |
| 5023 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5024 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5025 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5026 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5027 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5028 | if (ret) { |
| 5029 | GEM_BUG_ON(ret == -EIO); |
| 5030 | goto err_unlock; |
| 5031 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5032 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5033 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5034 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5035 | if (ret) { |
| 5036 | GEM_BUG_ON(ret == -EIO); |
| 5037 | goto err_ggtt; |
| 5038 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5039 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5040 | ret = i915_gem_contexts_init(dev_priv); |
| 5041 | if (ret) { |
| 5042 | GEM_BUG_ON(ret == -EIO); |
| 5043 | goto err_scratch; |
| 5044 | } |
| 5045 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5046 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5047 | if (ret) { |
| 5048 | GEM_BUG_ON(ret == -EIO); |
| 5049 | goto err_context; |
| 5050 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5051 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5052 | intel_init_gt_powersave(dev_priv); |
| 5053 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5054 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5055 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5056 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5057 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5058 | ret = i915_gem_init_hw(dev_priv); |
| 5059 | if (ret) |
| 5060 | goto err_uc_init; |
| 5061 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5062 | /* |
| 5063 | * Despite its name intel_init_clock_gating applies both display |
| 5064 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5065 | * GT power context workaround. Worse, sometimes it includes a context |
| 5066 | * register workaround which we need to apply before we record the |
| 5067 | * default HW state for all contexts. |
| 5068 | * |
| 5069 | * FIXME: break up the workarounds and apply them at the right time! |
| 5070 | */ |
| 5071 | intel_init_clock_gating(dev_priv); |
| 5072 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5073 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5074 | if (ret) |
| 5075 | goto err_init_hw; |
| 5076 | |
| 5077 | if (i915_inject_load_failure()) { |
| 5078 | ret = -ENODEV; |
| 5079 | goto err_init_hw; |
| 5080 | } |
| 5081 | |
| 5082 | if (i915_inject_load_failure()) { |
| 5083 | ret = -EIO; |
| 5084 | goto err_init_hw; |
| 5085 | } |
| 5086 | |
| 5087 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5088 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5089 | |
| 5090 | return 0; |
| 5091 | |
| 5092 | /* |
| 5093 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5094 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5095 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5096 | * driver doesn't explode during runtime. |
| 5097 | */ |
| 5098 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5099 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5100 | |
| 5101 | WARN_ON(i915_gem_suspend(dev_priv)); |
| 5102 | i915_gem_suspend_late(dev_priv); |
| 5103 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 5104 | i915_gem_drain_workqueue(dev_priv); |
| 5105 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5106 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5107 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5108 | err_uc_init: |
| 5109 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5110 | err_pm: |
| 5111 | if (ret != -EIO) { |
| 5112 | intel_cleanup_gt_powersave(dev_priv); |
| 5113 | i915_gem_cleanup_engines(dev_priv); |
| 5114 | } |
| 5115 | err_context: |
| 5116 | if (ret != -EIO) |
| 5117 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5118 | err_scratch: |
| 5119 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5120 | err_ggtt: |
| 5121 | err_unlock: |
| 5122 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5123 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5124 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5125 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5126 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5127 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5128 | if (ret != -EIO) |
| 5129 | i915_gem_cleanup_userptr(dev_priv); |
| 5130 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5131 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5132 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5133 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5134 | /* |
| 5135 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5136 | * wedged. But we only want to do this where the GPU is angry, |
| 5137 | * for all other failure, such as an allocation failure, bail. |
| 5138 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5139 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 5140 | i915_load_error(dev_priv, |
| 5141 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5142 | i915_gem_set_wedged(dev_priv); |
| 5143 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5144 | |
| 5145 | /* Minimal basic recovery for KMS */ |
| 5146 | ret = i915_ggtt_enable_hw(dev_priv); |
| 5147 | i915_gem_restore_gtt_mappings(dev_priv); |
| 5148 | i915_gem_restore_fences(dev_priv); |
| 5149 | intel_init_clock_gating(dev_priv); |
| 5150 | |
| 5151 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5152 | } |
| 5153 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5154 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5155 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5156 | } |
| 5157 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5158 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 5159 | { |
| 5160 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5161 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5162 | |
| 5163 | /* Flush any outstanding unpin_work. */ |
| 5164 | i915_gem_drain_workqueue(dev_priv); |
| 5165 | |
| 5166 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5167 | intel_uc_fini_hw(dev_priv); |
| 5168 | intel_uc_fini(dev_priv); |
| 5169 | i915_gem_cleanup_engines(dev_priv); |
| 5170 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5171 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5172 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5173 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5174 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 5175 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5176 | intel_cleanup_gt_powersave(dev_priv); |
| 5177 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5178 | intel_uc_fini_misc(dev_priv); |
| 5179 | i915_gem_cleanup_userptr(dev_priv); |
| 5180 | |
| 5181 | i915_gem_drain_freed_objects(dev_priv); |
| 5182 | |
| 5183 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 5184 | } |
| 5185 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5186 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5187 | { |
| 5188 | i915_gem_sanitize(i915); |
| 5189 | } |
| 5190 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5191 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5192 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5193 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5194 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5195 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5196 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5197 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5198 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5199 | } |
| 5200 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5201 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5202 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5203 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5204 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5205 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5206 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5207 | !IS_CHERRYVIEW(dev_priv)) |
| 5208 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5209 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5210 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5211 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5212 | dev_priv->num_fence_regs = 16; |
| 5213 | else |
| 5214 | dev_priv->num_fence_regs = 8; |
| 5215 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5216 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5217 | dev_priv->num_fence_regs = |
| 5218 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5219 | |
| 5220 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5221 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5222 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5223 | |
| 5224 | fence->i915 = dev_priv; |
| 5225 | fence->id = i; |
| 5226 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5227 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5228 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5229 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5230 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5231 | } |
| 5232 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5233 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5234 | { |
| 5235 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5236 | spin_lock_init(&i915->mm.obj_lock); |
| 5237 | spin_lock_init(&i915->mm.free_lock); |
| 5238 | |
| 5239 | init_llist_head(&i915->mm.free_list); |
| 5240 | |
| 5241 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5242 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5243 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5244 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5245 | |
| 5246 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5247 | } |
| 5248 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5249 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5250 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5251 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5252 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5253 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5254 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5255 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5256 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5257 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5258 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5259 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5260 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5261 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5262 | if (!dev_priv->luts) |
| 5263 | goto err_vmas; |
| 5264 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5265 | dev_priv->requests = KMEM_CACHE(i915_request, |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5266 | SLAB_HWCACHE_ALIGN | |
| 5267 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5268 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5269 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5270 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5271 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5272 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5273 | SLAB_HWCACHE_ALIGN | |
| 5274 | SLAB_RECLAIM_ACCOUNT); |
| 5275 | if (!dev_priv->dependencies) |
| 5276 | goto err_requests; |
| 5277 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5278 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5279 | if (!dev_priv->priorities) |
| 5280 | goto err_dependencies; |
| 5281 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5282 | INIT_LIST_HEAD(&dev_priv->gt.timelines); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5283 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 5284 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5285 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5286 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5287 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5288 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5289 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5290 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5291 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5292 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5293 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 5294 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5295 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5296 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5297 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5298 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5299 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5300 | err = i915_gemfs_init(dev_priv); |
| 5301 | if (err) |
| 5302 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5303 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5304 | return 0; |
| 5305 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5306 | err_dependencies: |
| 5307 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5308 | err_requests: |
| 5309 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5310 | err_luts: |
| 5311 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5312 | err_vmas: |
| 5313 | kmem_cache_destroy(dev_priv->vmas); |
| 5314 | err_objects: |
| 5315 | kmem_cache_destroy(dev_priv->objects); |
| 5316 | err_out: |
| 5317 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5318 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5319 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5320 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5321 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5322 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5323 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5324 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5325 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5326 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5327 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5328 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5329 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5330 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5331 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5332 | kmem_cache_destroy(dev_priv->vmas); |
| 5333 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5334 | |
| 5335 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5336 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5337 | |
| 5338 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5339 | } |
| 5340 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5341 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5342 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5343 | /* Discard all purgeable objects, let userspace recover those as |
| 5344 | * required after resuming. |
| 5345 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5346 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5347 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5348 | return 0; |
| 5349 | } |
| 5350 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5351 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5352 | { |
| 5353 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5354 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5355 | &i915->mm.unbound_list, |
| 5356 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5357 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5358 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5359 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5360 | /* |
| 5361 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5362 | * |
| 5363 | * We need to update the domain tracking to reflect that the CPU |
| 5364 | * will be accessing all the pages to create and restore from the |
| 5365 | * hibernation, and so upon restoration those pages will be in the |
| 5366 | * CPU domain. |
| 5367 | * |
| 5368 | * To make sure the hibernation image contains the latest state, |
| 5369 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5370 | * |
| 5371 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5372 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5373 | */ |
| 5374 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5375 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 5376 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5377 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5378 | mutex_lock(&i915->drm.struct_mutex); |
| 5379 | for (phase = phases; *phase; phase++) { |
| 5380 | list_for_each_entry(obj, *phase, mm.link) |
| 5381 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5382 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5383 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5384 | |
| 5385 | return 0; |
| 5386 | } |
| 5387 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5388 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5389 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5390 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5391 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5392 | |
| 5393 | /* Clean up our request list when the client is going away, so that |
| 5394 | * later retire_requests won't dereference our soon-to-be-gone |
| 5395 | * file_priv. |
| 5396 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5397 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5398 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5399 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5400 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5401 | } |
| 5402 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5403 | 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] | 5404 | { |
| 5405 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5406 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5407 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5408 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5409 | |
| 5410 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5411 | if (!file_priv) |
| 5412 | return -ENOMEM; |
| 5413 | |
| 5414 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5415 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5416 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5417 | |
| 5418 | spin_lock_init(&file_priv->mm.lock); |
| 5419 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5420 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5421 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 5422 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5423 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5424 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5425 | if (ret) |
| 5426 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5427 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5428 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5429 | } |
| 5430 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5431 | /** |
| 5432 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5433 | * @old: current GEM buffer for the frontbuffer slots |
| 5434 | * @new: new GEM buffer for the frontbuffer slots |
| 5435 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5436 | * |
| 5437 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5438 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5439 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5440 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5441 | struct drm_i915_gem_object *new, |
| 5442 | unsigned frontbuffer_bits) |
| 5443 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5444 | /* Control of individual bits within the mask are guarded by |
| 5445 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5446 | * manipulation of individual bits. But since the bitfield as a whole |
| 5447 | * is updated using RMW, we need to use atomics in order to update |
| 5448 | * the bits. |
| 5449 | */ |
| 5450 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 5451 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5452 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5453 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5454 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5455 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5459 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5460 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5461 | } |
| 5462 | } |
| 5463 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5464 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5465 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5466 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5467 | const void *data, size_t size) |
| 5468 | { |
| 5469 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5470 | struct file *file; |
| 5471 | size_t offset; |
| 5472 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5473 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5474 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5475 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5476 | return obj; |
| 5477 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5478 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5479 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5480 | file = obj->base.filp; |
| 5481 | offset = 0; |
| 5482 | do { |
| 5483 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5484 | struct page *page; |
| 5485 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5486 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5487 | err = pagecache_write_begin(file, file->f_mapping, |
| 5488 | offset, len, 0, |
| 5489 | &page, &pgdata); |
| 5490 | if (err < 0) |
| 5491 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5492 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5493 | vaddr = kmap(page); |
| 5494 | memcpy(vaddr, data, len); |
| 5495 | kunmap(page); |
| 5496 | |
| 5497 | err = pagecache_write_end(file, file->f_mapping, |
| 5498 | offset, len, len, |
| 5499 | page, pgdata); |
| 5500 | if (err < 0) |
| 5501 | goto fail; |
| 5502 | |
| 5503 | size -= len; |
| 5504 | data += len; |
| 5505 | offset += len; |
| 5506 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5507 | |
| 5508 | return obj; |
| 5509 | |
| 5510 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5511 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5512 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5513 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5514 | |
| 5515 | struct scatterlist * |
| 5516 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5517 | unsigned int n, |
| 5518 | unsigned int *offset) |
| 5519 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5520 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5521 | struct scatterlist *sg; |
| 5522 | unsigned int idx, count; |
| 5523 | |
| 5524 | might_sleep(); |
| 5525 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5526 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5527 | |
| 5528 | /* As we iterate forward through the sg, we record each entry in a |
| 5529 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5530 | * this index previously, we will have an entry for it. |
| 5531 | * |
| 5532 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5533 | * sequential page access (where each new request is consecutive |
| 5534 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5535 | * i.e. O(1) with a large constant! |
| 5536 | */ |
| 5537 | if (n < READ_ONCE(iter->sg_idx)) |
| 5538 | goto lookup; |
| 5539 | |
| 5540 | mutex_lock(&iter->lock); |
| 5541 | |
| 5542 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5543 | * (or the subsequent) sg are fast - comparing against the last |
| 5544 | * sg is faster than going through the radixtree. |
| 5545 | */ |
| 5546 | |
| 5547 | sg = iter->sg_pos; |
| 5548 | idx = iter->sg_idx; |
| 5549 | count = __sg_page_count(sg); |
| 5550 | |
| 5551 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5552 | void *entry; |
| 5553 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5554 | int ret; |
| 5555 | |
| 5556 | /* If we cannot allocate and insert this entry, or the |
| 5557 | * individual pages from this range, cancel updating the |
| 5558 | * sg_idx so that on this lookup we are forced to linearly |
| 5559 | * scan onwards, but on future lookups we will try the |
| 5560 | * insertion again (in which case we need to be careful of |
| 5561 | * the error return reporting that we have already inserted |
| 5562 | * this index). |
| 5563 | */ |
| 5564 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5565 | if (ret && ret != -EEXIST) |
| 5566 | goto scan; |
| 5567 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5568 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5569 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5570 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5571 | if (ret && ret != -EEXIST) |
| 5572 | goto scan; |
| 5573 | } |
| 5574 | |
| 5575 | idx += count; |
| 5576 | sg = ____sg_next(sg); |
| 5577 | count = __sg_page_count(sg); |
| 5578 | } |
| 5579 | |
| 5580 | scan: |
| 5581 | iter->sg_pos = sg; |
| 5582 | iter->sg_idx = idx; |
| 5583 | |
| 5584 | mutex_unlock(&iter->lock); |
| 5585 | |
| 5586 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5587 | goto lookup; |
| 5588 | |
| 5589 | /* In case we failed to insert the entry into the radixtree, we need |
| 5590 | * to look beyond the current sg. |
| 5591 | */ |
| 5592 | while (idx + count <= n) { |
| 5593 | idx += count; |
| 5594 | sg = ____sg_next(sg); |
| 5595 | count = __sg_page_count(sg); |
| 5596 | } |
| 5597 | |
| 5598 | *offset = n - idx; |
| 5599 | return sg; |
| 5600 | |
| 5601 | lookup: |
| 5602 | rcu_read_lock(); |
| 5603 | |
| 5604 | sg = radix_tree_lookup(&iter->radix, n); |
| 5605 | GEM_BUG_ON(!sg); |
| 5606 | |
| 5607 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5608 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5609 | * to the start of that range. We will return the pointer to |
| 5610 | * the base page and the offset of this page within the |
| 5611 | * sg entry's range. |
| 5612 | */ |
| 5613 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5614 | if (unlikely(xa_is_value(sg))) { |
| 5615 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5616 | |
| 5617 | sg = radix_tree_lookup(&iter->radix, base); |
| 5618 | GEM_BUG_ON(!sg); |
| 5619 | |
| 5620 | *offset = n - base; |
| 5621 | } |
| 5622 | |
| 5623 | rcu_read_unlock(); |
| 5624 | |
| 5625 | return sg; |
| 5626 | } |
| 5627 | |
| 5628 | struct page * |
| 5629 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5630 | { |
| 5631 | struct scatterlist *sg; |
| 5632 | unsigned int offset; |
| 5633 | |
| 5634 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5635 | |
| 5636 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5637 | return nth_page(sg_page(sg), offset); |
| 5638 | } |
| 5639 | |
| 5640 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5641 | struct page * |
| 5642 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5643 | unsigned int n) |
| 5644 | { |
| 5645 | struct page *page; |
| 5646 | |
| 5647 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5648 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5649 | set_page_dirty(page); |
| 5650 | |
| 5651 | return page; |
| 5652 | } |
| 5653 | |
| 5654 | dma_addr_t |
| 5655 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5656 | unsigned long n) |
| 5657 | { |
| 5658 | struct scatterlist *sg; |
| 5659 | unsigned int offset; |
| 5660 | |
| 5661 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5662 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5663 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5664 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5665 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5666 | { |
| 5667 | struct sg_table *pages; |
| 5668 | int err; |
| 5669 | |
| 5670 | if (align > obj->base.size) |
| 5671 | return -EINVAL; |
| 5672 | |
| 5673 | if (obj->ops == &i915_gem_phys_ops) |
| 5674 | return 0; |
| 5675 | |
| 5676 | if (obj->ops != &i915_gem_object_ops) |
| 5677 | return -EINVAL; |
| 5678 | |
| 5679 | err = i915_gem_object_unbind(obj); |
| 5680 | if (err) |
| 5681 | return err; |
| 5682 | |
| 5683 | mutex_lock(&obj->mm.lock); |
| 5684 | |
| 5685 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5686 | err = -EFAULT; |
| 5687 | goto err_unlock; |
| 5688 | } |
| 5689 | |
| 5690 | if (obj->mm.quirked) { |
| 5691 | err = -EFAULT; |
| 5692 | goto err_unlock; |
| 5693 | } |
| 5694 | |
| 5695 | if (obj->mm.mapping) { |
| 5696 | err = -EBUSY; |
| 5697 | goto err_unlock; |
| 5698 | } |
| 5699 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5700 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5701 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5702 | obj->ops = &i915_gem_phys_ops; |
| 5703 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5704 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5705 | if (err) |
| 5706 | goto err_xfer; |
| 5707 | |
| 5708 | /* Perma-pin (until release) the physical set of pages */ |
| 5709 | __i915_gem_object_pin_pages(obj); |
| 5710 | |
| 5711 | if (!IS_ERR_OR_NULL(pages)) |
| 5712 | i915_gem_object_ops.put_pages(obj, pages); |
| 5713 | mutex_unlock(&obj->mm.lock); |
| 5714 | return 0; |
| 5715 | |
| 5716 | err_xfer: |
| 5717 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5718 | if (!IS_ERR_OR_NULL(pages)) { |
| 5719 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 5720 | |
| 5721 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 5722 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5723 | err_unlock: |
| 5724 | mutex_unlock(&obj->mm.lock); |
| 5725 | return err; |
| 5726 | } |
| 5727 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5728 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5729 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5730 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5731 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5732 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5733 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5734 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 5735 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5736 | #endif |