Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
Daniel Vetter | be6a037 | 2015-03-18 10:46:04 +0100 | [diff] [blame] | 2 | * Copyright © 2008-2015 Intel Corporation |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 28 | #include <drm/drm_vma_manager.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/i915_drm.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 30 | #include "i915_drv.h" |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 31 | #include "i915_gem_clflush.h" |
Yu Zhang | eb82289 | 2015-02-10 19:05:49 +0800 | [diff] [blame] | 32 | #include "i915_vgpu.h" |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 33 | #include "i915_trace.h" |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 34 | #include "intel_drv.h" |
Chris Wilson | 5d723d7 | 2016-08-04 16:32:35 +0100 | [diff] [blame] | 35 | #include "intel_frontbuffer.h" |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 36 | #include "intel_mocs.h" |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 37 | #include "intel_workarounds.h" |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 38 | #include "i915_gemfs.h" |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 39 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 40 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 41 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 42 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 44 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 45 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 46 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 47 | #include <linux/dma-buf.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 48 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 49 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 50 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 51 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 52 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 53 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 54 | return false; |
| 55 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 56 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 57 | return true; |
| 58 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 59 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 62 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 63 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 64 | struct drm_mm_node *node, u32 size) |
| 65 | { |
| 66 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 67 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 68 | size, 0, I915_COLOR_UNEVICTABLE, |
| 69 | 0, ggtt->mappable_end, |
| 70 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void |
| 74 | remove_mappable_node(struct drm_mm_node *node) |
| 75 | { |
| 76 | drm_mm_remove_node(node); |
| 77 | } |
| 78 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 79 | /* some bookkeeping */ |
| 80 | 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] | 81 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 82 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 83 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 84 | dev_priv->mm.object_count++; |
| 85 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 86 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | 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] | 90 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 91 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 92 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 93 | dev_priv->mm.object_count--; |
| 94 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 95 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Chris Wilson | 21dd373 | 2011-01-26 15:55:56 +0000 | [diff] [blame] | 98 | static int |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 99 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 100 | { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 101 | int ret; |
| 102 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 103 | might_sleep(); |
| 104 | |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 105 | /* |
| 106 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging |
| 107 | * userspace. If it takes that long something really bad is going on and |
| 108 | * we should simply try to bail out and fail as gracefully as possible. |
| 109 | */ |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 110 | ret = wait_event_interruptible_timeout(error->reset_queue, |
Chris Wilson | 8c185ec | 2017-03-16 17:13:02 +0000 | [diff] [blame] | 111 | !i915_reset_backoff(error), |
Chris Wilson | b52992c | 2016-10-28 13:58:24 +0100 | [diff] [blame] | 112 | I915_RESET_TIMEOUT); |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 113 | if (ret == 0) { |
| 114 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); |
| 115 | return -EIO; |
| 116 | } else if (ret < 0) { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 117 | return ret; |
Chris Wilson | d98c52c | 2016-04-13 17:35:05 +0100 | [diff] [blame] | 118 | } else { |
| 119 | return 0; |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 120 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 123 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 124 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 125 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 126 | int ret; |
| 127 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 128 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 129 | if (ret) |
| 130 | return ret; |
| 131 | |
| 132 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 136 | return 0; |
| 137 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 138 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 139 | static u32 __i915_gem_park(struct drm_i915_private *i915) |
| 140 | { |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 141 | intel_wakeref_t wakeref; |
| 142 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 143 | GEM_TRACE("\n"); |
| 144 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 145 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 146 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 147 | GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 148 | |
| 149 | if (!i915->gt.awake) |
| 150 | return I915_EPOCH_INVALID; |
| 151 | |
| 152 | GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); |
| 153 | |
| 154 | /* |
| 155 | * Be paranoid and flush a concurrent interrupt to make sure |
| 156 | * we don't reactivate any irq tasklets after parking. |
| 157 | * |
| 158 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 159 | * there may still be an in-flight interrupt even though the CSB |
| 160 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 161 | * is completed before we continue, but it doesn't prevent the HW from |
| 162 | * raising a spurious interrupt later. To complete the shield we should |
| 163 | * coordinate disabling the CS irq with flushing the interrupts. |
| 164 | */ |
| 165 | synchronize_irq(i915->drm.irq); |
| 166 | |
| 167 | intel_engines_park(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 168 | i915_timelines_park(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 169 | |
| 170 | i915_pmu_gt_parked(i915); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 171 | i915_vma_parked(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 172 | |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 173 | wakeref = fetch_and_zero(&i915->gt.awake); |
| 174 | GEM_BUG_ON(!wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 175 | |
| 176 | if (INTEL_GEN(i915) >= 6) |
| 177 | gen6_rps_idle(i915); |
| 178 | |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 179 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 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); |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 204 | assert_rpm_wakelock_held(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 205 | |
| 206 | if (i915->gt.awake) |
| 207 | return; |
| 208 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 209 | /* |
| 210 | * It seems that the DMC likes to transition between the DC states a lot |
| 211 | * when there are no connected displays (no active power domains) during |
| 212 | * command submission. |
| 213 | * |
| 214 | * This activity has negative impact on the performance of the chip with |
| 215 | * huge latencies observed in the interrupt handler and elsewhere. |
| 216 | * |
| 217 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 218 | * GT activity, preventing any DC state transitions. |
| 219 | */ |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 220 | i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 221 | GEM_BUG_ON(!i915->gt.awake); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 222 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 223 | if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ |
| 224 | i915->gt.epoch = 1; |
| 225 | |
| 226 | intel_enable_gt_powersave(i915); |
| 227 | i915_update_gfx_val(i915); |
| 228 | if (INTEL_GEN(i915) >= 6) |
| 229 | gen6_rps_busy(i915); |
| 230 | i915_pmu_gt_unparked(i915); |
| 231 | |
| 232 | intel_engines_unpark(i915); |
| 233 | |
| 234 | i915_queue_hangcheck(i915); |
| 235 | |
| 236 | queue_delayed_work(i915->wq, |
| 237 | &i915->gt.retire_work, |
| 238 | round_jiffies_up_relative(HZ)); |
| 239 | } |
| 240 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 241 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 242 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 243 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 244 | { |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 245 | struct drm_i915_private *dev_priv = to_i915(dev); |
Joonas Lahtinen | 62106b4 | 2016-03-18 10:42:57 +0200 | [diff] [blame] | 246 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 247 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 248 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 249 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 250 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 251 | pinned = ggtt->vm.reserved; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 252 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 253 | list_for_each_entry(vma, &ggtt->vm.active_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 254 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 255 | pinned += vma->node.size; |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 256 | list_for_each_entry(vma, &ggtt->vm.inactive_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 257 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 258 | pinned += vma->node.size; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 259 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 260 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 261 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 262 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 263 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 267 | 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] | 268 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 269 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 270 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 271 | struct sg_table *st; |
| 272 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 273 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 274 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 275 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 276 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 277 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 278 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 279 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 280 | /* Always aligning to the object size, allows a single allocation |
| 281 | * to handle all possible callers, and given typical object sizes, |
| 282 | * the alignment of the buddy allocation will naturally match. |
| 283 | */ |
| 284 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 285 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 286 | roundup_pow_of_two(obj->base.size)); |
| 287 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 288 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 289 | |
| 290 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 291 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 292 | struct page *page; |
| 293 | char *src; |
| 294 | |
| 295 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 296 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 297 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 298 | goto err_phys; |
| 299 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 300 | |
| 301 | src = kmap_atomic(page); |
| 302 | memcpy(vaddr, src, PAGE_SIZE); |
| 303 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 304 | kunmap_atomic(src); |
| 305 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 306 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 307 | vaddr += PAGE_SIZE; |
| 308 | } |
| 309 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 310 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 311 | |
| 312 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 313 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 314 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 315 | goto err_phys; |
| 316 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 317 | |
| 318 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 319 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 320 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 321 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | sg = st->sgl; |
| 325 | sg->offset = 0; |
| 326 | sg->length = obj->base.size; |
| 327 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 328 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 329 | sg_dma_len(sg) = obj->base.size; |
| 330 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 331 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 332 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 333 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 334 | |
| 335 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 336 | |
| 337 | err_phys: |
| 338 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 339 | |
| 340 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 343 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 344 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 345 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 346 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 347 | if (cpu_write_needs_clflush(obj)) |
| 348 | obj->cache_dirty = true; |
| 349 | } |
| 350 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 351 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 352 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 353 | struct sg_table *pages, |
| 354 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 355 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 356 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 357 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 358 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 359 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 360 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 361 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 362 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 363 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 364 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 365 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 366 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void |
| 370 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 371 | struct sg_table *pages) |
| 372 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 373 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 374 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 375 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 376 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 377 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 378 | int i; |
| 379 | |
| 380 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 381 | struct page *page; |
| 382 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 383 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 384 | page = shmem_read_mapping_page(mapping, i); |
| 385 | if (IS_ERR(page)) |
| 386 | continue; |
| 387 | |
| 388 | dst = kmap_atomic(page); |
| 389 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 390 | memcpy(dst, vaddr, PAGE_SIZE); |
| 391 | kunmap_atomic(dst); |
| 392 | |
| 393 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 394 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 395 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 396 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 397 | vaddr += PAGE_SIZE; |
| 398 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 399 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 402 | sg_free_table(pages); |
| 403 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 404 | |
| 405 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static void |
| 409 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 410 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 411 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 415 | .get_pages = i915_gem_object_get_pages_phys, |
| 416 | .put_pages = i915_gem_object_put_pages_phys, |
| 417 | .release = i915_gem_object_release_phys, |
| 418 | }; |
| 419 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 420 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 421 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 422 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 423 | { |
| 424 | struct i915_vma *vma; |
| 425 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 426 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 427 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 428 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 429 | |
| 430 | /* Closed vma are removed from the obj->vma_list - but they may |
| 431 | * still have an active binding on the object. To remove those we |
| 432 | * must wait for all rendering to complete to the object (as unbinding |
| 433 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 434 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 435 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 436 | if (ret) |
| 437 | return ret; |
| 438 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 439 | while ((vma = list_first_entry_or_null(&obj->vma_list, |
| 440 | struct i915_vma, |
| 441 | obj_link))) { |
| 442 | list_move_tail(&vma->obj_link, &still_in_list); |
| 443 | ret = i915_vma_unbind(vma); |
| 444 | if (ret) |
| 445 | break; |
| 446 | } |
| 447 | list_splice(&still_in_list, &obj->vma_list); |
| 448 | |
| 449 | return ret; |
| 450 | } |
| 451 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 452 | static long |
| 453 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 454 | unsigned int flags, |
| 455 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 456 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 457 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 458 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 459 | |
| 460 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 461 | |
| 462 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 463 | return timeout; |
| 464 | |
| 465 | if (!dma_fence_is_i915(fence)) |
| 466 | return dma_fence_wait_timeout(fence, |
| 467 | flags & I915_WAIT_INTERRUPTIBLE, |
| 468 | timeout); |
| 469 | |
| 470 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 471 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 472 | goto out; |
| 473 | |
Chris Wilson | e9af4ea | 2018-01-18 13:16:09 +0000 | [diff] [blame] | 474 | /* |
| 475 | * 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] | 476 | * this is undesirable and limits the throughput of the system, as |
| 477 | * many clients cannot continue processing user input/output whilst |
| 478 | * blocked. RPS autotuning may take tens of milliseconds to respond |
| 479 | * to the GPU load and thus incurs additional latency for the client. |
| 480 | * We can circumvent that by promoting the GPU frequency to maximum |
| 481 | * before we wait. This makes the GPU throttle up much more quickly |
| 482 | * (good for benchmarks and user experience, e.g. window animations), |
| 483 | * but at a cost of spending more power processing the workload |
| 484 | * (bad for battery). Not all clients even want their results |
| 485 | * immediately and for them we should just let the GPU select its own |
| 486 | * frequency to maximise efficiency. To prevent a single client from |
| 487 | * forcing the clocks too high for the whole system, we only allow |
| 488 | * each client to waitboost once in a busy period. |
| 489 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 490 | if (rps_client && !i915_request_started(rq)) { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 491 | if (INTEL_GEN(rq->i915) >= 6) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 492 | gen6_rps_boost(rq, rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 495 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 496 | |
| 497 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 498 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 499 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 500 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 501 | return timeout; |
| 502 | } |
| 503 | |
| 504 | static long |
| 505 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 506 | unsigned int flags, |
| 507 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 508 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 509 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 510 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 511 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 512 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 513 | |
| 514 | if (flags & I915_WAIT_ALL) { |
| 515 | struct dma_fence **shared; |
| 516 | unsigned int count, i; |
| 517 | int ret; |
| 518 | |
| 519 | ret = reservation_object_get_fences_rcu(resv, |
| 520 | &excl, &count, &shared); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 524 | for (i = 0; i < count; i++) { |
| 525 | timeout = i915_gem_object_wait_fence(shared[i], |
| 526 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 527 | rps_client); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 528 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 529 | break; |
| 530 | |
| 531 | dma_fence_put(shared[i]); |
| 532 | } |
| 533 | |
| 534 | for (; i < count; i++) |
| 535 | dma_fence_put(shared[i]); |
| 536 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 537 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 538 | /* |
| 539 | * If both shared fences and an exclusive fence exist, |
| 540 | * then by construction the shared fences must be later |
| 541 | * than the exclusive fence. If we successfully wait for |
| 542 | * all the shared fences, we know that the exclusive fence |
| 543 | * must all be signaled. If all the shared fences are |
| 544 | * signaled, we can prune the array and recover the |
| 545 | * floating references on the fences/requests. |
| 546 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 547 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 548 | } else { |
| 549 | excl = reservation_object_get_excl_rcu(resv); |
| 550 | } |
| 551 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 552 | if (excl && timeout >= 0) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 553 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
| 554 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 555 | |
| 556 | dma_fence_put(excl); |
| 557 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 558 | /* |
| 559 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 560 | * signaled and that the reservation object has not been changed (i.e. |
| 561 | * no new fences have been added). |
| 562 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 563 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 564 | if (reservation_object_trylock(resv)) { |
| 565 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 566 | reservation_object_add_excl_fence(resv, NULL); |
| 567 | reservation_object_unlock(resv); |
| 568 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 571 | return timeout; |
| 572 | } |
| 573 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 574 | static void __fence_set_priority(struct dma_fence *fence, |
| 575 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 576 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 577 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 578 | struct intel_engine_cs *engine; |
| 579 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 580 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 581 | return; |
| 582 | |
| 583 | rq = to_request(fence); |
| 584 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 585 | |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 586 | local_bh_disable(); |
| 587 | rcu_read_lock(); /* RCU serialisation for set-wedged protection */ |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 588 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 589 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 590 | rcu_read_unlock(); |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 591 | local_bh_enable(); /* kick the tasklets if queues were reprioritised */ |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 594 | static void fence_set_priority(struct dma_fence *fence, |
| 595 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 596 | { |
| 597 | /* Recurse once into a fence-array */ |
| 598 | if (dma_fence_is_array(fence)) { |
| 599 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 600 | int i; |
| 601 | |
| 602 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 603 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 604 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 605 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
| 609 | int |
| 610 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 611 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 612 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 613 | { |
| 614 | struct dma_fence *excl; |
| 615 | |
| 616 | if (flags & I915_WAIT_ALL) { |
| 617 | struct dma_fence **shared; |
| 618 | unsigned int count, i; |
| 619 | int ret; |
| 620 | |
| 621 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 622 | &excl, &count, &shared); |
| 623 | if (ret) |
| 624 | return ret; |
| 625 | |
| 626 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 627 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 628 | dma_fence_put(shared[i]); |
| 629 | } |
| 630 | |
| 631 | kfree(shared); |
| 632 | } else { |
| 633 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 634 | } |
| 635 | |
| 636 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 637 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 638 | dma_fence_put(excl); |
| 639 | } |
| 640 | return 0; |
| 641 | } |
| 642 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 643 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 644 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 645 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 646 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 647 | * @timeout: how long to wait |
Chris Wilson | a0a8b1c | 2017-11-09 14:06:44 +0000 | [diff] [blame] | 648 | * @rps_client: client (user process) to charge for any waitboosting |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 649 | */ |
| 650 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 651 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 652 | unsigned int flags, |
| 653 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 654 | struct intel_rps_client *rps_client) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 655 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 656 | might_sleep(); |
| 657 | #if IS_ENABLED(CONFIG_LOCKDEP) |
| 658 | GEM_BUG_ON(debug_locks && |
| 659 | !!lockdep_is_held(&obj->base.dev->struct_mutex) != |
| 660 | !!(flags & I915_WAIT_LOCKED)); |
| 661 | #endif |
| 662 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 663 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 664 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 665 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 666 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 667 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 671 | { |
| 672 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 673 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 674 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 677 | static int |
| 678 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 679 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 680 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 681 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 682 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 683 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 684 | |
| 685 | /* We manually control the domain here and pretend that it |
| 686 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 687 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 688 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 689 | if (copy_from_user(vaddr, user_data, args->size)) |
| 690 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 691 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 692 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 693 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 694 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 695 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 696 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 699 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 700 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 701 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 705 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 706 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 707 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 710 | static int |
| 711 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 712 | struct drm_i915_private *dev_priv, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 713 | uint64_t size, |
| 714 | uint32_t *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 715 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 716 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 717 | int ret; |
| 718 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 719 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 720 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 721 | if (size == 0) |
| 722 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 723 | |
| 724 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 725 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 726 | if (IS_ERR(obj)) |
| 727 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 728 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 729 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 730 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 731 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 732 | if (ret) |
| 733 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 734 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 735 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 739 | int |
| 740 | i915_gem_dumb_create(struct drm_file *file, |
| 741 | struct drm_device *dev, |
| 742 | struct drm_mode_create_dumb *args) |
| 743 | { |
| 744 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 745 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 746 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 747 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 748 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 749 | } |
| 750 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 751 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 752 | { |
| 753 | return !(obj->cache_level == I915_CACHE_NONE || |
| 754 | obj->cache_level == I915_CACHE_WT); |
| 755 | } |
| 756 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 757 | /** |
| 758 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 759 | * @dev: drm device pointer |
| 760 | * @data: ioctl data blob |
| 761 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 762 | */ |
| 763 | int |
| 764 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 765 | struct drm_file *file) |
| 766 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 767 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 768 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 769 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 770 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 771 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 772 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 773 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 774 | } |
| 775 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 776 | static inline enum fb_op_origin |
| 777 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 778 | { |
| 779 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 780 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 781 | } |
| 782 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 783 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 784 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 785 | intel_wakeref_t wakeref; |
| 786 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 787 | /* |
| 788 | * No actual flushing is required for the GTT write domain for reads |
| 789 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 790 | * as far as we know, so there's no chipset flush. It also doesn't |
| 791 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 792 | * |
| 793 | * However, we do have to enforce the order so that all writes through |
| 794 | * the GTT land before any writes to the device, such as updates to |
| 795 | * the GATT itself. |
| 796 | * |
| 797 | * We also have to wait a bit for the writes to land from the GTT. |
| 798 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 799 | * timing. This issue has only been observed when switching quickly |
| 800 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 801 | * 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] | 802 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 803 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 804 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 805 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 806 | wmb(); |
| 807 | |
| 808 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 809 | return; |
| 810 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 811 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 812 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 813 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 814 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 815 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 816 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 817 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 818 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 819 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static void |
| 823 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 824 | { |
| 825 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 826 | struct i915_vma *vma; |
| 827 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 828 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 829 | return; |
| 830 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 831 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 832 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 833 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 834 | |
| 835 | intel_fb_obj_flush(obj, |
| 836 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 837 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 838 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 839 | if (vma->iomap) |
| 840 | continue; |
| 841 | |
| 842 | i915_vma_unset_ggtt_write(vma); |
| 843 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 844 | break; |
| 845 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 846 | case I915_GEM_DOMAIN_WC: |
| 847 | wmb(); |
| 848 | break; |
| 849 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 850 | case I915_GEM_DOMAIN_CPU: |
| 851 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 852 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 853 | |
| 854 | case I915_GEM_DOMAIN_RENDER: |
| 855 | if (gpu_write_needs_clflush(obj)) |
| 856 | obj->cache_dirty = true; |
| 857 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 860 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 861 | } |
| 862 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 863 | /* |
| 864 | * Pins the specified object's pages and synchronizes the object with |
| 865 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 866 | * flush the object from the CPU cache. |
| 867 | */ |
| 868 | 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] | 869 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 870 | { |
| 871 | int ret; |
| 872 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 873 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 874 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 875 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 876 | if (!i915_gem_object_has_struct_page(obj)) |
| 877 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 878 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 879 | ret = i915_gem_object_wait(obj, |
| 880 | I915_WAIT_INTERRUPTIBLE | |
| 881 | I915_WAIT_LOCKED, |
| 882 | MAX_SCHEDULE_TIMEOUT, |
| 883 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 884 | if (ret) |
| 885 | return ret; |
| 886 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 887 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 888 | if (ret) |
| 889 | return ret; |
| 890 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 891 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 892 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 893 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 894 | if (ret) |
| 895 | goto err_unpin; |
| 896 | else |
| 897 | goto out; |
| 898 | } |
| 899 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 900 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 901 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 902 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 903 | * read domain and manually flush cachelines (if required). This |
| 904 | * optimizes for the case when the gpu will dirty the data |
| 905 | * anyway again before the next pread happens. |
| 906 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 907 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 908 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 909 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 910 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 911 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 912 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 913 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 914 | |
| 915 | err_unpin: |
| 916 | i915_gem_object_unpin_pages(obj); |
| 917 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 921 | unsigned int *needs_clflush) |
| 922 | { |
| 923 | int ret; |
| 924 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 925 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 926 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 927 | *needs_clflush = 0; |
| 928 | if (!i915_gem_object_has_struct_page(obj)) |
| 929 | return -ENODEV; |
| 930 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 931 | ret = i915_gem_object_wait(obj, |
| 932 | I915_WAIT_INTERRUPTIBLE | |
| 933 | I915_WAIT_LOCKED | |
| 934 | I915_WAIT_ALL, |
| 935 | MAX_SCHEDULE_TIMEOUT, |
| 936 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 937 | if (ret) |
| 938 | return ret; |
| 939 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 940 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 941 | if (ret) |
| 942 | return ret; |
| 943 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 944 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 945 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 946 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 947 | if (ret) |
| 948 | goto err_unpin; |
| 949 | else |
| 950 | goto out; |
| 951 | } |
| 952 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 953 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 954 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 955 | /* If we're not in the cpu write domain, set ourself into the |
| 956 | * gtt write domain and manually flush cachelines (as required). |
| 957 | * This optimizes for the case when the gpu will use the data |
| 958 | * right away and we therefore have to clflush anyway. |
| 959 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 960 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 961 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 962 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 963 | /* |
| 964 | * Same trick applies to invalidate partially written |
| 965 | * cachelines read before writing. |
| 966 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 967 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 968 | *needs_clflush |= CLFLUSH_BEFORE; |
| 969 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 970 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 971 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 972 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 973 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 974 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 975 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 976 | |
| 977 | err_unpin: |
| 978 | i915_gem_object_unpin_pages(obj); |
| 979 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 980 | } |
| 981 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 982 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 983 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 984 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 985 | { |
| 986 | char *vaddr; |
| 987 | int ret; |
| 988 | |
| 989 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 990 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 991 | if (needs_clflush) |
| 992 | drm_clflush_virt_range(vaddr + offset, len); |
| 993 | |
| 994 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 995 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 996 | kunmap(page); |
| 997 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 998 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static int |
| 1002 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 1003 | struct drm_i915_gem_pread *args) |
| 1004 | { |
| 1005 | char __user *user_data; |
| 1006 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1007 | unsigned int needs_clflush; |
| 1008 | unsigned int idx, offset; |
| 1009 | int ret; |
| 1010 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1011 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 1012 | if (ret) |
| 1013 | return ret; |
| 1014 | |
| 1015 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 1016 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 1017 | if (ret) |
| 1018 | return ret; |
| 1019 | |
| 1020 | remain = args->size; |
| 1021 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1022 | offset = offset_in_page(args->offset); |
| 1023 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1024 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1025 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1026 | |
| 1027 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1028 | needs_clflush); |
| 1029 | if (ret) |
| 1030 | break; |
| 1031 | |
| 1032 | remain -= length; |
| 1033 | user_data += length; |
| 1034 | offset = 0; |
| 1035 | } |
| 1036 | |
| 1037 | i915_gem_obj_finish_shmem_access(obj); |
| 1038 | return ret; |
| 1039 | } |
| 1040 | |
| 1041 | static inline bool |
| 1042 | gtt_user_read(struct io_mapping *mapping, |
| 1043 | loff_t base, int offset, |
| 1044 | char __user *user_data, int length) |
| 1045 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1046 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1047 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1048 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1049 | /* 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] | 1050 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1051 | unwritten = __copy_to_user_inatomic(user_data, |
| 1052 | (void __force *)vaddr + offset, |
| 1053 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1054 | io_mapping_unmap_atomic(vaddr); |
| 1055 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1056 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1057 | unwritten = copy_to_user(user_data, |
| 1058 | (void __force *)vaddr + offset, |
| 1059 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1060 | io_mapping_unmap(vaddr); |
| 1061 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1062 | return unwritten; |
| 1063 | } |
| 1064 | |
| 1065 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1066 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1067 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1068 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1069 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1070 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1071 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1072 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1073 | struct i915_vma *vma; |
| 1074 | void __user *user_data; |
| 1075 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1076 | int ret; |
| 1077 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1078 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1079 | if (ret) |
| 1080 | return ret; |
| 1081 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1082 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1083 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1084 | PIN_MAPPABLE | |
| 1085 | PIN_NONFAULT | |
| 1086 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1087 | if (!IS_ERR(vma)) { |
| 1088 | node.start = i915_ggtt_offset(vma); |
| 1089 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1090 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1091 | if (ret) { |
| 1092 | i915_vma_unpin(vma); |
| 1093 | vma = ERR_PTR(ret); |
| 1094 | } |
| 1095 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1096 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1097 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1098 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1099 | goto out_unlock; |
| 1100 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1104 | if (ret) |
| 1105 | goto out_unpin; |
| 1106 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1107 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1108 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1109 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1110 | remain = args->size; |
| 1111 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1112 | |
| 1113 | while (remain > 0) { |
| 1114 | /* Operation in this page |
| 1115 | * |
| 1116 | * page_base = page offset within aperture |
| 1117 | * page_offset = offset within page |
| 1118 | * page_length = bytes to copy for this page |
| 1119 | */ |
| 1120 | u32 page_base = node.start; |
| 1121 | unsigned page_offset = offset_in_page(offset); |
| 1122 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1123 | page_length = remain < page_length ? remain : page_length; |
| 1124 | if (node.allocated) { |
| 1125 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1126 | ggtt->vm.insert_page(&ggtt->vm, |
| 1127 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1128 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1129 | wmb(); |
| 1130 | } else { |
| 1131 | page_base += offset & PAGE_MASK; |
| 1132 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1133 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1134 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1135 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1136 | ret = -EFAULT; |
| 1137 | break; |
| 1138 | } |
| 1139 | |
| 1140 | remain -= page_length; |
| 1141 | user_data += page_length; |
| 1142 | offset += page_length; |
| 1143 | } |
| 1144 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1145 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1146 | out_unpin: |
| 1147 | if (node.allocated) { |
| 1148 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1149 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1150 | remove_mappable_node(&node); |
| 1151 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1152 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1153 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1154 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1155 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1156 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1157 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1158 | return ret; |
| 1159 | } |
| 1160 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1161 | /** |
| 1162 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1163 | * @dev: drm device pointer |
| 1164 | * @data: ioctl data blob |
| 1165 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1166 | * |
| 1167 | * On error, the contents of *data are undefined. |
| 1168 | */ |
| 1169 | int |
| 1170 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1171 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1172 | { |
| 1173 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1174 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1175 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1176 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1177 | if (args->size == 0) |
| 1178 | return 0; |
| 1179 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1180 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1181 | args->size)) |
| 1182 | return -EFAULT; |
| 1183 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1184 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1185 | if (!obj) |
| 1186 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1187 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1188 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1189 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1190 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1191 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1192 | } |
| 1193 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1194 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1195 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1196 | ret = i915_gem_object_wait(obj, |
| 1197 | I915_WAIT_INTERRUPTIBLE, |
| 1198 | MAX_SCHEDULE_TIMEOUT, |
| 1199 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1200 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1201 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1202 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1203 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1204 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1205 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1206 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1207 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1208 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1209 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1210 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1211 | i915_gem_object_unpin_pages(obj); |
| 1212 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1213 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1214 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1217 | /* This is the fast write path which cannot handle |
| 1218 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1219 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1220 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1221 | static inline bool |
| 1222 | ggtt_write(struct io_mapping *mapping, |
| 1223 | loff_t base, int offset, |
| 1224 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1225 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1226 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1227 | unsigned long unwritten; |
| 1228 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1229 | /* 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] | 1230 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1231 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1232 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1233 | io_mapping_unmap_atomic(vaddr); |
| 1234 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1235 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1236 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1237 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1238 | io_mapping_unmap(vaddr); |
| 1239 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1240 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1241 | return unwritten; |
| 1242 | } |
| 1243 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1244 | /** |
| 1245 | * This is the fast pwrite path, where we copy the data directly from the |
| 1246 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1247 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1248 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1249 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1250 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1251 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1252 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1253 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1254 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1255 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1256 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1257 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1258 | struct i915_vma *vma; |
| 1259 | u64 remain, offset; |
| 1260 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1261 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1262 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1263 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1264 | if (ret) |
| 1265 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1266 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1267 | if (i915_gem_object_has_struct_page(obj)) { |
| 1268 | /* |
| 1269 | * Avoid waking the device up if we can fallback, as |
| 1270 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1271 | * depending on PCI sleeps and our own resume time). |
| 1272 | * This easily dwarfs any performance advantage from |
| 1273 | * using the cache bypass of indirect GGTT access. |
| 1274 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1275 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1276 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1277 | ret = -EFAULT; |
| 1278 | goto out_unlock; |
| 1279 | } |
| 1280 | } else { |
| 1281 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1282 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1285 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1286 | PIN_MAPPABLE | |
| 1287 | PIN_NONFAULT | |
| 1288 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1289 | if (!IS_ERR(vma)) { |
| 1290 | node.start = i915_ggtt_offset(vma); |
| 1291 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1292 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1293 | if (ret) { |
| 1294 | i915_vma_unpin(vma); |
| 1295 | vma = ERR_PTR(ret); |
| 1296 | } |
| 1297 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1298 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1299 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1300 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1301 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1302 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1303 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1304 | |
| 1305 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1306 | if (ret) |
| 1307 | goto out_unpin; |
| 1308 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1309 | mutex_unlock(&i915->drm.struct_mutex); |
| 1310 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1311 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1312 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1313 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1314 | offset = args->offset; |
| 1315 | remain = args->size; |
| 1316 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1317 | /* Operation in this page |
| 1318 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1319 | * page_base = page offset within aperture |
| 1320 | * page_offset = offset within page |
| 1321 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1322 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1323 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1324 | unsigned int page_offset = offset_in_page(offset); |
| 1325 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1326 | page_length = remain < page_length ? remain : page_length; |
| 1327 | if (node.allocated) { |
| 1328 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1329 | ggtt->vm.insert_page(&ggtt->vm, |
| 1330 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1331 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1332 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1333 | } else { |
| 1334 | page_base += offset & PAGE_MASK; |
| 1335 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1336 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1337 | * source page isn't available. Return the error and we'll |
| 1338 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1339 | * If the object is non-shmem backed, we retry again with the |
| 1340 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1341 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1342 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1343 | user_data, page_length)) { |
| 1344 | ret = -EFAULT; |
| 1345 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1346 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1347 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1348 | remain -= page_length; |
| 1349 | user_data += page_length; |
| 1350 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1351 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1352 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1353 | |
| 1354 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1355 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1356 | if (node.allocated) { |
| 1357 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1358 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1359 | remove_mappable_node(&node); |
| 1360 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1361 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1362 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1363 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1364 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1365 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1366 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1367 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1370 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1371 | * Flushes invalid cachelines before writing to the target if |
| 1372 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1373 | * writing if needs_clflush is set. |
| 1374 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1375 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1376 | 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] | 1377 | bool needs_clflush_before, |
| 1378 | bool needs_clflush_after) |
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 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1381 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1382 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1383 | vaddr = kmap(page); |
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 | if (needs_clflush_before) |
| 1386 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1387 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1388 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1389 | if (!ret && needs_clflush_after) |
| 1390 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1391 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1392 | kunmap(page); |
| 1393 | |
| 1394 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | static int |
| 1398 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1399 | const struct drm_i915_gem_pwrite *args) |
| 1400 | { |
| 1401 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1402 | void __user *user_data; |
| 1403 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1404 | unsigned int partial_cacheline_write; |
| 1405 | unsigned int needs_clflush; |
| 1406 | unsigned int offset, idx; |
| 1407 | int ret; |
| 1408 | |
| 1409 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1410 | if (ret) |
| 1411 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1412 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1413 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1414 | mutex_unlock(&i915->drm.struct_mutex); |
| 1415 | if (ret) |
| 1416 | return ret; |
| 1417 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1418 | /* If we don't overwrite a cacheline completely we need to be |
| 1419 | * careful to have up-to-date data by first clflushing. Don't |
| 1420 | * overcomplicate things and flush the entire patch. |
| 1421 | */ |
| 1422 | partial_cacheline_write = 0; |
| 1423 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1424 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1425 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1426 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1427 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1428 | offset = offset_in_page(args->offset); |
| 1429 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1430 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1431 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1432 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1433 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1434 | (offset | length) & partial_cacheline_write, |
| 1435 | needs_clflush & CLFLUSH_AFTER); |
| 1436 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1437 | break; |
| 1438 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1439 | remain -= length; |
| 1440 | user_data += length; |
| 1441 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1444 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1445 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1446 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /** |
| 1450 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1451 | * @dev: drm device |
| 1452 | * @data: ioctl data blob |
| 1453 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1454 | * |
| 1455 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1456 | */ |
| 1457 | int |
| 1458 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1459 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1460 | { |
| 1461 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1462 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1463 | int ret; |
| 1464 | |
| 1465 | if (args->size == 0) |
| 1466 | return 0; |
| 1467 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1468 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1469 | return -EFAULT; |
| 1470 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1471 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1472 | if (!obj) |
| 1473 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1474 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1475 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1476 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1477 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1478 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1479 | } |
| 1480 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1481 | /* Writes not allowed into this read-only object */ |
| 1482 | if (i915_gem_object_is_readonly(obj)) { |
| 1483 | ret = -EINVAL; |
| 1484 | goto err; |
| 1485 | } |
| 1486 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1487 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1488 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1489 | ret = -ENODEV; |
| 1490 | if (obj->ops->pwrite) |
| 1491 | ret = obj->ops->pwrite(obj, args); |
| 1492 | if (ret != -ENODEV) |
| 1493 | goto err; |
| 1494 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1495 | ret = i915_gem_object_wait(obj, |
| 1496 | I915_WAIT_INTERRUPTIBLE | |
| 1497 | I915_WAIT_ALL, |
| 1498 | MAX_SCHEDULE_TIMEOUT, |
| 1499 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1500 | if (ret) |
| 1501 | goto err; |
| 1502 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1503 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1504 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1505 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1506 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1507 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1508 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1509 | * it would end up going through the fenced access, and we'll get |
| 1510 | * different detiling behavior between reading and writing. |
| 1511 | * pread/pwrite currently are reading and writing from the CPU |
| 1512 | * perspective, requiring manual detiling by the client. |
| 1513 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1514 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1515 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1516 | /* Note that the gtt paths might fail with non-page-backed user |
| 1517 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1518 | * textures). Fallback to the shmem path in that case. |
| 1519 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1520 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1521 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1522 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1523 | if (obj->phys_handle) |
| 1524 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1525 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1526 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1527 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1528 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1529 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1530 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1531 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1532 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1535 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1536 | { |
| 1537 | struct drm_i915_private *i915; |
| 1538 | struct list_head *list; |
| 1539 | struct i915_vma *vma; |
| 1540 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1541 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1542 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1543 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1544 | if (i915_vma_is_active(vma)) |
| 1545 | continue; |
| 1546 | |
| 1547 | if (!drm_mm_node_allocated(&vma->node)) |
| 1548 | continue; |
| 1549 | |
| 1550 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 1551 | } |
| 1552 | |
| 1553 | i915 = to_i915(obj->base.dev); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1554 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1555 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1556 | list_move_tail(&obj->mm.link, list); |
| 1557 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1558 | } |
| 1559 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1560 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1561 | * Called when user space prepares to use an object with the CPU, either |
| 1562 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1563 | * @dev: drm device |
| 1564 | * @data: ioctl data blob |
| 1565 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1566 | */ |
| 1567 | int |
| 1568 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1569 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1570 | { |
| 1571 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1572 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1573 | uint32_t read_domains = args->read_domains; |
| 1574 | uint32_t write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1575 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1576 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1577 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1578 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1579 | return -EINVAL; |
| 1580 | |
| 1581 | /* Having something in the write domain implies it's in the read |
| 1582 | * domain, and only that read domain. Enforce that in the request. |
| 1583 | */ |
| 1584 | if (write_domain != 0 && read_domains != write_domain) |
| 1585 | return -EINVAL; |
| 1586 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1587 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1588 | if (!obj) |
| 1589 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1590 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1591 | /* Try to flush the object off the GPU without holding the lock. |
| 1592 | * We will repeat the flush holding the lock in the normal manner |
| 1593 | * to catch cases where we are gazumped. |
| 1594 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1595 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1596 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1597 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1598 | (write_domain ? I915_WAIT_ALL : 0), |
| 1599 | MAX_SCHEDULE_TIMEOUT, |
| 1600 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1601 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1602 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1603 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1604 | /* |
| 1605 | * Proxy objects do not control access to the backing storage, ergo |
| 1606 | * they cannot be used as a means to manipulate the cache domain |
| 1607 | * tracking for that backing storage. The proxy object is always |
| 1608 | * considered to be outside of any cache domain. |
| 1609 | */ |
| 1610 | if (i915_gem_object_is_proxy(obj)) { |
| 1611 | err = -ENXIO; |
| 1612 | goto out; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1617 | * direct access in memory with previous cached writes through |
| 1618 | * shmemfs and that our cache domain tracking remains valid. |
| 1619 | * For example, if the obj->filp was moved to swap without us |
| 1620 | * being notified and releasing the pages, we would mistakenly |
| 1621 | * continue to assume that the obj remained out of the CPU cached |
| 1622 | * domain. |
| 1623 | */ |
| 1624 | err = i915_gem_object_pin_pages(obj); |
| 1625 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1626 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1627 | |
| 1628 | err = i915_mutex_lock_interruptible(dev); |
| 1629 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1630 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1631 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1632 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1633 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1634 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1635 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1636 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1637 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1638 | |
| 1639 | /* And bump the LRU for this access */ |
| 1640 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1641 | |
| 1642 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1643 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1644 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1645 | intel_fb_obj_invalidate(obj, |
| 1646 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1647 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1648 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1649 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1650 | out: |
| 1651 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1652 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /** |
| 1656 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1657 | * @dev: drm device |
| 1658 | * @data: ioctl data blob |
| 1659 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1660 | */ |
| 1661 | int |
| 1662 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1663 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1664 | { |
| 1665 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1666 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1667 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1668 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1669 | if (!obj) |
| 1670 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1671 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1672 | /* |
| 1673 | * Proxy objects are barred from CPU access, so there is no |
| 1674 | * need to ban sw_finish as it is a nop. |
| 1675 | */ |
| 1676 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1677 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1678 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1679 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1680 | |
| 1681 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1685 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1686 | * it is mapped to. |
| 1687 | * @dev: drm device |
| 1688 | * @data: ioctl data blob |
| 1689 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1690 | * |
| 1691 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1692 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1693 | * |
| 1694 | * IMPORTANT: |
| 1695 | * |
| 1696 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1697 | * mmap support, please don't implement mmap support like here. The modern way |
| 1698 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1699 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1700 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1701 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1702 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1703 | */ |
| 1704 | int |
| 1705 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1706 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1707 | { |
| 1708 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1709 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1710 | unsigned long addr; |
| 1711 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1712 | if (args->flags & ~(I915_MMAP_WC)) |
| 1713 | return -EINVAL; |
| 1714 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1715 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1716 | return -ENODEV; |
| 1717 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1718 | obj = i915_gem_object_lookup(file, args->handle); |
| 1719 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1720 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1721 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1722 | /* prime objects have no backing filp to GEM mmap |
| 1723 | * pages from. |
| 1724 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1725 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1726 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1727 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1728 | } |
| 1729 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1730 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1731 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1732 | args->offset); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1733 | if (args->flags & I915_MMAP_WC) { |
| 1734 | struct mm_struct *mm = current->mm; |
| 1735 | struct vm_area_struct *vma; |
| 1736 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1737 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1738 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1739 | return -EINTR; |
| 1740 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1741 | vma = find_vma(mm, addr); |
| 1742 | if (vma) |
| 1743 | vma->vm_page_prot = |
| 1744 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1745 | else |
| 1746 | addr = -ENOMEM; |
| 1747 | up_write(&mm->mmap_sem); |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1748 | |
| 1749 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1750 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1751 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1752 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1753 | if (IS_ERR((void *)addr)) |
| 1754 | return addr; |
| 1755 | |
| 1756 | args->addr_ptr = (uint64_t) addr; |
| 1757 | |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1761 | 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] | 1762 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1763 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1764 | } |
| 1765 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1766 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1767 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1768 | * |
| 1769 | * A history of the GTT mmap interface: |
| 1770 | * |
| 1771 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1772 | * aligned and suitable for fencing, and still fit into the available |
| 1773 | * mappable space left by the pinned display objects. A classic problem |
| 1774 | * we called the page-fault-of-doom where we would ping-pong between |
| 1775 | * two objects that could not fit inside the GTT and so the memcpy |
| 1776 | * would page one object in at the expense of the other between every |
| 1777 | * single byte. |
| 1778 | * |
| 1779 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1780 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1781 | * object is too large for the available space (or simply too large |
| 1782 | * for the mappable aperture!), a view is created instead and faulted |
| 1783 | * into userspace. (This view is aligned and sized appropriately for |
| 1784 | * fenced access.) |
| 1785 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1786 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1787 | * delayed writes via GTT before performing direct access via WC. |
| 1788 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1789 | * Restrictions: |
| 1790 | * |
| 1791 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1792 | * hangs on some architectures, corruption on others. An attempt to service |
| 1793 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1794 | * |
| 1795 | * * the object must be able to fit into RAM (physical memory, though no |
| 1796 | * limited to the mappable aperture). |
| 1797 | * |
| 1798 | * |
| 1799 | * Caveats: |
| 1800 | * |
| 1801 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1802 | * all data to system memory. Subsequent access will not be synchronized. |
| 1803 | * |
| 1804 | * * all mappings are revoked on runtime device suspend. |
| 1805 | * |
| 1806 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1807 | * (older machines require fence register for display and blitter access |
| 1808 | * as well). Contention of the fence registers will cause the previous users |
| 1809 | * to be unmapped and any new access will generate new page faults. |
| 1810 | * |
| 1811 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1812 | * rather than the expected SIGSEGV. |
| 1813 | */ |
| 1814 | int i915_gem_mmap_gtt_version(void) |
| 1815 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1816 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1817 | } |
| 1818 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1819 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1820 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1821 | pgoff_t page_offset, |
| 1822 | unsigned int chunk) |
| 1823 | { |
| 1824 | struct i915_ggtt_view view; |
| 1825 | |
| 1826 | if (i915_gem_object_is_tiled(obj)) |
| 1827 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1828 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1829 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1830 | view.partial.offset = rounddown(page_offset, chunk); |
| 1831 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1832 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1833 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1834 | |
| 1835 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1836 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1837 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1838 | |
| 1839 | return view; |
| 1840 | } |
| 1841 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1842 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1843 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1844 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1845 | * |
| 1846 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1847 | * from userspace. The fault handler takes care of binding the object to |
| 1848 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1849 | * only if needed based on whether the old reg is still valid or the object |
| 1850 | * is tiled) and inserting a new PTE into the faulting process. |
| 1851 | * |
| 1852 | * Note that the faulting process may involve evicting existing objects |
| 1853 | * from the GTT and/or fence registers to make room. So performance may |
| 1854 | * suffer if the GTT working set is large or there are few fence registers |
| 1855 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1856 | * |
| 1857 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1858 | * 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] | 1859 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1860 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1861 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1862 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1863 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1864 | 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] | 1865 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1866 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1867 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1868 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1869 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1870 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1871 | pgoff_t page_offset; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1872 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1873 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1874 | /* Sanity check that we allow writing into this object */ |
| 1875 | if (i915_gem_object_is_readonly(obj) && write) |
| 1876 | return VM_FAULT_SIGBUS; |
| 1877 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1878 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1879 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1880 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1881 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1882 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1883 | /* 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] | 1884 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1885 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1886 | * where we are gazumped. |
| 1887 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1888 | ret = i915_gem_object_wait(obj, |
| 1889 | I915_WAIT_INTERRUPTIBLE, |
| 1890 | MAX_SCHEDULE_TIMEOUT, |
| 1891 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1892 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1893 | goto err; |
| 1894 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1895 | ret = i915_gem_object_pin_pages(obj); |
| 1896 | if (ret) |
| 1897 | goto err; |
| 1898 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1899 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1900 | |
| 1901 | ret = i915_mutex_lock_interruptible(dev); |
| 1902 | if (ret) |
| 1903 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1904 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1905 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1906 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1907 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1908 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 1911 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1912 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1913 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1914 | PIN_MAPPABLE | |
| 1915 | PIN_NONBLOCK | |
| 1916 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1917 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1918 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1919 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1920 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1921 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1922 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1923 | flags = PIN_MAPPABLE; |
| 1924 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1925 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1926 | |
| 1927 | /* |
| 1928 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1929 | * all hope that the hardware is able to track future writes. |
| 1930 | */ |
| 1931 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1932 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1933 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1934 | if (IS_ERR(vma) && !view.type) { |
| 1935 | flags = PIN_MAPPABLE; |
| 1936 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1937 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1938 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1939 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1940 | if (IS_ERR(vma)) { |
| 1941 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1942 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1943 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1944 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1945 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1946 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1947 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1948 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1949 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1950 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1951 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1952 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1953 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1954 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1955 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1956 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1957 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1958 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1959 | if (ret) |
| 1960 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1961 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1962 | /* Mark as being mmapped into userspace for later revocation */ |
| 1963 | assert_rpm_wakelock_held(dev_priv); |
| 1964 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1965 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1966 | GEM_BUG_ON(!obj->userfault_count); |
| 1967 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1968 | i915_vma_set_ggtt_write(vma); |
| 1969 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1970 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 1971 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1972 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1973 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1974 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1975 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1976 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1977 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1978 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1979 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1980 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1981 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1982 | /* |
| 1983 | * We eat errors when the gpu is terminally wedged to avoid |
| 1984 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1985 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1986 | * and so needs to be reported. |
| 1987 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1988 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) |
| 1989 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1990 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1991 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1992 | /* |
| 1993 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1994 | * handler to reset everything when re-faulting in |
| 1995 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1996 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1997 | case 0: |
| 1998 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1999 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 2000 | case -EBUSY: |
| 2001 | /* |
| 2002 | * EBUSY is ok: this just means that another thread |
| 2003 | * already did the job. |
| 2004 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2005 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2006 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2007 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2008 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2009 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2010 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2011 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2012 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 2013 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2017 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2018 | { |
| 2019 | struct i915_vma *vma; |
| 2020 | |
| 2021 | GEM_BUG_ON(!obj->userfault_count); |
| 2022 | |
| 2023 | obj->userfault_count = 0; |
| 2024 | list_del(&obj->userfault_link); |
| 2025 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2026 | obj->base.dev->anon_inode->i_mapping); |
| 2027 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 2028 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2029 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2030 | } |
| 2031 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2032 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2033 | * i915_gem_release_mmap - remove physical page mappings |
| 2034 | * @obj: obj in question |
| 2035 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2036 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2037 | * relinquish ownership of the pages back to the system. |
| 2038 | * |
| 2039 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2040 | * object through the GTT and then lose the fence register due to |
| 2041 | * resource pressure. Similarly if the object has been moved out of the |
| 2042 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2043 | * mapping will then trigger a page fault on the next user access, allowing |
| 2044 | * fixup by i915_gem_fault(). |
| 2045 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2046 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2047 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2048 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2049 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2050 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2051 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2052 | /* Serialisation between user GTT access and our code depends upon |
| 2053 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2054 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2055 | * |
| 2056 | * Note that RPM complicates somewhat by adding an additional |
| 2057 | * requirement that operations to the GGTT be made holding the RPM |
| 2058 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2059 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2060 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2061 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2062 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2063 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2064 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2065 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2066 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2067 | |
| 2068 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2069 | * memory transactions from userspace before we return. The TLB |
| 2070 | * flushing implied above by changing the PTE above *should* be |
| 2071 | * sufficient, an extra barrier here just provides us with a bit |
| 2072 | * of paranoid documentation about our requirement to serialise |
| 2073 | * memory writes before touching registers / GSM. |
| 2074 | */ |
| 2075 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2076 | |
| 2077 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2078 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2079 | } |
| 2080 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2081 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
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 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2084 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2085 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2086 | /* |
| 2087 | * Only called during RPM suspend. All users of the userfault_list |
| 2088 | * must be holding an RPM wakeref to ensure that this can not |
| 2089 | * run concurrently with themselves (and use the struct_mutex for |
| 2090 | * protection between themselves). |
| 2091 | */ |
| 2092 | |
| 2093 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2094 | &dev_priv->mm.userfault_list, userfault_link) |
| 2095 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2096 | |
| 2097 | /* The fence will be lost when the device powers down. If any were |
| 2098 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2099 | * down! All other fences will be reacquired by the user upon waking. |
| 2100 | */ |
| 2101 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2102 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2103 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2104 | /* Ideally we want to assert that the fence register is not |
| 2105 | * live at this point (i.e. that no piece of code will be |
| 2106 | * trying to write through fence + GTT, as that both violates |
| 2107 | * our tracking of activity and associated locking/barriers, |
| 2108 | * but also is illegal given that the hw is powered down). |
| 2109 | * |
| 2110 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2111 | * That is not sufficient, and we need a more fine-grained |
| 2112 | * tool if we want to have a sanity check here. |
| 2113 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2114 | |
| 2115 | if (!reg->vma) |
| 2116 | continue; |
| 2117 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2118 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2119 | reg->dirty = true; |
| 2120 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2123 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2124 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2125 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2126 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2127 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2128 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2129 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2130 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2131 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2132 | /* Attempt to reap some mmap space from dead objects */ |
| 2133 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2134 | err = i915_gem_wait_for_idle(dev_priv, |
| 2135 | I915_WAIT_INTERRUPTIBLE, |
| 2136 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2137 | if (err) |
| 2138 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2139 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2140 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2141 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2142 | if (!err) |
| 2143 | break; |
| 2144 | |
| 2145 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2146 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2147 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2151 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2152 | drm_gem_free_mmap_offset(&obj->base); |
| 2153 | } |
| 2154 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2155 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2156 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2157 | struct drm_device *dev, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2158 | uint32_t handle, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2159 | uint64_t *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2160 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2161 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2162 | int ret; |
| 2163 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2164 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2165 | if (!obj) |
| 2166 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2167 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2168 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2169 | if (ret == 0) |
| 2170 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2171 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2172 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2173 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2174 | } |
| 2175 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2176 | /** |
| 2177 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2178 | * @dev: DRM device |
| 2179 | * @data: GTT mapping ioctl data |
| 2180 | * @file: GEM object info |
| 2181 | * |
| 2182 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2183 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2184 | * up so we can get faults in the handler above. |
| 2185 | * |
| 2186 | * The fault handler will take care of binding the object into the GTT |
| 2187 | * (since it may have been evicted to make room for something), allocating |
| 2188 | * a fence register, and mapping the appropriate aperture address into |
| 2189 | * userspace. |
| 2190 | */ |
| 2191 | int |
| 2192 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2193 | struct drm_file *file) |
| 2194 | { |
| 2195 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2196 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2197 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2198 | } |
| 2199 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2200 | /* Immediately discard the backing storage */ |
| 2201 | static void |
| 2202 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2203 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2204 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2205 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2206 | if (obj->base.filp == NULL) |
| 2207 | return; |
| 2208 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2209 | /* Our goal here is to return as much of the memory as |
| 2210 | * is possible back to the system as we are called from OOM. |
| 2211 | * To do this we must instruct the shmfs to drop all of its |
| 2212 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2213 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2214 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2215 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2216 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2217 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2218 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2219 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2220 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2221 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2222 | struct address_space *mapping; |
| 2223 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2224 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2225 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2226 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2227 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2228 | case I915_MADV_DONTNEED: |
| 2229 | i915_gem_object_truncate(obj); |
| 2230 | case __I915_MADV_PURGED: |
| 2231 | return; |
| 2232 | } |
| 2233 | |
| 2234 | if (obj->base.filp == NULL) |
| 2235 | return; |
| 2236 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2237 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2238 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2239 | } |
| 2240 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2241 | /* |
| 2242 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2243 | * ref count of those pages. |
| 2244 | */ |
| 2245 | static void check_release_pagevec(struct pagevec *pvec) |
| 2246 | { |
| 2247 | check_move_unevictable_pages(pvec); |
| 2248 | __pagevec_release(pvec); |
| 2249 | cond_resched(); |
| 2250 | } |
| 2251 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2252 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2253 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2254 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2255 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2256 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2257 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2258 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2259 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2260 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2261 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2262 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2263 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2264 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2265 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2266 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2267 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2268 | |
| 2269 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2270 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2271 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2272 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2273 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2274 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2275 | mark_page_accessed(page); |
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_add(&pvec, page)) |
| 2278 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2279 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2280 | if (pagevec_count(&pvec)) |
| 2281 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2282 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2283 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2284 | sg_free_table(pages); |
| 2285 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2286 | } |
| 2287 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2288 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2289 | { |
| 2290 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2291 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2292 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2293 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2294 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2295 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2296 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2299 | static struct sg_table * |
| 2300 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2301 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2302 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2303 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2304 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2305 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2306 | if (IS_ERR_OR_NULL(pages)) |
| 2307 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2308 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2309 | spin_lock(&i915->mm.obj_lock); |
| 2310 | list_del(&obj->mm.link); |
| 2311 | spin_unlock(&i915->mm.obj_lock); |
| 2312 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2313 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2314 | void *ptr; |
| 2315 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2316 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2317 | if (is_vmalloc_addr(ptr)) |
| 2318 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2319 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2320 | kunmap(kmap_to_page(ptr)); |
| 2321 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2322 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2323 | } |
| 2324 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2325 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2326 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2327 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2328 | return pages; |
| 2329 | } |
| 2330 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2331 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2332 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2333 | { |
| 2334 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2335 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2336 | |
| 2337 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2338 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2339 | |
| 2340 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2341 | |
| 2342 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2343 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2344 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2345 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2346 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2347 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2348 | |
| 2349 | /* |
| 2350 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2351 | * array, hence protect them from being reaped by removing them from gtt |
| 2352 | * lists early. |
| 2353 | */ |
| 2354 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2355 | |
| 2356 | /* |
| 2357 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2358 | * NULL pages. In the future, when we have more asynchronous |
| 2359 | * get_pages backends we should be better able to handle the |
| 2360 | * cancellation of the async task in a more uniform manner. |
| 2361 | */ |
| 2362 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2363 | pages = ERR_PTR(-EINVAL); |
| 2364 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2365 | if (!IS_ERR(pages)) |
| 2366 | obj->ops->put_pages(obj, pages); |
| 2367 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2368 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2369 | unlock: |
| 2370 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame^] | 2371 | |
| 2372 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2373 | } |
| 2374 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2375 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2376 | { |
| 2377 | struct sg_table new_st; |
| 2378 | struct scatterlist *sg, *new_sg; |
| 2379 | unsigned int i; |
| 2380 | |
| 2381 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2382 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2383 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2384 | 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] | 2385 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2386 | |
| 2387 | new_sg = new_st.sgl; |
| 2388 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2389 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2390 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2391 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2392 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2393 | new_sg = sg_next(new_sg); |
| 2394 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2395 | 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] | 2396 | |
| 2397 | sg_free_table(orig_st); |
| 2398 | |
| 2399 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2400 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2403 | 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] | 2404 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2405 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2406 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2407 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2408 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2409 | struct sg_table *st; |
| 2410 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2411 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2412 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2413 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2414 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2415 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2416 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2417 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2418 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2419 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2420 | /* |
| 2421 | * 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] | 2422 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2423 | * a GPU cache |
| 2424 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2425 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2426 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2427 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2428 | /* |
| 2429 | * If there's no chance of allocating enough pages for the whole |
| 2430 | * object, bail early. |
| 2431 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2432 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2433 | return -ENOMEM; |
| 2434 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2435 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2436 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2437 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2438 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2439 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2440 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2441 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2442 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2443 | } |
| 2444 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2445 | /* |
| 2446 | * 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] | 2447 | * at this point until we release them. |
| 2448 | * |
| 2449 | * Fail silently without starting the shrinker |
| 2450 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2451 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2452 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2453 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2454 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2455 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2456 | sg = st->sgl; |
| 2457 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2458 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2459 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2460 | const unsigned int shrink[] = { |
| 2461 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2462 | 0, |
| 2463 | }, *s = shrink; |
| 2464 | gfp_t gfp = noreclaim; |
| 2465 | |
| 2466 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2467 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2468 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2469 | if (likely(!IS_ERR(page))) |
| 2470 | break; |
| 2471 | |
| 2472 | if (!*s) { |
| 2473 | ret = PTR_ERR(page); |
| 2474 | goto err_sg; |
| 2475 | } |
| 2476 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2477 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2478 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2479 | /* |
| 2480 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2481 | * our own buffer, now let the real VM do its job and |
| 2482 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2483 | * |
| 2484 | * However, since graphics tend to be disposable, |
| 2485 | * defer the oom here by reporting the ENOMEM back |
| 2486 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2487 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2488 | if (!*s) { |
| 2489 | /* reclaim and warn, but no oom */ |
| 2490 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2491 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2492 | /* |
| 2493 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2494 | * kswapd to reclaim our pages (direct reclaim |
| 2495 | * does not effectively begin pageout of our |
| 2496 | * buffers on its own). However, direct reclaim |
| 2497 | * only waits for kswapd when under allocation |
| 2498 | * congestion. So as a result __GFP_RECLAIM is |
| 2499 | * unreliable and fails to actually reclaim our |
| 2500 | * dirty pages -- unless you try over and over |
| 2501 | * again with !__GFP_NORETRY. However, we still |
| 2502 | * want to fail this allocation rather than |
| 2503 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2504 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2505 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2506 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2507 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2508 | } while (1); |
| 2509 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2510 | if (!i || |
| 2511 | sg->length >= max_segment || |
| 2512 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2513 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2514 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2515 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2516 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2517 | st->nents++; |
| 2518 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2519 | } else { |
| 2520 | sg->length += PAGE_SIZE; |
| 2521 | } |
| 2522 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2523 | |
| 2524 | /* Check that the i965g/gm workaround works. */ |
| 2525 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2526 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2527 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2528 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2529 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2530 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2531 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2532 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2533 | i915_sg_trim(st); |
| 2534 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2535 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2536 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2537 | /* |
| 2538 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2539 | * it could not reserve enough large entries, asking |
| 2540 | * for PAGE_SIZE chunks instead may be helpful. |
| 2541 | */ |
| 2542 | if (max_segment > PAGE_SIZE) { |
| 2543 | for_each_sgt_page(page, sgt_iter, st) |
| 2544 | put_page(page); |
| 2545 | sg_free_table(st); |
| 2546 | |
| 2547 | max_segment = PAGE_SIZE; |
| 2548 | goto rebuild_st; |
| 2549 | } else { |
| 2550 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2551 | "Failed to DMA remap %lu pages\n", |
| 2552 | page_count); |
| 2553 | goto err_pages; |
| 2554 | } |
| 2555 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2556 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2557 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2558 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2559 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2560 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2561 | |
| 2562 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2563 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2564 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2565 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2566 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2567 | mapping_clear_unevictable(mapping); |
| 2568 | pagevec_init(&pvec); |
| 2569 | for_each_sgt_page(page, sgt_iter, st) { |
| 2570 | if (!pagevec_add(&pvec, page)) |
| 2571 | check_release_pagevec(&pvec); |
| 2572 | } |
| 2573 | if (pagevec_count(&pvec)) |
| 2574 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2575 | sg_free_table(st); |
| 2576 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2577 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2578 | /* |
| 2579 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2580 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2581 | * ENOMEM for a genuine allocation failure. |
| 2582 | * |
| 2583 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2584 | * space and so want to translate the error from shmemfs back to our |
| 2585 | * usual understanding of ENOMEM. |
| 2586 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2587 | if (ret == -ENOSPC) |
| 2588 | ret = -ENOMEM; |
| 2589 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2590 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2594 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2595 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2596 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2597 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2598 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2599 | int i; |
| 2600 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2601 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2602 | |
| 2603 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2604 | obj->mm.get_page.sg_idx = 0; |
| 2605 | |
| 2606 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2607 | |
| 2608 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2609 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2610 | GEM_BUG_ON(obj->mm.quirked); |
| 2611 | __i915_gem_object_pin_pages(obj); |
| 2612 | obj->mm.quirked = true; |
| 2613 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2614 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2615 | GEM_BUG_ON(!sg_page_sizes); |
| 2616 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2617 | |
| 2618 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2619 | * Calculate the supported page-sizes which fit into the given |
| 2620 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2621 | * to use opportunistically when later inserting into the GTT. For |
| 2622 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2623 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2624 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2625 | */ |
| 2626 | obj->mm.page_sizes.sg = 0; |
| 2627 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2628 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2629 | obj->mm.page_sizes.sg |= BIT(i); |
| 2630 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2631 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2632 | |
| 2633 | spin_lock(&i915->mm.obj_lock); |
| 2634 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2635 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2639 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2640 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2641 | |
| 2642 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2643 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2644 | return -EFAULT; |
| 2645 | } |
| 2646 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2647 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2648 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2649 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2650 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2651 | } |
| 2652 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2653 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2654 | * 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] | 2655 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2656 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2657 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2658 | * or as the object is itself released. |
| 2659 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2660 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2661 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2662 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2663 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2664 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2665 | if (err) |
| 2666 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2667 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2668 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2669 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2670 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2671 | err = ____i915_gem_object_get_pages(obj); |
| 2672 | if (err) |
| 2673 | goto unlock; |
| 2674 | |
| 2675 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2676 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2677 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2678 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2679 | unlock: |
| 2680 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2681 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2682 | } |
| 2683 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2684 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2685 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2686 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2687 | { |
| 2688 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2689 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2690 | struct sgt_iter sgt_iter; |
| 2691 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2692 | struct page *stack_pages[32]; |
| 2693 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2694 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2695 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2696 | void *addr; |
| 2697 | |
| 2698 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2699 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2700 | return kmap(sg_page(sgt->sgl)); |
| 2701 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2702 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2703 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2704 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2705 | if (!pages) |
| 2706 | return NULL; |
| 2707 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2708 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2709 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2710 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2711 | |
| 2712 | /* Check that we have the expected number of pages */ |
| 2713 | GEM_BUG_ON(i != n_pages); |
| 2714 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2715 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2716 | default: |
| 2717 | MISSING_CASE(type); |
| 2718 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2719 | case I915_MAP_WB: |
| 2720 | pgprot = PAGE_KERNEL; |
| 2721 | break; |
| 2722 | case I915_MAP_WC: |
| 2723 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2724 | break; |
| 2725 | } |
| 2726 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2727 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2728 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2729 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2730 | |
| 2731 | return addr; |
| 2732 | } |
| 2733 | |
| 2734 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2735 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2736 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2737 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2738 | enum i915_map_type has_type; |
| 2739 | bool pinned; |
| 2740 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2741 | int ret; |
| 2742 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2743 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2744 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2745 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2746 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2747 | if (ret) |
| 2748 | return ERR_PTR(ret); |
| 2749 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2750 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2751 | type &= ~I915_MAP_OVERRIDE; |
| 2752 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2753 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2754 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2755 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2756 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2757 | ret = ____i915_gem_object_get_pages(obj); |
| 2758 | if (ret) |
| 2759 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2760 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2761 | smp_mb__before_atomic(); |
| 2762 | } |
| 2763 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2764 | pinned = false; |
| 2765 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2766 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2767 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2768 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2769 | if (ptr && has_type != type) { |
| 2770 | if (pinned) { |
| 2771 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2772 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2773 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2774 | |
| 2775 | if (is_vmalloc_addr(ptr)) |
| 2776 | vunmap(ptr); |
| 2777 | else |
| 2778 | kunmap(kmap_to_page(ptr)); |
| 2779 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2780 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2781 | } |
| 2782 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2783 | if (!ptr) { |
| 2784 | ptr = i915_gem_object_map(obj, type); |
| 2785 | if (!ptr) { |
| 2786 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2787 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2788 | } |
| 2789 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2790 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2791 | } |
| 2792 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2793 | out_unlock: |
| 2794 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2795 | return ptr; |
| 2796 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2797 | err_unpin: |
| 2798 | atomic_dec(&obj->mm.pages_pin_count); |
| 2799 | err_unlock: |
| 2800 | ptr = ERR_PTR(ret); |
| 2801 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2802 | } |
| 2803 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2804 | static int |
| 2805 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2806 | const struct drm_i915_gem_pwrite *arg) |
| 2807 | { |
| 2808 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2809 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2810 | u64 remain, offset; |
| 2811 | unsigned int pg; |
| 2812 | |
| 2813 | /* Before we instantiate/pin the backing store for our use, we |
| 2814 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2815 | * the pagecache. We avoid the penalty of instantiating all the |
| 2816 | * pages, important if the user is just writing to a few and never |
| 2817 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2818 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2819 | * or clearing-before-use) before it is overwritten. |
| 2820 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2821 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2822 | return -ENODEV; |
| 2823 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2824 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2825 | return -EFAULT; |
| 2826 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2827 | /* Before the pages are instantiated the object is treated as being |
| 2828 | * in the CPU domain. The pages will be clflushed as required before |
| 2829 | * use, and we can freely write into the pages directly. If userspace |
| 2830 | * races pwrite with any other operation; corruption will ensue - |
| 2831 | * that is userspace's prerogative! |
| 2832 | */ |
| 2833 | |
| 2834 | remain = arg->size; |
| 2835 | offset = arg->offset; |
| 2836 | pg = offset_in_page(offset); |
| 2837 | |
| 2838 | do { |
| 2839 | unsigned int len, unwritten; |
| 2840 | struct page *page; |
| 2841 | void *data, *vaddr; |
| 2842 | int err; |
| 2843 | |
| 2844 | len = PAGE_SIZE - pg; |
| 2845 | if (len > remain) |
| 2846 | len = remain; |
| 2847 | |
| 2848 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2849 | offset, len, 0, |
| 2850 | &page, &data); |
| 2851 | if (err < 0) |
| 2852 | return err; |
| 2853 | |
| 2854 | vaddr = kmap(page); |
| 2855 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2856 | kunmap(page); |
| 2857 | |
| 2858 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2859 | offset, len, len - unwritten, |
| 2860 | page, data); |
| 2861 | if (err < 0) |
| 2862 | return err; |
| 2863 | |
| 2864 | if (unwritten) |
| 2865 | return -EFAULT; |
| 2866 | |
| 2867 | remain -= len; |
| 2868 | user_data += len; |
| 2869 | offset += len; |
| 2870 | pg = 0; |
| 2871 | } while (remain); |
| 2872 | |
| 2873 | return 0; |
| 2874 | } |
| 2875 | |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2876 | static void i915_gem_client_mark_guilty(struct drm_i915_file_private *file_priv, |
| 2877 | const struct i915_gem_context *ctx) |
| 2878 | { |
| 2879 | unsigned int score; |
| 2880 | unsigned long prev_hang; |
| 2881 | |
| 2882 | if (i915_gem_context_is_banned(ctx)) |
| 2883 | score = I915_CLIENT_SCORE_CONTEXT_BAN; |
| 2884 | else |
| 2885 | score = 0; |
| 2886 | |
| 2887 | prev_hang = xchg(&file_priv->hang_timestamp, jiffies); |
| 2888 | if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES)) |
| 2889 | score += I915_CLIENT_SCORE_HANG_FAST; |
| 2890 | |
| 2891 | if (score) { |
| 2892 | atomic_add(score, &file_priv->ban_score); |
| 2893 | |
| 2894 | DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n", |
| 2895 | ctx->name, score, |
| 2896 | atomic_read(&file_priv->ban_score)); |
| 2897 | } |
| 2898 | } |
| 2899 | |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2900 | static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2901 | { |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2902 | unsigned int score; |
| 2903 | bool banned, bannable; |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2904 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2905 | atomic_inc(&ctx->guilty_count); |
| 2906 | |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2907 | bannable = i915_gem_context_is_bannable(ctx); |
| 2908 | score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score); |
| 2909 | banned = score >= CONTEXT_SCORE_BAN_THRESHOLD; |
Chris Wilson | 24eae08 | 2018-02-05 09:22:01 +0000 | [diff] [blame] | 2910 | |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2911 | /* Cool contexts don't accumulate client ban score */ |
| 2912 | if (!bannable) |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2913 | return; |
| 2914 | |
Chris Wilson | bcc2661 | 2018-06-18 08:31:35 +0100 | [diff] [blame] | 2915 | if (banned) { |
| 2916 | DRM_DEBUG_DRIVER("context %s: guilty %d, score %u, banned\n", |
| 2917 | ctx->name, atomic_read(&ctx->guilty_count), |
| 2918 | score); |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2919 | i915_gem_context_set_banned(ctx); |
Chris Wilson | bcc2661 | 2018-06-18 08:31:35 +0100 | [diff] [blame] | 2920 | } |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 2921 | |
| 2922 | if (!IS_ERR_OR_NULL(ctx->file_priv)) |
| 2923 | i915_gem_client_mark_guilty(ctx->file_priv, ctx); |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) |
| 2927 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2928 | atomic_inc(&ctx->active_count); |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2929 | } |
| 2930 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2931 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2932 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2933 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2934 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2935 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2936 | |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2937 | /* |
| 2938 | * We are called by the error capture, reset and to dump engine |
| 2939 | * state at random points in time. In particular, note that neither is |
| 2940 | * crucially ordered with an interrupt. After a hang, the GPU is dead |
| 2941 | * and we assume that no more writes can happen (we waited long enough |
| 2942 | * for all writes that were in transaction to be flushed) - adding an |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2943 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2944 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2945 | * At all other times, we must assume the GPU is still running, but |
| 2946 | * we only care about the snapshot of this moment. |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2947 | */ |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2948 | spin_lock_irqsave(&engine->timeline.lock, flags); |
| 2949 | list_for_each_entry(request, &engine->timeline.requests, link) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2950 | if (__i915_request_completed(request, request->global_seqno)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2951 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2952 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2953 | active = request; |
| 2954 | break; |
| 2955 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2956 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2957 | |
| 2958 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2959 | } |
| 2960 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2961 | /* |
| 2962 | * Ensure irq handler finishes, and not run again. |
| 2963 | * Also return the active request so that we only search for it once. |
| 2964 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2965 | struct i915_request * |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2966 | i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) |
| 2967 | { |
Chris Wilson | 5adfb77 | 2018-05-16 19:33:51 +0100 | [diff] [blame] | 2968 | struct i915_request *request; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2969 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 2970 | /* |
| 2971 | * During the reset sequence, we must prevent the engine from |
| 2972 | * entering RC6. As the context state is undefined until we restart |
| 2973 | * the engine, if it does enter RC6 during the reset, the state |
| 2974 | * written to the powercontext is undefined and so we may lose |
| 2975 | * GPU state upon resume, i.e. fail to restart after a reset. |
| 2976 | */ |
| 2977 | intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); |
| 2978 | |
Chris Wilson | 5adfb77 | 2018-05-16 19:33:51 +0100 | [diff] [blame] | 2979 | request = engine->reset.prepare(engine); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 2980 | if (request && request->fence.error == -EIO) |
| 2981 | request = ERR_PTR(-EIO); /* Previous reset failed! */ |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2982 | |
| 2983 | return request; |
| 2984 | } |
| 2985 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2986 | int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2987 | { |
| 2988 | struct intel_engine_cs *engine; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2989 | struct i915_request *request; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2990 | enum intel_engine_id id; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2991 | int err = 0; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 2992 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2993 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2994 | request = i915_gem_reset_prepare_engine(engine); |
| 2995 | if (IS_ERR(request)) { |
| 2996 | err = PTR_ERR(request); |
| 2997 | continue; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 2998 | } |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 2999 | |
| 3000 | engine->hangcheck.active_request = request; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3003 | i915_gem_revoke_fences(dev_priv); |
Michal Wajdeczko | c37d572 | 2018-03-12 13:03:07 +0000 | [diff] [blame] | 3004 | intel_uc_sanitize(dev_priv); |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3005 | |
| 3006 | return err; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3007 | } |
| 3008 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3009 | static void engine_skip_context(struct i915_request *request) |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3010 | { |
| 3011 | struct intel_engine_cs *engine = request->engine; |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3012 | struct i915_gem_context *hung_ctx = request->gem_context; |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3013 | struct i915_timeline *timeline = request->timeline; |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3014 | unsigned long flags; |
| 3015 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3016 | GEM_BUG_ON(timeline == &engine->timeline); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3017 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3018 | spin_lock_irqsave(&engine->timeline.lock, flags); |
Chris Wilson | 890fd18 | 2018-07-06 22:07:10 +0100 | [diff] [blame] | 3019 | spin_lock(&timeline->lock); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3020 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3021 | list_for_each_entry_continue(request, &engine->timeline.requests, link) |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3022 | if (request->gem_context == hung_ctx) |
Chris Wilson | 6dd7526 | 2018-07-06 11:39:43 +0100 | [diff] [blame] | 3023 | i915_request_skip(request, -EIO); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3024 | |
| 3025 | list_for_each_entry(request, &timeline->requests, link) |
Chris Wilson | 6dd7526 | 2018-07-06 11:39:43 +0100 | [diff] [blame] | 3026 | i915_request_skip(request, -EIO); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3027 | |
| 3028 | spin_unlock(&timeline->lock); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3029 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3030 | } |
| 3031 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3032 | /* Returns the request if it was guilty of the hang */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3033 | static struct i915_request * |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3034 | i915_gem_reset_request(struct intel_engine_cs *engine, |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3035 | struct i915_request *request, |
| 3036 | bool stalled) |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3037 | { |
Mika Kuoppala | 71895a0 | 2017-01-17 17:59:07 +0200 | [diff] [blame] | 3038 | /* The guilty request will get skipped on a hung engine. |
| 3039 | * |
| 3040 | * Users of client default contexts do not rely on logical |
| 3041 | * state preserved between batches so it is safe to execute |
| 3042 | * queued requests following the hang. Non default contexts |
| 3043 | * rely on preserved state, so skipping a batch loses the |
| 3044 | * evolution of the state and it needs to be considered corrupted. |
| 3045 | * Executing more queued batches on top of corrupted state is |
| 3046 | * risky. But we take the risk by trying to advance through |
| 3047 | * the queued requests in order to make the client behaviour |
| 3048 | * more predictable around resets, by not throwing away random |
| 3049 | * amount of batches it has prepared for execution. Sophisticated |
| 3050 | * clients can use gem_reset_stats_ioctl and dma fence status |
| 3051 | * (exported via sync_file info ioctl on explicit fences) to observe |
| 3052 | * when it loses the context state and should rebuild accordingly. |
| 3053 | * |
| 3054 | * The context ban, and ultimately the client ban, mechanism are safety |
| 3055 | * valves if client submission ends up resulting in nothing more than |
| 3056 | * subsequent hangs. |
| 3057 | */ |
| 3058 | |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3059 | if (i915_request_completed(request)) { |
| 3060 | GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n", |
| 3061 | engine->name, request->global_seqno, |
| 3062 | request->fence.context, request->fence.seqno, |
| 3063 | intel_engine_get_seqno(engine)); |
| 3064 | stalled = false; |
| 3065 | } |
| 3066 | |
| 3067 | if (stalled) { |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3068 | i915_gem_context_mark_guilty(request->gem_context); |
Chris Wilson | 6dd7526 | 2018-07-06 11:39:43 +0100 | [diff] [blame] | 3069 | i915_request_skip(request, -EIO); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3070 | |
| 3071 | /* If this context is now banned, skip all pending requests. */ |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3072 | if (i915_gem_context_is_banned(request->gem_context)) |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3073 | engine_skip_context(request); |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3074 | } else { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3075 | /* |
| 3076 | * Since this is not the hung engine, it may have advanced |
| 3077 | * since the hang declaration. Double check by refinding |
| 3078 | * the active request at the time of the reset. |
| 3079 | */ |
| 3080 | request = i915_gem_find_active_request(engine); |
| 3081 | if (request) { |
Chris Wilson | 042ed2d | 2018-06-15 10:31:36 +0100 | [diff] [blame] | 3082 | unsigned long flags; |
| 3083 | |
Chris Wilson | 4e0d64d | 2018-05-17 22:26:30 +0100 | [diff] [blame] | 3084 | i915_gem_context_mark_innocent(request->gem_context); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3085 | dma_fence_set_error(&request->fence, -EAGAIN); |
| 3086 | |
| 3087 | /* Rewind the engine to replay the incomplete rq */ |
Chris Wilson | 042ed2d | 2018-06-15 10:31:36 +0100 | [diff] [blame] | 3088 | spin_lock_irqsave(&engine->timeline.lock, flags); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3089 | request = list_prev_entry(request, link); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3090 | if (&request->link == &engine->timeline.requests) |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3091 | request = NULL; |
Chris Wilson | 042ed2d | 2018-06-15 10:31:36 +0100 | [diff] [blame] | 3092 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3093 | } |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3094 | } |
| 3095 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3096 | return request; |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3097 | } |
| 3098 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3099 | void i915_gem_reset_engine(struct intel_engine_cs *engine, |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3100 | struct i915_request *request, |
| 3101 | bool stalled) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3102 | { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3103 | if (request) |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3104 | request = i915_gem_reset_request(engine, request, stalled); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3105 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3106 | /* Setup the CS to resume from the breadcrumb of the hung request */ |
Chris Wilson | 5adfb77 | 2018-05-16 19:33:51 +0100 | [diff] [blame] | 3107 | engine->reset.reset(engine, request); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3108 | } |
| 3109 | |
Chris Wilson | d0667e9 | 2018-04-06 23:03:54 +0100 | [diff] [blame] | 3110 | void i915_gem_reset(struct drm_i915_private *dev_priv, |
| 3111 | unsigned int stalled_mask) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3112 | { |
| 3113 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3114 | enum intel_engine_id id; |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3115 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3116 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 3117 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3118 | i915_retire_requests(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3119 | |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3120 | for_each_engine(engine, dev_priv, id) { |
Chris Wilson | 1fc44d9 | 2018-05-17 22:26:32 +0100 | [diff] [blame] | 3121 | struct intel_context *ce; |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3122 | |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3123 | i915_gem_reset_engine(engine, |
| 3124 | engine->hangcheck.active_request, |
Chris Wilson | d0667e9 | 2018-04-06 23:03:54 +0100 | [diff] [blame] | 3125 | stalled_mask & ENGINE_MASK(id)); |
Chris Wilson | 1fc44d9 | 2018-05-17 22:26:32 +0100 | [diff] [blame] | 3126 | ce = fetch_and_zero(&engine->last_retired_context); |
| 3127 | if (ce) |
| 3128 | intel_context_unpin(ce); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3129 | |
| 3130 | /* |
| 3131 | * Ostensibily, we always want a context loaded for powersaving, |
| 3132 | * so if the engine is idle after the reset, send a request |
| 3133 | * to load our scratch kernel_context. |
| 3134 | * |
| 3135 | * More mysteriously, if we leave the engine idle after a reset, |
| 3136 | * the next userspace batch may hang, with what appears to be |
| 3137 | * an incoherent read by the CS (presumably stale TLB). An |
| 3138 | * empty request appears sufficient to paper over the glitch. |
| 3139 | */ |
Chris Wilson | 01b8fdc | 2018-02-05 15:24:31 +0000 | [diff] [blame] | 3140 | if (intel_engine_is_idle(engine)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3141 | struct i915_request *rq; |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3142 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3143 | rq = i915_request_alloc(engine, |
| 3144 | dev_priv->kernel_context); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3145 | if (!IS_ERR(rq)) |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 3146 | i915_request_add(rq); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3147 | } |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3148 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3149 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 3150 | i915_gem_restore_fences(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3151 | } |
| 3152 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3153 | void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) |
| 3154 | { |
Chris Wilson | 5adfb77 | 2018-05-16 19:33:51 +0100 | [diff] [blame] | 3155 | engine->reset.finish(engine); |
| 3156 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3157 | intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3158 | } |
| 3159 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3160 | void i915_gem_reset_finish(struct drm_i915_private *dev_priv) |
| 3161 | { |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3162 | struct intel_engine_cs *engine; |
| 3163 | enum intel_engine_id id; |
| 3164 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3165 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3166 | |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3167 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3168 | engine->hangcheck.active_request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3169 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3170 | } |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3173 | static void nop_submit_request(struct i915_request *request) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3174 | { |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3175 | unsigned long flags; |
| 3176 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3177 | GEM_TRACE("%s fence %llx:%d -> -EIO\n", |
| 3178 | request->engine->name, |
| 3179 | request->fence.context, request->fence.seqno); |
Chris Wilson | 3cd9442 | 2017-01-10 17:22:45 +0000 | [diff] [blame] | 3180 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3181 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3182 | spin_lock_irqsave(&request->engine->timeline.lock, flags); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3183 | __i915_request_submit(request); |
Chris Wilson | 6faf591 | 2018-12-28 14:07:35 +0000 | [diff] [blame] | 3184 | intel_engine_write_global_seqno(request->engine, request->global_seqno); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3185 | spin_unlock_irqrestore(&request->engine->timeline.lock, flags); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3186 | } |
| 3187 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3188 | void i915_gem_set_wedged(struct drm_i915_private *i915) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3189 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 3190 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3191 | enum intel_engine_id id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3192 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3193 | GEM_TRACE("start\n"); |
| 3194 | |
Chris Wilson | decd29e | 2019-01-15 12:20:57 +0000 | [diff] [blame] | 3195 | if (GEM_SHOW_DEBUG() && !intel_engines_are_idle(i915)) { |
Chris Wilson | 559e040 | 2018-02-05 09:21:59 +0000 | [diff] [blame] | 3196 | struct drm_printer p = drm_debug_printer(__func__); |
| 3197 | |
| 3198 | for_each_engine(engine, i915, id) |
| 3199 | intel_engine_dump(engine, &p, "%s\n", engine->name); |
| 3200 | } |
| 3201 | |
Chris Wilson | 3970c65 | 2018-07-23 15:53:35 +0100 | [diff] [blame] | 3202 | if (test_and_set_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3203 | goto out; |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3204 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3205 | /* |
| 3206 | * First, stop submission to hw, but do not yet complete requests by |
| 3207 | * rolling the global seqno forward (since this would complete requests |
| 3208 | * for which we haven't set the fence error to EIO yet). |
| 3209 | */ |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3210 | for_each_engine(engine, i915, id) |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3211 | i915_gem_reset_prepare_engine(engine); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3212 | |
Chris Wilson | ac697ae | 2018-03-15 15:10:15 +0000 | [diff] [blame] | 3213 | /* Even if the GPU reset fails, it should still stop the engines */ |
Chris Wilson | ec5b65a | 2018-07-26 09:50:33 +0100 | [diff] [blame] | 3214 | if (INTEL_GEN(i915) >= 5) |
| 3215 | intel_gpu_reset(i915, ALL_ENGINES); |
Chris Wilson | ac697ae | 2018-03-15 15:10:15 +0000 | [diff] [blame] | 3216 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3217 | for_each_engine(engine, i915, id) { |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3218 | engine->submit_request = nop_submit_request; |
| 3219 | engine->schedule = NULL; |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3220 | } |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3221 | i915->caps.scheduler = 0; |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3222 | |
| 3223 | /* |
| 3224 | * Make sure no request can slip through without getting completed by |
Chris Wilson | 6faf591 | 2018-12-28 14:07:35 +0000 | [diff] [blame] | 3225 | * 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] | 3226 | * in nop_submit_request. |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3227 | */ |
| 3228 | synchronize_rcu(); |
| 3229 | |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3230 | /* Mark all executing requests as skipped */ |
| 3231 | for_each_engine(engine, i915, id) |
| 3232 | engine->cancel_requests(engine); |
| 3233 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3234 | for_each_engine(engine, i915, id) { |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3235 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | 3800960 | 2018-12-03 11:36:55 +0000 | [diff] [blame] | 3236 | intel_engine_wakeup(engine); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3237 | } |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3238 | |
Chris Wilson | 3970c65 | 2018-07-23 15:53:35 +0100 | [diff] [blame] | 3239 | out: |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3240 | GEM_TRACE("end\n"); |
| 3241 | |
Chris Wilson | 3d7adbb | 2017-07-21 13:32:27 +0100 | [diff] [blame] | 3242 | wake_up_all(&i915->gpu_error.reset_queue); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3245 | bool i915_gem_unset_wedged(struct drm_i915_private *i915) |
| 3246 | { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3247 | struct i915_timeline *tl; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3248 | |
| 3249 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3250 | if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3251 | return true; |
| 3252 | |
Chris Wilson | 55c1551 | 2019-01-03 21:33:40 +0000 | [diff] [blame] | 3253 | if (!i915->gt.scratch) /* Never full initialised, recovery impossible */ |
| 3254 | return false; |
| 3255 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3256 | GEM_TRACE("start\n"); |
| 3257 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3258 | /* |
| 3259 | * Before unwedging, make sure that all pending operations |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3260 | * are flushed and errored out - we may have requests waiting upon |
| 3261 | * third party fences. We marked all inflight requests as EIO, and |
| 3262 | * every execbuf since returned EIO, for consistency we want all |
| 3263 | * the currently pending requests to also be marked as EIO, which |
| 3264 | * is done inside our nop_submit_request - and so we must wait. |
| 3265 | * |
| 3266 | * No more can be submitted until we reset the wedged bit. |
| 3267 | */ |
| 3268 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3269 | struct i915_request *rq; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3270 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3271 | rq = i915_gem_active_peek(&tl->last_request, |
| 3272 | &i915->drm.struct_mutex); |
| 3273 | if (!rq) |
| 3274 | continue; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3275 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3276 | /* |
| 3277 | * We can't use our normal waiter as we want to |
| 3278 | * avoid recursively trying to handle the current |
| 3279 | * reset. The basic dma_fence_default_wait() installs |
| 3280 | * a callback for dma_fence_signal(), which is |
| 3281 | * triggered by our nop handler (indirectly, the |
| 3282 | * callback enables the signaler thread which is |
| 3283 | * woken by the nop_submit_request() advancing the seqno |
| 3284 | * and when the seqno passes the fence, the signaler |
| 3285 | * then signals the fence waking us up). |
| 3286 | */ |
| 3287 | if (dma_fence_default_wait(&rq->fence, true, |
| 3288 | MAX_SCHEDULE_TIMEOUT) < 0) |
| 3289 | return false; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3290 | } |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3291 | i915_retire_requests(i915); |
| 3292 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3293 | |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 3294 | intel_engines_sanitize(i915, false); |
Chris Wilson | 8db601f | 2018-09-14 09:00:17 +0100 | [diff] [blame] | 3295 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3296 | /* |
| 3297 | * Undo nop_submit_request. We prevent all new i915 requests from |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3298 | * being queued (by disallowing execbuf whilst wedged) so having |
| 3299 | * waited for all active requests above, we know the system is idle |
| 3300 | * and do not have to worry about a thread being inside |
| 3301 | * engine->submit_request() as we swap over. So unlike installing |
| 3302 | * the nop_submit_request on reset, we can do this from normal |
| 3303 | * context and do not require stop_machine(). |
| 3304 | */ |
| 3305 | intel_engines_reset_default_submission(i915); |
Chris Wilson | 36703e7 | 2017-06-22 11:56:25 +0100 | [diff] [blame] | 3306 | i915_gem_contexts_lost(i915); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3307 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3308 | GEM_TRACE("end\n"); |
| 3309 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3310 | smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ |
| 3311 | clear_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3312 | |
| 3313 | return true; |
| 3314 | } |
| 3315 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 3316 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3317 | i915_gem_retire_work_handler(struct work_struct *work) |
| 3318 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3319 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3320 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 3321 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3322 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3323 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3324 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3325 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3326 | mutex_unlock(&dev->struct_mutex); |
| 3327 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3328 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3329 | /* |
| 3330 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3331 | * We do not need to do this test under locking as in the worst-case |
| 3332 | * we queue the retire worker once too often. |
| 3333 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3334 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3335 | queue_delayed_work(dev_priv->wq, |
| 3336 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 3337 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3338 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3339 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3340 | static void shrink_caches(struct drm_i915_private *i915) |
| 3341 | { |
| 3342 | /* |
| 3343 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 3344 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 3345 | * with the aim of reducing fragmentation. |
| 3346 | */ |
| 3347 | kmem_cache_shrink(i915->priorities); |
| 3348 | kmem_cache_shrink(i915->dependencies); |
| 3349 | kmem_cache_shrink(i915->requests); |
| 3350 | kmem_cache_shrink(i915->luts); |
| 3351 | kmem_cache_shrink(i915->vmas); |
| 3352 | kmem_cache_shrink(i915->objects); |
| 3353 | } |
| 3354 | |
| 3355 | struct sleep_rcu_work { |
| 3356 | union { |
| 3357 | struct rcu_head rcu; |
| 3358 | struct work_struct work; |
| 3359 | }; |
| 3360 | struct drm_i915_private *i915; |
| 3361 | unsigned int epoch; |
| 3362 | }; |
| 3363 | |
| 3364 | static inline bool |
| 3365 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 3366 | { |
| 3367 | /* |
| 3368 | * There is a small chance that the epoch wrapped since we started |
| 3369 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 3370 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 3371 | */ |
| 3372 | return epoch == READ_ONCE(i915->gt.epoch); |
| 3373 | } |
| 3374 | |
| 3375 | static void __sleep_work(struct work_struct *work) |
| 3376 | { |
| 3377 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 3378 | struct drm_i915_private *i915 = s->i915; |
| 3379 | unsigned int epoch = s->epoch; |
| 3380 | |
| 3381 | kfree(s); |
| 3382 | if (same_epoch(i915, epoch)) |
| 3383 | shrink_caches(i915); |
| 3384 | } |
| 3385 | |
| 3386 | static void __sleep_rcu(struct rcu_head *rcu) |
| 3387 | { |
| 3388 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 3389 | struct drm_i915_private *i915 = s->i915; |
| 3390 | |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3391 | destroy_rcu_head(&s->rcu); |
| 3392 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3393 | if (same_epoch(i915, s->epoch)) { |
| 3394 | INIT_WORK(&s->work, __sleep_work); |
| 3395 | queue_work(i915->wq, &s->work); |
| 3396 | } else { |
| 3397 | kfree(s); |
| 3398 | } |
| 3399 | } |
| 3400 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3401 | static inline bool |
| 3402 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 3403 | { |
| 3404 | return (READ_ONCE(i915->gt.active_requests) || |
| 3405 | work_pending(&i915->gt.idle_work.work)); |
| 3406 | } |
| 3407 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3408 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
| 3409 | { |
| 3410 | struct intel_engine_cs *engine; |
| 3411 | enum intel_engine_id id; |
| 3412 | |
| 3413 | if (i915_terminally_wedged(&i915->gpu_error)) |
| 3414 | return; |
| 3415 | |
| 3416 | GEM_BUG_ON(i915->gt.active_requests); |
| 3417 | for_each_engine(engine, i915, id) { |
| 3418 | GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline.last_request)); |
| 3419 | GEM_BUG_ON(engine->last_retired_context != |
| 3420 | to_intel_context(i915->kernel_context, engine)); |
| 3421 | } |
| 3422 | } |
| 3423 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3424 | static void |
| 3425 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3426 | { |
| 3427 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3428 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3429 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3430 | bool rearm_hangcheck; |
| 3431 | |
| 3432 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3433 | return; |
| 3434 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 3435 | if (READ_ONCE(dev_priv->gt.active_requests)) |
| 3436 | return; |
| 3437 | |
| 3438 | /* |
| 3439 | * Flush out the last user context, leaving only the pinned |
| 3440 | * kernel context resident. When we are idling on the kernel_context, |
| 3441 | * no more new requests (with a context switch) are emitted and we |
| 3442 | * can finally rest. A consequence is that the idle work handler is |
| 3443 | * always called at least twice before idling (and if the system is |
| 3444 | * idle that implies a round trip through the retire worker). |
| 3445 | */ |
| 3446 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 3447 | i915_gem_switch_to_kernel_context(dev_priv); |
| 3448 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 3449 | |
| 3450 | GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n", |
| 3451 | READ_ONCE(dev_priv->gt.active_requests)); |
| 3452 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3453 | /* |
| 3454 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3455 | * new request is submitted. As we don't trust the hardware, we |
| 3456 | * continue on if the wait times out. This is necessary to allow |
| 3457 | * the machine to suspend even if the hardware dies, and we will |
| 3458 | * try to recover in resume (after depriving the hardware of power, |
| 3459 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3460 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3461 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3462 | intel_engines_are_idle(dev_priv), |
| 3463 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3464 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3465 | |
| 3466 | rearm_hangcheck = |
| 3467 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3468 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3469 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3470 | /* Currently busy, come back later */ |
| 3471 | mod_delayed_work(dev_priv->wq, |
| 3472 | &dev_priv->gt.idle_work, |
| 3473 | msecs_to_jiffies(50)); |
| 3474 | goto out_rearm; |
| 3475 | } |
| 3476 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3477 | /* |
| 3478 | * New request retired after this work handler started, extend active |
| 3479 | * period until next instance of the work. |
| 3480 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3481 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3482 | goto out_unlock; |
| 3483 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 3484 | epoch = __i915_gem_park(dev_priv); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3485 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3486 | assert_kernel_context_is_current(dev_priv); |
| 3487 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3488 | rearm_hangcheck = false; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3489 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3490 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3491 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3492 | out_rearm: |
| 3493 | if (rearm_hangcheck) { |
| 3494 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3495 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3496 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3497 | |
| 3498 | /* |
| 3499 | * When we are idle, it is an opportune time to reap our caches. |
| 3500 | * However, we have many objects that utilise RCU and the ordered |
| 3501 | * i915->wq that this work is executing on. To try and flush any |
| 3502 | * pending frees now we are idle, we first wait for an RCU grace |
| 3503 | * period, and then queue a task (that will run last on the wq) to |
| 3504 | * shrink and re-optimize the caches. |
| 3505 | */ |
| 3506 | if (same_epoch(dev_priv, epoch)) { |
| 3507 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3508 | if (s) { |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3509 | init_rcu_head(&s->rcu); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3510 | s->i915 = dev_priv; |
| 3511 | s->epoch = epoch; |
| 3512 | call_rcu(&s->rcu, __sleep_rcu); |
| 3513 | } |
| 3514 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3517 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3518 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3519 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3520 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3521 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3522 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3523 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3524 | mutex_lock(&i915->drm.struct_mutex); |
| 3525 | |
| 3526 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3527 | struct i915_gem_context *ctx = lut->ctx; |
| 3528 | struct i915_vma *vma; |
| 3529 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3530 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3531 | if (ctx->file_priv != fpriv) |
| 3532 | continue; |
| 3533 | |
| 3534 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3535 | GEM_BUG_ON(vma->obj != obj); |
| 3536 | |
| 3537 | /* We allow the process to have multiple handles to the same |
| 3538 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3539 | */ |
| 3540 | GEM_BUG_ON(!vma->open_count); |
| 3541 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3542 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3543 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3544 | list_del(&lut->obj_link); |
| 3545 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3546 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3547 | kmem_cache_free(i915->luts, lut); |
| 3548 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3549 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3550 | |
| 3551 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3552 | } |
| 3553 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3554 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3555 | { |
| 3556 | if (timeout_ns < 0) |
| 3557 | return MAX_SCHEDULE_TIMEOUT; |
| 3558 | |
| 3559 | if (timeout_ns == 0) |
| 3560 | return 0; |
| 3561 | |
| 3562 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3563 | } |
| 3564 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3565 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3566 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3567 | * @dev: drm device pointer |
| 3568 | * @data: ioctl data blob |
| 3569 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3570 | * |
| 3571 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3572 | * the timeout parameter. |
| 3573 | * -ETIME: object is still busy after timeout |
| 3574 | * -ERESTARTSYS: signal interrupted the wait |
| 3575 | * -ENONENT: object doesn't exist |
| 3576 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3577 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3578 | * -ENOMEM: damn |
| 3579 | * -ENODEV: Internal IRQ fail |
| 3580 | * -E?: The add request failed |
| 3581 | * |
| 3582 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3583 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3584 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3585 | * without holding struct_mutex the object may become re-busied before this |
| 3586 | * function completes. A similar but shorter * race condition exists in the busy |
| 3587 | * ioctl |
| 3588 | */ |
| 3589 | int |
| 3590 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3591 | { |
| 3592 | struct drm_i915_gem_wait *args = data; |
| 3593 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3594 | ktime_t start; |
| 3595 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3596 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3597 | if (args->flags != 0) |
| 3598 | return -EINVAL; |
| 3599 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3600 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3601 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3602 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3603 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3604 | start = ktime_get(); |
| 3605 | |
| 3606 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 3607 | I915_WAIT_INTERRUPTIBLE | |
| 3608 | I915_WAIT_PRIORITY | |
| 3609 | I915_WAIT_ALL, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3610 | to_wait_timeout(args->timeout_ns), |
| 3611 | to_rps_client(file)); |
| 3612 | |
| 3613 | if (args->timeout_ns > 0) { |
| 3614 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3615 | if (args->timeout_ns < 0) |
| 3616 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3617 | |
| 3618 | /* |
| 3619 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3620 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3621 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3622 | * |
| 3623 | * This is a regression from the timespec->ktime conversion. |
| 3624 | */ |
| 3625 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3626 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3627 | |
| 3628 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3629 | if (ret == -ETIME && args->timeout_ns) |
| 3630 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3631 | } |
| 3632 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3633 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3634 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3635 | } |
| 3636 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3637 | static long wait_for_timeline(struct i915_timeline *tl, |
| 3638 | unsigned int flags, long timeout) |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3639 | { |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3640 | struct i915_request *rq; |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3641 | |
| 3642 | rq = i915_gem_active_get_unlocked(&tl->last_request); |
| 3643 | if (!rq) |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3644 | return timeout; |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3645 | |
| 3646 | /* |
| 3647 | * "Race-to-idle". |
| 3648 | * |
| 3649 | * Switching to the kernel context is often used a synchronous |
| 3650 | * step prior to idling, e.g. in suspend for flushing all |
| 3651 | * current operations to memory before sleeping. These we |
| 3652 | * want to complete as quickly as possible to avoid prolonged |
| 3653 | * stalls, so allow the gpu to boost to maximum clocks. |
| 3654 | */ |
| 3655 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
| 3656 | gen6_rps_boost(rq, NULL); |
| 3657 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3658 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 3659 | i915_request_put(rq); |
| 3660 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3661 | return timeout; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3662 | } |
| 3663 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3664 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3665 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3666 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3667 | dev_err(i915->drm.dev, |
| 3668 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3669 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3670 | i915_gem_set_wedged(i915); |
| 3671 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | return 0; |
| 3675 | } |
| 3676 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3677 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 3678 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3679 | { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3680 | GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", |
| 3681 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
| 3682 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3683 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3684 | /* If the device is asleep, we have no requests outstanding */ |
| 3685 | if (!READ_ONCE(i915->gt.awake)) |
| 3686 | return 0; |
| 3687 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3688 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3689 | struct i915_timeline *tl; |
| 3690 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3691 | |
| 3692 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3693 | |
| 3694 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3695 | timeout = wait_for_timeline(tl, flags, timeout); |
| 3696 | if (timeout < 0) |
| 3697 | return timeout; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3698 | } |
Chris Wilson | c1e63f6 | 2018-08-08 11:50:59 +0100 | [diff] [blame] | 3699 | if (GEM_SHOW_DEBUG() && !timeout) { |
| 3700 | /* Presume that timeout was non-zero to begin with! */ |
| 3701 | dev_warn(&i915->drm.pdev->dev, |
| 3702 | "Missed idle-completion interrupt!\n"); |
| 3703 | GEM_TRACE_DUMP(); |
| 3704 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3705 | |
| 3706 | err = wait_for_engines(i915); |
| 3707 | if (err) |
| 3708 | return err; |
| 3709 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3710 | i915_retire_requests(i915); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3711 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3712 | } else { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3713 | struct intel_engine_cs *engine; |
| 3714 | enum intel_engine_id id; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3715 | |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3716 | for_each_engine(engine, i915, id) { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3717 | struct i915_timeline *tl = &engine->timeline; |
| 3718 | |
| 3719 | timeout = wait_for_timeline(tl, flags, timeout); |
| 3720 | if (timeout < 0) |
| 3721 | return timeout; |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3722 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3723 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3724 | |
| 3725 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3726 | } |
| 3727 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3728 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3729 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3730 | /* |
| 3731 | * We manually flush the CPU domain so that we can override and |
| 3732 | * force the flush for the display, and perform it asyncrhonously. |
| 3733 | */ |
| 3734 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3735 | if (obj->cache_dirty) |
| 3736 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3737 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3741 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3742 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3743 | return; |
| 3744 | |
| 3745 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3746 | __i915_gem_object_flush_for_display(obj); |
| 3747 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3748 | } |
| 3749 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3750 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3751 | * Moves a single object to the WC read, and possibly write domain. |
| 3752 | * @obj: object to act on |
| 3753 | * @write: ask for write access or read only |
| 3754 | * |
| 3755 | * This function returns when the move is complete, including waiting on |
| 3756 | * flushes to occur. |
| 3757 | */ |
| 3758 | int |
| 3759 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3760 | { |
| 3761 | int ret; |
| 3762 | |
| 3763 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3764 | |
| 3765 | ret = i915_gem_object_wait(obj, |
| 3766 | I915_WAIT_INTERRUPTIBLE | |
| 3767 | I915_WAIT_LOCKED | |
| 3768 | (write ? I915_WAIT_ALL : 0), |
| 3769 | MAX_SCHEDULE_TIMEOUT, |
| 3770 | NULL); |
| 3771 | if (ret) |
| 3772 | return ret; |
| 3773 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3774 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3775 | return 0; |
| 3776 | |
| 3777 | /* Flush and acquire obj->pages so that we are coherent through |
| 3778 | * direct access in memory with previous cached writes through |
| 3779 | * shmemfs and that our cache domain tracking remains valid. |
| 3780 | * For example, if the obj->filp was moved to swap without us |
| 3781 | * being notified and releasing the pages, we would mistakenly |
| 3782 | * continue to assume that the obj remained out of the CPU cached |
| 3783 | * domain. |
| 3784 | */ |
| 3785 | ret = i915_gem_object_pin_pages(obj); |
| 3786 | if (ret) |
| 3787 | return ret; |
| 3788 | |
| 3789 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3790 | |
| 3791 | /* Serialise direct access to this object with the barriers for |
| 3792 | * coherent writes from the GPU, by effectively invalidating the |
| 3793 | * WC domain upon first access. |
| 3794 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3795 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3796 | mb(); |
| 3797 | |
| 3798 | /* It should now be out of any other write domains, and we can update |
| 3799 | * the domain values for our changes. |
| 3800 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3801 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3802 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3803 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3804 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3805 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3806 | obj->mm.dirty = true; |
| 3807 | } |
| 3808 | |
| 3809 | i915_gem_object_unpin_pages(obj); |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
| 3813 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3814 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3815 | * @obj: object to act on |
| 3816 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3817 | * |
| 3818 | * This function returns when the move is complete, including waiting on |
| 3819 | * flushes to occur. |
| 3820 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3821 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3822 | 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] | 3823 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3824 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3825 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3826 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3827 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3828 | ret = i915_gem_object_wait(obj, |
| 3829 | I915_WAIT_INTERRUPTIBLE | |
| 3830 | I915_WAIT_LOCKED | |
| 3831 | (write ? I915_WAIT_ALL : 0), |
| 3832 | MAX_SCHEDULE_TIMEOUT, |
| 3833 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3834 | if (ret) |
| 3835 | return ret; |
| 3836 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3837 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3838 | return 0; |
| 3839 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3840 | /* Flush and acquire obj->pages so that we are coherent through |
| 3841 | * direct access in memory with previous cached writes through |
| 3842 | * shmemfs and that our cache domain tracking remains valid. |
| 3843 | * For example, if the obj->filp was moved to swap without us |
| 3844 | * being notified and releasing the pages, we would mistakenly |
| 3845 | * continue to assume that the obj remained out of the CPU cached |
| 3846 | * domain. |
| 3847 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3848 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3849 | if (ret) |
| 3850 | return ret; |
| 3851 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3852 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3853 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3854 | /* Serialise direct access to this object with the barriers for |
| 3855 | * coherent writes from the GPU, by effectively invalidating the |
| 3856 | * GTT domain upon first access. |
| 3857 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3858 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3859 | mb(); |
| 3860 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3861 | /* It should now be out of any other write domains, and we can update |
| 3862 | * the domain values for our changes. |
| 3863 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3864 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3865 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3866 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3867 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3868 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3869 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3870 | } |
| 3871 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3872 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3873 | return 0; |
| 3874 | } |
| 3875 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3876 | /** |
| 3877 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3878 | * @obj: object to act on |
| 3879 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3880 | * |
| 3881 | * After this function returns, the object will be in the new cache-level |
| 3882 | * across all GTT and the contents of the backing storage will be coherent, |
| 3883 | * with respect to the new cache-level. In order to keep the backing storage |
| 3884 | * coherent for all users, we only allow a single cache level to be set |
| 3885 | * globally on the object and prevent it from being changed whilst the |
| 3886 | * hardware is reading from the object. That is if the object is currently |
| 3887 | * on the scanout it will be set to uncached (or equivalent display |
| 3888 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3889 | * that all direct access to the scanout remains coherent. |
| 3890 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3891 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3892 | enum i915_cache_level cache_level) |
| 3893 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3894 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3895 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3896 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3897 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3898 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3899 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3900 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3901 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3902 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3903 | * be invalid given the new cache-level. This is principally to |
| 3904 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3905 | * reading an invalid PTE on older architectures. |
| 3906 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3907 | restart: |
| 3908 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3909 | if (!drm_mm_node_allocated(&vma->node)) |
| 3910 | continue; |
| 3911 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3912 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3913 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3914 | return -EBUSY; |
| 3915 | } |
| 3916 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3917 | if (!i915_vma_is_closed(vma) && |
| 3918 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3919 | continue; |
| 3920 | |
| 3921 | ret = i915_vma_unbind(vma); |
| 3922 | if (ret) |
| 3923 | return ret; |
| 3924 | |
| 3925 | /* As unbinding may affect other elements in the |
| 3926 | * obj->vma_list (due to side-effects from retiring |
| 3927 | * an active vma), play safe and restart the iterator. |
| 3928 | */ |
| 3929 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3930 | } |
| 3931 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3932 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3933 | * cache-level on the PTE. We could simply unbind them all and |
| 3934 | * rebind with the correct cache-level on next use. However since |
| 3935 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3936 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3937 | * state and so involves less work. |
| 3938 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3939 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3940 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3941 | * If we wait upon the object, we know that all the bound |
| 3942 | * VMA are no longer active. |
| 3943 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3944 | ret = i915_gem_object_wait(obj, |
| 3945 | I915_WAIT_INTERRUPTIBLE | |
| 3946 | I915_WAIT_LOCKED | |
| 3947 | I915_WAIT_ALL, |
| 3948 | MAX_SCHEDULE_TIMEOUT, |
| 3949 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3950 | if (ret) |
| 3951 | return ret; |
| 3952 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3953 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3954 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3955 | /* Access to snoopable pages through the GTT is |
| 3956 | * incoherent and on some machines causes a hard |
| 3957 | * lockup. Relinquish the CPU mmaping to force |
| 3958 | * userspace to refault in the pages and we can |
| 3959 | * then double check if the GTT mapping is still |
| 3960 | * valid for that pointer access. |
| 3961 | */ |
| 3962 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3963 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3964 | /* As we no longer need a fence for GTT access, |
| 3965 | * we can relinquish it now (and so prevent having |
| 3966 | * to steal a fence from someone else on the next |
| 3967 | * fence request). Note GPU activity would have |
| 3968 | * dropped the fence as all snoopable access is |
| 3969 | * supposed to be linear. |
| 3970 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3971 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3972 | ret = i915_vma_put_fence(vma); |
| 3973 | if (ret) |
| 3974 | return ret; |
| 3975 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3976 | } else { |
| 3977 | /* We either have incoherent backing store and |
| 3978 | * so no GTT access or the architecture is fully |
| 3979 | * coherent. In such cases, existing GTT mmaps |
| 3980 | * ignore the cache bit in the PTE and we can |
| 3981 | * rewrite it without confusing the GPU or having |
| 3982 | * to force userspace to fault back in its mmaps. |
| 3983 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3986 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3987 | if (!drm_mm_node_allocated(&vma->node)) |
| 3988 | continue; |
| 3989 | |
| 3990 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3991 | if (ret) |
| 3992 | return ret; |
| 3993 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 3996 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3997 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3998 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3999 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 4000 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4001 | return 0; |
| 4002 | } |
| 4003 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4004 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 4005 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4006 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4007 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4008 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4009 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4010 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4011 | rcu_read_lock(); |
| 4012 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 4013 | if (!obj) { |
| 4014 | err = -ENOENT; |
| 4015 | goto out; |
| 4016 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4017 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4018 | switch (obj->cache_level) { |
| 4019 | case I915_CACHE_LLC: |
| 4020 | case I915_CACHE_L3_LLC: |
| 4021 | args->caching = I915_CACHING_CACHED; |
| 4022 | break; |
| 4023 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4024 | case I915_CACHE_WT: |
| 4025 | args->caching = I915_CACHING_DISPLAY; |
| 4026 | break; |
| 4027 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4028 | default: |
| 4029 | args->caching = I915_CACHING_NONE; |
| 4030 | break; |
| 4031 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4032 | out: |
| 4033 | rcu_read_unlock(); |
| 4034 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4035 | } |
| 4036 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4037 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 4038 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4039 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4040 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4041 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4042 | struct drm_i915_gem_object *obj; |
| 4043 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4044 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4045 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4046 | switch (args->caching) { |
| 4047 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4048 | level = I915_CACHE_NONE; |
| 4049 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4050 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4051 | /* |
| 4052 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 4053 | * snooped mapping may leave stale data in a corresponding CPU |
| 4054 | * cacheline, whereas normally such cachelines would get |
| 4055 | * invalidated. |
| 4056 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4057 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4058 | return -ENODEV; |
| 4059 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4060 | level = I915_CACHE_LLC; |
| 4061 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4062 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4063 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4064 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4065 | default: |
| 4066 | return -EINVAL; |
| 4067 | } |
| 4068 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4069 | obj = i915_gem_object_lookup(file, args->handle); |
| 4070 | if (!obj) |
| 4071 | return -ENOENT; |
| 4072 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 4073 | /* |
| 4074 | * The caching mode of proxy object is handled by its generator, and |
| 4075 | * not allowed to be changed by userspace. |
| 4076 | */ |
| 4077 | if (i915_gem_object_is_proxy(obj)) { |
| 4078 | ret = -ENXIO; |
| 4079 | goto out; |
| 4080 | } |
| 4081 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4082 | if (obj->cache_level == level) |
| 4083 | goto out; |
| 4084 | |
| 4085 | ret = i915_gem_object_wait(obj, |
| 4086 | I915_WAIT_INTERRUPTIBLE, |
| 4087 | MAX_SCHEDULE_TIMEOUT, |
| 4088 | to_rps_client(file)); |
| 4089 | if (ret) |
| 4090 | goto out; |
| 4091 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 4092 | ret = i915_mutex_lock_interruptible(dev); |
| 4093 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4094 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4095 | |
| 4096 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4097 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4098 | |
| 4099 | out: |
| 4100 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4101 | return ret; |
| 4102 | } |
| 4103 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4104 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 4105 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 4106 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 4107 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 4108 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4109 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4110 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4111 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 4112 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4113 | const struct i915_ggtt_view *view, |
| 4114 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4115 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4116 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4117 | int ret; |
| 4118 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4119 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4120 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4121 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4122 | * display coherency whilst setting up the cache domains. |
| 4123 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4124 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4125 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4126 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 4127 | * a result, we make sure that the pinning that is about to occur is |
| 4128 | * done with uncached PTEs. This is lowest common denominator for all |
| 4129 | * chipsets. |
| 4130 | * |
| 4131 | * However for gen6+, we could do better by using the GFDT bit instead |
| 4132 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 4133 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 4134 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4135 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 4136 | HAS_WT(to_i915(obj->base.dev)) ? |
| 4137 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4138 | if (ret) { |
| 4139 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4140 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4141 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4142 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4143 | /* As the user may map the buffer once pinned in the display plane |
| 4144 | * (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] | 4145 | * always use map_and_fenceable for all scanout buffers. However, |
| 4146 | * it may simply be too big to fit into mappable, in which case |
| 4147 | * put it anyway and hope that userspace can cope (but always first |
| 4148 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4149 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4150 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4151 | if ((flags & PIN_MAPPABLE) == 0 && |
| 4152 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4153 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4154 | flags | |
| 4155 | PIN_MAPPABLE | |
| 4156 | PIN_NONBLOCK); |
| 4157 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 4158 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4159 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4160 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4161 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 4162 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 4163 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 4164 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 4165 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4166 | /* It should now be out of any other write domains, and we can update |
| 4167 | * the domain values for our changes. |
| 4168 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4169 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4170 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4171 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4172 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4173 | err_unpin_global: |
| 4174 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4175 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4179 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4180 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 4181 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4182 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4183 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 4184 | return; |
| 4185 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4186 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 4187 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 4188 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4189 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 4190 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4191 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4192 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4193 | } |
| 4194 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4195 | /** |
| 4196 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 4197 | * @obj: object to act on |
| 4198 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4199 | * |
| 4200 | * This function returns when the move is complete, including waiting on |
| 4201 | * flushes to occur. |
| 4202 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 4203 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 4204 | 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] | 4205 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4206 | int ret; |
| 4207 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4208 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4209 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4210 | ret = i915_gem_object_wait(obj, |
| 4211 | I915_WAIT_INTERRUPTIBLE | |
| 4212 | I915_WAIT_LOCKED | |
| 4213 | (write ? I915_WAIT_ALL : 0), |
| 4214 | MAX_SCHEDULE_TIMEOUT, |
| 4215 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 4216 | if (ret) |
| 4217 | return ret; |
| 4218 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 4219 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4220 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4221 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4222 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4223 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4224 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | /* It should now be out of any other write domains, and we can update |
| 4228 | * the domain values for our changes. |
| 4229 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4230 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4231 | |
| 4232 | /* If we're writing through the CPU, then the GPU read domains will |
| 4233 | * need to be invalidated at next use. |
| 4234 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4235 | if (write) |
| 4236 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 4237 | |
| 4238 | return 0; |
| 4239 | } |
| 4240 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4241 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 4242 | * emitted over 20 msec ago. |
| 4243 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4244 | * Note that if we were to use the current jiffies each time around the loop, |
| 4245 | * we wouldn't escape the function with any frames outstanding if the time to |
| 4246 | * render a frame was over 20ms. |
| 4247 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4248 | * This should get us reasonable parallelism between CPU and GPU but also |
| 4249 | * relatively low latency when blocking on a particular request to finish. |
| 4250 | */ |
| 4251 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4252 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4253 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4254 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4255 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 4256 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4257 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4258 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4259 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 4260 | /* ABI: return -EIO if already wedged */ |
| 4261 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 4262 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 4263 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4264 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4265 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4266 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 4267 | break; |
| 4268 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4269 | if (target) { |
| 4270 | list_del(&target->client_link); |
| 4271 | target->file_priv = NULL; |
| 4272 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 4273 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4274 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4275 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4276 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4277 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4278 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4279 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4280 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4281 | return 0; |
| 4282 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4283 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4284 | I915_WAIT_INTERRUPTIBLE, |
| 4285 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4286 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4287 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4288 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4289 | } |
| 4290 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4291 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4292 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 4293 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 4294 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 4295 | u64 alignment, |
| 4296 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4297 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4298 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 4299 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4300 | struct i915_vma *vma; |
| 4301 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 4302 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4303 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4304 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 4305 | if (flags & PIN_MAPPABLE && |
| 4306 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4307 | /* If the required space is larger than the available |
| 4308 | * aperture, we will not able to find a slot for the |
| 4309 | * object and unbinding the object now will be in |
| 4310 | * vain. Worse, doing so may cause us to ping-pong |
| 4311 | * the object in and out of the Global GTT and |
| 4312 | * waste a lot of cycles under the mutex. |
| 4313 | */ |
| 4314 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 4315 | return ERR_PTR(-E2BIG); |
| 4316 | |
| 4317 | /* If NONBLOCK is set the caller is optimistically |
| 4318 | * trying to cache the full object within the mappable |
| 4319 | * aperture, and *must* have a fallback in place for |
| 4320 | * situations where we cannot bind the object. We |
| 4321 | * can be a little more lax here and use the fallback |
| 4322 | * more often to avoid costly migrations of ourselves |
| 4323 | * and other objects within the aperture. |
| 4324 | * |
| 4325 | * Half-the-aperture is used as a simple heuristic. |
| 4326 | * More interesting would to do search for a free |
| 4327 | * block prior to making the commitment to unbind. |
| 4328 | * That caters for the self-harm case, and with a |
| 4329 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 4330 | * we could try to minimise harm to others. |
| 4331 | */ |
| 4332 | if (flags & PIN_NONBLOCK && |
| 4333 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 4334 | return ERR_PTR(-ENOSPC); |
| 4335 | } |
| 4336 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 4337 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 4338 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4339 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4340 | |
| 4341 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4342 | if (flags & PIN_NONBLOCK) { |
| 4343 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 4344 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4345 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4346 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 4347 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4348 | return ERR_PTR(-ENOSPC); |
| 4349 | } |
| 4350 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4351 | WARN(i915_vma_is_pinned(vma), |
| 4352 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4353 | " offset=%08x, req.alignment=%llx," |
| 4354 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 4355 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4356 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4357 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4358 | ret = i915_vma_unbind(vma); |
| 4359 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4360 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4361 | } |
| 4362 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4363 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 4364 | if (ret) |
| 4365 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4366 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4367 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4368 | } |
| 4369 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4370 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4371 | { |
| 4372 | /* Note that we could alias engines in the execbuf API, but |
| 4373 | * that would be very unwise as it prevents userspace from |
| 4374 | * fine control over engine selection. Ahem. |
| 4375 | * |
| 4376 | * This should be something like EXEC_MAX_ENGINE instead of |
| 4377 | * I915_NUM_ENGINES. |
| 4378 | */ |
| 4379 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 4380 | return 0x10000 << id; |
| 4381 | } |
| 4382 | |
| 4383 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 4384 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 4385 | /* The uABI guarantees an active writer is also amongst the read |
| 4386 | * engines. This would be true if we accessed the activity tracking |
| 4387 | * under the lock, but as we perform the lookup of the object and |
| 4388 | * its activity locklessly we can not guarantee that the last_write |
| 4389 | * being active implies that we have set the same engine flag from |
| 4390 | * last_read - hence we always set both read and write busy for |
| 4391 | * last_write. |
| 4392 | */ |
| 4393 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4396 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4397 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4398 | unsigned int (*flag)(unsigned int id)) |
| 4399 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4400 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4401 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4402 | /* We have to check the current hw status of the fence as the uABI |
| 4403 | * guarantees forward progress. We could rely on the idle worker |
| 4404 | * to eventually flush us, but to minimise latency just ask the |
| 4405 | * hardware. |
| 4406 | * |
| 4407 | * Note we only report on the status of native fences. |
| 4408 | */ |
| 4409 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4410 | return 0; |
| 4411 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4412 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4413 | rq = container_of(fence, struct i915_request, fence); |
| 4414 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4415 | return 0; |
| 4416 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4417 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4418 | } |
| 4419 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4420 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4421 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4422 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4423 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4424 | } |
| 4425 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4426 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4427 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4428 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4429 | if (!fence) |
| 4430 | return 0; |
| 4431 | |
| 4432 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4433 | } |
| 4434 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4435 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4436 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4437 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4438 | { |
| 4439 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4440 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4441 | struct reservation_object_list *list; |
| 4442 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4443 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4444 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4445 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4446 | rcu_read_lock(); |
| 4447 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4448 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4449 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4450 | |
| 4451 | /* A discrepancy here is that we do not report the status of |
| 4452 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4453 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4454 | * This also means that wait-ioctl may report an object as busy, |
| 4455 | * where busy-ioctl considers it idle. |
| 4456 | * |
| 4457 | * We trade the ability to warn of foreign fences to report on which |
| 4458 | * i915 engines are active for the object. |
| 4459 | * |
| 4460 | * Alternatively, we can trade that extra information on read/write |
| 4461 | * activity with |
| 4462 | * args->busy = |
| 4463 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4464 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4465 | * |
| 4466 | */ |
| 4467 | retry: |
| 4468 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4469 | |
| 4470 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4471 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4472 | |
| 4473 | /* Translate shared fences to READ set of engines */ |
| 4474 | list = rcu_dereference(obj->resv->fence); |
| 4475 | if (list) { |
| 4476 | unsigned int shared_count = list->shared_count, i; |
| 4477 | |
| 4478 | for (i = 0; i < shared_count; ++i) { |
| 4479 | struct dma_fence *fence = |
| 4480 | rcu_dereference(list->shared[i]); |
| 4481 | |
| 4482 | args->busy |= busy_check_reader(fence); |
| 4483 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4484 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4485 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4486 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4487 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4488 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4489 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4490 | out: |
| 4491 | rcu_read_unlock(); |
| 4492 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4493 | } |
| 4494 | |
| 4495 | int |
| 4496 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4497 | struct drm_file *file_priv) |
| 4498 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4499 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4500 | } |
| 4501 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4502 | int |
| 4503 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4504 | struct drm_file *file_priv) |
| 4505 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4506 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4507 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4508 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4509 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4510 | |
| 4511 | switch (args->madv) { |
| 4512 | case I915_MADV_DONTNEED: |
| 4513 | case I915_MADV_WILLNEED: |
| 4514 | break; |
| 4515 | default: |
| 4516 | return -EINVAL; |
| 4517 | } |
| 4518 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4519 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4520 | if (!obj) |
| 4521 | return -ENOENT; |
| 4522 | |
| 4523 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4524 | if (err) |
| 4525 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4526 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4527 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4528 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4529 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4530 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4531 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4532 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4533 | obj->mm.quirked = false; |
| 4534 | } |
| 4535 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4536 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4537 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4538 | obj->mm.quirked = true; |
| 4539 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4540 | } |
| 4541 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4542 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4543 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4544 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4545 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4546 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4547 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4548 | i915_gem_object_truncate(obj); |
| 4549 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4550 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4551 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4552 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4553 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4554 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4555 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4556 | } |
| 4557 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4558 | static void |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4559 | frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4560 | { |
| 4561 | struct drm_i915_gem_object *obj = |
| 4562 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4563 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4564 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4567 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4568 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4569 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4570 | mutex_init(&obj->mm.lock); |
| 4571 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4572 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4573 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4574 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4575 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4576 | init_rcu_head(&obj->rcu); |
| 4577 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4578 | obj->ops = ops; |
| 4579 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4580 | reservation_object_init(&obj->__builtin_resv); |
| 4581 | obj->resv = &obj->__builtin_resv; |
| 4582 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4583 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4584 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4585 | |
| 4586 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4587 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4588 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4589 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4590 | 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] | 4591 | } |
| 4592 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4593 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4594 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4595 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4596 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4597 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4598 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4599 | |
| 4600 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4601 | }; |
| 4602 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4603 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4604 | struct drm_gem_object *obj, |
| 4605 | size_t size) |
| 4606 | { |
| 4607 | struct drm_i915_private *i915 = to_i915(dev); |
| 4608 | unsigned long flags = VM_NORESERVE; |
| 4609 | struct file *filp; |
| 4610 | |
| 4611 | drm_gem_private_object_init(dev, obj, size); |
| 4612 | |
| 4613 | if (i915->mm.gemfs) |
| 4614 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4615 | flags); |
| 4616 | else |
| 4617 | filp = shmem_file_setup("i915", size, flags); |
| 4618 | |
| 4619 | if (IS_ERR(filp)) |
| 4620 | return PTR_ERR(filp); |
| 4621 | |
| 4622 | obj->filp = filp; |
| 4623 | |
| 4624 | return 0; |
| 4625 | } |
| 4626 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4627 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4628 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4629 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4630 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4631 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4632 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4633 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4634 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4635 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4636 | /* There is a prevalence of the assumption that we fit the object's |
| 4637 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4638 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4639 | * such a local variable, please consider fixing! |
| 4640 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4641 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4642 | return ERR_PTR(-E2BIG); |
| 4643 | |
| 4644 | if (overflows_type(size, obj->base.size)) |
| 4645 | return ERR_PTR(-E2BIG); |
| 4646 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4647 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4648 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4649 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4650 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4651 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4652 | if (ret) |
| 4653 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4654 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4655 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4656 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4657 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4658 | mask &= ~__GFP_HIGHMEM; |
| 4659 | mask |= __GFP_DMA32; |
| 4660 | } |
| 4661 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4662 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4663 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4664 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4665 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4666 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4667 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4668 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4669 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4670 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4671 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4672 | /* 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] | 4673 | * cache) for about a 10% performance improvement |
| 4674 | * compared to uncached. Graphics requests other than |
| 4675 | * display scanout are coherent with the CPU in |
| 4676 | * accessing this cache. This means in this mode we |
| 4677 | * don't need to clflush on the CPU side, and on the |
| 4678 | * GPU side we only need to flush internal caches to |
| 4679 | * get data visible to the CPU. |
| 4680 | * |
| 4681 | * However, we maintain the display planes as UC, and so |
| 4682 | * need to rebind when first used as such. |
| 4683 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4684 | cache_level = I915_CACHE_LLC; |
| 4685 | else |
| 4686 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4687 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4688 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4689 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4690 | trace_i915_gem_object_create(obj); |
| 4691 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4692 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4693 | |
| 4694 | fail: |
| 4695 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4696 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4699 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4700 | { |
| 4701 | /* If we are the last user of the backing storage (be it shmemfs |
| 4702 | * pages or stolen etc), we know that the pages are going to be |
| 4703 | * immediately released. In this case, we can then skip copying |
| 4704 | * back the contents from the GPU. |
| 4705 | */ |
| 4706 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4707 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4708 | return false; |
| 4709 | |
| 4710 | if (obj->base.filp == NULL) |
| 4711 | return true; |
| 4712 | |
| 4713 | /* At first glance, this looks racy, but then again so would be |
| 4714 | * userspace racing mmap against close. However, the first external |
| 4715 | * reference to the filp can only be obtained through the |
| 4716 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4717 | * acquiring such a reference whilst we are in the middle of |
| 4718 | * freeing the object. |
| 4719 | */ |
| 4720 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4721 | } |
| 4722 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4723 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4724 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4725 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4726 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4727 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4728 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4729 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4730 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4731 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4732 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4733 | trace_i915_gem_object_destroy(obj); |
| 4734 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4735 | mutex_lock(&i915->drm.struct_mutex); |
| 4736 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4737 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
| 4738 | list_for_each_entry_safe(vma, vn, |
| 4739 | &obj->vma_list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4740 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4741 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4742 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4743 | } |
Chris Wilson | db6c2b4 | 2016-11-01 11:54:00 +0000 | [diff] [blame] | 4744 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
| 4745 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4746 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4747 | /* This serializes freeing with the shrinker. Since the free |
| 4748 | * is delayed, first by RCU then by the workqueue, we want the |
| 4749 | * shrinker to be able to free pages of unreferenced objects, |
| 4750 | * or else we may oom whilst there are plenty of deferred |
| 4751 | * freed objects. |
| 4752 | */ |
| 4753 | if (i915_gem_object_has_pages(obj)) { |
| 4754 | spin_lock(&i915->mm.obj_lock); |
| 4755 | list_del_init(&obj->mm.link); |
| 4756 | spin_unlock(&i915->mm.obj_lock); |
| 4757 | } |
| 4758 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4759 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4760 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4761 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4762 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4763 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4764 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4765 | |
| 4766 | if (obj->ops->release) |
| 4767 | obj->ops->release(obj); |
| 4768 | |
| 4769 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4770 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4771 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4772 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4773 | |
| 4774 | if (obj->base.import_attach) |
| 4775 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4776 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4777 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4778 | drm_gem_object_release(&obj->base); |
| 4779 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4780 | |
| 4781 | kfree(obj->bit_17); |
| 4782 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4783 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4784 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4785 | atomic_dec(&i915->mm.free_count); |
| 4786 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4787 | if (on) |
| 4788 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4789 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4790 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4794 | { |
| 4795 | struct llist_node *freed; |
| 4796 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4797 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4798 | freed = NULL; |
| 4799 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4800 | /* Only one consumer of llist_del_first() allowed */ |
| 4801 | spin_lock(&i915->mm.free_lock); |
| 4802 | freed = llist_del_first(&i915->mm.free_list); |
| 4803 | spin_unlock(&i915->mm.free_lock); |
| 4804 | } |
| 4805 | if (unlikely(freed)) { |
| 4806 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4807 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4808 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | static void __i915_gem_free_work(struct work_struct *work) |
| 4812 | { |
| 4813 | struct drm_i915_private *i915 = |
| 4814 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4815 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4816 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4817 | /* |
| 4818 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4819 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4820 | * However, the object may also be bound into the global GTT (e.g. |
| 4821 | * older GPUs without per-process support, or for direct access through |
| 4822 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4823 | * unbound now. |
| 4824 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4825 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4826 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4827 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4828 | spin_unlock(&i915->mm.free_lock); |
| 4829 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4830 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4831 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4832 | return; |
| 4833 | |
| 4834 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4835 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4836 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4837 | } |
| 4838 | |
| 4839 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4840 | { |
| 4841 | struct drm_i915_gem_object *obj = |
| 4842 | container_of(head, typeof(*obj), rcu); |
| 4843 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4844 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4845 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4846 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4847 | * it like a rcu_head from this point forwards. And we expect all |
| 4848 | * objects to be freed via this path. |
| 4849 | */ |
| 4850 | destroy_rcu_head(&obj->rcu); |
| 4851 | |
| 4852 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4853 | * Since we require blocking on struct_mutex to unbind the freed |
| 4854 | * object from the GPU before releasing resources back to the |
| 4855 | * system, we can not do that directly from the RCU callback (which may |
| 4856 | * be a softirq context), but must instead then defer that work onto a |
| 4857 | * kthread. We use the RCU callback rather than move the freed object |
| 4858 | * directly onto the work queue so that we can mix between using the |
| 4859 | * worker and performing frees directly from subsequent allocations for |
| 4860 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4861 | */ |
| 4862 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4863 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4867 | { |
| 4868 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4869 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4870 | if (obj->mm.quirked) |
| 4871 | __i915_gem_object_unpin_pages(obj); |
| 4872 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4873 | if (discard_backing_storage(obj)) |
| 4874 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4875 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4876 | /* |
| 4877 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4878 | * read-side critical sections are complete, e.g. |
| 4879 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4880 | * lookup see i915_gem_object_lookup_rcu(). |
| 4881 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4882 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4883 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4884 | } |
| 4885 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4886 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4887 | { |
| 4888 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4889 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4890 | if (!i915_gem_object_has_active_reference(obj) && |
| 4891 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4892 | i915_gem_object_set_active_reference(obj); |
| 4893 | else |
| 4894 | i915_gem_object_put(obj); |
| 4895 | } |
| 4896 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4897 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4898 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4899 | intel_wakeref_t wakeref; |
| 4900 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4901 | GEM_TRACE("\n"); |
| 4902 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4903 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4904 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4905 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4906 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 4907 | |
| 4908 | /* |
| 4909 | * As we have just resumed the machine and woken the device up from |
| 4910 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4911 | * back to defaults, recovering from whatever wedged state we left it |
| 4912 | * in and so worth trying to use the device once more. |
| 4913 | */ |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4914 | if (i915_terminally_wedged(&i915->gpu_error)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4915 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4916 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4917 | /* |
| 4918 | * If we inherit context state from the BIOS or earlier occupants |
| 4919 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4920 | * try to take over. The only way to remove the earlier state |
| 4921 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4922 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4923 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4924 | */ |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 4925 | intel_engines_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4926 | |
| 4927 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4928 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4929 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4930 | i915_gem_contexts_lost(i915); |
| 4931 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4932 | } |
| 4933 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4934 | int i915_gem_suspend(struct drm_i915_private *i915) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4935 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4936 | intel_wakeref_t wakeref; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4937 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4938 | |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 4939 | GEM_TRACE("\n"); |
| 4940 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4941 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4942 | intel_suspend_gt_powersave(i915); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4943 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4944 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4945 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4946 | /* |
| 4947 | * We have to flush all the executing contexts to main memory so |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4948 | * that they can saved in the hibernation image. To ensure the last |
| 4949 | * context image is coherent, we have to switch away from it. That |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4950 | * leaves the i915->kernel_context still active when |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4951 | * we actually suspend, and its image in memory may not match the GPU |
| 4952 | * state. Fortunately, the kernel_context is disposable and we do |
| 4953 | * not rely on its state. |
| 4954 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4955 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 4956 | ret = i915_gem_switch_to_kernel_context(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4957 | if (ret) |
| 4958 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4959 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4960 | ret = i915_gem_wait_for_idle(i915, |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4961 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 4962 | I915_WAIT_LOCKED | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4963 | I915_WAIT_FOR_IDLE_BOOST, |
| 4964 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4965 | if (ret && ret != -EIO) |
| 4966 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4967 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4968 | assert_kernel_context_is_current(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4969 | } |
Chris Wilson | 01f8f33 | 2018-07-17 09:41:21 +0100 | [diff] [blame] | 4970 | i915_retire_requests(i915); /* ensure we flush after wedging */ |
| 4971 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4972 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4973 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4974 | intel_uc_suspend(i915); |
Sagar Arun Kamble | 63987bf | 2017-04-05 15:51:50 +0530 | [diff] [blame] | 4975 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4976 | cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work); |
| 4977 | cancel_delayed_work_sync(&i915->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4978 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4979 | /* |
| 4980 | * 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] | 4981 | * repeat the flush until it is definitely idle. |
| 4982 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4983 | drain_delayed_work(&i915->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4984 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4985 | /* |
| 4986 | * Assert that we successfully flushed all the work and |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4987 | * reset the GPU back to its idle, low power state. |
| 4988 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4989 | WARN_ON(i915->gt.awake); |
| 4990 | if (WARN_ON(!intel_engines_are_idle(i915))) |
| 4991 | i915_gem_set_wedged(i915); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4992 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4993 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4994 | return 0; |
| 4995 | |
| 4996 | err_unlock: |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4997 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4998 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4999 | return ret; |
| 5000 | } |
| 5001 | |
| 5002 | void i915_gem_suspend_late(struct drm_i915_private *i915) |
| 5003 | { |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 5004 | struct drm_i915_gem_object *obj; |
| 5005 | struct list_head *phases[] = { |
| 5006 | &i915->mm.unbound_list, |
| 5007 | &i915->mm.bound_list, |
| 5008 | NULL |
| 5009 | }, **phase; |
| 5010 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 5011 | /* |
| 5012 | * Neither the BIOS, ourselves or any other kernel |
| 5013 | * expects the system to be in execlists mode on startup, |
| 5014 | * so we need to reset the GPU back to legacy mode. And the only |
| 5015 | * known way to disable logical contexts is through a GPU reset. |
| 5016 | * |
| 5017 | * So in order to leave the system in a known default configuration, |
| 5018 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 5019 | * clean up the GEM state tracking, flushing off the requests and |
| 5020 | * leaving the system in a known idle state. |
| 5021 | * |
| 5022 | * Note that is of the upmost importance that the GPU is idle and |
| 5023 | * all stray writes are flushed *before* we dismantle the backing |
| 5024 | * storage for the pinned objects. |
| 5025 | * |
| 5026 | * However, since we are uncertain that resetting the GPU on older |
| 5027 | * machines is a good idea, we don't - just in case it leaves the |
| 5028 | * machine in an unusable condition. |
| 5029 | */ |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 5030 | |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 5031 | mutex_lock(&i915->drm.struct_mutex); |
| 5032 | for (phase = phases; *phase; phase++) { |
| 5033 | list_for_each_entry(obj, *phase, mm.link) |
| 5034 | WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); |
| 5035 | } |
| 5036 | mutex_unlock(&i915->drm.struct_mutex); |
| 5037 | |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 5038 | intel_uc_sanitize(i915); |
| 5039 | i915_gem_sanitize(i915); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5040 | } |
| 5041 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5042 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5043 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 5044 | GEM_TRACE("\n"); |
| 5045 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5046 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5047 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5048 | mutex_lock(&i915->drm.struct_mutex); |
| 5049 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 5050 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5051 | i915_gem_restore_gtt_mappings(i915); |
| 5052 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5053 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5054 | /* |
| 5055 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5056 | * guarantee that the context image is complete. So let's just reset |
| 5057 | * it and start again. |
| 5058 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5059 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5060 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5061 | if (i915_gem_init_hw(i915)) |
| 5062 | goto err_wedged; |
| 5063 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 5064 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 5065 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5066 | /* Always reload a context for powersaving. */ |
| 5067 | if (i915_gem_switch_to_kernel_context(i915)) |
| 5068 | goto err_wedged; |
| 5069 | |
| 5070 | out_unlock: |
| 5071 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 5072 | mutex_unlock(&i915->drm.struct_mutex); |
| 5073 | return; |
| 5074 | |
| 5075 | err_wedged: |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5076 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 5077 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 5078 | i915_gem_set_wedged(i915); |
| 5079 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5080 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5081 | } |
| 5082 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5083 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5084 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5085 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5086 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 5087 | return; |
| 5088 | |
| 5089 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 5090 | DISP_TILE_SURFACE_SWIZZLING); |
| 5091 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5092 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 5093 | return; |
| 5094 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5095 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5096 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5097 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5098 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5099 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5100 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 5101 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 5102 | else |
| 5103 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5104 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 5105 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5106 | 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] | 5107 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5108 | I915_WRITE(RING_CTL(base), 0); |
| 5109 | I915_WRITE(RING_HEAD(base), 0); |
| 5110 | I915_WRITE(RING_TAIL(base), 0); |
| 5111 | I915_WRITE(RING_START(base), 0); |
| 5112 | } |
| 5113 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5114 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5115 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5116 | if (IS_I830(dev_priv)) { |
| 5117 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5118 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5119 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5120 | init_unused_ring(dev_priv, SRB2_BASE); |
| 5121 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5122 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5123 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5124 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5125 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5126 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5127 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5128 | } |
| 5129 | } |
| 5130 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5131 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5132 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5133 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5134 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5135 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5136 | int err; |
| 5137 | |
| 5138 | for_each_engine(engine, i915, id) { |
| 5139 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5140 | if (err) { |
| 5141 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 5142 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5143 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5144 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | return 0; |
| 5148 | } |
| 5149 | |
| 5150 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 5151 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 5152 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5153 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 5154 | dev_priv->gt.last_init_time = ktime_get(); |
| 5155 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5156 | /* Double layer security blanket, see i915_gem_init() */ |
| 5157 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5158 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 5159 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 5160 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5161 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 5162 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5163 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 5164 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 5165 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 5166 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5167 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 5168 | /* ...and determine whether they are sticking. */ |
| 5169 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 5170 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5171 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5172 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5173 | /* |
| 5174 | * At least 830 can leave some of the unused rings |
| 5175 | * "active" (ie. head != tail) after resume which |
| 5176 | * will prevent c3 entry. Makes sure all unused rings |
| 5177 | * are totally idle. |
| 5178 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5179 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5180 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 5181 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5182 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5183 | ret = -EIO; |
| 5184 | goto out; |
| 5185 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 5186 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5187 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5188 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5189 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5190 | goto out; |
| 5191 | } |
| 5192 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 5193 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 5194 | if (ret) { |
| 5195 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 5196 | goto out; |
| 5197 | } |
| 5198 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5199 | /* We can't enable contexts until all firmware is loaded */ |
| 5200 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5201 | if (ret) { |
| 5202 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5203 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5204 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5205 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5206 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 5207 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 5208 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 5209 | ret = __i915_gem_restart_engines(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 5210 | if (ret) |
| 5211 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 5212 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5213 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 5214 | |
| 5215 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 5216 | |
| 5217 | cleanup_uc: |
| 5218 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 5219 | out: |
| 5220 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5221 | |
| 5222 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5223 | } |
| 5224 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5225 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 5226 | { |
| 5227 | struct i915_gem_context *ctx; |
| 5228 | struct intel_engine_cs *engine; |
| 5229 | enum intel_engine_id id; |
| 5230 | int err; |
| 5231 | |
| 5232 | /* |
| 5233 | * As we reset the gpu during very early sanitisation, the current |
| 5234 | * register state on the GPU should reflect its defaults values. |
| 5235 | * We load a context onto the hw (with restore-inhibit), then switch |
| 5236 | * over to a second context to save that default register state. We |
| 5237 | * can then prime every new context with that state so they all start |
| 5238 | * from the same default HW values. |
| 5239 | */ |
| 5240 | |
| 5241 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 5242 | if (IS_ERR(ctx)) |
| 5243 | return PTR_ERR(ctx); |
| 5244 | |
| 5245 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5246 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5247 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5248 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5249 | if (IS_ERR(rq)) { |
| 5250 | err = PTR_ERR(rq); |
| 5251 | goto out_ctx; |
| 5252 | } |
| 5253 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 5254 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5255 | if (engine->init_context) |
| 5256 | err = engine->init_context(rq); |
| 5257 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 5258 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5259 | if (err) |
| 5260 | goto err_active; |
| 5261 | } |
| 5262 | |
| 5263 | err = i915_gem_switch_to_kernel_context(i915); |
| 5264 | if (err) |
| 5265 | goto err_active; |
| 5266 | |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 5267 | if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) { |
| 5268 | i915_gem_set_wedged(i915); |
| 5269 | err = -EIO; /* Caller will declare us wedged */ |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5270 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 5271 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5272 | |
| 5273 | assert_kernel_context_is_current(i915); |
| 5274 | |
Chris Wilson | 8e1cb32 | 2018-09-20 17:13:43 +0100 | [diff] [blame] | 5275 | /* |
| 5276 | * Immediately park the GPU so that we enable powersaving and |
| 5277 | * treat it as idle. The next time we issue a request, we will |
| 5278 | * unpark and start using the engine->pinned_default_state, otherwise |
| 5279 | * it is in limbo and an early reset may fail. |
| 5280 | */ |
| 5281 | __i915_gem_park(i915); |
| 5282 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5283 | for_each_engine(engine, i915, id) { |
| 5284 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 5285 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5286 | |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 5287 | GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count); |
| 5288 | |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 5289 | state = to_intel_context(ctx, engine)->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5290 | if (!state) |
| 5291 | continue; |
| 5292 | |
| 5293 | /* |
| 5294 | * As we will hold a reference to the logical state, it will |
| 5295 | * not be torn down with the context, and importantly the |
| 5296 | * object will hold onto its vma (making it possible for a |
| 5297 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 5298 | * from the GTT to prevent such accidents and reclaim the |
| 5299 | * space. |
| 5300 | */ |
| 5301 | err = i915_vma_unbind(state); |
| 5302 | if (err) |
| 5303 | goto err_active; |
| 5304 | |
| 5305 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 5306 | if (err) |
| 5307 | goto err_active; |
| 5308 | |
| 5309 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 5310 | |
| 5311 | /* Check we can acquire the image of the context state */ |
| 5312 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 5313 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 5314 | if (IS_ERR(vaddr)) { |
| 5315 | err = PTR_ERR(vaddr); |
| 5316 | goto err_active; |
| 5317 | } |
| 5318 | |
| 5319 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 5323 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 5324 | |
| 5325 | /* |
| 5326 | * Make sure that classes with multiple engine instances all |
| 5327 | * share the same basic configuration. |
| 5328 | */ |
| 5329 | for_each_engine(engine, i915, id) { |
| 5330 | unsigned int bit = BIT(engine->uabi_class); |
| 5331 | unsigned int expected = engine->default_state ? bit : 0; |
| 5332 | |
| 5333 | if ((found & bit) != expected) { |
| 5334 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 5335 | engine->uabi_class, engine->name); |
| 5336 | } |
| 5337 | } |
| 5338 | } |
| 5339 | |
| 5340 | out_ctx: |
| 5341 | i915_gem_context_set_closed(ctx); |
| 5342 | i915_gem_context_put(ctx); |
| 5343 | return err; |
| 5344 | |
| 5345 | err_active: |
| 5346 | /* |
| 5347 | * If we have to abandon now, we expect the engines to be idle |
| 5348 | * and ready to be torn-down. First try to flush any remaining |
| 5349 | * request, ensure we are pointing at the kernel context and |
| 5350 | * then remove it. |
| 5351 | */ |
| 5352 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 5353 | goto out_ctx; |
| 5354 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 5355 | if (WARN_ON(i915_gem_wait_for_idle(i915, |
| 5356 | I915_WAIT_LOCKED, |
| 5357 | MAX_SCHEDULE_TIMEOUT))) |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5358 | goto out_ctx; |
| 5359 | |
| 5360 | i915_gem_contexts_lost(i915); |
| 5361 | goto out_ctx; |
| 5362 | } |
| 5363 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5364 | static int |
| 5365 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 5366 | { |
| 5367 | struct drm_i915_gem_object *obj; |
| 5368 | struct i915_vma *vma; |
| 5369 | int ret; |
| 5370 | |
| 5371 | obj = i915_gem_object_create_stolen(i915, size); |
| 5372 | if (!obj) |
| 5373 | obj = i915_gem_object_create_internal(i915, size); |
| 5374 | if (IS_ERR(obj)) { |
| 5375 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 5376 | return PTR_ERR(obj); |
| 5377 | } |
| 5378 | |
| 5379 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 5380 | if (IS_ERR(vma)) { |
| 5381 | ret = PTR_ERR(vma); |
| 5382 | goto err_unref; |
| 5383 | } |
| 5384 | |
| 5385 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 5386 | if (ret) |
| 5387 | goto err_unref; |
| 5388 | |
| 5389 | i915->gt.scratch = vma; |
| 5390 | return 0; |
| 5391 | |
| 5392 | err_unref: |
| 5393 | i915_gem_object_put(obj); |
| 5394 | return ret; |
| 5395 | } |
| 5396 | |
| 5397 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 5398 | { |
| 5399 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 5400 | } |
| 5401 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5402 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5403 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5404 | int ret; |
| 5405 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 5406 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 5407 | 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] | 5408 | mkwrite_device_info(dev_priv)->page_sizes = |
| 5409 | I915_GTT_PAGE_SIZE_4K; |
| 5410 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 5411 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 5412 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5413 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 5414 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5415 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5416 | } else { |
| 5417 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5418 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5419 | } |
| 5420 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5421 | ret = i915_gem_init_userptr(dev_priv); |
| 5422 | if (ret) |
| 5423 | return ret; |
| 5424 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5425 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 5426 | if (ret) |
| 5427 | return ret; |
| 5428 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5429 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 5430 | if (ret) |
| 5431 | goto err_uc_misc; |
| 5432 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5433 | /* This is just a security blanket to placate dragons. |
| 5434 | * On some systems, we very sporadically observe that the first TLBs |
| 5435 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5436 | * we hold the forcewake during initialisation these problems |
| 5437 | * just magically go away. |
| 5438 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5439 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5440 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5441 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5442 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5443 | if (ret) { |
| 5444 | GEM_BUG_ON(ret == -EIO); |
| 5445 | goto err_unlock; |
| 5446 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5447 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5448 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5449 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5450 | if (ret) { |
| 5451 | GEM_BUG_ON(ret == -EIO); |
| 5452 | goto err_ggtt; |
| 5453 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5454 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5455 | ret = i915_gem_contexts_init(dev_priv); |
| 5456 | if (ret) { |
| 5457 | GEM_BUG_ON(ret == -EIO); |
| 5458 | goto err_scratch; |
| 5459 | } |
| 5460 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5461 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5462 | if (ret) { |
| 5463 | GEM_BUG_ON(ret == -EIO); |
| 5464 | goto err_context; |
| 5465 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5466 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5467 | intel_init_gt_powersave(dev_priv); |
| 5468 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5469 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5470 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5471 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5472 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5473 | ret = i915_gem_init_hw(dev_priv); |
| 5474 | if (ret) |
| 5475 | goto err_uc_init; |
| 5476 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5477 | /* |
| 5478 | * Despite its name intel_init_clock_gating applies both display |
| 5479 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5480 | * GT power context workaround. Worse, sometimes it includes a context |
| 5481 | * register workaround which we need to apply before we record the |
| 5482 | * default HW state for all contexts. |
| 5483 | * |
| 5484 | * FIXME: break up the workarounds and apply them at the right time! |
| 5485 | */ |
| 5486 | intel_init_clock_gating(dev_priv); |
| 5487 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5488 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5489 | if (ret) |
| 5490 | goto err_init_hw; |
| 5491 | |
| 5492 | if (i915_inject_load_failure()) { |
| 5493 | ret = -ENODEV; |
| 5494 | goto err_init_hw; |
| 5495 | } |
| 5496 | |
| 5497 | if (i915_inject_load_failure()) { |
| 5498 | ret = -EIO; |
| 5499 | goto err_init_hw; |
| 5500 | } |
| 5501 | |
| 5502 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5503 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5504 | |
| 5505 | return 0; |
| 5506 | |
| 5507 | /* |
| 5508 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5509 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5510 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5511 | * driver doesn't explode during runtime. |
| 5512 | */ |
| 5513 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5514 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5515 | |
| 5516 | WARN_ON(i915_gem_suspend(dev_priv)); |
| 5517 | i915_gem_suspend_late(dev_priv); |
| 5518 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 5519 | i915_gem_drain_workqueue(dev_priv); |
| 5520 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5521 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5522 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5523 | err_uc_init: |
| 5524 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5525 | err_pm: |
| 5526 | if (ret != -EIO) { |
| 5527 | intel_cleanup_gt_powersave(dev_priv); |
| 5528 | i915_gem_cleanup_engines(dev_priv); |
| 5529 | } |
| 5530 | err_context: |
| 5531 | if (ret != -EIO) |
| 5532 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5533 | err_scratch: |
| 5534 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5535 | err_ggtt: |
| 5536 | err_unlock: |
| 5537 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5538 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5539 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5540 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5541 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5542 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5543 | if (ret != -EIO) |
| 5544 | i915_gem_cleanup_userptr(dev_priv); |
| 5545 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5546 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5547 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5548 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5549 | /* |
| 5550 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5551 | * wedged. But we only want to do this where the GPU is angry, |
| 5552 | * for all other failure, such as an allocation failure, bail. |
| 5553 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5554 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 5555 | i915_load_error(dev_priv, |
| 5556 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5557 | i915_gem_set_wedged(dev_priv); |
| 5558 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5559 | |
| 5560 | /* Minimal basic recovery for KMS */ |
| 5561 | ret = i915_ggtt_enable_hw(dev_priv); |
| 5562 | i915_gem_restore_gtt_mappings(dev_priv); |
| 5563 | i915_gem_restore_fences(dev_priv); |
| 5564 | intel_init_clock_gating(dev_priv); |
| 5565 | |
| 5566 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5567 | } |
| 5568 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5569 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5570 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5571 | } |
| 5572 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5573 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 5574 | { |
| 5575 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5576 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5577 | |
| 5578 | /* Flush any outstanding unpin_work. */ |
| 5579 | i915_gem_drain_workqueue(dev_priv); |
| 5580 | |
| 5581 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5582 | intel_uc_fini_hw(dev_priv); |
| 5583 | intel_uc_fini(dev_priv); |
| 5584 | i915_gem_cleanup_engines(dev_priv); |
| 5585 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5586 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5587 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5588 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5589 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 5590 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5591 | intel_cleanup_gt_powersave(dev_priv); |
| 5592 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5593 | intel_uc_fini_misc(dev_priv); |
| 5594 | i915_gem_cleanup_userptr(dev_priv); |
| 5595 | |
| 5596 | i915_gem_drain_freed_objects(dev_priv); |
| 5597 | |
| 5598 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 5599 | } |
| 5600 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5601 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5602 | { |
| 5603 | i915_gem_sanitize(i915); |
| 5604 | } |
| 5605 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5606 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5607 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5608 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5609 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5610 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5611 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5612 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5613 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5614 | } |
| 5615 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5616 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5617 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5618 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5619 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5620 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5621 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5622 | !IS_CHERRYVIEW(dev_priv)) |
| 5623 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5624 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5625 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5626 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5627 | dev_priv->num_fence_regs = 16; |
| 5628 | else |
| 5629 | dev_priv->num_fence_regs = 8; |
| 5630 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5631 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5632 | dev_priv->num_fence_regs = |
| 5633 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5634 | |
| 5635 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5636 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5637 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5638 | |
| 5639 | fence->i915 = dev_priv; |
| 5640 | fence->id = i; |
| 5641 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5642 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5643 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5644 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5645 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5646 | } |
| 5647 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5648 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5649 | { |
| 5650 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5651 | spin_lock_init(&i915->mm.obj_lock); |
| 5652 | spin_lock_init(&i915->mm.free_lock); |
| 5653 | |
| 5654 | init_llist_head(&i915->mm.free_list); |
| 5655 | |
| 5656 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5657 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5658 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5659 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5660 | |
| 5661 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5662 | } |
| 5663 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5664 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5665 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5666 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5667 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5668 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5669 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5670 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5671 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5672 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5673 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5674 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5675 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5676 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5677 | if (!dev_priv->luts) |
| 5678 | goto err_vmas; |
| 5679 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5680 | dev_priv->requests = KMEM_CACHE(i915_request, |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5681 | SLAB_HWCACHE_ALIGN | |
| 5682 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5683 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5684 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5685 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5686 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5687 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5688 | SLAB_HWCACHE_ALIGN | |
| 5689 | SLAB_RECLAIM_ACCOUNT); |
| 5690 | if (!dev_priv->dependencies) |
| 5691 | goto err_requests; |
| 5692 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5693 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5694 | if (!dev_priv->priorities) |
| 5695 | goto err_dependencies; |
| 5696 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5697 | INIT_LIST_HEAD(&dev_priv->gt.timelines); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5698 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 5699 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5700 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5701 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5702 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5703 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5704 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5705 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5706 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5707 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5708 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5709 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5710 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5711 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5712 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5713 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5714 | err = i915_gemfs_init(dev_priv); |
| 5715 | if (err) |
| 5716 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5717 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5718 | return 0; |
| 5719 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5720 | err_dependencies: |
| 5721 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5722 | err_requests: |
| 5723 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5724 | err_luts: |
| 5725 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5726 | err_vmas: |
| 5727 | kmem_cache_destroy(dev_priv->vmas); |
| 5728 | err_objects: |
| 5729 | kmem_cache_destroy(dev_priv->objects); |
| 5730 | err_out: |
| 5731 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5732 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5733 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5734 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5735 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5736 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5737 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5738 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5739 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5740 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5741 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5742 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5743 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5744 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5745 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5746 | kmem_cache_destroy(dev_priv->vmas); |
| 5747 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5748 | |
| 5749 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5750 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5751 | |
| 5752 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5753 | } |
| 5754 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5755 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5756 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5757 | /* Discard all purgeable objects, let userspace recover those as |
| 5758 | * required after resuming. |
| 5759 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5760 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5761 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5762 | return 0; |
| 5763 | } |
| 5764 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5765 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5766 | { |
| 5767 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5768 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5769 | &i915->mm.unbound_list, |
| 5770 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5771 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5772 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5773 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5774 | /* |
| 5775 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5776 | * |
| 5777 | * We need to update the domain tracking to reflect that the CPU |
| 5778 | * will be accessing all the pages to create and restore from the |
| 5779 | * hibernation, and so upon restoration those pages will be in the |
| 5780 | * CPU domain. |
| 5781 | * |
| 5782 | * To make sure the hibernation image contains the latest state, |
| 5783 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5784 | * |
| 5785 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5786 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5787 | */ |
| 5788 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5789 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 5790 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5791 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5792 | mutex_lock(&i915->drm.struct_mutex); |
| 5793 | for (phase = phases; *phase; phase++) { |
| 5794 | list_for_each_entry(obj, *phase, mm.link) |
| 5795 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5796 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5797 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5798 | |
| 5799 | return 0; |
| 5800 | } |
| 5801 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5802 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5803 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5804 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5805 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5806 | |
| 5807 | /* Clean up our request list when the client is going away, so that |
| 5808 | * later retire_requests won't dereference our soon-to-be-gone |
| 5809 | * file_priv. |
| 5810 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5811 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5812 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5813 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5814 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5815 | } |
| 5816 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5817 | 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] | 5818 | { |
| 5819 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5820 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5821 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5822 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5823 | |
| 5824 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5825 | if (!file_priv) |
| 5826 | return -ENOMEM; |
| 5827 | |
| 5828 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5829 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5830 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5831 | |
| 5832 | spin_lock_init(&file_priv->mm.lock); |
| 5833 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5834 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5835 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 5836 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5837 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5838 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5839 | if (ret) |
| 5840 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5841 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5842 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5843 | } |
| 5844 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5845 | /** |
| 5846 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5847 | * @old: current GEM buffer for the frontbuffer slots |
| 5848 | * @new: new GEM buffer for the frontbuffer slots |
| 5849 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5850 | * |
| 5851 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5852 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5853 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5854 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5855 | struct drm_i915_gem_object *new, |
| 5856 | unsigned frontbuffer_bits) |
| 5857 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5858 | /* Control of individual bits within the mask are guarded by |
| 5859 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5860 | * manipulation of individual bits. But since the bitfield as a whole |
| 5861 | * is updated using RMW, we need to use atomics in order to update |
| 5862 | * the bits. |
| 5863 | */ |
| 5864 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 5865 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5866 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5867 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5868 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5869 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5870 | } |
| 5871 | |
| 5872 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5873 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5874 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5875 | } |
| 5876 | } |
| 5877 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5878 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5879 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5880 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5881 | const void *data, size_t size) |
| 5882 | { |
| 5883 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5884 | struct file *file; |
| 5885 | size_t offset; |
| 5886 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5887 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5888 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5889 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5890 | return obj; |
| 5891 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5892 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5893 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5894 | file = obj->base.filp; |
| 5895 | offset = 0; |
| 5896 | do { |
| 5897 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5898 | struct page *page; |
| 5899 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5900 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5901 | err = pagecache_write_begin(file, file->f_mapping, |
| 5902 | offset, len, 0, |
| 5903 | &page, &pgdata); |
| 5904 | if (err < 0) |
| 5905 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5906 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5907 | vaddr = kmap(page); |
| 5908 | memcpy(vaddr, data, len); |
| 5909 | kunmap(page); |
| 5910 | |
| 5911 | err = pagecache_write_end(file, file->f_mapping, |
| 5912 | offset, len, len, |
| 5913 | page, pgdata); |
| 5914 | if (err < 0) |
| 5915 | goto fail; |
| 5916 | |
| 5917 | size -= len; |
| 5918 | data += len; |
| 5919 | offset += len; |
| 5920 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5921 | |
| 5922 | return obj; |
| 5923 | |
| 5924 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5925 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5926 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5927 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5928 | |
| 5929 | struct scatterlist * |
| 5930 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5931 | unsigned int n, |
| 5932 | unsigned int *offset) |
| 5933 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5934 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5935 | struct scatterlist *sg; |
| 5936 | unsigned int idx, count; |
| 5937 | |
| 5938 | might_sleep(); |
| 5939 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5940 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5941 | |
| 5942 | /* As we iterate forward through the sg, we record each entry in a |
| 5943 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5944 | * this index previously, we will have an entry for it. |
| 5945 | * |
| 5946 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5947 | * sequential page access (where each new request is consecutive |
| 5948 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5949 | * i.e. O(1) with a large constant! |
| 5950 | */ |
| 5951 | if (n < READ_ONCE(iter->sg_idx)) |
| 5952 | goto lookup; |
| 5953 | |
| 5954 | mutex_lock(&iter->lock); |
| 5955 | |
| 5956 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5957 | * (or the subsequent) sg are fast - comparing against the last |
| 5958 | * sg is faster than going through the radixtree. |
| 5959 | */ |
| 5960 | |
| 5961 | sg = iter->sg_pos; |
| 5962 | idx = iter->sg_idx; |
| 5963 | count = __sg_page_count(sg); |
| 5964 | |
| 5965 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5966 | void *entry; |
| 5967 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5968 | int ret; |
| 5969 | |
| 5970 | /* If we cannot allocate and insert this entry, or the |
| 5971 | * individual pages from this range, cancel updating the |
| 5972 | * sg_idx so that on this lookup we are forced to linearly |
| 5973 | * scan onwards, but on future lookups we will try the |
| 5974 | * insertion again (in which case we need to be careful of |
| 5975 | * the error return reporting that we have already inserted |
| 5976 | * this index). |
| 5977 | */ |
| 5978 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5979 | if (ret && ret != -EEXIST) |
| 5980 | goto scan; |
| 5981 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5982 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5983 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5984 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5985 | if (ret && ret != -EEXIST) |
| 5986 | goto scan; |
| 5987 | } |
| 5988 | |
| 5989 | idx += count; |
| 5990 | sg = ____sg_next(sg); |
| 5991 | count = __sg_page_count(sg); |
| 5992 | } |
| 5993 | |
| 5994 | scan: |
| 5995 | iter->sg_pos = sg; |
| 5996 | iter->sg_idx = idx; |
| 5997 | |
| 5998 | mutex_unlock(&iter->lock); |
| 5999 | |
| 6000 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 6001 | goto lookup; |
| 6002 | |
| 6003 | /* In case we failed to insert the entry into the radixtree, we need |
| 6004 | * to look beyond the current sg. |
| 6005 | */ |
| 6006 | while (idx + count <= n) { |
| 6007 | idx += count; |
| 6008 | sg = ____sg_next(sg); |
| 6009 | count = __sg_page_count(sg); |
| 6010 | } |
| 6011 | |
| 6012 | *offset = n - idx; |
| 6013 | return sg; |
| 6014 | |
| 6015 | lookup: |
| 6016 | rcu_read_lock(); |
| 6017 | |
| 6018 | sg = radix_tree_lookup(&iter->radix, n); |
| 6019 | GEM_BUG_ON(!sg); |
| 6020 | |
| 6021 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 6022 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 6023 | * to the start of that range. We will return the pointer to |
| 6024 | * the base page and the offset of this page within the |
| 6025 | * sg entry's range. |
| 6026 | */ |
| 6027 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 6028 | if (unlikely(xa_is_value(sg))) { |
| 6029 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 6030 | |
| 6031 | sg = radix_tree_lookup(&iter->radix, base); |
| 6032 | GEM_BUG_ON(!sg); |
| 6033 | |
| 6034 | *offset = n - base; |
| 6035 | } |
| 6036 | |
| 6037 | rcu_read_unlock(); |
| 6038 | |
| 6039 | return sg; |
| 6040 | } |
| 6041 | |
| 6042 | struct page * |
| 6043 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 6044 | { |
| 6045 | struct scatterlist *sg; |
| 6046 | unsigned int offset; |
| 6047 | |
| 6048 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 6049 | |
| 6050 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 6051 | return nth_page(sg_page(sg), offset); |
| 6052 | } |
| 6053 | |
| 6054 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 6055 | struct page * |
| 6056 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 6057 | unsigned int n) |
| 6058 | { |
| 6059 | struct page *page; |
| 6060 | |
| 6061 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 6062 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 6063 | set_page_dirty(page); |
| 6064 | |
| 6065 | return page; |
| 6066 | } |
| 6067 | |
| 6068 | dma_addr_t |
| 6069 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 6070 | unsigned long n) |
| 6071 | { |
| 6072 | struct scatterlist *sg; |
| 6073 | unsigned int offset; |
| 6074 | |
| 6075 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 6076 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 6077 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6078 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6079 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 6080 | { |
| 6081 | struct sg_table *pages; |
| 6082 | int err; |
| 6083 | |
| 6084 | if (align > obj->base.size) |
| 6085 | return -EINVAL; |
| 6086 | |
| 6087 | if (obj->ops == &i915_gem_phys_ops) |
| 6088 | return 0; |
| 6089 | |
| 6090 | if (obj->ops != &i915_gem_object_ops) |
| 6091 | return -EINVAL; |
| 6092 | |
| 6093 | err = i915_gem_object_unbind(obj); |
| 6094 | if (err) |
| 6095 | return err; |
| 6096 | |
| 6097 | mutex_lock(&obj->mm.lock); |
| 6098 | |
| 6099 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 6100 | err = -EFAULT; |
| 6101 | goto err_unlock; |
| 6102 | } |
| 6103 | |
| 6104 | if (obj->mm.quirked) { |
| 6105 | err = -EFAULT; |
| 6106 | goto err_unlock; |
| 6107 | } |
| 6108 | |
| 6109 | if (obj->mm.mapping) { |
| 6110 | err = -EBUSY; |
| 6111 | goto err_unlock; |
| 6112 | } |
| 6113 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 6114 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 6115 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6116 | obj->ops = &i915_gem_phys_ops; |
| 6117 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 6118 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6119 | if (err) |
| 6120 | goto err_xfer; |
| 6121 | |
| 6122 | /* Perma-pin (until release) the physical set of pages */ |
| 6123 | __i915_gem_object_pin_pages(obj); |
| 6124 | |
| 6125 | if (!IS_ERR_OR_NULL(pages)) |
| 6126 | i915_gem_object_ops.put_pages(obj, pages); |
| 6127 | mutex_unlock(&obj->mm.lock); |
| 6128 | return 0; |
| 6129 | |
| 6130 | err_xfer: |
| 6131 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 6132 | if (!IS_ERR_OR_NULL(pages)) { |
| 6133 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 6134 | |
| 6135 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 6136 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6137 | err_unlock: |
| 6138 | mutex_unlock(&obj->mm.lock); |
| 6139 | return err; |
| 6140 | } |
| 6141 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6142 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 6143 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 6144 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 6145 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 6146 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 6147 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 6148 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 6149 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6150 | #endif |