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 Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 28 | #include <drm/drmP.h> |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 29 | #include <drm/drm_vma_manager.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/i915_drm.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 31 | #include "i915_drv.h" |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 32 | #include "i915_gem_clflush.h" |
Yu Zhang | eb82289 | 2015-02-10 19:05:49 +0800 | [diff] [blame] | 33 | #include "i915_vgpu.h" |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 34 | #include "i915_trace.h" |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 35 | #include "intel_drv.h" |
Chris Wilson | 5d723d7 | 2016-08-04 16:32:35 +0100 | [diff] [blame] | 36 | #include "intel_frontbuffer.h" |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 37 | #include "intel_mocs.h" |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 38 | #include "intel_workarounds.h" |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 39 | #include "i915_gemfs.h" |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 40 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 41 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 42 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 43 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 44 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 45 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 46 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 47 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 48 | #include <linux/dma-buf.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 49 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 50 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 51 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 52 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 53 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 54 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 55 | return false; |
| 56 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 57 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 58 | return true; |
| 59 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 60 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 63 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 64 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 65 | struct drm_mm_node *node, u32 size) |
| 66 | { |
| 67 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 68 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 69 | size, 0, I915_COLOR_UNEVICTABLE, |
| 70 | 0, ggtt->mappable_end, |
| 71 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void |
| 75 | remove_mappable_node(struct drm_mm_node *node) |
| 76 | { |
| 77 | drm_mm_remove_node(node); |
| 78 | } |
| 79 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 80 | /* some bookkeeping */ |
| 81 | 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] | 82 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 83 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 84 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 85 | dev_priv->mm.object_count++; |
| 86 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 87 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | 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] | 91 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 92 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 93 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 94 | dev_priv->mm.object_count--; |
| 95 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 96 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Chris Wilson | 21dd373 | 2011-01-26 15:55:56 +0000 | [diff] [blame] | 99 | static int |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 100 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 101 | { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 102 | int ret; |
| 103 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 104 | might_sleep(); |
| 105 | |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging |
| 108 | * userspace. If it takes that long something really bad is going on and |
| 109 | * we should simply try to bail out and fail as gracefully as possible. |
| 110 | */ |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 111 | ret = wait_event_interruptible_timeout(error->reset_queue, |
Chris Wilson | 8c185ec | 2017-03-16 17:13:02 +0000 | [diff] [blame] | 112 | !i915_reset_backoff(error), |
Chris Wilson | b52992c | 2016-10-28 13:58:24 +0100 | [diff] [blame] | 113 | I915_RESET_TIMEOUT); |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 114 | if (ret == 0) { |
| 115 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); |
| 116 | return -EIO; |
| 117 | } else if (ret < 0) { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 118 | return ret; |
Chris Wilson | d98c52c | 2016-04-13 17:35:05 +0100 | [diff] [blame] | 119 | } else { |
| 120 | return 0; |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 121 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 124 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 125 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 126 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 127 | int ret; |
| 128 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 129 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 137 | return 0; |
| 138 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 139 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 140 | static u32 __i915_gem_park(struct drm_i915_private *i915) |
| 141 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 142 | GEM_TRACE("\n"); |
| 143 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 144 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 145 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 146 | GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 147 | |
| 148 | if (!i915->gt.awake) |
| 149 | return I915_EPOCH_INVALID; |
| 150 | |
| 151 | GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); |
| 152 | |
| 153 | /* |
| 154 | * Be paranoid and flush a concurrent interrupt to make sure |
| 155 | * we don't reactivate any irq tasklets after parking. |
| 156 | * |
| 157 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 158 | * there may still be an in-flight interrupt even though the CSB |
| 159 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 160 | * is completed before we continue, but it doesn't prevent the HW from |
| 161 | * raising a spurious interrupt later. To complete the shield we should |
| 162 | * coordinate disabling the CS irq with flushing the interrupts. |
| 163 | */ |
| 164 | synchronize_irq(i915->drm.irq); |
| 165 | |
| 166 | intel_engines_park(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 167 | i915_timelines_park(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 168 | |
| 169 | i915_pmu_gt_parked(i915); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 170 | i915_vma_parked(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 171 | |
| 172 | i915->gt.awake = false; |
| 173 | |
| 174 | if (INTEL_GEN(i915) >= 6) |
| 175 | gen6_rps_idle(i915); |
| 176 | |
| 177 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ); |
| 178 | |
| 179 | intel_runtime_pm_put(i915); |
| 180 | |
| 181 | return i915->gt.epoch; |
| 182 | } |
| 183 | |
| 184 | void i915_gem_park(struct drm_i915_private *i915) |
| 185 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 186 | GEM_TRACE("\n"); |
| 187 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 188 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 189 | GEM_BUG_ON(i915->gt.active_requests); |
| 190 | |
| 191 | if (!i915->gt.awake) |
| 192 | return; |
| 193 | |
| 194 | /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ |
| 195 | mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); |
| 196 | } |
| 197 | |
| 198 | void i915_gem_unpark(struct drm_i915_private *i915) |
| 199 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 200 | GEM_TRACE("\n"); |
| 201 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 202 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 203 | GEM_BUG_ON(!i915->gt.active_requests); |
| 204 | |
| 205 | if (i915->gt.awake) |
| 206 | return; |
| 207 | |
| 208 | intel_runtime_pm_get_noresume(i915); |
| 209 | |
| 210 | /* |
| 211 | * It seems that the DMC likes to transition between the DC states a lot |
| 212 | * when there are no connected displays (no active power domains) during |
| 213 | * command submission. |
| 214 | * |
| 215 | * This activity has negative impact on the performance of the chip with |
| 216 | * huge latencies observed in the interrupt handler and elsewhere. |
| 217 | * |
| 218 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 219 | * GT activity, preventing any DC state transitions. |
| 220 | */ |
| 221 | intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 222 | |
| 223 | i915->gt.awake = true; |
| 224 | if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ |
| 225 | i915->gt.epoch = 1; |
| 226 | |
| 227 | intel_enable_gt_powersave(i915); |
| 228 | i915_update_gfx_val(i915); |
| 229 | if (INTEL_GEN(i915) >= 6) |
| 230 | gen6_rps_busy(i915); |
| 231 | i915_pmu_gt_unparked(i915); |
| 232 | |
| 233 | intel_engines_unpark(i915); |
| 234 | |
| 235 | i915_queue_hangcheck(i915); |
| 236 | |
| 237 | queue_delayed_work(i915->wq, |
| 238 | &i915->gt.retire_work, |
| 239 | round_jiffies_up_relative(HZ)); |
| 240 | } |
| 241 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 242 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 243 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 244 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 245 | { |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 246 | struct drm_i915_private *dev_priv = to_i915(dev); |
Joonas Lahtinen | 62106b4 | 2016-03-18 10:42:57 +0200 | [diff] [blame] | 247 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 248 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 249 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 250 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 251 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 252 | pinned = ggtt->vm.reserved; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 253 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 254 | list_for_each_entry(vma, &ggtt->vm.active_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 255 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 256 | pinned += vma->node.size; |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 257 | list_for_each_entry(vma, &ggtt->vm.inactive_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 258 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 259 | pinned += vma->node.size; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 260 | mutex_unlock(&dev->struct_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(); |
| 658 | #if IS_ENABLED(CONFIG_LOCKDEP) |
| 659 | GEM_BUG_ON(debug_locks && |
| 660 | !!lockdep_is_held(&obj->base.dev->struct_mutex) != |
| 661 | !!(flags & I915_WAIT_LOCKED)); |
| 662 | #endif |
| 663 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 664 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 665 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 666 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 667 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 668 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 672 | { |
| 673 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 674 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 675 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 678 | static int |
| 679 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 680 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 681 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 682 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 683 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 684 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 685 | |
| 686 | /* We manually control the domain here and pretend that it |
| 687 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 688 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 689 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 690 | if (copy_from_user(vaddr, user_data, args->size)) |
| 691 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 692 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 693 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 694 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 695 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 696 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 697 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 698 | } |
| 699 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 700 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 701 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 702 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 706 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 707 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 708 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 711 | static int |
| 712 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 713 | struct drm_i915_private *dev_priv, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 714 | uint64_t size, |
| 715 | uint32_t *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 716 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 717 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 718 | int ret; |
| 719 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 720 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 721 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 722 | if (size == 0) |
| 723 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 724 | |
| 725 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 726 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 727 | if (IS_ERR(obj)) |
| 728 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 729 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 730 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 731 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 732 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 733 | if (ret) |
| 734 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 735 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 736 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 740 | int |
| 741 | i915_gem_dumb_create(struct drm_file *file, |
| 742 | struct drm_device *dev, |
| 743 | struct drm_mode_create_dumb *args) |
| 744 | { |
| 745 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 746 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 747 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 748 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 749 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 750 | } |
| 751 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 752 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 753 | { |
| 754 | return !(obj->cache_level == I915_CACHE_NONE || |
| 755 | obj->cache_level == I915_CACHE_WT); |
| 756 | } |
| 757 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 758 | /** |
| 759 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 760 | * @dev: drm device pointer |
| 761 | * @data: ioctl data blob |
| 762 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 763 | */ |
| 764 | int |
| 765 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 766 | struct drm_file *file) |
| 767 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 768 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 769 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 770 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 771 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 772 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 773 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 774 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 775 | } |
| 776 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 777 | static inline enum fb_op_origin |
| 778 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 779 | { |
| 780 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 781 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 782 | } |
| 783 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 784 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 785 | { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 786 | /* |
| 787 | * No actual flushing is required for the GTT write domain for reads |
| 788 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 789 | * as far as we know, so there's no chipset flush. It also doesn't |
| 790 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 791 | * |
| 792 | * However, we do have to enforce the order so that all writes through |
| 793 | * the GTT land before any writes to the device, such as updates to |
| 794 | * the GATT itself. |
| 795 | * |
| 796 | * We also have to wait a bit for the writes to land from the GTT. |
| 797 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 798 | * timing. This issue has only been observed when switching quickly |
| 799 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 800 | * 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] | 801 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 802 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 803 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 804 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 805 | wmb(); |
| 806 | |
| 807 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 808 | return; |
| 809 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 810 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 811 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 812 | intel_runtime_pm_get(dev_priv); |
| 813 | spin_lock_irq(&dev_priv->uncore.lock); |
| 814 | |
| 815 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
| 816 | |
| 817 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 818 | intel_runtime_pm_put(dev_priv); |
| 819 | } |
| 820 | |
| 821 | static void |
| 822 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 823 | { |
| 824 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 825 | struct i915_vma *vma; |
| 826 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 827 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 828 | return; |
| 829 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 830 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 831 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 832 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 833 | |
| 834 | intel_fb_obj_flush(obj, |
| 835 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 836 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 837 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 838 | if (vma->iomap) |
| 839 | continue; |
| 840 | |
| 841 | i915_vma_unset_ggtt_write(vma); |
| 842 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 843 | break; |
| 844 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 845 | case I915_GEM_DOMAIN_WC: |
| 846 | wmb(); |
| 847 | break; |
| 848 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 849 | case I915_GEM_DOMAIN_CPU: |
| 850 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 851 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 852 | |
| 853 | case I915_GEM_DOMAIN_RENDER: |
| 854 | if (gpu_write_needs_clflush(obj)) |
| 855 | obj->cache_dirty = true; |
| 856 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 859 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 862 | /* |
| 863 | * Pins the specified object's pages and synchronizes the object with |
| 864 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 865 | * flush the object from the CPU cache. |
| 866 | */ |
| 867 | 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] | 868 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 869 | { |
| 870 | int ret; |
| 871 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 872 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 873 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 874 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 875 | if (!i915_gem_object_has_struct_page(obj)) |
| 876 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 877 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 878 | ret = i915_gem_object_wait(obj, |
| 879 | I915_WAIT_INTERRUPTIBLE | |
| 880 | I915_WAIT_LOCKED, |
| 881 | MAX_SCHEDULE_TIMEOUT, |
| 882 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 883 | if (ret) |
| 884 | return ret; |
| 885 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 886 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 887 | if (ret) |
| 888 | return ret; |
| 889 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 890 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 891 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 892 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 893 | if (ret) |
| 894 | goto err_unpin; |
| 895 | else |
| 896 | goto out; |
| 897 | } |
| 898 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 899 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 900 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 901 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 902 | * read domain and manually flush cachelines (if required). This |
| 903 | * optimizes for the case when the gpu will dirty the data |
| 904 | * anyway again before the next pread happens. |
| 905 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 906 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 907 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 908 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 909 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 910 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 911 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 912 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 913 | |
| 914 | err_unpin: |
| 915 | i915_gem_object_unpin_pages(obj); |
| 916 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 920 | unsigned int *needs_clflush) |
| 921 | { |
| 922 | int ret; |
| 923 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 924 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 925 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 926 | *needs_clflush = 0; |
| 927 | if (!i915_gem_object_has_struct_page(obj)) |
| 928 | return -ENODEV; |
| 929 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 930 | ret = i915_gem_object_wait(obj, |
| 931 | I915_WAIT_INTERRUPTIBLE | |
| 932 | I915_WAIT_LOCKED | |
| 933 | I915_WAIT_ALL, |
| 934 | MAX_SCHEDULE_TIMEOUT, |
| 935 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 936 | if (ret) |
| 937 | return ret; |
| 938 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 939 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 940 | if (ret) |
| 941 | return ret; |
| 942 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 943 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 944 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 945 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 946 | if (ret) |
| 947 | goto err_unpin; |
| 948 | else |
| 949 | goto out; |
| 950 | } |
| 951 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 952 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 953 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 954 | /* If we're not in the cpu write domain, set ourself into the |
| 955 | * gtt write domain and manually flush cachelines (as required). |
| 956 | * This optimizes for the case when the gpu will use the data |
| 957 | * right away and we therefore have to clflush anyway. |
| 958 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 959 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 960 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 961 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 962 | /* |
| 963 | * Same trick applies to invalidate partially written |
| 964 | * cachelines read before writing. |
| 965 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 966 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 967 | *needs_clflush |= CLFLUSH_BEFORE; |
| 968 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 969 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 970 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 971 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 972 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 973 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 974 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 975 | |
| 976 | err_unpin: |
| 977 | i915_gem_object_unpin_pages(obj); |
| 978 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 979 | } |
| 980 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 981 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame^] | 982 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 983 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 984 | { |
| 985 | char *vaddr; |
| 986 | int ret; |
| 987 | |
| 988 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 989 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame^] | 990 | if (needs_clflush) |
| 991 | drm_clflush_virt_range(vaddr + offset, len); |
| 992 | |
| 993 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 994 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 995 | kunmap(page); |
| 996 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame^] | 997 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static int |
| 1001 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 1002 | struct drm_i915_gem_pread *args) |
| 1003 | { |
| 1004 | char __user *user_data; |
| 1005 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1006 | unsigned int needs_clflush; |
| 1007 | unsigned int idx, offset; |
| 1008 | int ret; |
| 1009 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1010 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 1011 | if (ret) |
| 1012 | return ret; |
| 1013 | |
| 1014 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 1015 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 1016 | if (ret) |
| 1017 | return ret; |
| 1018 | |
| 1019 | remain = args->size; |
| 1020 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1021 | offset = offset_in_page(args->offset); |
| 1022 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1023 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1024 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1025 | |
| 1026 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1027 | needs_clflush); |
| 1028 | if (ret) |
| 1029 | break; |
| 1030 | |
| 1031 | remain -= length; |
| 1032 | user_data += length; |
| 1033 | offset = 0; |
| 1034 | } |
| 1035 | |
| 1036 | i915_gem_obj_finish_shmem_access(obj); |
| 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | static inline bool |
| 1041 | gtt_user_read(struct io_mapping *mapping, |
| 1042 | loff_t base, int offset, |
| 1043 | char __user *user_data, int length) |
| 1044 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1045 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1046 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1047 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1048 | /* 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] | 1049 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1050 | unwritten = __copy_to_user_inatomic(user_data, |
| 1051 | (void __force *)vaddr + offset, |
| 1052 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1053 | io_mapping_unmap_atomic(vaddr); |
| 1054 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1055 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1056 | unwritten = copy_to_user(user_data, |
| 1057 | (void __force *)vaddr + offset, |
| 1058 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1059 | io_mapping_unmap(vaddr); |
| 1060 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1061 | return unwritten; |
| 1062 | } |
| 1063 | |
| 1064 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1065 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1066 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1067 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1068 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1069 | struct i915_ggtt *ggtt = &i915->ggtt; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1070 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1071 | struct i915_vma *vma; |
| 1072 | void __user *user_data; |
| 1073 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1074 | int ret; |
| 1075 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1076 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1077 | if (ret) |
| 1078 | return ret; |
| 1079 | |
| 1080 | intel_runtime_pm_get(i915); |
| 1081 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1082 | PIN_MAPPABLE | |
| 1083 | PIN_NONFAULT | |
| 1084 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1085 | if (!IS_ERR(vma)) { |
| 1086 | node.start = i915_ggtt_offset(vma); |
| 1087 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1088 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1089 | if (ret) { |
| 1090 | i915_vma_unpin(vma); |
| 1091 | vma = ERR_PTR(ret); |
| 1092 | } |
| 1093 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1094 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1095 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1096 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1097 | goto out_unlock; |
| 1098 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1102 | if (ret) |
| 1103 | goto out_unpin; |
| 1104 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1105 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1106 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1107 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1108 | remain = args->size; |
| 1109 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1110 | |
| 1111 | while (remain > 0) { |
| 1112 | /* Operation in this page |
| 1113 | * |
| 1114 | * page_base = page offset within aperture |
| 1115 | * page_offset = offset within page |
| 1116 | * page_length = bytes to copy for this page |
| 1117 | */ |
| 1118 | u32 page_base = node.start; |
| 1119 | unsigned page_offset = offset_in_page(offset); |
| 1120 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1121 | page_length = remain < page_length ? remain : page_length; |
| 1122 | if (node.allocated) { |
| 1123 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1124 | ggtt->vm.insert_page(&ggtt->vm, |
| 1125 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1126 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1127 | wmb(); |
| 1128 | } else { |
| 1129 | page_base += offset & PAGE_MASK; |
| 1130 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1131 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1132 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1133 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1134 | ret = -EFAULT; |
| 1135 | break; |
| 1136 | } |
| 1137 | |
| 1138 | remain -= page_length; |
| 1139 | user_data += page_length; |
| 1140 | offset += page_length; |
| 1141 | } |
| 1142 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1143 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1144 | out_unpin: |
| 1145 | if (node.allocated) { |
| 1146 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1147 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1148 | remove_mappable_node(&node); |
| 1149 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1150 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1151 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1152 | out_unlock: |
| 1153 | intel_runtime_pm_put(i915); |
| 1154 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1155 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1156 | return ret; |
| 1157 | } |
| 1158 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1159 | /** |
| 1160 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1161 | * @dev: drm device pointer |
| 1162 | * @data: ioctl data blob |
| 1163 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1164 | * |
| 1165 | * On error, the contents of *data are undefined. |
| 1166 | */ |
| 1167 | int |
| 1168 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1169 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1170 | { |
| 1171 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1172 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1173 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1174 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1175 | if (args->size == 0) |
| 1176 | return 0; |
| 1177 | |
| 1178 | if (!access_ok(VERIFY_WRITE, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1179 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1180 | args->size)) |
| 1181 | return -EFAULT; |
| 1182 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1183 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1184 | if (!obj) |
| 1185 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1186 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1187 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1188 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1189 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1190 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1193 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1194 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1195 | ret = i915_gem_object_wait(obj, |
| 1196 | I915_WAIT_INTERRUPTIBLE, |
| 1197 | MAX_SCHEDULE_TIMEOUT, |
| 1198 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1199 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1200 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1201 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1202 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1203 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1204 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1205 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1206 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1207 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1208 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1209 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1210 | i915_gem_object_unpin_pages(obj); |
| 1211 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1212 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1213 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1214 | } |
| 1215 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1216 | /* This is the fast write path which cannot handle |
| 1217 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1218 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1219 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1220 | static inline bool |
| 1221 | ggtt_write(struct io_mapping *mapping, |
| 1222 | loff_t base, int offset, |
| 1223 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1224 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1225 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1226 | unsigned long unwritten; |
| 1227 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1228 | /* 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] | 1229 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1230 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1231 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1232 | io_mapping_unmap_atomic(vaddr); |
| 1233 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1234 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1235 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1236 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1237 | io_mapping_unmap(vaddr); |
| 1238 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1239 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1240 | return unwritten; |
| 1241 | } |
| 1242 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1243 | /** |
| 1244 | * This is the fast pwrite path, where we copy the data directly from the |
| 1245 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1246 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1247 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1248 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1249 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1250 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1251 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1252 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1253 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1254 | struct i915_ggtt *ggtt = &i915->ggtt; |
| 1255 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1256 | struct i915_vma *vma; |
| 1257 | u64 remain, offset; |
| 1258 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1259 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1260 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1261 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1262 | if (ret) |
| 1263 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1264 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1265 | if (i915_gem_object_has_struct_page(obj)) { |
| 1266 | /* |
| 1267 | * Avoid waking the device up if we can fallback, as |
| 1268 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1269 | * depending on PCI sleeps and our own resume time). |
| 1270 | * This easily dwarfs any performance advantage from |
| 1271 | * using the cache bypass of indirect GGTT access. |
| 1272 | */ |
| 1273 | if (!intel_runtime_pm_get_if_in_use(i915)) { |
| 1274 | ret = -EFAULT; |
| 1275 | goto out_unlock; |
| 1276 | } |
| 1277 | } else { |
| 1278 | /* No backing pages, no fallback, we must force GGTT access */ |
| 1279 | intel_runtime_pm_get(i915); |
| 1280 | } |
| 1281 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1282 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1283 | PIN_MAPPABLE | |
| 1284 | PIN_NONFAULT | |
| 1285 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1286 | if (!IS_ERR(vma)) { |
| 1287 | node.start = i915_ggtt_offset(vma); |
| 1288 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1289 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1290 | if (ret) { |
| 1291 | i915_vma_unpin(vma); |
| 1292 | vma = ERR_PTR(ret); |
| 1293 | } |
| 1294 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1295 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1296 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1297 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1298 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1299 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1300 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1301 | |
| 1302 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1303 | if (ret) |
| 1304 | goto out_unpin; |
| 1305 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1306 | mutex_unlock(&i915->drm.struct_mutex); |
| 1307 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1308 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1309 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1310 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1311 | offset = args->offset; |
| 1312 | remain = args->size; |
| 1313 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1314 | /* Operation in this page |
| 1315 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1316 | * page_base = page offset within aperture |
| 1317 | * page_offset = offset within page |
| 1318 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1319 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1320 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1321 | unsigned int page_offset = offset_in_page(offset); |
| 1322 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1323 | page_length = remain < page_length ? remain : page_length; |
| 1324 | if (node.allocated) { |
| 1325 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1326 | ggtt->vm.insert_page(&ggtt->vm, |
| 1327 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1328 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1329 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1330 | } else { |
| 1331 | page_base += offset & PAGE_MASK; |
| 1332 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1333 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1334 | * source page isn't available. Return the error and we'll |
| 1335 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1336 | * If the object is non-shmem backed, we retry again with the |
| 1337 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1338 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1339 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1340 | user_data, page_length)) { |
| 1341 | ret = -EFAULT; |
| 1342 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1343 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1344 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1345 | remain -= page_length; |
| 1346 | user_data += page_length; |
| 1347 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1348 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1349 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1350 | |
| 1351 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1352 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1353 | if (node.allocated) { |
| 1354 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1355 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1356 | remove_mappable_node(&node); |
| 1357 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1358 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1359 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1360 | out_rpm: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1361 | intel_runtime_pm_put(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1362 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1363 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1364 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1367 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1368 | * Flushes invalid cachelines before writing to the target if |
| 1369 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1370 | * writing if needs_clflush is set. |
| 1371 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1372 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1373 | 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] | 1374 | bool needs_clflush_before, |
| 1375 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1376 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame^] | 1377 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1378 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1379 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame^] | 1380 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1381 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame^] | 1382 | if (needs_clflush_before) |
| 1383 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1384 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame^] | 1385 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1386 | if (!ret && needs_clflush_after) |
| 1387 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1388 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame^] | 1389 | kunmap(page); |
| 1390 | |
| 1391 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | static int |
| 1395 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1396 | const struct drm_i915_gem_pwrite *args) |
| 1397 | { |
| 1398 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1399 | void __user *user_data; |
| 1400 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1401 | unsigned int partial_cacheline_write; |
| 1402 | unsigned int needs_clflush; |
| 1403 | unsigned int offset, idx; |
| 1404 | int ret; |
| 1405 | |
| 1406 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1407 | if (ret) |
| 1408 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1409 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1410 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1411 | mutex_unlock(&i915->drm.struct_mutex); |
| 1412 | if (ret) |
| 1413 | return ret; |
| 1414 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1415 | /* If we don't overwrite a cacheline completely we need to be |
| 1416 | * careful to have up-to-date data by first clflushing. Don't |
| 1417 | * overcomplicate things and flush the entire patch. |
| 1418 | */ |
| 1419 | partial_cacheline_write = 0; |
| 1420 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1421 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1422 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1423 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1424 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1425 | offset = offset_in_page(args->offset); |
| 1426 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1427 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1428 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1429 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1430 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1431 | (offset | length) & partial_cacheline_write, |
| 1432 | needs_clflush & CLFLUSH_AFTER); |
| 1433 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1434 | break; |
| 1435 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1436 | remain -= length; |
| 1437 | user_data += length; |
| 1438 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1441 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1442 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1443 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /** |
| 1447 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1448 | * @dev: drm device |
| 1449 | * @data: ioctl data blob |
| 1450 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1451 | * |
| 1452 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1453 | */ |
| 1454 | int |
| 1455 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1456 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1457 | { |
| 1458 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1459 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1460 | int ret; |
| 1461 | |
| 1462 | if (args->size == 0) |
| 1463 | return 0; |
| 1464 | |
| 1465 | if (!access_ok(VERIFY_READ, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1466 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1467 | args->size)) |
| 1468 | return -EFAULT; |
| 1469 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1470 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1471 | if (!obj) |
| 1472 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1473 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1474 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1475 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1476 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1477 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1478 | } |
| 1479 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1480 | /* Writes not allowed into this read-only object */ |
| 1481 | if (i915_gem_object_is_readonly(obj)) { |
| 1482 | ret = -EINVAL; |
| 1483 | goto err; |
| 1484 | } |
| 1485 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1486 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1487 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1488 | ret = -ENODEV; |
| 1489 | if (obj->ops->pwrite) |
| 1490 | ret = obj->ops->pwrite(obj, args); |
| 1491 | if (ret != -ENODEV) |
| 1492 | goto err; |
| 1493 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1494 | ret = i915_gem_object_wait(obj, |
| 1495 | I915_WAIT_INTERRUPTIBLE | |
| 1496 | I915_WAIT_ALL, |
| 1497 | MAX_SCHEDULE_TIMEOUT, |
| 1498 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1499 | if (ret) |
| 1500 | goto err; |
| 1501 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1502 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1503 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1504 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1505 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1506 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1507 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1508 | * it would end up going through the fenced access, and we'll get |
| 1509 | * different detiling behavior between reading and writing. |
| 1510 | * pread/pwrite currently are reading and writing from the CPU |
| 1511 | * perspective, requiring manual detiling by the client. |
| 1512 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1513 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1514 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1515 | /* Note that the gtt paths might fail with non-page-backed user |
| 1516 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1517 | * textures). Fallback to the shmem path in that case. |
| 1518 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1519 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1520 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1521 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1522 | if (obj->phys_handle) |
| 1523 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1524 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1525 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1526 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1527 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1528 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1529 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1530 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1531 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1534 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1535 | { |
| 1536 | struct drm_i915_private *i915; |
| 1537 | struct list_head *list; |
| 1538 | struct i915_vma *vma; |
| 1539 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1540 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1541 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1542 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1543 | if (i915_vma_is_active(vma)) |
| 1544 | continue; |
| 1545 | |
| 1546 | if (!drm_mm_node_allocated(&vma->node)) |
| 1547 | continue; |
| 1548 | |
| 1549 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 1550 | } |
| 1551 | |
| 1552 | i915 = to_i915(obj->base.dev); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1553 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1554 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1555 | list_move_tail(&obj->mm.link, list); |
| 1556 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1557 | } |
| 1558 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1559 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1560 | * Called when user space prepares to use an object with the CPU, either |
| 1561 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1562 | * @dev: drm device |
| 1563 | * @data: ioctl data blob |
| 1564 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1565 | */ |
| 1566 | int |
| 1567 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1568 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1569 | { |
| 1570 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1571 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1572 | uint32_t read_domains = args->read_domains; |
| 1573 | uint32_t write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1574 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1575 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1576 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1577 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1578 | return -EINVAL; |
| 1579 | |
| 1580 | /* Having something in the write domain implies it's in the read |
| 1581 | * domain, and only that read domain. Enforce that in the request. |
| 1582 | */ |
| 1583 | if (write_domain != 0 && read_domains != write_domain) |
| 1584 | return -EINVAL; |
| 1585 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1586 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1587 | if (!obj) |
| 1588 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1589 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1590 | /* Try to flush the object off the GPU without holding the lock. |
| 1591 | * We will repeat the flush holding the lock in the normal manner |
| 1592 | * to catch cases where we are gazumped. |
| 1593 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1594 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1595 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1596 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1597 | (write_domain ? I915_WAIT_ALL : 0), |
| 1598 | MAX_SCHEDULE_TIMEOUT, |
| 1599 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1600 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1601 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1602 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1603 | /* |
| 1604 | * Proxy objects do not control access to the backing storage, ergo |
| 1605 | * they cannot be used as a means to manipulate the cache domain |
| 1606 | * tracking for that backing storage. The proxy object is always |
| 1607 | * considered to be outside of any cache domain. |
| 1608 | */ |
| 1609 | if (i915_gem_object_is_proxy(obj)) { |
| 1610 | err = -ENXIO; |
| 1611 | goto out; |
| 1612 | } |
| 1613 | |
| 1614 | /* |
| 1615 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1616 | * direct access in memory with previous cached writes through |
| 1617 | * shmemfs and that our cache domain tracking remains valid. |
| 1618 | * For example, if the obj->filp was moved to swap without us |
| 1619 | * being notified and releasing the pages, we would mistakenly |
| 1620 | * continue to assume that the obj remained out of the CPU cached |
| 1621 | * domain. |
| 1622 | */ |
| 1623 | err = i915_gem_object_pin_pages(obj); |
| 1624 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1625 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1626 | |
| 1627 | err = i915_mutex_lock_interruptible(dev); |
| 1628 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1629 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1630 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1631 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1632 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1633 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1634 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1635 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1636 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1637 | |
| 1638 | /* And bump the LRU for this access */ |
| 1639 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1640 | |
| 1641 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1642 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1643 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1644 | intel_fb_obj_invalidate(obj, |
| 1645 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1646 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1647 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1648 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1649 | out: |
| 1650 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1651 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | /** |
| 1655 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1656 | * @dev: drm device |
| 1657 | * @data: ioctl data blob |
| 1658 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1659 | */ |
| 1660 | int |
| 1661 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1662 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1663 | { |
| 1664 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1665 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1666 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1667 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1668 | if (!obj) |
| 1669 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1670 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1671 | /* |
| 1672 | * Proxy objects are barred from CPU access, so there is no |
| 1673 | * need to ban sw_finish as it is a nop. |
| 1674 | */ |
| 1675 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1676 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1677 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1678 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1679 | |
| 1680 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1684 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1685 | * it is mapped to. |
| 1686 | * @dev: drm device |
| 1687 | * @data: ioctl data blob |
| 1688 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1689 | * |
| 1690 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1691 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1692 | * |
| 1693 | * IMPORTANT: |
| 1694 | * |
| 1695 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1696 | * mmap support, please don't implement mmap support like here. The modern way |
| 1697 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1698 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1699 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1700 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1701 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1702 | */ |
| 1703 | int |
| 1704 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1705 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1706 | { |
| 1707 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1708 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1709 | unsigned long addr; |
| 1710 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1711 | if (args->flags & ~(I915_MMAP_WC)) |
| 1712 | return -EINVAL; |
| 1713 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1714 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1715 | return -ENODEV; |
| 1716 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1717 | obj = i915_gem_object_lookup(file, args->handle); |
| 1718 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1719 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1720 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1721 | /* prime objects have no backing filp to GEM mmap |
| 1722 | * pages from. |
| 1723 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1724 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1725 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1726 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1727 | } |
| 1728 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1729 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1730 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1731 | args->offset); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1732 | if (args->flags & I915_MMAP_WC) { |
| 1733 | struct mm_struct *mm = current->mm; |
| 1734 | struct vm_area_struct *vma; |
| 1735 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1736 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1737 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1738 | return -EINTR; |
| 1739 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1740 | vma = find_vma(mm, addr); |
| 1741 | if (vma) |
| 1742 | vma->vm_page_prot = |
| 1743 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1744 | else |
| 1745 | addr = -ENOMEM; |
| 1746 | up_write(&mm->mmap_sem); |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1747 | |
| 1748 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1749 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1750 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1751 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1752 | if (IS_ERR((void *)addr)) |
| 1753 | return addr; |
| 1754 | |
| 1755 | args->addr_ptr = (uint64_t) addr; |
| 1756 | |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1760 | 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] | 1761 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1762 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1763 | } |
| 1764 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1765 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1766 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1767 | * |
| 1768 | * A history of the GTT mmap interface: |
| 1769 | * |
| 1770 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1771 | * aligned and suitable for fencing, and still fit into the available |
| 1772 | * mappable space left by the pinned display objects. A classic problem |
| 1773 | * we called the page-fault-of-doom where we would ping-pong between |
| 1774 | * two objects that could not fit inside the GTT and so the memcpy |
| 1775 | * would page one object in at the expense of the other between every |
| 1776 | * single byte. |
| 1777 | * |
| 1778 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1779 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1780 | * object is too large for the available space (or simply too large |
| 1781 | * for the mappable aperture!), a view is created instead and faulted |
| 1782 | * into userspace. (This view is aligned and sized appropriately for |
| 1783 | * fenced access.) |
| 1784 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1785 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1786 | * delayed writes via GTT before performing direct access via WC. |
| 1787 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1788 | * Restrictions: |
| 1789 | * |
| 1790 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1791 | * hangs on some architectures, corruption on others. An attempt to service |
| 1792 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1793 | * |
| 1794 | * * the object must be able to fit into RAM (physical memory, though no |
| 1795 | * limited to the mappable aperture). |
| 1796 | * |
| 1797 | * |
| 1798 | * Caveats: |
| 1799 | * |
| 1800 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1801 | * all data to system memory. Subsequent access will not be synchronized. |
| 1802 | * |
| 1803 | * * all mappings are revoked on runtime device suspend. |
| 1804 | * |
| 1805 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1806 | * (older machines require fence register for display and blitter access |
| 1807 | * as well). Contention of the fence registers will cause the previous users |
| 1808 | * to be unmapped and any new access will generate new page faults. |
| 1809 | * |
| 1810 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1811 | * rather than the expected SIGSEGV. |
| 1812 | */ |
| 1813 | int i915_gem_mmap_gtt_version(void) |
| 1814 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1815 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1816 | } |
| 1817 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1818 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1819 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1820 | pgoff_t page_offset, |
| 1821 | unsigned int chunk) |
| 1822 | { |
| 1823 | struct i915_ggtt_view view; |
| 1824 | |
| 1825 | if (i915_gem_object_is_tiled(obj)) |
| 1826 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1827 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1828 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1829 | view.partial.offset = rounddown(page_offset, chunk); |
| 1830 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1831 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1832 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1833 | |
| 1834 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1835 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1836 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1837 | |
| 1838 | return view; |
| 1839 | } |
| 1840 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1841 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1842 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1843 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1844 | * |
| 1845 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1846 | * from userspace. The fault handler takes care of binding the object to |
| 1847 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1848 | * only if needed based on whether the old reg is still valid or the object |
| 1849 | * is tiled) and inserting a new PTE into the faulting process. |
| 1850 | * |
| 1851 | * Note that the faulting process may involve evicting existing objects |
| 1852 | * from the GTT and/or fence registers to make room. So performance may |
| 1853 | * suffer if the GTT working set is large or there are few fence registers |
| 1854 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1855 | * |
| 1856 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1857 | * 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] | 1858 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1859 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1860 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1861 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1862 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1863 | 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] | 1864 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1865 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1866 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1867 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1868 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1869 | pgoff_t page_offset; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1870 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1871 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1872 | /* Sanity check that we allow writing into this object */ |
| 1873 | if (i915_gem_object_is_readonly(obj) && write) |
| 1874 | return VM_FAULT_SIGBUS; |
| 1875 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1876 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1877 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1878 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1879 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1880 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1881 | /* 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] | 1882 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1883 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1884 | * where we are gazumped. |
| 1885 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1886 | ret = i915_gem_object_wait(obj, |
| 1887 | I915_WAIT_INTERRUPTIBLE, |
| 1888 | MAX_SCHEDULE_TIMEOUT, |
| 1889 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1890 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1891 | goto err; |
| 1892 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1893 | ret = i915_gem_object_pin_pages(obj); |
| 1894 | if (ret) |
| 1895 | goto err; |
| 1896 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1897 | intel_runtime_pm_get(dev_priv); |
| 1898 | |
| 1899 | ret = i915_mutex_lock_interruptible(dev); |
| 1900 | if (ret) |
| 1901 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1902 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1903 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1904 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1905 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1906 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1909 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1910 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1911 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1912 | PIN_MAPPABLE | |
| 1913 | PIN_NONBLOCK | |
| 1914 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1915 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1916 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1917 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1918 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1919 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1920 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1921 | flags = PIN_MAPPABLE; |
| 1922 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1923 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1924 | |
| 1925 | /* |
| 1926 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1927 | * all hope that the hardware is able to track future writes. |
| 1928 | */ |
| 1929 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1930 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1931 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1932 | if (IS_ERR(vma) && !view.type) { |
| 1933 | flags = PIN_MAPPABLE; |
| 1934 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1935 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1936 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1937 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1938 | if (IS_ERR(vma)) { |
| 1939 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1940 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1941 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1942 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1943 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1944 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1945 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1946 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1947 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1948 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1949 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1950 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1951 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1952 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1953 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1954 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1955 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1956 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1957 | if (ret) |
| 1958 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1959 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1960 | /* Mark as being mmapped into userspace for later revocation */ |
| 1961 | assert_rpm_wakelock_held(dev_priv); |
| 1962 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1963 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1964 | GEM_BUG_ON(!obj->userfault_count); |
| 1965 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1966 | i915_vma_set_ggtt_write(vma); |
| 1967 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1968 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1969 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1970 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1971 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1972 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1973 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1974 | err_rpm: |
| 1975 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1976 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1977 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1978 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1979 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1980 | /* |
| 1981 | * We eat errors when the gpu is terminally wedged to avoid |
| 1982 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1983 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1984 | * and so needs to be reported. |
| 1985 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1986 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) |
| 1987 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1988 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1989 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1990 | /* |
| 1991 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1992 | * handler to reset everything when re-faulting in |
| 1993 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1994 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1995 | case 0: |
| 1996 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1997 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1998 | case -EBUSY: |
| 1999 | /* |
| 2000 | * EBUSY is ok: this just means that another thread |
| 2001 | * already did the job. |
| 2002 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2003 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2004 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2005 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2006 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2007 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2008 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2009 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2010 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2011 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2012 | } |
| 2013 | } |
| 2014 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2015 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2016 | { |
| 2017 | struct i915_vma *vma; |
| 2018 | |
| 2019 | GEM_BUG_ON(!obj->userfault_count); |
| 2020 | |
| 2021 | obj->userfault_count = 0; |
| 2022 | list_del(&obj->userfault_link); |
| 2023 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2024 | obj->base.dev->anon_inode->i_mapping); |
| 2025 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 2026 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2027 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2028 | } |
| 2029 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2030 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2031 | * i915_gem_release_mmap - remove physical page mappings |
| 2032 | * @obj: obj in question |
| 2033 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2034 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2035 | * relinquish ownership of the pages back to the system. |
| 2036 | * |
| 2037 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2038 | * object through the GTT and then lose the fence register due to |
| 2039 | * resource pressure. Similarly if the object has been moved out of the |
| 2040 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2041 | * mapping will then trigger a page fault on the next user access, allowing |
| 2042 | * fixup by i915_gem_fault(). |
| 2043 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2044 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2045 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2046 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2047 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2048 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2049 | /* Serialisation between user GTT access and our code depends upon |
| 2050 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2051 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2052 | * |
| 2053 | * Note that RPM complicates somewhat by adding an additional |
| 2054 | * requirement that operations to the GGTT be made holding the RPM |
| 2055 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2056 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2057 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2058 | intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2059 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2060 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2061 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2062 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2063 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2064 | |
| 2065 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2066 | * memory transactions from userspace before we return. The TLB |
| 2067 | * flushing implied above by changing the PTE above *should* be |
| 2068 | * sufficient, an extra barrier here just provides us with a bit |
| 2069 | * of paranoid documentation about our requirement to serialise |
| 2070 | * memory writes before touching registers / GSM. |
| 2071 | */ |
| 2072 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2073 | |
| 2074 | out: |
| 2075 | intel_runtime_pm_put(i915); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2076 | } |
| 2077 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2078 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
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 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2081 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2082 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2083 | /* |
| 2084 | * Only called during RPM suspend. All users of the userfault_list |
| 2085 | * must be holding an RPM wakeref to ensure that this can not |
| 2086 | * run concurrently with themselves (and use the struct_mutex for |
| 2087 | * protection between themselves). |
| 2088 | */ |
| 2089 | |
| 2090 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2091 | &dev_priv->mm.userfault_list, userfault_link) |
| 2092 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2093 | |
| 2094 | /* The fence will be lost when the device powers down. If any were |
| 2095 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2096 | * down! All other fences will be reacquired by the user upon waking. |
| 2097 | */ |
| 2098 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2099 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2100 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2101 | /* Ideally we want to assert that the fence register is not |
| 2102 | * live at this point (i.e. that no piece of code will be |
| 2103 | * trying to write through fence + GTT, as that both violates |
| 2104 | * our tracking of activity and associated locking/barriers, |
| 2105 | * but also is illegal given that the hw is powered down). |
| 2106 | * |
| 2107 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2108 | * That is not sufficient, and we need a more fine-grained |
| 2109 | * tool if we want to have a sanity check here. |
| 2110 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2111 | |
| 2112 | if (!reg->vma) |
| 2113 | continue; |
| 2114 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2115 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2116 | reg->dirty = true; |
| 2117 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2118 | } |
| 2119 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2120 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2121 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2122 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2123 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2124 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2125 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2126 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2127 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2128 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2129 | /* Attempt to reap some mmap space from dead objects */ |
| 2130 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2131 | err = i915_gem_wait_for_idle(dev_priv, |
| 2132 | I915_WAIT_INTERRUPTIBLE, |
| 2133 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2134 | if (err) |
| 2135 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2136 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2137 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2138 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2139 | if (!err) |
| 2140 | break; |
| 2141 | |
| 2142 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2143 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2144 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2148 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2149 | drm_gem_free_mmap_offset(&obj->base); |
| 2150 | } |
| 2151 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2152 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2153 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2154 | struct drm_device *dev, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2155 | uint32_t handle, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2156 | uint64_t *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2157 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2158 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2159 | int ret; |
| 2160 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2161 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2162 | if (!obj) |
| 2163 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2164 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2165 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2166 | if (ret == 0) |
| 2167 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2168 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2169 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2170 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2171 | } |
| 2172 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2173 | /** |
| 2174 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2175 | * @dev: DRM device |
| 2176 | * @data: GTT mapping ioctl data |
| 2177 | * @file: GEM object info |
| 2178 | * |
| 2179 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2180 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2181 | * up so we can get faults in the handler above. |
| 2182 | * |
| 2183 | * The fault handler will take care of binding the object into the GTT |
| 2184 | * (since it may have been evicted to make room for something), allocating |
| 2185 | * a fence register, and mapping the appropriate aperture address into |
| 2186 | * userspace. |
| 2187 | */ |
| 2188 | int |
| 2189 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2190 | struct drm_file *file) |
| 2191 | { |
| 2192 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2193 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2194 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2195 | } |
| 2196 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2197 | /* Immediately discard the backing storage */ |
| 2198 | static void |
| 2199 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2200 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2201 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2202 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2203 | if (obj->base.filp == NULL) |
| 2204 | return; |
| 2205 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2206 | /* Our goal here is to return as much of the memory as |
| 2207 | * is possible back to the system as we are called from OOM. |
| 2208 | * To do this we must instruct the shmfs to drop all of its |
| 2209 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2210 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2211 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2212 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2213 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2214 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2215 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2216 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2217 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2218 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2219 | struct address_space *mapping; |
| 2220 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2221 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2222 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2223 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2224 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2225 | case I915_MADV_DONTNEED: |
| 2226 | i915_gem_object_truncate(obj); |
| 2227 | case __I915_MADV_PURGED: |
| 2228 | return; |
| 2229 | } |
| 2230 | |
| 2231 | if (obj->base.filp == NULL) |
| 2232 | return; |
| 2233 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2234 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2235 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2236 | } |
| 2237 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2238 | /* |
| 2239 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2240 | * ref count of those pages. |
| 2241 | */ |
| 2242 | static void check_release_pagevec(struct pagevec *pvec) |
| 2243 | { |
| 2244 | check_move_unevictable_pages(pvec); |
| 2245 | __pagevec_release(pvec); |
| 2246 | cond_resched(); |
| 2247 | } |
| 2248 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2249 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2250 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2251 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2252 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2253 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2254 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2255 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2256 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2257 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2258 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2259 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2260 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2261 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2262 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2263 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2264 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2265 | |
| 2266 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2267 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2268 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2269 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2270 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2271 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2272 | mark_page_accessed(page); |
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_add(&pvec, page)) |
| 2275 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2276 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2277 | if (pagevec_count(&pvec)) |
| 2278 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2279 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2280 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2281 | sg_free_table(pages); |
| 2282 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2283 | } |
| 2284 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2285 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2286 | { |
| 2287 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2288 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2289 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2290 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2291 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2292 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2293 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2294 | } |
| 2295 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2296 | static struct sg_table * |
| 2297 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2298 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2299 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2300 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2301 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2302 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2303 | if (!pages) |
| 2304 | return NULL; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2305 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2306 | spin_lock(&i915->mm.obj_lock); |
| 2307 | list_del(&obj->mm.link); |
| 2308 | spin_unlock(&i915->mm.obj_lock); |
| 2309 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2310 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2311 | void *ptr; |
| 2312 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2313 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2314 | if (is_vmalloc_addr(ptr)) |
| 2315 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2316 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2317 | kunmap(kmap_to_page(ptr)); |
| 2318 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2319 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2320 | } |
| 2321 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2322 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2323 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2324 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2325 | return pages; |
| 2326 | } |
| 2327 | |
| 2328 | void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2329 | enum i915_mm_subclass subclass) |
| 2330 | { |
| 2331 | struct sg_table *pages; |
| 2332 | |
| 2333 | if (i915_gem_object_has_pinned_pages(obj)) |
| 2334 | return; |
| 2335 | |
| 2336 | GEM_BUG_ON(obj->bind_count); |
| 2337 | if (!i915_gem_object_has_pages(obj)) |
| 2338 | return; |
| 2339 | |
| 2340 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2341 | mutex_lock_nested(&obj->mm.lock, subclass); |
| 2342 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) |
| 2343 | goto unlock; |
| 2344 | |
| 2345 | /* |
| 2346 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2347 | * array, hence protect them from being reaped by removing them from gtt |
| 2348 | * lists early. |
| 2349 | */ |
| 2350 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2351 | if (!IS_ERR(pages)) |
| 2352 | obj->ops->put_pages(obj, pages); |
| 2353 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2354 | unlock: |
| 2355 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2356 | } |
| 2357 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2358 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2359 | { |
| 2360 | struct sg_table new_st; |
| 2361 | struct scatterlist *sg, *new_sg; |
| 2362 | unsigned int i; |
| 2363 | |
| 2364 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2365 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2366 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2367 | 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] | 2368 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2369 | |
| 2370 | new_sg = new_st.sgl; |
| 2371 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2372 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2373 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2374 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2375 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2376 | new_sg = sg_next(new_sg); |
| 2377 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2378 | 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] | 2379 | |
| 2380 | sg_free_table(orig_st); |
| 2381 | |
| 2382 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2383 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2386 | 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] | 2387 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2388 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2389 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2390 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2391 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2392 | struct sg_table *st; |
| 2393 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2394 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2395 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2396 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2397 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2398 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2399 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2400 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2401 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2402 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2403 | /* |
| 2404 | * 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] | 2405 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2406 | * a GPU cache |
| 2407 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2408 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2409 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2410 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2411 | /* |
| 2412 | * If there's no chance of allocating enough pages for the whole |
| 2413 | * object, bail early. |
| 2414 | */ |
| 2415 | if (page_count > totalram_pages) |
| 2416 | return -ENOMEM; |
| 2417 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2418 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2419 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2420 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2421 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2422 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2423 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2424 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2425 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2426 | } |
| 2427 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2428 | /* |
| 2429 | * 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] | 2430 | * at this point until we release them. |
| 2431 | * |
| 2432 | * Fail silently without starting the shrinker |
| 2433 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2434 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2435 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2436 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2437 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2438 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2439 | sg = st->sgl; |
| 2440 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2441 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2442 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2443 | const unsigned int shrink[] = { |
| 2444 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2445 | 0, |
| 2446 | }, *s = shrink; |
| 2447 | gfp_t gfp = noreclaim; |
| 2448 | |
| 2449 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2450 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2451 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2452 | if (likely(!IS_ERR(page))) |
| 2453 | break; |
| 2454 | |
| 2455 | if (!*s) { |
| 2456 | ret = PTR_ERR(page); |
| 2457 | goto err_sg; |
| 2458 | } |
| 2459 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2460 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2461 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2462 | /* |
| 2463 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2464 | * our own buffer, now let the real VM do its job and |
| 2465 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2466 | * |
| 2467 | * However, since graphics tend to be disposable, |
| 2468 | * defer the oom here by reporting the ENOMEM back |
| 2469 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2470 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2471 | if (!*s) { |
| 2472 | /* reclaim and warn, but no oom */ |
| 2473 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2474 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2475 | /* |
| 2476 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2477 | * kswapd to reclaim our pages (direct reclaim |
| 2478 | * does not effectively begin pageout of our |
| 2479 | * buffers on its own). However, direct reclaim |
| 2480 | * only waits for kswapd when under allocation |
| 2481 | * congestion. So as a result __GFP_RECLAIM is |
| 2482 | * unreliable and fails to actually reclaim our |
| 2483 | * dirty pages -- unless you try over and over |
| 2484 | * again with !__GFP_NORETRY. However, we still |
| 2485 | * want to fail this allocation rather than |
| 2486 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2487 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2488 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2489 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2490 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2491 | } while (1); |
| 2492 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2493 | if (!i || |
| 2494 | sg->length >= max_segment || |
| 2495 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2496 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2497 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2498 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2499 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2500 | st->nents++; |
| 2501 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2502 | } else { |
| 2503 | sg->length += PAGE_SIZE; |
| 2504 | } |
| 2505 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2506 | |
| 2507 | /* Check that the i965g/gm workaround works. */ |
| 2508 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2509 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2510 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2511 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2512 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2513 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2514 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2515 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2516 | i915_sg_trim(st); |
| 2517 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2518 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2519 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2520 | /* |
| 2521 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2522 | * it could not reserve enough large entries, asking |
| 2523 | * for PAGE_SIZE chunks instead may be helpful. |
| 2524 | */ |
| 2525 | if (max_segment > PAGE_SIZE) { |
| 2526 | for_each_sgt_page(page, sgt_iter, st) |
| 2527 | put_page(page); |
| 2528 | sg_free_table(st); |
| 2529 | |
| 2530 | max_segment = PAGE_SIZE; |
| 2531 | goto rebuild_st; |
| 2532 | } else { |
| 2533 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2534 | "Failed to DMA remap %lu pages\n", |
| 2535 | page_count); |
| 2536 | goto err_pages; |
| 2537 | } |
| 2538 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2539 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2540 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2541 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2542 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2543 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2544 | |
| 2545 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2546 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2547 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2548 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2549 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2550 | mapping_clear_unevictable(mapping); |
| 2551 | pagevec_init(&pvec); |
| 2552 | for_each_sgt_page(page, sgt_iter, st) { |
| 2553 | if (!pagevec_add(&pvec, page)) |
| 2554 | check_release_pagevec(&pvec); |
| 2555 | } |
| 2556 | if (pagevec_count(&pvec)) |
| 2557 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2558 | sg_free_table(st); |
| 2559 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2560 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2561 | /* |
| 2562 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2563 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2564 | * ENOMEM for a genuine allocation failure. |
| 2565 | * |
| 2566 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2567 | * space and so want to translate the error from shmemfs back to our |
| 2568 | * usual understanding of ENOMEM. |
| 2569 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2570 | if (ret == -ENOSPC) |
| 2571 | ret = -ENOMEM; |
| 2572 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2573 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2577 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2578 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2579 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2580 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2581 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2582 | int i; |
| 2583 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2584 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2585 | |
| 2586 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2587 | obj->mm.get_page.sg_idx = 0; |
| 2588 | |
| 2589 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2590 | |
| 2591 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2592 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2593 | GEM_BUG_ON(obj->mm.quirked); |
| 2594 | __i915_gem_object_pin_pages(obj); |
| 2595 | obj->mm.quirked = true; |
| 2596 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2597 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2598 | GEM_BUG_ON(!sg_page_sizes); |
| 2599 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2600 | |
| 2601 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2602 | * Calculate the supported page-sizes which fit into the given |
| 2603 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2604 | * to use opportunistically when later inserting into the GTT. For |
| 2605 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2606 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2607 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2608 | */ |
| 2609 | obj->mm.page_sizes.sg = 0; |
| 2610 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2611 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2612 | obj->mm.page_sizes.sg |= BIT(i); |
| 2613 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2614 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2615 | |
| 2616 | spin_lock(&i915->mm.obj_lock); |
| 2617 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2618 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2622 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2623 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2624 | |
| 2625 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2626 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2627 | return -EFAULT; |
| 2628 | } |
| 2629 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2630 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2631 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2632 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2633 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2636 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2637 | * 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] | 2638 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2639 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2640 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2641 | * or as the object is itself released. |
| 2642 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2643 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2644 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2645 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2646 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2647 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2648 | if (err) |
| 2649 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2650 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2651 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2652 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2653 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2654 | err = ____i915_gem_object_get_pages(obj); |
| 2655 | if (err) |
| 2656 | goto unlock; |
| 2657 | |
| 2658 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2659 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2660 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2661 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2662 | unlock: |
| 2663 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2664 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2665 | } |
| 2666 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2667 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2668 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2669 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2670 | { |
| 2671 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2672 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2673 | struct sgt_iter sgt_iter; |
| 2674 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2675 | struct page *stack_pages[32]; |
| 2676 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2677 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2678 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2679 | void *addr; |
| 2680 | |
| 2681 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2682 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2683 | return kmap(sg_page(sgt->sgl)); |
| 2684 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2685 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2686 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2687 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2688 | if (!pages) |
| 2689 | return NULL; |
| 2690 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2691 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2692 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2693 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2694 | |
| 2695 | /* Check that we have the expected number of pages */ |
| 2696 | GEM_BUG_ON(i != n_pages); |
| 2697 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2698 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2699 | default: |
| 2700 | MISSING_CASE(type); |
| 2701 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2702 | case I915_MAP_WB: |
| 2703 | pgprot = PAGE_KERNEL; |
| 2704 | break; |
| 2705 | case I915_MAP_WC: |
| 2706 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2707 | break; |
| 2708 | } |
| 2709 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2710 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2711 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2712 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2713 | |
| 2714 | return addr; |
| 2715 | } |
| 2716 | |
| 2717 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2718 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2719 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2720 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2721 | enum i915_map_type has_type; |
| 2722 | bool pinned; |
| 2723 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2724 | int ret; |
| 2725 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2726 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2727 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2728 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2729 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2730 | if (ret) |
| 2731 | return ERR_PTR(ret); |
| 2732 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2733 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2734 | type &= ~I915_MAP_OVERRIDE; |
| 2735 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2736 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2737 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2738 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2739 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2740 | ret = ____i915_gem_object_get_pages(obj); |
| 2741 | if (ret) |
| 2742 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2743 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2744 | smp_mb__before_atomic(); |
| 2745 | } |
| 2746 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2747 | pinned = false; |
| 2748 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2749 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2750 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2751 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2752 | if (ptr && has_type != type) { |
| 2753 | if (pinned) { |
| 2754 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2755 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2756 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2757 | |
| 2758 | if (is_vmalloc_addr(ptr)) |
| 2759 | vunmap(ptr); |
| 2760 | else |
| 2761 | kunmap(kmap_to_page(ptr)); |
| 2762 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2763 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2764 | } |
| 2765 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2766 | if (!ptr) { |
| 2767 | ptr = i915_gem_object_map(obj, type); |
| 2768 | if (!ptr) { |
| 2769 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2770 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2771 | } |
| 2772 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2773 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2774 | } |
| 2775 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2776 | out_unlock: |
| 2777 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2778 | return ptr; |
| 2779 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2780 | err_unpin: |
| 2781 | atomic_dec(&obj->mm.pages_pin_count); |
| 2782 | err_unlock: |
| 2783 | ptr = ERR_PTR(ret); |
| 2784 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2785 | } |
| 2786 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2787 | static int |
| 2788 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2789 | const struct drm_i915_gem_pwrite *arg) |
| 2790 | { |
| 2791 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2792 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2793 | u64 remain, offset; |
| 2794 | unsigned int pg; |
| 2795 | |
| 2796 | /* Before we instantiate/pin the backing store for our use, we |
| 2797 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2798 | * the pagecache. We avoid the penalty of instantiating all the |
| 2799 | * pages, important if the user is just writing to a few and never |
| 2800 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2801 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2802 | * or clearing-before-use) before it is overwritten. |
| 2803 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2804 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2805 | return -ENODEV; |
| 2806 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2807 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2808 | return -EFAULT; |
| 2809 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2810 | /* Before the pages are instantiated the object is treated as being |
| 2811 | * in the CPU domain. The pages will be clflushed as required before |
| 2812 | * use, and we can freely write into the pages directly. If userspace |
| 2813 | * races pwrite with any other operation; corruption will ensue - |
| 2814 | * that is userspace's prerogative! |
| 2815 | */ |
| 2816 | |
| 2817 | remain = arg->size; |
| 2818 | offset = arg->offset; |
| 2819 | pg = offset_in_page(offset); |
| 2820 | |
| 2821 | do { |
| 2822 | unsigned int len, unwritten; |
| 2823 | struct page *page; |
| 2824 | void *data, *vaddr; |
| 2825 | int err; |
| 2826 | |
| 2827 | len = PAGE_SIZE - pg; |
| 2828 | if (len > remain) |
| 2829 | len = remain; |
| 2830 | |
| 2831 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2832 | offset, len, 0, |
| 2833 | &page, &data); |
| 2834 | if (err < 0) |
| 2835 | return err; |
| 2836 | |
| 2837 | vaddr = kmap(page); |
| 2838 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2839 | kunmap(page); |
| 2840 | |
| 2841 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2842 | offset, len, len - unwritten, |
| 2843 | page, data); |
| 2844 | if (err < 0) |
| 2845 | return err; |
| 2846 | |
| 2847 | if (unwritten) |
| 2848 | return -EFAULT; |
| 2849 | |
| 2850 | remain -= len; |
| 2851 | user_data += len; |
| 2852 | offset += len; |
| 2853 | pg = 0; |
| 2854 | } while (remain); |
| 2855 | |
| 2856 | return 0; |
| 2857 | } |
| 2858 | |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2859 | static void i915_gem_client_mark_guilty(struct drm_i915_file_private *file_priv, |
| 2860 | const struct i915_gem_context *ctx) |
| 2861 | { |
| 2862 | unsigned int score; |
| 2863 | unsigned long prev_hang; |
| 2864 | |
| 2865 | if (i915_gem_context_is_banned(ctx)) |
| 2866 | score = I915_CLIENT_SCORE_CONTEXT_BAN; |
| 2867 | else |
| 2868 | score = 0; |
| 2869 | |
| 2870 | prev_hang = xchg(&file_priv->hang_timestamp, jiffies); |
| 2871 | if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES)) |
| 2872 | score += I915_CLIENT_SCORE_HANG_FAST; |
| 2873 | |
| 2874 | if (score) { |
| 2875 | atomic_add(score, &file_priv->ban_score); |
| 2876 | |
| 2877 | DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n", |
| 2878 | ctx->name, score, |
| 2879 | atomic_read(&file_priv->ban_score)); |
| 2880 | } |
| 2881 | } |
| 2882 | |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2883 | static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2884 | { |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2885 | unsigned int score; |
| 2886 | bool banned, bannable; |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2887 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2888 | atomic_inc(&ctx->guilty_count); |
| 2889 | |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2890 | bannable = i915_gem_context_is_bannable(ctx); |
| 2891 | score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score); |
| 2892 | banned = score >= CONTEXT_SCORE_BAN_THRESHOLD; |
Chris Wilson | 24eae08 | 2018-02-05 09:22:01 +0000 | [diff] [blame] | 2893 | |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2894 | /* Cool contexts don't accumulate client ban score */ |
| 2895 | if (!bannable) |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2896 | return; |
| 2897 | |
Chris Wilson | bcc2661 | 2018-06-18 08:31:35 +0100 | [diff] [blame] | 2898 | if (banned) { |
| 2899 | DRM_DEBUG_DRIVER("context %s: guilty %d, score %u, banned\n", |
| 2900 | ctx->name, atomic_read(&ctx->guilty_count), |
| 2901 | score); |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2902 | i915_gem_context_set_banned(ctx); |
Chris Wilson | bcc2661 | 2018-06-18 08:31:35 +0100 | [diff] [blame] | 2903 | } |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2904 | |
| 2905 | if (!IS_ERR_OR_NULL(ctx->file_priv)) |
| 2906 | i915_gem_client_mark_guilty(ctx->file_priv, ctx); |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) |
| 2910 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2911 | atomic_inc(&ctx->active_count); |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2912 | } |
| 2913 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2914 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2915 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2916 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2917 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2918 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2919 | |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2920 | /* |
| 2921 | * We are called by the error capture, reset and to dump engine |
| 2922 | * state at random points in time. In particular, note that neither is |
| 2923 | * crucially ordered with an interrupt. After a hang, the GPU is dead |
| 2924 | * and we assume that no more writes can happen (we waited long enough |
| 2925 | * for all writes that were in transaction to be flushed) - adding an |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2926 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2927 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2928 | * At all other times, we must assume the GPU is still running, but |
| 2929 | * we only care about the snapshot of this moment. |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2930 | */ |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2931 | spin_lock_irqsave(&engine->timeline.lock, flags); |
| 2932 | list_for_each_entry(request, &engine->timeline.requests, link) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2933 | if (__i915_request_completed(request, request->global_seqno)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2934 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2935 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2936 | active = request; |
| 2937 | break; |
| 2938 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2939 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2940 | |
| 2941 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2942 | } |
| 2943 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2944 | /* |
| 2945 | * Ensure irq handler finishes, and not run again. |
| 2946 | * Also return the active request so that we only search for it once. |
| 2947 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2948 | struct i915_request * |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2949 | i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) |
| 2950 | { |
Chris Wilson | 5adfb77 | 2018-05-16 19:33:51 +0100 | [diff] [blame] | 2951 | struct i915_request *request; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2952 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 2953 | /* |
| 2954 | * During the reset sequence, we must prevent the engine from |
| 2955 | * entering RC6. As the context state is undefined until we restart |
| 2956 | * the engine, if it does enter RC6 during the reset, the state |
| 2957 | * written to the powercontext is undefined and so we may lose |
| 2958 | * GPU state upon resume, i.e. fail to restart after a reset. |
| 2959 | */ |
| 2960 | intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); |
| 2961 | |
Chris Wilson | 5adfb77 | 2018-05-16 19:33:51 +0100 | [diff] [blame] | 2962 | request = engine->reset.prepare(engine); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 2963 | if (request && request->fence.error == -EIO) |
| 2964 | request = ERR_PTR(-EIO); /* Previous reset failed! */ |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2965 | |
| 2966 | return request; |
| 2967 | } |
| 2968 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2969 | int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2970 | { |
| 2971 | struct intel_engine_cs *engine; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2972 | struct i915_request *request; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2973 | enum intel_engine_id id; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2974 | int err = 0; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2975 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2976 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2977 | request = i915_gem_reset_prepare_engine(engine); |
| 2978 | if (IS_ERR(request)) { |
| 2979 | err = PTR_ERR(request); |
| 2980 | continue; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2981 | } |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 2982 | |
| 2983 | engine->hangcheck.active_request = request; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2984 | } |
| 2985 | |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2986 | i915_gem_revoke_fences(dev_priv); |
Michal Wajdeczko | c37d572 | 2018-03-12 13:03:07 +0000 | [diff] [blame] | 2987 | intel_uc_sanitize(dev_priv); |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2988 | |
| 2989 | return err; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2990 | } |
| 2991 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2992 | static void engine_skip_context(struct i915_request *request) |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2993 | { |
| 2994 | struct intel_engine_cs *engine = request->engine; |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 2995 | struct i915_gem_context *hung_ctx = request->gem_context; |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2996 | struct i915_timeline *timeline = request->timeline; |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2997 | unsigned long flags; |
| 2998 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2999 | GEM_BUG_ON(timeline == &engine->timeline); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3000 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3001 | spin_lock_irqsave(&engine->timeline.lock, flags); |
Chris Wilson | 890fd18 | 2018-07-06 22:07:10 +0100 | [diff] [blame] | 3002 | spin_lock(&timeline->lock); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3003 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3004 | list_for_each_entry_continue(request, &engine->timeline.requests, link) |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3005 | if (request->gem_context == hung_ctx) |
Chris Wilson | 6dd7526 | 2018-07-06 11:39:43 +0100 | [diff] [blame] | 3006 | i915_request_skip(request, -EIO); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3007 | |
| 3008 | list_for_each_entry(request, &timeline->requests, link) |
Chris Wilson | 6dd7526 | 2018-07-06 11:39:43 +0100 | [diff] [blame] | 3009 | i915_request_skip(request, -EIO); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3010 | |
| 3011 | spin_unlock(&timeline->lock); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3012 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3013 | } |
| 3014 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3015 | /* Returns the request if it was guilty of the hang */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3016 | static struct i915_request * |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3017 | i915_gem_reset_request(struct intel_engine_cs *engine, |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3018 | struct i915_request *request, |
| 3019 | bool stalled) |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3020 | { |
Mika Kuoppala | 71895a0 | 2017-01-17 17:59:07 +0200 | [diff] [blame] | 3021 | /* The guilty request will get skipped on a hung engine. |
| 3022 | * |
| 3023 | * Users of client default contexts do not rely on logical |
| 3024 | * state preserved between batches so it is safe to execute |
| 3025 | * queued requests following the hang. Non default contexts |
| 3026 | * rely on preserved state, so skipping a batch loses the |
| 3027 | * evolution of the state and it needs to be considered corrupted. |
| 3028 | * Executing more queued batches on top of corrupted state is |
| 3029 | * risky. But we take the risk by trying to advance through |
| 3030 | * the queued requests in order to make the client behaviour |
| 3031 | * more predictable around resets, by not throwing away random |
| 3032 | * amount of batches it has prepared for execution. Sophisticated |
| 3033 | * clients can use gem_reset_stats_ioctl and dma fence status |
| 3034 | * (exported via sync_file info ioctl on explicit fences) to observe |
| 3035 | * when it loses the context state and should rebuild accordingly. |
| 3036 | * |
| 3037 | * The context ban, and ultimately the client ban, mechanism are safety |
| 3038 | * valves if client submission ends up resulting in nothing more than |
| 3039 | * subsequent hangs. |
| 3040 | */ |
| 3041 | |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3042 | if (i915_request_completed(request)) { |
| 3043 | GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n", |
| 3044 | engine->name, request->global_seqno, |
| 3045 | request->fence.context, request->fence.seqno, |
| 3046 | intel_engine_get_seqno(engine)); |
| 3047 | stalled = false; |
| 3048 | } |
| 3049 | |
| 3050 | if (stalled) { |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3051 | i915_gem_context_mark_guilty(request->gem_context); |
Chris Wilson | 6dd7526 | 2018-07-06 11:39:43 +0100 | [diff] [blame] | 3052 | i915_request_skip(request, -EIO); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3053 | |
| 3054 | /* If this context is now banned, skip all pending requests. */ |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3055 | if (i915_gem_context_is_banned(request->gem_context)) |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3056 | engine_skip_context(request); |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3057 | } else { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3058 | /* |
| 3059 | * Since this is not the hung engine, it may have advanced |
| 3060 | * since the hang declaration. Double check by refinding |
| 3061 | * the active request at the time of the reset. |
| 3062 | */ |
| 3063 | request = i915_gem_find_active_request(engine); |
| 3064 | if (request) { |
Chris Wilson | 042ed2d | 2018-06-15 10:31:36 +0100 | [diff] [blame] | 3065 | unsigned long flags; |
| 3066 | |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3067 | i915_gem_context_mark_innocent(request->gem_context); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3068 | dma_fence_set_error(&request->fence, -EAGAIN); |
| 3069 | |
| 3070 | /* Rewind the engine to replay the incomplete rq */ |
Chris Wilson | 042ed2d | 2018-06-15 10:31:36 +0100 | [diff] [blame] | 3071 | spin_lock_irqsave(&engine->timeline.lock, flags); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3072 | request = list_prev_entry(request, link); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3073 | if (&request->link == &engine->timeline.requests) |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3074 | request = NULL; |
Chris Wilson | 042ed2d | 2018-06-15 10:31:36 +0100 | [diff] [blame] | 3075 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3076 | } |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3077 | } |
| 3078 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3079 | return request; |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3080 | } |
| 3081 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3082 | void i915_gem_reset_engine(struct intel_engine_cs *engine, |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3083 | struct i915_request *request, |
| 3084 | bool stalled) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3085 | { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3086 | if (request) |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3087 | request = i915_gem_reset_request(engine, request, stalled); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3088 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3089 | /* Setup the CS to resume from the breadcrumb of the hung request */ |
Chris Wilson | 5adfb77 | 2018-05-16 19:33:51 +0100 | [diff] [blame] | 3090 | engine->reset.reset(engine, request); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3091 | } |
| 3092 | |
Chris Wilson | d0667e9 | 2018-04-06 23:03:54 +0100 | [diff] [blame] | 3093 | void i915_gem_reset(struct drm_i915_private *dev_priv, |
| 3094 | unsigned int stalled_mask) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3095 | { |
| 3096 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3097 | enum intel_engine_id id; |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3098 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3099 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 3100 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3101 | i915_retire_requests(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3102 | |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3103 | for_each_engine(engine, dev_priv, id) { |
Chris Wilson | 1fc44d9 | 2018-05-17 22:26:32 +0100 | [diff] [blame] | 3104 | struct intel_context *ce; |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3105 | |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3106 | i915_gem_reset_engine(engine, |
| 3107 | engine->hangcheck.active_request, |
Chris Wilson | d0667e9 | 2018-04-06 23:03:54 +0100 | [diff] [blame] | 3108 | stalled_mask & ENGINE_MASK(id)); |
Chris Wilson | 1fc44d9 | 2018-05-17 22:26:32 +0100 | [diff] [blame] | 3109 | ce = fetch_and_zero(&engine->last_retired_context); |
| 3110 | if (ce) |
| 3111 | intel_context_unpin(ce); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3112 | |
| 3113 | /* |
| 3114 | * Ostensibily, we always want a context loaded for powersaving, |
| 3115 | * so if the engine is idle after the reset, send a request |
| 3116 | * to load our scratch kernel_context. |
| 3117 | * |
| 3118 | * More mysteriously, if we leave the engine idle after a reset, |
| 3119 | * the next userspace batch may hang, with what appears to be |
| 3120 | * an incoherent read by the CS (presumably stale TLB). An |
| 3121 | * empty request appears sufficient to paper over the glitch. |
| 3122 | */ |
Chris Wilson | 01b8fdc | 2018-02-05 15:24:31 +0000 | [diff] [blame] | 3123 | if (intel_engine_is_idle(engine)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3124 | struct i915_request *rq; |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3125 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3126 | rq = i915_request_alloc(engine, |
| 3127 | dev_priv->kernel_context); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3128 | if (!IS_ERR(rq)) |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 3129 | i915_request_add(rq); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3130 | } |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3131 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3132 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 3133 | i915_gem_restore_fences(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3134 | } |
| 3135 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3136 | void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) |
| 3137 | { |
Chris Wilson | 5adfb77 | 2018-05-16 19:33:51 +0100 | [diff] [blame] | 3138 | engine->reset.finish(engine); |
| 3139 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3140 | intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3141 | } |
| 3142 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3143 | void i915_gem_reset_finish(struct drm_i915_private *dev_priv) |
| 3144 | { |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3145 | struct intel_engine_cs *engine; |
| 3146 | enum intel_engine_id id; |
| 3147 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3148 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3149 | |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3150 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3151 | engine->hangcheck.active_request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3152 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3153 | } |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3156 | static void nop_submit_request(struct i915_request *request) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3157 | { |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3158 | unsigned long flags; |
| 3159 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3160 | GEM_TRACE("%s fence %llx:%d -> -EIO\n", |
| 3161 | request->engine->name, |
| 3162 | request->fence.context, request->fence.seqno); |
Chris Wilson | 3cd9442 | 2017-01-10 17:22:45 +0000 | [diff] [blame] | 3163 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3164 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3165 | spin_lock_irqsave(&request->engine->timeline.lock, flags); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3166 | __i915_request_submit(request); |
Chris Wilson | 6faf591 | 2018-12-28 14:07:35 +0000 | [diff] [blame] | 3167 | intel_engine_write_global_seqno(request->engine, request->global_seqno); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3168 | spin_unlock_irqrestore(&request->engine->timeline.lock, flags); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3169 | } |
| 3170 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3171 | void i915_gem_set_wedged(struct drm_i915_private *i915) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3172 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 3173 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3174 | enum intel_engine_id id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3175 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3176 | GEM_TRACE("start\n"); |
| 3177 | |
Chris Wilson | 7f961d7 | 2018-04-26 11:32:19 +0100 | [diff] [blame] | 3178 | if (GEM_SHOW_DEBUG()) { |
Chris Wilson | 559e040 | 2018-02-05 09:21:59 +0000 | [diff] [blame] | 3179 | struct drm_printer p = drm_debug_printer(__func__); |
| 3180 | |
| 3181 | for_each_engine(engine, i915, id) |
| 3182 | intel_engine_dump(engine, &p, "%s\n", engine->name); |
| 3183 | } |
| 3184 | |
Chris Wilson | 3970c65 | 2018-07-23 15:53:35 +0100 | [diff] [blame] | 3185 | if (test_and_set_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3186 | goto out; |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3187 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3188 | /* |
| 3189 | * First, stop submission to hw, but do not yet complete requests by |
| 3190 | * rolling the global seqno forward (since this would complete requests |
| 3191 | * for which we haven't set the fence error to EIO yet). |
| 3192 | */ |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3193 | for_each_engine(engine, i915, id) |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3194 | i915_gem_reset_prepare_engine(engine); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3195 | |
Chris Wilson | ac697ae | 2018-03-15 15:10:15 +0000 | [diff] [blame] | 3196 | /* Even if the GPU reset fails, it should still stop the engines */ |
Chris Wilson | ec5b65a | 2018-07-26 09:50:33 +0100 | [diff] [blame] | 3197 | if (INTEL_GEN(i915) >= 5) |
| 3198 | intel_gpu_reset(i915, ALL_ENGINES); |
Chris Wilson | ac697ae | 2018-03-15 15:10:15 +0000 | [diff] [blame] | 3199 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3200 | for_each_engine(engine, i915, id) { |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3201 | engine->submit_request = nop_submit_request; |
| 3202 | engine->schedule = NULL; |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3203 | } |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3204 | i915->caps.scheduler = 0; |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3205 | |
| 3206 | /* |
| 3207 | * Make sure no request can slip through without getting completed by |
Chris Wilson | 6faf591 | 2018-12-28 14:07:35 +0000 | [diff] [blame] | 3208 | * either this call here to intel_engine_write_global_seqno, or the one |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3209 | * in nop_submit_request. |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3210 | */ |
| 3211 | synchronize_rcu(); |
| 3212 | |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3213 | /* Mark all executing requests as skipped */ |
| 3214 | for_each_engine(engine, i915, id) |
| 3215 | engine->cancel_requests(engine); |
| 3216 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3217 | for_each_engine(engine, i915, id) { |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3218 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3219 | intel_engine_wakeup(engine); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3220 | } |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3221 | |
Chris Wilson | 3970c65 | 2018-07-23 15:53:35 +0100 | [diff] [blame] | 3222 | out: |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3223 | GEM_TRACE("end\n"); |
| 3224 | |
Chris Wilson | 3d7adbb | 2017-07-21 13:32:27 +0100 | [diff] [blame] | 3225 | wake_up_all(&i915->gpu_error.reset_queue); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3226 | } |
| 3227 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3228 | bool i915_gem_unset_wedged(struct drm_i915_private *i915) |
| 3229 | { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3230 | struct i915_timeline *tl; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3231 | |
| 3232 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3233 | if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3234 | return true; |
| 3235 | |
Chris Wilson | 55c1551 | 2019-01-03 21:33:40 +0000 | [diff] [blame] | 3236 | if (!i915->gt.scratch) /* Never full initialised, recovery impossible */ |
| 3237 | return false; |
| 3238 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3239 | GEM_TRACE("start\n"); |
| 3240 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3241 | /* |
| 3242 | * Before unwedging, make sure that all pending operations |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3243 | * are flushed and errored out - we may have requests waiting upon |
| 3244 | * third party fences. We marked all inflight requests as EIO, and |
| 3245 | * every execbuf since returned EIO, for consistency we want all |
| 3246 | * the currently pending requests to also be marked as EIO, which |
| 3247 | * is done inside our nop_submit_request - and so we must wait. |
| 3248 | * |
| 3249 | * No more can be submitted until we reset the wedged bit. |
| 3250 | */ |
| 3251 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3252 | struct i915_request *rq; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3253 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3254 | rq = i915_gem_active_peek(&tl->last_request, |
| 3255 | &i915->drm.struct_mutex); |
| 3256 | if (!rq) |
| 3257 | continue; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3258 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3259 | /* |
| 3260 | * We can't use our normal waiter as we want to |
| 3261 | * avoid recursively trying to handle the current |
| 3262 | * reset. The basic dma_fence_default_wait() installs |
| 3263 | * a callback for dma_fence_signal(), which is |
| 3264 | * triggered by our nop handler (indirectly, the |
| 3265 | * callback enables the signaler thread which is |
| 3266 | * woken by the nop_submit_request() advancing the seqno |
| 3267 | * and when the seqno passes the fence, the signaler |
| 3268 | * then signals the fence waking us up). |
| 3269 | */ |
| 3270 | if (dma_fence_default_wait(&rq->fence, true, |
| 3271 | MAX_SCHEDULE_TIMEOUT) < 0) |
| 3272 | return false; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3273 | } |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3274 | i915_retire_requests(i915); |
| 3275 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3276 | |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 3277 | intel_engines_sanitize(i915, false); |
Chris Wilson | 8db601f | 2018-09-14 09:00:17 +0100 | [diff] [blame] | 3278 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3279 | /* |
| 3280 | * Undo nop_submit_request. We prevent all new i915 requests from |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3281 | * being queued (by disallowing execbuf whilst wedged) so having |
| 3282 | * waited for all active requests above, we know the system is idle |
| 3283 | * and do not have to worry about a thread being inside |
| 3284 | * engine->submit_request() as we swap over. So unlike installing |
| 3285 | * the nop_submit_request on reset, we can do this from normal |
| 3286 | * context and do not require stop_machine(). |
| 3287 | */ |
| 3288 | intel_engines_reset_default_submission(i915); |
Chris Wilson | 36703e7 | 2017-06-22 11:56:25 +0100 | [diff] [blame] | 3289 | i915_gem_contexts_lost(i915); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3290 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3291 | GEM_TRACE("end\n"); |
| 3292 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3293 | smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ |
| 3294 | clear_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3295 | |
| 3296 | return true; |
| 3297 | } |
| 3298 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 3299 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3300 | i915_gem_retire_work_handler(struct work_struct *work) |
| 3301 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3302 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3303 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 3304 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3305 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3306 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3307 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3308 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3309 | mutex_unlock(&dev->struct_mutex); |
| 3310 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3311 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3312 | /* |
| 3313 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3314 | * We do not need to do this test under locking as in the worst-case |
| 3315 | * we queue the retire worker once too often. |
| 3316 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3317 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3318 | queue_delayed_work(dev_priv->wq, |
| 3319 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 3320 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3321 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3322 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3323 | static void shrink_caches(struct drm_i915_private *i915) |
| 3324 | { |
| 3325 | /* |
| 3326 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 3327 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 3328 | * with the aim of reducing fragmentation. |
| 3329 | */ |
| 3330 | kmem_cache_shrink(i915->priorities); |
| 3331 | kmem_cache_shrink(i915->dependencies); |
| 3332 | kmem_cache_shrink(i915->requests); |
| 3333 | kmem_cache_shrink(i915->luts); |
| 3334 | kmem_cache_shrink(i915->vmas); |
| 3335 | kmem_cache_shrink(i915->objects); |
| 3336 | } |
| 3337 | |
| 3338 | struct sleep_rcu_work { |
| 3339 | union { |
| 3340 | struct rcu_head rcu; |
| 3341 | struct work_struct work; |
| 3342 | }; |
| 3343 | struct drm_i915_private *i915; |
| 3344 | unsigned int epoch; |
| 3345 | }; |
| 3346 | |
| 3347 | static inline bool |
| 3348 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 3349 | { |
| 3350 | /* |
| 3351 | * There is a small chance that the epoch wrapped since we started |
| 3352 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 3353 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 3354 | */ |
| 3355 | return epoch == READ_ONCE(i915->gt.epoch); |
| 3356 | } |
| 3357 | |
| 3358 | static void __sleep_work(struct work_struct *work) |
| 3359 | { |
| 3360 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 3361 | struct drm_i915_private *i915 = s->i915; |
| 3362 | unsigned int epoch = s->epoch; |
| 3363 | |
| 3364 | kfree(s); |
| 3365 | if (same_epoch(i915, epoch)) |
| 3366 | shrink_caches(i915); |
| 3367 | } |
| 3368 | |
| 3369 | static void __sleep_rcu(struct rcu_head *rcu) |
| 3370 | { |
| 3371 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 3372 | struct drm_i915_private *i915 = s->i915; |
| 3373 | |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3374 | destroy_rcu_head(&s->rcu); |
| 3375 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3376 | if (same_epoch(i915, s->epoch)) { |
| 3377 | INIT_WORK(&s->work, __sleep_work); |
| 3378 | queue_work(i915->wq, &s->work); |
| 3379 | } else { |
| 3380 | kfree(s); |
| 3381 | } |
| 3382 | } |
| 3383 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3384 | static inline bool |
| 3385 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 3386 | { |
| 3387 | return (READ_ONCE(i915->gt.active_requests) || |
| 3388 | work_pending(&i915->gt.idle_work.work)); |
| 3389 | } |
| 3390 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3391 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
| 3392 | { |
| 3393 | struct intel_engine_cs *engine; |
| 3394 | enum intel_engine_id id; |
| 3395 | |
| 3396 | if (i915_terminally_wedged(&i915->gpu_error)) |
| 3397 | return; |
| 3398 | |
| 3399 | GEM_BUG_ON(i915->gt.active_requests); |
| 3400 | for_each_engine(engine, i915, id) { |
| 3401 | GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request)); |
| 3402 | GEM_BUG_ON(engine->last_retired_context != |
| 3403 | to_intel_context(i915->kernel_context, engine)); |
| 3404 | } |
| 3405 | } |
| 3406 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3407 | static void |
| 3408 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3409 | { |
| 3410 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3411 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3412 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3413 | bool rearm_hangcheck; |
| 3414 | |
| 3415 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3416 | return; |
| 3417 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 3418 | if (READ_ONCE(dev_priv->gt.active_requests)) |
| 3419 | return; |
| 3420 | |
| 3421 | /* |
| 3422 | * Flush out the last user context, leaving only the pinned |
| 3423 | * kernel context resident. When we are idling on the kernel_context, |
| 3424 | * no more new requests (with a context switch) are emitted and we |
| 3425 | * can finally rest. A consequence is that the idle work handler is |
| 3426 | * always called at least twice before idling (and if the system is |
| 3427 | * idle that implies a round trip through the retire worker). |
| 3428 | */ |
| 3429 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 3430 | i915_gem_switch_to_kernel_context(dev_priv); |
| 3431 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 3432 | |
| 3433 | GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n", |
| 3434 | READ_ONCE(dev_priv->gt.active_requests)); |
| 3435 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3436 | /* |
| 3437 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3438 | * new request is submitted. As we don't trust the hardware, we |
| 3439 | * continue on if the wait times out. This is necessary to allow |
| 3440 | * the machine to suspend even if the hardware dies, and we will |
| 3441 | * try to recover in resume (after depriving the hardware of power, |
| 3442 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3443 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3444 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3445 | intel_engines_are_idle(dev_priv), |
| 3446 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3447 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3448 | |
| 3449 | rearm_hangcheck = |
| 3450 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3451 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3452 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3453 | /* Currently busy, come back later */ |
| 3454 | mod_delayed_work(dev_priv->wq, |
| 3455 | &dev_priv->gt.idle_work, |
| 3456 | msecs_to_jiffies(50)); |
| 3457 | goto out_rearm; |
| 3458 | } |
| 3459 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3460 | /* |
| 3461 | * New request retired after this work handler started, extend active |
| 3462 | * period until next instance of the work. |
| 3463 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3464 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3465 | goto out_unlock; |
| 3466 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 3467 | epoch = __i915_gem_park(dev_priv); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3468 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3469 | assert_kernel_context_is_current(dev_priv); |
| 3470 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3471 | rearm_hangcheck = false; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3472 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3473 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3474 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3475 | out_rearm: |
| 3476 | if (rearm_hangcheck) { |
| 3477 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3478 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3479 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3480 | |
| 3481 | /* |
| 3482 | * When we are idle, it is an opportune time to reap our caches. |
| 3483 | * However, we have many objects that utilise RCU and the ordered |
| 3484 | * i915->wq that this work is executing on. To try and flush any |
| 3485 | * pending frees now we are idle, we first wait for an RCU grace |
| 3486 | * period, and then queue a task (that will run last on the wq) to |
| 3487 | * shrink and re-optimize the caches. |
| 3488 | */ |
| 3489 | if (same_epoch(dev_priv, epoch)) { |
| 3490 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3491 | if (s) { |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3492 | init_rcu_head(&s->rcu); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3493 | s->i915 = dev_priv; |
| 3494 | s->epoch = epoch; |
| 3495 | call_rcu(&s->rcu, __sleep_rcu); |
| 3496 | } |
| 3497 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3498 | } |
| 3499 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3500 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3501 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3502 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3503 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3504 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3505 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3506 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3507 | mutex_lock(&i915->drm.struct_mutex); |
| 3508 | |
| 3509 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3510 | struct i915_gem_context *ctx = lut->ctx; |
| 3511 | struct i915_vma *vma; |
| 3512 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3513 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3514 | if (ctx->file_priv != fpriv) |
| 3515 | continue; |
| 3516 | |
| 3517 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3518 | GEM_BUG_ON(vma->obj != obj); |
| 3519 | |
| 3520 | /* We allow the process to have multiple handles to the same |
| 3521 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3522 | */ |
| 3523 | GEM_BUG_ON(!vma->open_count); |
| 3524 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3525 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3526 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3527 | list_del(&lut->obj_link); |
| 3528 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3529 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3530 | kmem_cache_free(i915->luts, lut); |
| 3531 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3532 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3533 | |
| 3534 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3535 | } |
| 3536 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3537 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3538 | { |
| 3539 | if (timeout_ns < 0) |
| 3540 | return MAX_SCHEDULE_TIMEOUT; |
| 3541 | |
| 3542 | if (timeout_ns == 0) |
| 3543 | return 0; |
| 3544 | |
| 3545 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3546 | } |
| 3547 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3548 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3549 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3550 | * @dev: drm device pointer |
| 3551 | * @data: ioctl data blob |
| 3552 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3553 | * |
| 3554 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3555 | * the timeout parameter. |
| 3556 | * -ETIME: object is still busy after timeout |
| 3557 | * -ERESTARTSYS: signal interrupted the wait |
| 3558 | * -ENONENT: object doesn't exist |
| 3559 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3560 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3561 | * -ENOMEM: damn |
| 3562 | * -ENODEV: Internal IRQ fail |
| 3563 | * -E?: The add request failed |
| 3564 | * |
| 3565 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3566 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3567 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3568 | * without holding struct_mutex the object may become re-busied before this |
| 3569 | * function completes. A similar but shorter * race condition exists in the busy |
| 3570 | * ioctl |
| 3571 | */ |
| 3572 | int |
| 3573 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3574 | { |
| 3575 | struct drm_i915_gem_wait *args = data; |
| 3576 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3577 | ktime_t start; |
| 3578 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3579 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3580 | if (args->flags != 0) |
| 3581 | return -EINVAL; |
| 3582 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3583 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3584 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3585 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3586 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3587 | start = ktime_get(); |
| 3588 | |
| 3589 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 3590 | I915_WAIT_INTERRUPTIBLE | |
| 3591 | I915_WAIT_PRIORITY | |
| 3592 | I915_WAIT_ALL, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3593 | to_wait_timeout(args->timeout_ns), |
| 3594 | to_rps_client(file)); |
| 3595 | |
| 3596 | if (args->timeout_ns > 0) { |
| 3597 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3598 | if (args->timeout_ns < 0) |
| 3599 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3600 | |
| 3601 | /* |
| 3602 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3603 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3604 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3605 | * |
| 3606 | * This is a regression from the timespec->ktime conversion. |
| 3607 | */ |
| 3608 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3609 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3610 | |
| 3611 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3612 | if (ret == -ETIME && args->timeout_ns) |
| 3613 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3614 | } |
| 3615 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3616 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3617 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3618 | } |
| 3619 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3620 | static long wait_for_timeline(struct i915_timeline *tl, |
| 3621 | unsigned int flags, long timeout) |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3622 | { |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3623 | struct i915_request *rq; |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3624 | |
| 3625 | rq = i915_gem_active_get_unlocked(&tl->last_request); |
| 3626 | if (!rq) |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3627 | return timeout; |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3628 | |
| 3629 | /* |
| 3630 | * "Race-to-idle". |
| 3631 | * |
| 3632 | * Switching to the kernel context is often used a synchronous |
| 3633 | * step prior to idling, e.g. in suspend for flushing all |
| 3634 | * current operations to memory before sleeping. These we |
| 3635 | * want to complete as quickly as possible to avoid prolonged |
| 3636 | * stalls, so allow the gpu to boost to maximum clocks. |
| 3637 | */ |
| 3638 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
| 3639 | gen6_rps_boost(rq, NULL); |
| 3640 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3641 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3642 | i915_request_put(rq); |
| 3643 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3644 | return timeout; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3645 | } |
| 3646 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3647 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3648 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3649 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3650 | dev_err(i915->drm.dev, |
| 3651 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3652 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3653 | i915_gem_set_wedged(i915); |
| 3654 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | return 0; |
| 3658 | } |
| 3659 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3660 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 3661 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3662 | { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3663 | GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", |
| 3664 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
| 3665 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3666 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3667 | /* If the device is asleep, we have no requests outstanding */ |
| 3668 | if (!READ_ONCE(i915->gt.awake)) |
| 3669 | return 0; |
| 3670 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3671 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3672 | struct i915_timeline *tl; |
| 3673 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3674 | |
| 3675 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3676 | |
| 3677 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3678 | timeout = wait_for_timeline(tl, flags, timeout); |
| 3679 | if (timeout < 0) |
| 3680 | return timeout; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3681 | } |
Chris Wilson | c1e63f6 | 2018-08-08 11:50:59 +0100 | [diff] [blame] | 3682 | if (GEM_SHOW_DEBUG() && !timeout) { |
| 3683 | /* Presume that timeout was non-zero to begin with! */ |
| 3684 | dev_warn(&i915->drm.pdev->dev, |
| 3685 | "Missed idle-completion interrupt!\n"); |
| 3686 | GEM_TRACE_DUMP(); |
| 3687 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3688 | |
| 3689 | err = wait_for_engines(i915); |
| 3690 | if (err) |
| 3691 | return err; |
| 3692 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3693 | i915_retire_requests(i915); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3694 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3695 | } else { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3696 | struct intel_engine_cs *engine; |
| 3697 | enum intel_engine_id id; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3698 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3699 | for_each_engine(engine, i915, id) { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3700 | struct i915_timeline *tl = &engine->timeline; |
| 3701 | |
| 3702 | timeout = wait_for_timeline(tl, flags, timeout); |
| 3703 | if (timeout < 0) |
| 3704 | return timeout; |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3705 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3706 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3707 | |
| 3708 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3709 | } |
| 3710 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3711 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3712 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3713 | /* |
| 3714 | * We manually flush the CPU domain so that we can override and |
| 3715 | * force the flush for the display, and perform it asyncrhonously. |
| 3716 | */ |
| 3717 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3718 | if (obj->cache_dirty) |
| 3719 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3720 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3724 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3725 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3726 | return; |
| 3727 | |
| 3728 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3729 | __i915_gem_object_flush_for_display(obj); |
| 3730 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3731 | } |
| 3732 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3733 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3734 | * Moves a single object to the WC read, and possibly write domain. |
| 3735 | * @obj: object to act on |
| 3736 | * @write: ask for write access or read only |
| 3737 | * |
| 3738 | * This function returns when the move is complete, including waiting on |
| 3739 | * flushes to occur. |
| 3740 | */ |
| 3741 | int |
| 3742 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3743 | { |
| 3744 | int ret; |
| 3745 | |
| 3746 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3747 | |
| 3748 | ret = i915_gem_object_wait(obj, |
| 3749 | I915_WAIT_INTERRUPTIBLE | |
| 3750 | I915_WAIT_LOCKED | |
| 3751 | (write ? I915_WAIT_ALL : 0), |
| 3752 | MAX_SCHEDULE_TIMEOUT, |
| 3753 | NULL); |
| 3754 | if (ret) |
| 3755 | return ret; |
| 3756 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3757 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3758 | return 0; |
| 3759 | |
| 3760 | /* Flush and acquire obj->pages so that we are coherent through |
| 3761 | * direct access in memory with previous cached writes through |
| 3762 | * shmemfs and that our cache domain tracking remains valid. |
| 3763 | * For example, if the obj->filp was moved to swap without us |
| 3764 | * being notified and releasing the pages, we would mistakenly |
| 3765 | * continue to assume that the obj remained out of the CPU cached |
| 3766 | * domain. |
| 3767 | */ |
| 3768 | ret = i915_gem_object_pin_pages(obj); |
| 3769 | if (ret) |
| 3770 | return ret; |
| 3771 | |
| 3772 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3773 | |
| 3774 | /* Serialise direct access to this object with the barriers for |
| 3775 | * coherent writes from the GPU, by effectively invalidating the |
| 3776 | * WC domain upon first access. |
| 3777 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3778 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3779 | mb(); |
| 3780 | |
| 3781 | /* It should now be out of any other write domains, and we can update |
| 3782 | * the domain values for our changes. |
| 3783 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3784 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3785 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3786 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3787 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3788 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3789 | obj->mm.dirty = true; |
| 3790 | } |
| 3791 | |
| 3792 | i915_gem_object_unpin_pages(obj); |
| 3793 | return 0; |
| 3794 | } |
| 3795 | |
| 3796 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3797 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3798 | * @obj: object to act on |
| 3799 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3800 | * |
| 3801 | * This function returns when the move is complete, including waiting on |
| 3802 | * flushes to occur. |
| 3803 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3804 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3805 | 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] | 3806 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3807 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3808 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3809 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3810 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3811 | ret = i915_gem_object_wait(obj, |
| 3812 | I915_WAIT_INTERRUPTIBLE | |
| 3813 | I915_WAIT_LOCKED | |
| 3814 | (write ? I915_WAIT_ALL : 0), |
| 3815 | MAX_SCHEDULE_TIMEOUT, |
| 3816 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3817 | if (ret) |
| 3818 | return ret; |
| 3819 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3820 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3821 | return 0; |
| 3822 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3823 | /* Flush and acquire obj->pages so that we are coherent through |
| 3824 | * direct access in memory with previous cached writes through |
| 3825 | * shmemfs and that our cache domain tracking remains valid. |
| 3826 | * For example, if the obj->filp was moved to swap without us |
| 3827 | * being notified and releasing the pages, we would mistakenly |
| 3828 | * continue to assume that the obj remained out of the CPU cached |
| 3829 | * domain. |
| 3830 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3831 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3832 | if (ret) |
| 3833 | return ret; |
| 3834 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3835 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3836 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3837 | /* Serialise direct access to this object with the barriers for |
| 3838 | * coherent writes from the GPU, by effectively invalidating the |
| 3839 | * GTT domain upon first access. |
| 3840 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3841 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3842 | mb(); |
| 3843 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3844 | /* It should now be out of any other write domains, and we can update |
| 3845 | * the domain values for our changes. |
| 3846 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3847 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3848 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3849 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3850 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3851 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3852 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3853 | } |
| 3854 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3855 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3856 | return 0; |
| 3857 | } |
| 3858 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3859 | /** |
| 3860 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3861 | * @obj: object to act on |
| 3862 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3863 | * |
| 3864 | * After this function returns, the object will be in the new cache-level |
| 3865 | * across all GTT and the contents of the backing storage will be coherent, |
| 3866 | * with respect to the new cache-level. In order to keep the backing storage |
| 3867 | * coherent for all users, we only allow a single cache level to be set |
| 3868 | * globally on the object and prevent it from being changed whilst the |
| 3869 | * hardware is reading from the object. That is if the object is currently |
| 3870 | * on the scanout it will be set to uncached (or equivalent display |
| 3871 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3872 | * that all direct access to the scanout remains coherent. |
| 3873 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3874 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3875 | enum i915_cache_level cache_level) |
| 3876 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3877 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3878 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3879 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3880 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3881 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3882 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3883 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3884 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3885 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3886 | * be invalid given the new cache-level. This is principally to |
| 3887 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3888 | * reading an invalid PTE on older architectures. |
| 3889 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3890 | restart: |
| 3891 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3892 | if (!drm_mm_node_allocated(&vma->node)) |
| 3893 | continue; |
| 3894 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3895 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3896 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3897 | return -EBUSY; |
| 3898 | } |
| 3899 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3900 | if (!i915_vma_is_closed(vma) && |
| 3901 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3902 | continue; |
| 3903 | |
| 3904 | ret = i915_vma_unbind(vma); |
| 3905 | if (ret) |
| 3906 | return ret; |
| 3907 | |
| 3908 | /* As unbinding may affect other elements in the |
| 3909 | * obj->vma_list (due to side-effects from retiring |
| 3910 | * an active vma), play safe and restart the iterator. |
| 3911 | */ |
| 3912 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3913 | } |
| 3914 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3915 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3916 | * cache-level on the PTE. We could simply unbind them all and |
| 3917 | * rebind with the correct cache-level on next use. However since |
| 3918 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3919 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3920 | * state and so involves less work. |
| 3921 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3922 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3923 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3924 | * If we wait upon the object, we know that all the bound |
| 3925 | * VMA are no longer active. |
| 3926 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3927 | ret = i915_gem_object_wait(obj, |
| 3928 | I915_WAIT_INTERRUPTIBLE | |
| 3929 | I915_WAIT_LOCKED | |
| 3930 | I915_WAIT_ALL, |
| 3931 | MAX_SCHEDULE_TIMEOUT, |
| 3932 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3933 | if (ret) |
| 3934 | return ret; |
| 3935 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3936 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3937 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3938 | /* Access to snoopable pages through the GTT is |
| 3939 | * incoherent and on some machines causes a hard |
| 3940 | * lockup. Relinquish the CPU mmaping to force |
| 3941 | * userspace to refault in the pages and we can |
| 3942 | * then double check if the GTT mapping is still |
| 3943 | * valid for that pointer access. |
| 3944 | */ |
| 3945 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3946 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3947 | /* As we no longer need a fence for GTT access, |
| 3948 | * we can relinquish it now (and so prevent having |
| 3949 | * to steal a fence from someone else on the next |
| 3950 | * fence request). Note GPU activity would have |
| 3951 | * dropped the fence as all snoopable access is |
| 3952 | * supposed to be linear. |
| 3953 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3954 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3955 | ret = i915_vma_put_fence(vma); |
| 3956 | if (ret) |
| 3957 | return ret; |
| 3958 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3959 | } else { |
| 3960 | /* We either have incoherent backing store and |
| 3961 | * so no GTT access or the architecture is fully |
| 3962 | * coherent. In such cases, existing GTT mmaps |
| 3963 | * ignore the cache bit in the PTE and we can |
| 3964 | * rewrite it without confusing the GPU or having |
| 3965 | * to force userspace to fault back in its mmaps. |
| 3966 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3967 | } |
| 3968 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3969 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3970 | if (!drm_mm_node_allocated(&vma->node)) |
| 3971 | continue; |
| 3972 | |
| 3973 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3974 | if (ret) |
| 3975 | return ret; |
| 3976 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3977 | } |
| 3978 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3979 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3980 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3981 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3982 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3983 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3984 | return 0; |
| 3985 | } |
| 3986 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3987 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3988 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3989 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3990 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3991 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3992 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3993 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3994 | rcu_read_lock(); |
| 3995 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3996 | if (!obj) { |
| 3997 | err = -ENOENT; |
| 3998 | goto out; |
| 3999 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4000 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4001 | switch (obj->cache_level) { |
| 4002 | case I915_CACHE_LLC: |
| 4003 | case I915_CACHE_L3_LLC: |
| 4004 | args->caching = I915_CACHING_CACHED; |
| 4005 | break; |
| 4006 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4007 | case I915_CACHE_WT: |
| 4008 | args->caching = I915_CACHING_DISPLAY; |
| 4009 | break; |
| 4010 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4011 | default: |
| 4012 | args->caching = I915_CACHING_NONE; |
| 4013 | break; |
| 4014 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4015 | out: |
| 4016 | rcu_read_unlock(); |
| 4017 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4018 | } |
| 4019 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4020 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 4021 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4022 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4023 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4024 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4025 | struct drm_i915_gem_object *obj; |
| 4026 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4027 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4028 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4029 | switch (args->caching) { |
| 4030 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4031 | level = I915_CACHE_NONE; |
| 4032 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4033 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4034 | /* |
| 4035 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 4036 | * snooped mapping may leave stale data in a corresponding CPU |
| 4037 | * cacheline, whereas normally such cachelines would get |
| 4038 | * invalidated. |
| 4039 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4040 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4041 | return -ENODEV; |
| 4042 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4043 | level = I915_CACHE_LLC; |
| 4044 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4045 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4046 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4047 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4048 | default: |
| 4049 | return -EINVAL; |
| 4050 | } |
| 4051 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4052 | obj = i915_gem_object_lookup(file, args->handle); |
| 4053 | if (!obj) |
| 4054 | return -ENOENT; |
| 4055 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 4056 | /* |
| 4057 | * The caching mode of proxy object is handled by its generator, and |
| 4058 | * not allowed to be changed by userspace. |
| 4059 | */ |
| 4060 | if (i915_gem_object_is_proxy(obj)) { |
| 4061 | ret = -ENXIO; |
| 4062 | goto out; |
| 4063 | } |
| 4064 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4065 | if (obj->cache_level == level) |
| 4066 | goto out; |
| 4067 | |
| 4068 | ret = i915_gem_object_wait(obj, |
| 4069 | I915_WAIT_INTERRUPTIBLE, |
| 4070 | MAX_SCHEDULE_TIMEOUT, |
| 4071 | to_rps_client(file)); |
| 4072 | if (ret) |
| 4073 | goto out; |
| 4074 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 4075 | ret = i915_mutex_lock_interruptible(dev); |
| 4076 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4077 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4078 | |
| 4079 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4080 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4081 | |
| 4082 | out: |
| 4083 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4084 | return ret; |
| 4085 | } |
| 4086 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4087 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 4088 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 4089 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 4090 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 4091 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4092 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4093 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4094 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 4095 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4096 | const struct i915_ggtt_view *view, |
| 4097 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4098 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4099 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4100 | int ret; |
| 4101 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4102 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4103 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4104 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4105 | * display coherency whilst setting up the cache domains. |
| 4106 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4107 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4108 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4109 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 4110 | * a result, we make sure that the pinning that is about to occur is |
| 4111 | * done with uncached PTEs. This is lowest common denominator for all |
| 4112 | * chipsets. |
| 4113 | * |
| 4114 | * However for gen6+, we could do better by using the GFDT bit instead |
| 4115 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 4116 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 4117 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4118 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 4119 | HAS_WT(to_i915(obj->base.dev)) ? |
| 4120 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4121 | if (ret) { |
| 4122 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4123 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4124 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4125 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4126 | /* As the user may map the buffer once pinned in the display plane |
| 4127 | * (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] | 4128 | * always use map_and_fenceable for all scanout buffers. However, |
| 4129 | * it may simply be too big to fit into mappable, in which case |
| 4130 | * put it anyway and hope that userspace can cope (but always first |
| 4131 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4132 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4133 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4134 | if ((flags & PIN_MAPPABLE) == 0 && |
| 4135 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4136 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4137 | flags | |
| 4138 | PIN_MAPPABLE | |
| 4139 | PIN_NONBLOCK); |
| 4140 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 4141 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4142 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4143 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4144 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 4145 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 4146 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 4147 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 4148 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4149 | /* It should now be out of any other write domains, and we can update |
| 4150 | * the domain values for our changes. |
| 4151 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4152 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4153 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4154 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4155 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4156 | err_unpin_global: |
| 4157 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4158 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4162 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4163 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 4164 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4165 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4166 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 4167 | return; |
| 4168 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4169 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 4170 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 4171 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4172 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 4173 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4174 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4175 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4176 | } |
| 4177 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4178 | /** |
| 4179 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 4180 | * @obj: object to act on |
| 4181 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4182 | * |
| 4183 | * This function returns when the move is complete, including waiting on |
| 4184 | * flushes to occur. |
| 4185 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 4186 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 4187 | 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] | 4188 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4189 | int ret; |
| 4190 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4191 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4192 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4193 | ret = i915_gem_object_wait(obj, |
| 4194 | I915_WAIT_INTERRUPTIBLE | |
| 4195 | I915_WAIT_LOCKED | |
| 4196 | (write ? I915_WAIT_ALL : 0), |
| 4197 | MAX_SCHEDULE_TIMEOUT, |
| 4198 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 4199 | if (ret) |
| 4200 | return ret; |
| 4201 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 4202 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4203 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4204 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4205 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4206 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4207 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | /* It should now be out of any other write domains, and we can update |
| 4211 | * the domain values for our changes. |
| 4212 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4213 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4214 | |
| 4215 | /* If we're writing through the CPU, then the GPU read domains will |
| 4216 | * need to be invalidated at next use. |
| 4217 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4218 | if (write) |
| 4219 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 4220 | |
| 4221 | return 0; |
| 4222 | } |
| 4223 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4224 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 4225 | * emitted over 20 msec ago. |
| 4226 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4227 | * Note that if we were to use the current jiffies each time around the loop, |
| 4228 | * we wouldn't escape the function with any frames outstanding if the time to |
| 4229 | * render a frame was over 20ms. |
| 4230 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4231 | * This should get us reasonable parallelism between CPU and GPU but also |
| 4232 | * relatively low latency when blocking on a particular request to finish. |
| 4233 | */ |
| 4234 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4235 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4236 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4237 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4238 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 4239 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4240 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4241 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4242 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 4243 | /* ABI: return -EIO if already wedged */ |
| 4244 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 4245 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 4246 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4247 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4248 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4249 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 4250 | break; |
| 4251 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4252 | if (target) { |
| 4253 | list_del(&target->client_link); |
| 4254 | target->file_priv = NULL; |
| 4255 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 4256 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4257 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4258 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4259 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4260 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4261 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4262 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4263 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4264 | return 0; |
| 4265 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4266 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4267 | I915_WAIT_INTERRUPTIBLE, |
| 4268 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4269 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4270 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4271 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4274 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4275 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 4276 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 4277 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 4278 | u64 alignment, |
| 4279 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4280 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4281 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 4282 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4283 | struct i915_vma *vma; |
| 4284 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 4285 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4286 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4287 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 4288 | if (flags & PIN_MAPPABLE && |
| 4289 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4290 | /* If the required space is larger than the available |
| 4291 | * aperture, we will not able to find a slot for the |
| 4292 | * object and unbinding the object now will be in |
| 4293 | * vain. Worse, doing so may cause us to ping-pong |
| 4294 | * the object in and out of the Global GTT and |
| 4295 | * waste a lot of cycles under the mutex. |
| 4296 | */ |
| 4297 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 4298 | return ERR_PTR(-E2BIG); |
| 4299 | |
| 4300 | /* If NONBLOCK is set the caller is optimistically |
| 4301 | * trying to cache the full object within the mappable |
| 4302 | * aperture, and *must* have a fallback in place for |
| 4303 | * situations where we cannot bind the object. We |
| 4304 | * can be a little more lax here and use the fallback |
| 4305 | * more often to avoid costly migrations of ourselves |
| 4306 | * and other objects within the aperture. |
| 4307 | * |
| 4308 | * Half-the-aperture is used as a simple heuristic. |
| 4309 | * More interesting would to do search for a free |
| 4310 | * block prior to making the commitment to unbind. |
| 4311 | * That caters for the self-harm case, and with a |
| 4312 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 4313 | * we could try to minimise harm to others. |
| 4314 | */ |
| 4315 | if (flags & PIN_NONBLOCK && |
| 4316 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 4317 | return ERR_PTR(-ENOSPC); |
| 4318 | } |
| 4319 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 4320 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 4321 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4322 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4323 | |
| 4324 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4325 | if (flags & PIN_NONBLOCK) { |
| 4326 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 4327 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4328 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4329 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 4330 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4331 | return ERR_PTR(-ENOSPC); |
| 4332 | } |
| 4333 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4334 | WARN(i915_vma_is_pinned(vma), |
| 4335 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4336 | " offset=%08x, req.alignment=%llx," |
| 4337 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 4338 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4339 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4340 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4341 | ret = i915_vma_unbind(vma); |
| 4342 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4343 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4344 | } |
| 4345 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4346 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 4347 | if (ret) |
| 4348 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4349 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4350 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4351 | } |
| 4352 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4353 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4354 | { |
| 4355 | /* Note that we could alias engines in the execbuf API, but |
| 4356 | * that would be very unwise as it prevents userspace from |
| 4357 | * fine control over engine selection. Ahem. |
| 4358 | * |
| 4359 | * This should be something like EXEC_MAX_ENGINE instead of |
| 4360 | * I915_NUM_ENGINES. |
| 4361 | */ |
| 4362 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 4363 | return 0x10000 << id; |
| 4364 | } |
| 4365 | |
| 4366 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 4367 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 4368 | /* The uABI guarantees an active writer is also amongst the read |
| 4369 | * engines. This would be true if we accessed the activity tracking |
| 4370 | * under the lock, but as we perform the lookup of the object and |
| 4371 | * its activity locklessly we can not guarantee that the last_write |
| 4372 | * being active implies that we have set the same engine flag from |
| 4373 | * last_read - hence we always set both read and write busy for |
| 4374 | * last_write. |
| 4375 | */ |
| 4376 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4379 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4380 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4381 | unsigned int (*flag)(unsigned int id)) |
| 4382 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4383 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4384 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4385 | /* We have to check the current hw status of the fence as the uABI |
| 4386 | * guarantees forward progress. We could rely on the idle worker |
| 4387 | * to eventually flush us, but to minimise latency just ask the |
| 4388 | * hardware. |
| 4389 | * |
| 4390 | * Note we only report on the status of native fences. |
| 4391 | */ |
| 4392 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4393 | return 0; |
| 4394 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4395 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4396 | rq = container_of(fence, struct i915_request, fence); |
| 4397 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4398 | return 0; |
| 4399 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4400 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4401 | } |
| 4402 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4403 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4404 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4405 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4406 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4407 | } |
| 4408 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4409 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4410 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4411 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4412 | if (!fence) |
| 4413 | return 0; |
| 4414 | |
| 4415 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4418 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4419 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4420 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4421 | { |
| 4422 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4423 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4424 | struct reservation_object_list *list; |
| 4425 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4426 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4427 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4428 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4429 | rcu_read_lock(); |
| 4430 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4431 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4432 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4433 | |
| 4434 | /* A discrepancy here is that we do not report the status of |
| 4435 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4436 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4437 | * This also means that wait-ioctl may report an object as busy, |
| 4438 | * where busy-ioctl considers it idle. |
| 4439 | * |
| 4440 | * We trade the ability to warn of foreign fences to report on which |
| 4441 | * i915 engines are active for the object. |
| 4442 | * |
| 4443 | * Alternatively, we can trade that extra information on read/write |
| 4444 | * activity with |
| 4445 | * args->busy = |
| 4446 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4447 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4448 | * |
| 4449 | */ |
| 4450 | retry: |
| 4451 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4452 | |
| 4453 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4454 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4455 | |
| 4456 | /* Translate shared fences to READ set of engines */ |
| 4457 | list = rcu_dereference(obj->resv->fence); |
| 4458 | if (list) { |
| 4459 | unsigned int shared_count = list->shared_count, i; |
| 4460 | |
| 4461 | for (i = 0; i < shared_count; ++i) { |
| 4462 | struct dma_fence *fence = |
| 4463 | rcu_dereference(list->shared[i]); |
| 4464 | |
| 4465 | args->busy |= busy_check_reader(fence); |
| 4466 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4467 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4468 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4469 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4470 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4471 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4472 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4473 | out: |
| 4474 | rcu_read_unlock(); |
| 4475 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | int |
| 4479 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4480 | struct drm_file *file_priv) |
| 4481 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4482 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4483 | } |
| 4484 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4485 | int |
| 4486 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4487 | struct drm_file *file_priv) |
| 4488 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4489 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4490 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4491 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4492 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4493 | |
| 4494 | switch (args->madv) { |
| 4495 | case I915_MADV_DONTNEED: |
| 4496 | case I915_MADV_WILLNEED: |
| 4497 | break; |
| 4498 | default: |
| 4499 | return -EINVAL; |
| 4500 | } |
| 4501 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4502 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4503 | if (!obj) |
| 4504 | return -ENOENT; |
| 4505 | |
| 4506 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4507 | if (err) |
| 4508 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4509 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4510 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4511 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4512 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4513 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4514 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4515 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4516 | obj->mm.quirked = false; |
| 4517 | } |
| 4518 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4519 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4520 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4521 | obj->mm.quirked = true; |
| 4522 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4523 | } |
| 4524 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4525 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4526 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4527 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4528 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4529 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4530 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4531 | i915_gem_object_truncate(obj); |
| 4532 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4533 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4534 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4535 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4536 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4537 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4538 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4539 | } |
| 4540 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4541 | static void |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4542 | frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4543 | { |
| 4544 | struct drm_i915_gem_object *obj = |
| 4545 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4546 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4547 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4550 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4551 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4552 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4553 | mutex_init(&obj->mm.lock); |
| 4554 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4555 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4556 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4557 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4558 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4559 | init_rcu_head(&obj->rcu); |
| 4560 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4561 | obj->ops = ops; |
| 4562 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4563 | reservation_object_init(&obj->__builtin_resv); |
| 4564 | obj->resv = &obj->__builtin_resv; |
| 4565 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4566 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4567 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4568 | |
| 4569 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4570 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4571 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4572 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4573 | 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] | 4574 | } |
| 4575 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4576 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4577 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4578 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4579 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4580 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4581 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4582 | |
| 4583 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4584 | }; |
| 4585 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4586 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4587 | struct drm_gem_object *obj, |
| 4588 | size_t size) |
| 4589 | { |
| 4590 | struct drm_i915_private *i915 = to_i915(dev); |
| 4591 | unsigned long flags = VM_NORESERVE; |
| 4592 | struct file *filp; |
| 4593 | |
| 4594 | drm_gem_private_object_init(dev, obj, size); |
| 4595 | |
| 4596 | if (i915->mm.gemfs) |
| 4597 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4598 | flags); |
| 4599 | else |
| 4600 | filp = shmem_file_setup("i915", size, flags); |
| 4601 | |
| 4602 | if (IS_ERR(filp)) |
| 4603 | return PTR_ERR(filp); |
| 4604 | |
| 4605 | obj->filp = filp; |
| 4606 | |
| 4607 | return 0; |
| 4608 | } |
| 4609 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4610 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4611 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4612 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4613 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4614 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4615 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4616 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4617 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4618 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4619 | /* There is a prevalence of the assumption that we fit the object's |
| 4620 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4621 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4622 | * such a local variable, please consider fixing! |
| 4623 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4624 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4625 | return ERR_PTR(-E2BIG); |
| 4626 | |
| 4627 | if (overflows_type(size, obj->base.size)) |
| 4628 | return ERR_PTR(-E2BIG); |
| 4629 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4630 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4631 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4632 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4633 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4634 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4635 | if (ret) |
| 4636 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4637 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4638 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4639 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4640 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4641 | mask &= ~__GFP_HIGHMEM; |
| 4642 | mask |= __GFP_DMA32; |
| 4643 | } |
| 4644 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4645 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4646 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4647 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4648 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4649 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4650 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4651 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4652 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4653 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4654 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4655 | /* 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] | 4656 | * cache) for about a 10% performance improvement |
| 4657 | * compared to uncached. Graphics requests other than |
| 4658 | * display scanout are coherent with the CPU in |
| 4659 | * accessing this cache. This means in this mode we |
| 4660 | * don't need to clflush on the CPU side, and on the |
| 4661 | * GPU side we only need to flush internal caches to |
| 4662 | * get data visible to the CPU. |
| 4663 | * |
| 4664 | * However, we maintain the display planes as UC, and so |
| 4665 | * need to rebind when first used as such. |
| 4666 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4667 | cache_level = I915_CACHE_LLC; |
| 4668 | else |
| 4669 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4670 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4671 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4672 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4673 | trace_i915_gem_object_create(obj); |
| 4674 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4675 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4676 | |
| 4677 | fail: |
| 4678 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4679 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4680 | } |
| 4681 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4682 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4683 | { |
| 4684 | /* If we are the last user of the backing storage (be it shmemfs |
| 4685 | * pages or stolen etc), we know that the pages are going to be |
| 4686 | * immediately released. In this case, we can then skip copying |
| 4687 | * back the contents from the GPU. |
| 4688 | */ |
| 4689 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4690 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4691 | return false; |
| 4692 | |
| 4693 | if (obj->base.filp == NULL) |
| 4694 | return true; |
| 4695 | |
| 4696 | /* At first glance, this looks racy, but then again so would be |
| 4697 | * userspace racing mmap against close. However, the first external |
| 4698 | * reference to the filp can only be obtained through the |
| 4699 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4700 | * acquiring such a reference whilst we are in the middle of |
| 4701 | * freeing the object. |
| 4702 | */ |
| 4703 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4704 | } |
| 4705 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4706 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4707 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4708 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4709 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4710 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4711 | intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4712 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4713 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4714 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4715 | trace_i915_gem_object_destroy(obj); |
| 4716 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4717 | mutex_lock(&i915->drm.struct_mutex); |
| 4718 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4719 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
| 4720 | list_for_each_entry_safe(vma, vn, |
| 4721 | &obj->vma_list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4722 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4723 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4724 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4725 | } |
Chris Wilson | db6c2b4 | 2016-11-01 11:54:00 +0000 | [diff] [blame] | 4726 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
| 4727 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4728 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4729 | /* This serializes freeing with the shrinker. Since the free |
| 4730 | * is delayed, first by RCU then by the workqueue, we want the |
| 4731 | * shrinker to be able to free pages of unreferenced objects, |
| 4732 | * or else we may oom whilst there are plenty of deferred |
| 4733 | * freed objects. |
| 4734 | */ |
| 4735 | if (i915_gem_object_has_pages(obj)) { |
| 4736 | spin_lock(&i915->mm.obj_lock); |
| 4737 | list_del_init(&obj->mm.link); |
| 4738 | spin_unlock(&i915->mm.obj_lock); |
| 4739 | } |
| 4740 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4741 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4742 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4743 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4744 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4745 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4746 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4747 | |
| 4748 | if (obj->ops->release) |
| 4749 | obj->ops->release(obj); |
| 4750 | |
| 4751 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4752 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4753 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4754 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4755 | |
| 4756 | if (obj->base.import_attach) |
| 4757 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4758 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4759 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4760 | drm_gem_object_release(&obj->base); |
| 4761 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4762 | |
| 4763 | kfree(obj->bit_17); |
| 4764 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4765 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4766 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4767 | atomic_dec(&i915->mm.free_count); |
| 4768 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4769 | if (on) |
| 4770 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4771 | } |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4772 | intel_runtime_pm_put(i915); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4776 | { |
| 4777 | struct llist_node *freed; |
| 4778 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4779 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4780 | freed = NULL; |
| 4781 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4782 | /* Only one consumer of llist_del_first() allowed */ |
| 4783 | spin_lock(&i915->mm.free_lock); |
| 4784 | freed = llist_del_first(&i915->mm.free_list); |
| 4785 | spin_unlock(&i915->mm.free_lock); |
| 4786 | } |
| 4787 | if (unlikely(freed)) { |
| 4788 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4789 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4790 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | static void __i915_gem_free_work(struct work_struct *work) |
| 4794 | { |
| 4795 | struct drm_i915_private *i915 = |
| 4796 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4797 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4798 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4799 | /* |
| 4800 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4801 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4802 | * However, the object may also be bound into the global GTT (e.g. |
| 4803 | * older GPUs without per-process support, or for direct access through |
| 4804 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4805 | * unbound now. |
| 4806 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4807 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4808 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4809 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4810 | spin_unlock(&i915->mm.free_lock); |
| 4811 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4812 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4813 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4814 | return; |
| 4815 | |
| 4816 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4817 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4818 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4819 | } |
| 4820 | |
| 4821 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4822 | { |
| 4823 | struct drm_i915_gem_object *obj = |
| 4824 | container_of(head, typeof(*obj), rcu); |
| 4825 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4826 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4827 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4828 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4829 | * it like a rcu_head from this point forwards. And we expect all |
| 4830 | * objects to be freed via this path. |
| 4831 | */ |
| 4832 | destroy_rcu_head(&obj->rcu); |
| 4833 | |
| 4834 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4835 | * Since we require blocking on struct_mutex to unbind the freed |
| 4836 | * object from the GPU before releasing resources back to the |
| 4837 | * system, we can not do that directly from the RCU callback (which may |
| 4838 | * be a softirq context), but must instead then defer that work onto a |
| 4839 | * kthread. We use the RCU callback rather than move the freed object |
| 4840 | * directly onto the work queue so that we can mix between using the |
| 4841 | * worker and performing frees directly from subsequent allocations for |
| 4842 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4843 | */ |
| 4844 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4845 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4849 | { |
| 4850 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4851 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4852 | if (obj->mm.quirked) |
| 4853 | __i915_gem_object_unpin_pages(obj); |
| 4854 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4855 | if (discard_backing_storage(obj)) |
| 4856 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4857 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4858 | /* |
| 4859 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4860 | * read-side critical sections are complete, e.g. |
| 4861 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4862 | * lookup see i915_gem_object_lookup_rcu(). |
| 4863 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4864 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4865 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4866 | } |
| 4867 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4868 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4869 | { |
| 4870 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4871 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4872 | if (!i915_gem_object_has_active_reference(obj) && |
| 4873 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4874 | i915_gem_object_set_active_reference(obj); |
| 4875 | else |
| 4876 | i915_gem_object_put(obj); |
| 4877 | } |
| 4878 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4879 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4880 | { |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4881 | GEM_TRACE("\n"); |
| 4882 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4883 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4884 | |
| 4885 | intel_runtime_pm_get(i915); |
| 4886 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 4887 | |
| 4888 | /* |
| 4889 | * As we have just resumed the machine and woken the device up from |
| 4890 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4891 | * back to defaults, recovering from whatever wedged state we left it |
| 4892 | * in and so worth trying to use the device once more. |
| 4893 | */ |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4894 | if (i915_terminally_wedged(&i915->gpu_error)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4895 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4896 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4897 | /* |
| 4898 | * If we inherit context state from the BIOS or earlier occupants |
| 4899 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4900 | * try to take over. The only way to remove the earlier state |
| 4901 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4902 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4903 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4904 | */ |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 4905 | intel_engines_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4906 | |
| 4907 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 4908 | intel_runtime_pm_put(i915); |
| 4909 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4910 | i915_gem_contexts_lost(i915); |
| 4911 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4914 | int i915_gem_suspend(struct drm_i915_private *i915) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4915 | { |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4916 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4917 | |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 4918 | GEM_TRACE("\n"); |
| 4919 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4920 | intel_runtime_pm_get(i915); |
| 4921 | intel_suspend_gt_powersave(i915); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4922 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4923 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4924 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4925 | /* |
| 4926 | * We have to flush all the executing contexts to main memory so |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4927 | * that they can saved in the hibernation image. To ensure the last |
| 4928 | * context image is coherent, we have to switch away from it. That |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4929 | * leaves the i915->kernel_context still active when |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4930 | * we actually suspend, and its image in memory may not match the GPU |
| 4931 | * state. Fortunately, the kernel_context is disposable and we do |
| 4932 | * not rely on its state. |
| 4933 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4934 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 4935 | ret = i915_gem_switch_to_kernel_context(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4936 | if (ret) |
| 4937 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4938 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4939 | ret = i915_gem_wait_for_idle(i915, |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4940 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 4941 | I915_WAIT_LOCKED | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4942 | I915_WAIT_FOR_IDLE_BOOST, |
| 4943 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4944 | if (ret && ret != -EIO) |
| 4945 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4946 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4947 | assert_kernel_context_is_current(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4948 | } |
Chris Wilson | 01f8f33 | 2018-07-17 09:41:21 +0100 | [diff] [blame] | 4949 | i915_retire_requests(i915); /* ensure we flush after wedging */ |
| 4950 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4951 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4952 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4953 | intel_uc_suspend(i915); |
Sagar Arun Kamble | 63987bf | 2017-04-05 15:51:50 +0530 | [diff] [blame] | 4954 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4955 | cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work); |
| 4956 | cancel_delayed_work_sync(&i915->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4957 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4958 | /* |
| 4959 | * 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] | 4960 | * repeat the flush until it is definitely idle. |
| 4961 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4962 | drain_delayed_work(&i915->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4963 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4964 | /* |
| 4965 | * Assert that we successfully flushed all the work and |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4966 | * reset the GPU back to its idle, low power state. |
| 4967 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4968 | WARN_ON(i915->gt.awake); |
| 4969 | if (WARN_ON(!intel_engines_are_idle(i915))) |
| 4970 | i915_gem_set_wedged(i915); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4971 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4972 | intel_runtime_pm_put(i915); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4973 | return 0; |
| 4974 | |
| 4975 | err_unlock: |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4976 | mutex_unlock(&i915->drm.struct_mutex); |
| 4977 | intel_runtime_pm_put(i915); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4978 | return ret; |
| 4979 | } |
| 4980 | |
| 4981 | void i915_gem_suspend_late(struct drm_i915_private *i915) |
| 4982 | { |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4983 | struct drm_i915_gem_object *obj; |
| 4984 | struct list_head *phases[] = { |
| 4985 | &i915->mm.unbound_list, |
| 4986 | &i915->mm.bound_list, |
| 4987 | NULL |
| 4988 | }, **phase; |
| 4989 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4990 | /* |
| 4991 | * Neither the BIOS, ourselves or any other kernel |
| 4992 | * expects the system to be in execlists mode on startup, |
| 4993 | * so we need to reset the GPU back to legacy mode. And the only |
| 4994 | * known way to disable logical contexts is through a GPU reset. |
| 4995 | * |
| 4996 | * So in order to leave the system in a known default configuration, |
| 4997 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4998 | * clean up the GEM state tracking, flushing off the requests and |
| 4999 | * leaving the system in a known idle state. |
| 5000 | * |
| 5001 | * Note that is of the upmost importance that the GPU is idle and |
| 5002 | * all stray writes are flushed *before* we dismantle the backing |
| 5003 | * storage for the pinned objects. |
| 5004 | * |
| 5005 | * However, since we are uncertain that resetting the GPU on older |
| 5006 | * machines is a good idea, we don't - just in case it leaves the |
| 5007 | * machine in an unusable condition. |
| 5008 | */ |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 5009 | |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 5010 | mutex_lock(&i915->drm.struct_mutex); |
| 5011 | for (phase = phases; *phase; phase++) { |
| 5012 | list_for_each_entry(obj, *phase, mm.link) |
| 5013 | WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); |
| 5014 | } |
| 5015 | mutex_unlock(&i915->drm.struct_mutex); |
| 5016 | |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 5017 | intel_uc_sanitize(i915); |
| 5018 | i915_gem_sanitize(i915); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5019 | } |
| 5020 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5021 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5022 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 5023 | GEM_TRACE("\n"); |
| 5024 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5025 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5026 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5027 | mutex_lock(&i915->drm.struct_mutex); |
| 5028 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 5029 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5030 | i915_gem_restore_gtt_mappings(i915); |
| 5031 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5032 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5033 | /* |
| 5034 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5035 | * guarantee that the context image is complete. So let's just reset |
| 5036 | * it and start again. |
| 5037 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5038 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5039 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5040 | if (i915_gem_init_hw(i915)) |
| 5041 | goto err_wedged; |
| 5042 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 5043 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 5044 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5045 | /* Always reload a context for powersaving. */ |
| 5046 | if (i915_gem_switch_to_kernel_context(i915)) |
| 5047 | goto err_wedged; |
| 5048 | |
| 5049 | out_unlock: |
| 5050 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 5051 | mutex_unlock(&i915->drm.struct_mutex); |
| 5052 | return; |
| 5053 | |
| 5054 | err_wedged: |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5055 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 5056 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 5057 | i915_gem_set_wedged(i915); |
| 5058 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5059 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5060 | } |
| 5061 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5062 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5063 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5064 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5065 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 5066 | return; |
| 5067 | |
| 5068 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 5069 | DISP_TILE_SURFACE_SWIZZLING); |
| 5070 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5071 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 5072 | return; |
| 5073 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5074 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5075 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5076 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5077 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5078 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5079 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 5080 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 5081 | else |
| 5082 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5083 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 5084 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5085 | 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] | 5086 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5087 | I915_WRITE(RING_CTL(base), 0); |
| 5088 | I915_WRITE(RING_HEAD(base), 0); |
| 5089 | I915_WRITE(RING_TAIL(base), 0); |
| 5090 | I915_WRITE(RING_START(base), 0); |
| 5091 | } |
| 5092 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5093 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5094 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5095 | if (IS_I830(dev_priv)) { |
| 5096 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5097 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5098 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5099 | init_unused_ring(dev_priv, SRB2_BASE); |
| 5100 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5101 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5102 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5103 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5104 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5105 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5106 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5107 | } |
| 5108 | } |
| 5109 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5110 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5111 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5112 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5113 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5114 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5115 | int err; |
| 5116 | |
| 5117 | for_each_engine(engine, i915, id) { |
| 5118 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5119 | if (err) { |
| 5120 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 5121 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5122 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5123 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | return 0; |
| 5127 | } |
| 5128 | |
| 5129 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 5130 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 5131 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5132 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 5133 | dev_priv->gt.last_init_time = ktime_get(); |
| 5134 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5135 | /* Double layer security blanket, see i915_gem_init() */ |
| 5136 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5137 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 5138 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 5139 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5140 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 5141 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5142 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 5143 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 5144 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 5145 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5146 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 5147 | /* ...and determine whether they are sticking. */ |
| 5148 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 5149 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5150 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5151 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5152 | /* |
| 5153 | * At least 830 can leave some of the unused rings |
| 5154 | * "active" (ie. head != tail) after resume which |
| 5155 | * will prevent c3 entry. Makes sure all unused rings |
| 5156 | * are totally idle. |
| 5157 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5158 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5159 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 5160 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5161 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5162 | ret = -EIO; |
| 5163 | goto out; |
| 5164 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 5165 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5166 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5167 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5168 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5169 | goto out; |
| 5170 | } |
| 5171 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 5172 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 5173 | if (ret) { |
| 5174 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 5175 | goto out; |
| 5176 | } |
| 5177 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5178 | /* We can't enable contexts until all firmware is loaded */ |
| 5179 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5180 | if (ret) { |
| 5181 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5182 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5183 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5184 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5185 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 5186 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 5187 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 5188 | ret = __i915_gem_restart_engines(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 5189 | if (ret) |
| 5190 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 5191 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5192 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 5193 | |
| 5194 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 5195 | |
| 5196 | cleanup_uc: |
| 5197 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 5198 | out: |
| 5199 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5200 | |
| 5201 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5202 | } |
| 5203 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5204 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 5205 | { |
| 5206 | struct i915_gem_context *ctx; |
| 5207 | struct intel_engine_cs *engine; |
| 5208 | enum intel_engine_id id; |
| 5209 | int err; |
| 5210 | |
| 5211 | /* |
| 5212 | * As we reset the gpu during very early sanitisation, the current |
| 5213 | * register state on the GPU should reflect its defaults values. |
| 5214 | * We load a context onto the hw (with restore-inhibit), then switch |
| 5215 | * over to a second context to save that default register state. We |
| 5216 | * can then prime every new context with that state so they all start |
| 5217 | * from the same default HW values. |
| 5218 | */ |
| 5219 | |
| 5220 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 5221 | if (IS_ERR(ctx)) |
| 5222 | return PTR_ERR(ctx); |
| 5223 | |
| 5224 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5225 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5226 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5227 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5228 | if (IS_ERR(rq)) { |
| 5229 | err = PTR_ERR(rq); |
| 5230 | goto out_ctx; |
| 5231 | } |
| 5232 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 5233 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5234 | if (engine->init_context) |
| 5235 | err = engine->init_context(rq); |
| 5236 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 5237 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5238 | if (err) |
| 5239 | goto err_active; |
| 5240 | } |
| 5241 | |
| 5242 | err = i915_gem_switch_to_kernel_context(i915); |
| 5243 | if (err) |
| 5244 | goto err_active; |
| 5245 | |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 5246 | if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) { |
| 5247 | i915_gem_set_wedged(i915); |
| 5248 | err = -EIO; /* Caller will declare us wedged */ |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5249 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 5250 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5251 | |
| 5252 | assert_kernel_context_is_current(i915); |
| 5253 | |
Chris Wilson | 8e1cb32 | 2018-09-20 17:13:43 +0100 | [diff] [blame] | 5254 | /* |
| 5255 | * Immediately park the GPU so that we enable powersaving and |
| 5256 | * treat it as idle. The next time we issue a request, we will |
| 5257 | * unpark and start using the engine->pinned_default_state, otherwise |
| 5258 | * it is in limbo and an early reset may fail. |
| 5259 | */ |
| 5260 | __i915_gem_park(i915); |
| 5261 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5262 | for_each_engine(engine, i915, id) { |
| 5263 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 5264 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5265 | |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 5266 | GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count); |
| 5267 | |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 5268 | state = to_intel_context(ctx, engine)->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5269 | if (!state) |
| 5270 | continue; |
| 5271 | |
| 5272 | /* |
| 5273 | * As we will hold a reference to the logical state, it will |
| 5274 | * not be torn down with the context, and importantly the |
| 5275 | * object will hold onto its vma (making it possible for a |
| 5276 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 5277 | * from the GTT to prevent such accidents and reclaim the |
| 5278 | * space. |
| 5279 | */ |
| 5280 | err = i915_vma_unbind(state); |
| 5281 | if (err) |
| 5282 | goto err_active; |
| 5283 | |
| 5284 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 5285 | if (err) |
| 5286 | goto err_active; |
| 5287 | |
| 5288 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 5289 | |
| 5290 | /* Check we can acquire the image of the context state */ |
| 5291 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 5292 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 5293 | if (IS_ERR(vaddr)) { |
| 5294 | err = PTR_ERR(vaddr); |
| 5295 | goto err_active; |
| 5296 | } |
| 5297 | |
| 5298 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 5302 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 5303 | |
| 5304 | /* |
| 5305 | * Make sure that classes with multiple engine instances all |
| 5306 | * share the same basic configuration. |
| 5307 | */ |
| 5308 | for_each_engine(engine, i915, id) { |
| 5309 | unsigned int bit = BIT(engine->uabi_class); |
| 5310 | unsigned int expected = engine->default_state ? bit : 0; |
| 5311 | |
| 5312 | if ((found & bit) != expected) { |
| 5313 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 5314 | engine->uabi_class, engine->name); |
| 5315 | } |
| 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | out_ctx: |
| 5320 | i915_gem_context_set_closed(ctx); |
| 5321 | i915_gem_context_put(ctx); |
| 5322 | return err; |
| 5323 | |
| 5324 | err_active: |
| 5325 | /* |
| 5326 | * If we have to abandon now, we expect the engines to be idle |
| 5327 | * and ready to be torn-down. First try to flush any remaining |
| 5328 | * request, ensure we are pointing at the kernel context and |
| 5329 | * then remove it. |
| 5330 | */ |
| 5331 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 5332 | goto out_ctx; |
| 5333 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 5334 | if (WARN_ON(i915_gem_wait_for_idle(i915, |
| 5335 | I915_WAIT_LOCKED, |
| 5336 | MAX_SCHEDULE_TIMEOUT))) |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5337 | goto out_ctx; |
| 5338 | |
| 5339 | i915_gem_contexts_lost(i915); |
| 5340 | goto out_ctx; |
| 5341 | } |
| 5342 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5343 | static int |
| 5344 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 5345 | { |
| 5346 | struct drm_i915_gem_object *obj; |
| 5347 | struct i915_vma *vma; |
| 5348 | int ret; |
| 5349 | |
| 5350 | obj = i915_gem_object_create_stolen(i915, size); |
| 5351 | if (!obj) |
| 5352 | obj = i915_gem_object_create_internal(i915, size); |
| 5353 | if (IS_ERR(obj)) { |
| 5354 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 5355 | return PTR_ERR(obj); |
| 5356 | } |
| 5357 | |
| 5358 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 5359 | if (IS_ERR(vma)) { |
| 5360 | ret = PTR_ERR(vma); |
| 5361 | goto err_unref; |
| 5362 | } |
| 5363 | |
| 5364 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 5365 | if (ret) |
| 5366 | goto err_unref; |
| 5367 | |
| 5368 | i915->gt.scratch = vma; |
| 5369 | return 0; |
| 5370 | |
| 5371 | err_unref: |
| 5372 | i915_gem_object_put(obj); |
| 5373 | return ret; |
| 5374 | } |
| 5375 | |
| 5376 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 5377 | { |
| 5378 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 5379 | } |
| 5380 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5381 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5382 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5383 | int ret; |
| 5384 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 5385 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 5386 | 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] | 5387 | mkwrite_device_info(dev_priv)->page_sizes = |
| 5388 | I915_GTT_PAGE_SIZE_4K; |
| 5389 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 5390 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 5391 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5392 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 5393 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5394 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5395 | } else { |
| 5396 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5397 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5398 | } |
| 5399 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5400 | ret = i915_gem_init_userptr(dev_priv); |
| 5401 | if (ret) |
| 5402 | return ret; |
| 5403 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5404 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 5405 | if (ret) |
| 5406 | return ret; |
| 5407 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5408 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 5409 | if (ret) |
| 5410 | goto err_uc_misc; |
| 5411 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5412 | /* This is just a security blanket to placate dragons. |
| 5413 | * On some systems, we very sporadically observe that the first TLBs |
| 5414 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5415 | * we hold the forcewake during initialisation these problems |
| 5416 | * just magically go away. |
| 5417 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5418 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5419 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5420 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5421 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5422 | if (ret) { |
| 5423 | GEM_BUG_ON(ret == -EIO); |
| 5424 | goto err_unlock; |
| 5425 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5426 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5427 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5428 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5429 | if (ret) { |
| 5430 | GEM_BUG_ON(ret == -EIO); |
| 5431 | goto err_ggtt; |
| 5432 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5433 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5434 | ret = i915_gem_contexts_init(dev_priv); |
| 5435 | if (ret) { |
| 5436 | GEM_BUG_ON(ret == -EIO); |
| 5437 | goto err_scratch; |
| 5438 | } |
| 5439 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5440 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5441 | if (ret) { |
| 5442 | GEM_BUG_ON(ret == -EIO); |
| 5443 | goto err_context; |
| 5444 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5445 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5446 | intel_init_gt_powersave(dev_priv); |
| 5447 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5448 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5449 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5450 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5451 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5452 | ret = i915_gem_init_hw(dev_priv); |
| 5453 | if (ret) |
| 5454 | goto err_uc_init; |
| 5455 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5456 | /* |
| 5457 | * Despite its name intel_init_clock_gating applies both display |
| 5458 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5459 | * GT power context workaround. Worse, sometimes it includes a context |
| 5460 | * register workaround which we need to apply before we record the |
| 5461 | * default HW state for all contexts. |
| 5462 | * |
| 5463 | * FIXME: break up the workarounds and apply them at the right time! |
| 5464 | */ |
| 5465 | intel_init_clock_gating(dev_priv); |
| 5466 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5467 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5468 | if (ret) |
| 5469 | goto err_init_hw; |
| 5470 | |
| 5471 | if (i915_inject_load_failure()) { |
| 5472 | ret = -ENODEV; |
| 5473 | goto err_init_hw; |
| 5474 | } |
| 5475 | |
| 5476 | if (i915_inject_load_failure()) { |
| 5477 | ret = -EIO; |
| 5478 | goto err_init_hw; |
| 5479 | } |
| 5480 | |
| 5481 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5482 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5483 | |
| 5484 | return 0; |
| 5485 | |
| 5486 | /* |
| 5487 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5488 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5489 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5490 | * driver doesn't explode during runtime. |
| 5491 | */ |
| 5492 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5493 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5494 | |
| 5495 | WARN_ON(i915_gem_suspend(dev_priv)); |
| 5496 | i915_gem_suspend_late(dev_priv); |
| 5497 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 5498 | i915_gem_drain_workqueue(dev_priv); |
| 5499 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5500 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5501 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5502 | err_uc_init: |
| 5503 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5504 | err_pm: |
| 5505 | if (ret != -EIO) { |
| 5506 | intel_cleanup_gt_powersave(dev_priv); |
| 5507 | i915_gem_cleanup_engines(dev_priv); |
| 5508 | } |
| 5509 | err_context: |
| 5510 | if (ret != -EIO) |
| 5511 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5512 | err_scratch: |
| 5513 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5514 | err_ggtt: |
| 5515 | err_unlock: |
| 5516 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5517 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5518 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5519 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5520 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5521 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5522 | if (ret != -EIO) |
| 5523 | i915_gem_cleanup_userptr(dev_priv); |
| 5524 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5525 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5526 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5527 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5528 | /* |
| 5529 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5530 | * wedged. But we only want to do this where the GPU is angry, |
| 5531 | * for all other failure, such as an allocation failure, bail. |
| 5532 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5533 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 5534 | i915_load_error(dev_priv, |
| 5535 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5536 | i915_gem_set_wedged(dev_priv); |
| 5537 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5538 | |
| 5539 | /* Minimal basic recovery for KMS */ |
| 5540 | ret = i915_ggtt_enable_hw(dev_priv); |
| 5541 | i915_gem_restore_gtt_mappings(dev_priv); |
| 5542 | i915_gem_restore_fences(dev_priv); |
| 5543 | intel_init_clock_gating(dev_priv); |
| 5544 | |
| 5545 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5546 | } |
| 5547 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5548 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5549 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5550 | } |
| 5551 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5552 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 5553 | { |
| 5554 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5555 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5556 | |
| 5557 | /* Flush any outstanding unpin_work. */ |
| 5558 | i915_gem_drain_workqueue(dev_priv); |
| 5559 | |
| 5560 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5561 | intel_uc_fini_hw(dev_priv); |
| 5562 | intel_uc_fini(dev_priv); |
| 5563 | i915_gem_cleanup_engines(dev_priv); |
| 5564 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5565 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5566 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5567 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5568 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 5569 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5570 | intel_cleanup_gt_powersave(dev_priv); |
| 5571 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5572 | intel_uc_fini_misc(dev_priv); |
| 5573 | i915_gem_cleanup_userptr(dev_priv); |
| 5574 | |
| 5575 | i915_gem_drain_freed_objects(dev_priv); |
| 5576 | |
| 5577 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 5578 | } |
| 5579 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5580 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5581 | { |
| 5582 | i915_gem_sanitize(i915); |
| 5583 | } |
| 5584 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5585 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5586 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5587 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5588 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5589 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5590 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5591 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5592 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5593 | } |
| 5594 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5595 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5596 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5597 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5598 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5599 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5600 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5601 | !IS_CHERRYVIEW(dev_priv)) |
| 5602 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5603 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5604 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5605 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5606 | dev_priv->num_fence_regs = 16; |
| 5607 | else |
| 5608 | dev_priv->num_fence_regs = 8; |
| 5609 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5610 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5611 | dev_priv->num_fence_regs = |
| 5612 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5613 | |
| 5614 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5615 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5616 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5617 | |
| 5618 | fence->i915 = dev_priv; |
| 5619 | fence->id = i; |
| 5620 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5621 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5622 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5623 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5624 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5625 | } |
| 5626 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5627 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5628 | { |
| 5629 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5630 | spin_lock_init(&i915->mm.obj_lock); |
| 5631 | spin_lock_init(&i915->mm.free_lock); |
| 5632 | |
| 5633 | init_llist_head(&i915->mm.free_list); |
| 5634 | |
| 5635 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5636 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5637 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5638 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5639 | |
| 5640 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5641 | } |
| 5642 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5643 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5644 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5645 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5646 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5647 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5648 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5649 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5650 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5651 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5652 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5653 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5654 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5655 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5656 | if (!dev_priv->luts) |
| 5657 | goto err_vmas; |
| 5658 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5659 | dev_priv->requests = KMEM_CACHE(i915_request, |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5660 | SLAB_HWCACHE_ALIGN | |
| 5661 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5662 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5663 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5664 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5665 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5666 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5667 | SLAB_HWCACHE_ALIGN | |
| 5668 | SLAB_RECLAIM_ACCOUNT); |
| 5669 | if (!dev_priv->dependencies) |
| 5670 | goto err_requests; |
| 5671 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5672 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5673 | if (!dev_priv->priorities) |
| 5674 | goto err_dependencies; |
| 5675 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5676 | INIT_LIST_HEAD(&dev_priv->gt.timelines); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5677 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 5678 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5679 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5680 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5681 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5682 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5683 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5684 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5685 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5686 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5687 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5688 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5689 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5690 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5691 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5692 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5693 | err = i915_gemfs_init(dev_priv); |
| 5694 | if (err) |
| 5695 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5696 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5697 | return 0; |
| 5698 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5699 | err_dependencies: |
| 5700 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5701 | err_requests: |
| 5702 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5703 | err_luts: |
| 5704 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5705 | err_vmas: |
| 5706 | kmem_cache_destroy(dev_priv->vmas); |
| 5707 | err_objects: |
| 5708 | kmem_cache_destroy(dev_priv->objects); |
| 5709 | err_out: |
| 5710 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5711 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5712 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5713 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5714 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5715 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5716 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5717 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5718 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5719 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5720 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5721 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5722 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5723 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5724 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5725 | kmem_cache_destroy(dev_priv->vmas); |
| 5726 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5727 | |
| 5728 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5729 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5730 | |
| 5731 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5732 | } |
| 5733 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5734 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5735 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5736 | /* Discard all purgeable objects, let userspace recover those as |
| 5737 | * required after resuming. |
| 5738 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5739 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5740 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5741 | return 0; |
| 5742 | } |
| 5743 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5744 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5745 | { |
| 5746 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5747 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5748 | &i915->mm.unbound_list, |
| 5749 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5750 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5751 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5752 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5753 | /* |
| 5754 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5755 | * |
| 5756 | * We need to update the domain tracking to reflect that the CPU |
| 5757 | * will be accessing all the pages to create and restore from the |
| 5758 | * hibernation, and so upon restoration those pages will be in the |
| 5759 | * CPU domain. |
| 5760 | * |
| 5761 | * To make sure the hibernation image contains the latest state, |
| 5762 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5763 | * |
| 5764 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5765 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5766 | */ |
| 5767 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5768 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 5769 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5770 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5771 | mutex_lock(&i915->drm.struct_mutex); |
| 5772 | for (phase = phases; *phase; phase++) { |
| 5773 | list_for_each_entry(obj, *phase, mm.link) |
| 5774 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5775 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5776 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5777 | |
| 5778 | return 0; |
| 5779 | } |
| 5780 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5781 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5782 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5783 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5784 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5785 | |
| 5786 | /* Clean up our request list when the client is going away, so that |
| 5787 | * later retire_requests won't dereference our soon-to-be-gone |
| 5788 | * file_priv. |
| 5789 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5790 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5791 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5792 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5793 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5794 | } |
| 5795 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5796 | 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] | 5797 | { |
| 5798 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5799 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5800 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5801 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5802 | |
| 5803 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5804 | if (!file_priv) |
| 5805 | return -ENOMEM; |
| 5806 | |
| 5807 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5808 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5809 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5810 | |
| 5811 | spin_lock_init(&file_priv->mm.lock); |
| 5812 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5813 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5814 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 5815 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5816 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5817 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5818 | if (ret) |
| 5819 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5820 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5821 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5822 | } |
| 5823 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5824 | /** |
| 5825 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5826 | * @old: current GEM buffer for the frontbuffer slots |
| 5827 | * @new: new GEM buffer for the frontbuffer slots |
| 5828 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5829 | * |
| 5830 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5831 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5832 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5833 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5834 | struct drm_i915_gem_object *new, |
| 5835 | unsigned frontbuffer_bits) |
| 5836 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5837 | /* Control of individual bits within the mask are guarded by |
| 5838 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5839 | * manipulation of individual bits. But since the bitfield as a whole |
| 5840 | * is updated using RMW, we need to use atomics in order to update |
| 5841 | * the bits. |
| 5842 | */ |
| 5843 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 5844 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5845 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5846 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5847 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5848 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5849 | } |
| 5850 | |
| 5851 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5852 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5853 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5854 | } |
| 5855 | } |
| 5856 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5857 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5858 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5859 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5860 | const void *data, size_t size) |
| 5861 | { |
| 5862 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5863 | struct file *file; |
| 5864 | size_t offset; |
| 5865 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5866 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5867 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5868 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5869 | return obj; |
| 5870 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5871 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5872 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5873 | file = obj->base.filp; |
| 5874 | offset = 0; |
| 5875 | do { |
| 5876 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5877 | struct page *page; |
| 5878 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5879 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5880 | err = pagecache_write_begin(file, file->f_mapping, |
| 5881 | offset, len, 0, |
| 5882 | &page, &pgdata); |
| 5883 | if (err < 0) |
| 5884 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5885 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5886 | vaddr = kmap(page); |
| 5887 | memcpy(vaddr, data, len); |
| 5888 | kunmap(page); |
| 5889 | |
| 5890 | err = pagecache_write_end(file, file->f_mapping, |
| 5891 | offset, len, len, |
| 5892 | page, pgdata); |
| 5893 | if (err < 0) |
| 5894 | goto fail; |
| 5895 | |
| 5896 | size -= len; |
| 5897 | data += len; |
| 5898 | offset += len; |
| 5899 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5900 | |
| 5901 | return obj; |
| 5902 | |
| 5903 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5904 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5905 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5906 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5907 | |
| 5908 | struct scatterlist * |
| 5909 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5910 | unsigned int n, |
| 5911 | unsigned int *offset) |
| 5912 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5913 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5914 | struct scatterlist *sg; |
| 5915 | unsigned int idx, count; |
| 5916 | |
| 5917 | might_sleep(); |
| 5918 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5919 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5920 | |
| 5921 | /* As we iterate forward through the sg, we record each entry in a |
| 5922 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5923 | * this index previously, we will have an entry for it. |
| 5924 | * |
| 5925 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5926 | * sequential page access (where each new request is consecutive |
| 5927 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5928 | * i.e. O(1) with a large constant! |
| 5929 | */ |
| 5930 | if (n < READ_ONCE(iter->sg_idx)) |
| 5931 | goto lookup; |
| 5932 | |
| 5933 | mutex_lock(&iter->lock); |
| 5934 | |
| 5935 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5936 | * (or the subsequent) sg are fast - comparing against the last |
| 5937 | * sg is faster than going through the radixtree. |
| 5938 | */ |
| 5939 | |
| 5940 | sg = iter->sg_pos; |
| 5941 | idx = iter->sg_idx; |
| 5942 | count = __sg_page_count(sg); |
| 5943 | |
| 5944 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5945 | void *entry; |
| 5946 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5947 | int ret; |
| 5948 | |
| 5949 | /* If we cannot allocate and insert this entry, or the |
| 5950 | * individual pages from this range, cancel updating the |
| 5951 | * sg_idx so that on this lookup we are forced to linearly |
| 5952 | * scan onwards, but on future lookups we will try the |
| 5953 | * insertion again (in which case we need to be careful of |
| 5954 | * the error return reporting that we have already inserted |
| 5955 | * this index). |
| 5956 | */ |
| 5957 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5958 | if (ret && ret != -EEXIST) |
| 5959 | goto scan; |
| 5960 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5961 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5962 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5963 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5964 | if (ret && ret != -EEXIST) |
| 5965 | goto scan; |
| 5966 | } |
| 5967 | |
| 5968 | idx += count; |
| 5969 | sg = ____sg_next(sg); |
| 5970 | count = __sg_page_count(sg); |
| 5971 | } |
| 5972 | |
| 5973 | scan: |
| 5974 | iter->sg_pos = sg; |
| 5975 | iter->sg_idx = idx; |
| 5976 | |
| 5977 | mutex_unlock(&iter->lock); |
| 5978 | |
| 5979 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5980 | goto lookup; |
| 5981 | |
| 5982 | /* In case we failed to insert the entry into the radixtree, we need |
| 5983 | * to look beyond the current sg. |
| 5984 | */ |
| 5985 | while (idx + count <= n) { |
| 5986 | idx += count; |
| 5987 | sg = ____sg_next(sg); |
| 5988 | count = __sg_page_count(sg); |
| 5989 | } |
| 5990 | |
| 5991 | *offset = n - idx; |
| 5992 | return sg; |
| 5993 | |
| 5994 | lookup: |
| 5995 | rcu_read_lock(); |
| 5996 | |
| 5997 | sg = radix_tree_lookup(&iter->radix, n); |
| 5998 | GEM_BUG_ON(!sg); |
| 5999 | |
| 6000 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 6001 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 6002 | * to the start of that range. We will return the pointer to |
| 6003 | * the base page and the offset of this page within the |
| 6004 | * sg entry's range. |
| 6005 | */ |
| 6006 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 6007 | if (unlikely(xa_is_value(sg))) { |
| 6008 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 6009 | |
| 6010 | sg = radix_tree_lookup(&iter->radix, base); |
| 6011 | GEM_BUG_ON(!sg); |
| 6012 | |
| 6013 | *offset = n - base; |
| 6014 | } |
| 6015 | |
| 6016 | rcu_read_unlock(); |
| 6017 | |
| 6018 | return sg; |
| 6019 | } |
| 6020 | |
| 6021 | struct page * |
| 6022 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 6023 | { |
| 6024 | struct scatterlist *sg; |
| 6025 | unsigned int offset; |
| 6026 | |
| 6027 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 6028 | |
| 6029 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 6030 | return nth_page(sg_page(sg), offset); |
| 6031 | } |
| 6032 | |
| 6033 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 6034 | struct page * |
| 6035 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 6036 | unsigned int n) |
| 6037 | { |
| 6038 | struct page *page; |
| 6039 | |
| 6040 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 6041 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 6042 | set_page_dirty(page); |
| 6043 | |
| 6044 | return page; |
| 6045 | } |
| 6046 | |
| 6047 | dma_addr_t |
| 6048 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 6049 | unsigned long n) |
| 6050 | { |
| 6051 | struct scatterlist *sg; |
| 6052 | unsigned int offset; |
| 6053 | |
| 6054 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 6055 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 6056 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6057 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6058 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 6059 | { |
| 6060 | struct sg_table *pages; |
| 6061 | int err; |
| 6062 | |
| 6063 | if (align > obj->base.size) |
| 6064 | return -EINVAL; |
| 6065 | |
| 6066 | if (obj->ops == &i915_gem_phys_ops) |
| 6067 | return 0; |
| 6068 | |
| 6069 | if (obj->ops != &i915_gem_object_ops) |
| 6070 | return -EINVAL; |
| 6071 | |
| 6072 | err = i915_gem_object_unbind(obj); |
| 6073 | if (err) |
| 6074 | return err; |
| 6075 | |
| 6076 | mutex_lock(&obj->mm.lock); |
| 6077 | |
| 6078 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 6079 | err = -EFAULT; |
| 6080 | goto err_unlock; |
| 6081 | } |
| 6082 | |
| 6083 | if (obj->mm.quirked) { |
| 6084 | err = -EFAULT; |
| 6085 | goto err_unlock; |
| 6086 | } |
| 6087 | |
| 6088 | if (obj->mm.mapping) { |
| 6089 | err = -EBUSY; |
| 6090 | goto err_unlock; |
| 6091 | } |
| 6092 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 6093 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 6094 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6095 | obj->ops = &i915_gem_phys_ops; |
| 6096 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 6097 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6098 | if (err) |
| 6099 | goto err_xfer; |
| 6100 | |
| 6101 | /* Perma-pin (until release) the physical set of pages */ |
| 6102 | __i915_gem_object_pin_pages(obj); |
| 6103 | |
| 6104 | if (!IS_ERR_OR_NULL(pages)) |
| 6105 | i915_gem_object_ops.put_pages(obj, pages); |
| 6106 | mutex_unlock(&obj->mm.lock); |
| 6107 | return 0; |
| 6108 | |
| 6109 | err_xfer: |
| 6110 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 6111 | if (!IS_ERR_OR_NULL(pages)) { |
| 6112 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 6113 | |
| 6114 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 6115 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6116 | err_unlock: |
| 6117 | mutex_unlock(&obj->mm.lock); |
| 6118 | return err; |
| 6119 | } |
| 6120 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6121 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 6122 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 6123 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 6124 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 6125 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 6126 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 6127 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 6128 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6129 | #endif |