Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
Daniel Vetter | be6a037 | 2015-03-18 10:46:04 +0100 | [diff] [blame] | 2 | * Copyright © 2008-2015 Intel Corporation |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 28 | #include <drm/drmP.h> |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 29 | #include <drm/drm_vma_manager.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/i915_drm.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 31 | #include "i915_drv.h" |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 32 | #include "i915_gem_clflush.h" |
Yu Zhang | eb82289 | 2015-02-10 19:05:49 +0800 | [diff] [blame] | 33 | #include "i915_vgpu.h" |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 34 | #include "i915_trace.h" |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 35 | #include "intel_drv.h" |
Chris Wilson | 5d723d7 | 2016-08-04 16:32:35 +0100 | [diff] [blame] | 36 | #include "intel_frontbuffer.h" |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 37 | #include "intel_mocs.h" |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 38 | #include "intel_workarounds.h" |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 39 | #include "i915_gemfs.h" |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 40 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 41 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 42 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 43 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 44 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 45 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 46 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 47 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 48 | #include <linux/dma-buf.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 49 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 50 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 51 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 52 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 53 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 54 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 55 | return false; |
| 56 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 57 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 58 | return true; |
| 59 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 60 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 63 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 64 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 65 | struct drm_mm_node *node, u32 size) |
| 66 | { |
| 67 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 68 | return drm_mm_insert_node_in_range(&ggtt->base.mm, node, |
| 69 | size, 0, I915_COLOR_UNEVICTABLE, |
| 70 | 0, ggtt->mappable_end, |
| 71 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static void |
| 75 | remove_mappable_node(struct drm_mm_node *node) |
| 76 | { |
| 77 | drm_mm_remove_node(node); |
| 78 | } |
| 79 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 80 | /* some bookkeeping */ |
| 81 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, |
Chris Wilson | 3ef7f22 | 2016-10-18 13:02:48 +0100 | [diff] [blame] | 82 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 83 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 84 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 85 | dev_priv->mm.object_count++; |
| 86 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 87 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, |
Chris Wilson | 3ef7f22 | 2016-10-18 13:02:48 +0100 | [diff] [blame] | 91 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 92 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 93 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 94 | dev_priv->mm.object_count--; |
| 95 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 96 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Chris Wilson | 21dd373 | 2011-01-26 15:55:56 +0000 | [diff] [blame] | 99 | static int |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 100 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 101 | { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 102 | int ret; |
| 103 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 104 | might_sleep(); |
| 105 | |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging |
| 108 | * userspace. If it takes that long something really bad is going on and |
| 109 | * we should simply try to bail out and fail as gracefully as possible. |
| 110 | */ |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 111 | ret = wait_event_interruptible_timeout(error->reset_queue, |
Chris Wilson | 8c185ec | 2017-03-16 17:13:02 +0000 | [diff] [blame] | 112 | !i915_reset_backoff(error), |
Chris Wilson | b52992c | 2016-10-28 13:58:24 +0100 | [diff] [blame] | 113 | I915_RESET_TIMEOUT); |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 114 | if (ret == 0) { |
| 115 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); |
| 116 | return -EIO; |
| 117 | } else if (ret < 0) { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 118 | return ret; |
Chris Wilson | d98c52c | 2016-04-13 17:35:05 +0100 | [diff] [blame] | 119 | } else { |
| 120 | return 0; |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 121 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 124 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 125 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 126 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 127 | int ret; |
| 128 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 129 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 130 | if (ret) |
| 131 | return ret; |
| 132 | |
| 133 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 137 | return 0; |
| 138 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 139 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 140 | static u32 __i915_gem_park(struct drm_i915_private *i915) |
| 141 | { |
| 142 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 143 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame^] | 144 | GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 145 | |
| 146 | if (!i915->gt.awake) |
| 147 | return I915_EPOCH_INVALID; |
| 148 | |
| 149 | GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); |
| 150 | |
| 151 | /* |
| 152 | * Be paranoid and flush a concurrent interrupt to make sure |
| 153 | * we don't reactivate any irq tasklets after parking. |
| 154 | * |
| 155 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 156 | * there may still be an in-flight interrupt even though the CSB |
| 157 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 158 | * is completed before we continue, but it doesn't prevent the HW from |
| 159 | * raising a spurious interrupt later. To complete the shield we should |
| 160 | * coordinate disabling the CS irq with flushing the interrupts. |
| 161 | */ |
| 162 | synchronize_irq(i915->drm.irq); |
| 163 | |
| 164 | intel_engines_park(i915); |
| 165 | i915_gem_timelines_park(i915); |
| 166 | |
| 167 | i915_pmu_gt_parked(i915); |
| 168 | |
| 169 | i915->gt.awake = false; |
| 170 | |
| 171 | if (INTEL_GEN(i915) >= 6) |
| 172 | gen6_rps_idle(i915); |
| 173 | |
| 174 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ); |
| 175 | |
| 176 | intel_runtime_pm_put(i915); |
| 177 | |
| 178 | return i915->gt.epoch; |
| 179 | } |
| 180 | |
| 181 | void i915_gem_park(struct drm_i915_private *i915) |
| 182 | { |
| 183 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 184 | GEM_BUG_ON(i915->gt.active_requests); |
| 185 | |
| 186 | if (!i915->gt.awake) |
| 187 | return; |
| 188 | |
| 189 | /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ |
| 190 | mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); |
| 191 | } |
| 192 | |
| 193 | void i915_gem_unpark(struct drm_i915_private *i915) |
| 194 | { |
| 195 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 196 | GEM_BUG_ON(!i915->gt.active_requests); |
| 197 | |
| 198 | if (i915->gt.awake) |
| 199 | return; |
| 200 | |
| 201 | intel_runtime_pm_get_noresume(i915); |
| 202 | |
| 203 | /* |
| 204 | * It seems that the DMC likes to transition between the DC states a lot |
| 205 | * when there are no connected displays (no active power domains) during |
| 206 | * command submission. |
| 207 | * |
| 208 | * This activity has negative impact on the performance of the chip with |
| 209 | * huge latencies observed in the interrupt handler and elsewhere. |
| 210 | * |
| 211 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 212 | * GT activity, preventing any DC state transitions. |
| 213 | */ |
| 214 | intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 215 | |
| 216 | i915->gt.awake = true; |
| 217 | if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ |
| 218 | i915->gt.epoch = 1; |
| 219 | |
| 220 | intel_enable_gt_powersave(i915); |
| 221 | i915_update_gfx_val(i915); |
| 222 | if (INTEL_GEN(i915) >= 6) |
| 223 | gen6_rps_busy(i915); |
| 224 | i915_pmu_gt_unparked(i915); |
| 225 | |
| 226 | intel_engines_unpark(i915); |
| 227 | |
| 228 | i915_queue_hangcheck(i915); |
| 229 | |
| 230 | queue_delayed_work(i915->wq, |
| 231 | &i915->gt.retire_work, |
| 232 | round_jiffies_up_relative(HZ)); |
| 233 | } |
| 234 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 235 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 236 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 237 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 238 | { |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 239 | struct drm_i915_private *dev_priv = to_i915(dev); |
Joonas Lahtinen | 62106b4 | 2016-03-18 10:42:57 +0200 | [diff] [blame] | 240 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 241 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 242 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 243 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 244 | |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 245 | pinned = ggtt->base.reserved; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 246 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 247 | list_for_each_entry(vma, &ggtt->base.active_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 248 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 249 | pinned += vma->node.size; |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 250 | list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 251 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 252 | pinned += vma->node.size; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 253 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 254 | |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 255 | args->aper_size = ggtt->base.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 256 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 257 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 261 | 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] | 262 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 263 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 264 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 265 | struct sg_table *st; |
| 266 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 267 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 268 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 269 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 270 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 271 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 272 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 273 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 274 | /* Always aligning to the object size, allows a single allocation |
| 275 | * to handle all possible callers, and given typical object sizes, |
| 276 | * the alignment of the buddy allocation will naturally match. |
| 277 | */ |
| 278 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 279 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 280 | roundup_pow_of_two(obj->base.size)); |
| 281 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 282 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 283 | |
| 284 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 285 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 286 | struct page *page; |
| 287 | char *src; |
| 288 | |
| 289 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 290 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 291 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 292 | goto err_phys; |
| 293 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 294 | |
| 295 | src = kmap_atomic(page); |
| 296 | memcpy(vaddr, src, PAGE_SIZE); |
| 297 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 298 | kunmap_atomic(src); |
| 299 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 300 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 301 | vaddr += PAGE_SIZE; |
| 302 | } |
| 303 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 304 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 305 | |
| 306 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 307 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 308 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 309 | goto err_phys; |
| 310 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 311 | |
| 312 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 313 | kfree(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; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | sg = st->sgl; |
| 319 | sg->offset = 0; |
| 320 | sg->length = obj->base.size; |
| 321 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 322 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 323 | sg_dma_len(sg) = obj->base.size; |
| 324 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 325 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 326 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 327 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 328 | |
| 329 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 330 | |
| 331 | err_phys: |
| 332 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 333 | |
| 334 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 337 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 338 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 339 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 340 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 341 | if (cpu_write_needs_clflush(obj)) |
| 342 | obj->cache_dirty = true; |
| 343 | } |
| 344 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 345 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 346 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 347 | struct sg_table *pages, |
| 348 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 349 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 350 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 351 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 352 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 353 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 354 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 355 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 356 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 357 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 358 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 359 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 360 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static void |
| 364 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 365 | struct sg_table *pages) |
| 366 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 367 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 368 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 369 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 370 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 371 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 372 | int i; |
| 373 | |
| 374 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 375 | struct page *page; |
| 376 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 377 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 378 | page = shmem_read_mapping_page(mapping, i); |
| 379 | if (IS_ERR(page)) |
| 380 | continue; |
| 381 | |
| 382 | dst = kmap_atomic(page); |
| 383 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 384 | memcpy(dst, vaddr, PAGE_SIZE); |
| 385 | kunmap_atomic(dst); |
| 386 | |
| 387 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 388 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 389 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 390 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 391 | vaddr += PAGE_SIZE; |
| 392 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 393 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 396 | sg_free_table(pages); |
| 397 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 398 | |
| 399 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static void |
| 403 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 404 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 405 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 409 | .get_pages = i915_gem_object_get_pages_phys, |
| 410 | .put_pages = i915_gem_object_put_pages_phys, |
| 411 | .release = i915_gem_object_release_phys, |
| 412 | }; |
| 413 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 414 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 415 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 416 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 417 | { |
| 418 | struct i915_vma *vma; |
| 419 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 420 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 421 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 422 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 423 | |
| 424 | /* Closed vma are removed from the obj->vma_list - but they may |
| 425 | * still have an active binding on the object. To remove those we |
| 426 | * must wait for all rendering to complete to the object (as unbinding |
| 427 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 428 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 429 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 430 | if (ret) |
| 431 | return ret; |
| 432 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 433 | while ((vma = list_first_entry_or_null(&obj->vma_list, |
| 434 | struct i915_vma, |
| 435 | obj_link))) { |
| 436 | list_move_tail(&vma->obj_link, &still_in_list); |
| 437 | ret = i915_vma_unbind(vma); |
| 438 | if (ret) |
| 439 | break; |
| 440 | } |
| 441 | list_splice(&still_in_list, &obj->vma_list); |
| 442 | |
| 443 | return ret; |
| 444 | } |
| 445 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 446 | static long |
| 447 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 448 | unsigned int flags, |
| 449 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 450 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 451 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 452 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 453 | |
| 454 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 455 | |
| 456 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 457 | return timeout; |
| 458 | |
| 459 | if (!dma_fence_is_i915(fence)) |
| 460 | return dma_fence_wait_timeout(fence, |
| 461 | flags & I915_WAIT_INTERRUPTIBLE, |
| 462 | timeout); |
| 463 | |
| 464 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 465 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 466 | goto out; |
| 467 | |
Chris Wilson | e9af4ea | 2018-01-18 13:16:09 +0000 | [diff] [blame] | 468 | /* |
| 469 | * 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] | 470 | * this is undesirable and limits the throughput of the system, as |
| 471 | * many clients cannot continue processing user input/output whilst |
| 472 | * blocked. RPS autotuning may take tens of milliseconds to respond |
| 473 | * to the GPU load and thus incurs additional latency for the client. |
| 474 | * We can circumvent that by promoting the GPU frequency to maximum |
| 475 | * before we wait. This makes the GPU throttle up much more quickly |
| 476 | * (good for benchmarks and user experience, e.g. window animations), |
| 477 | * but at a cost of spending more power processing the workload |
| 478 | * (bad for battery). Not all clients even want their results |
| 479 | * immediately and for them we should just let the GPU select its own |
| 480 | * frequency to maximise efficiency. To prevent a single client from |
| 481 | * forcing the clocks too high for the whole system, we only allow |
| 482 | * each client to waitboost once in a busy period. |
| 483 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 484 | if (rps_client && !i915_request_started(rq)) { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 485 | if (INTEL_GEN(rq->i915) >= 6) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 486 | gen6_rps_boost(rq, rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 489 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 490 | |
| 491 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 492 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 493 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 494 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 495 | return timeout; |
| 496 | } |
| 497 | |
| 498 | static long |
| 499 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 500 | unsigned int flags, |
| 501 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 502 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 503 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 504 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 505 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 506 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 507 | |
| 508 | if (flags & I915_WAIT_ALL) { |
| 509 | struct dma_fence **shared; |
| 510 | unsigned int count, i; |
| 511 | int ret; |
| 512 | |
| 513 | ret = reservation_object_get_fences_rcu(resv, |
| 514 | &excl, &count, &shared); |
| 515 | if (ret) |
| 516 | return ret; |
| 517 | |
| 518 | for (i = 0; i < count; i++) { |
| 519 | timeout = i915_gem_object_wait_fence(shared[i], |
| 520 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 521 | rps_client); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 522 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 523 | break; |
| 524 | |
| 525 | dma_fence_put(shared[i]); |
| 526 | } |
| 527 | |
| 528 | for (; i < count; i++) |
| 529 | dma_fence_put(shared[i]); |
| 530 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 531 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 532 | /* |
| 533 | * If both shared fences and an exclusive fence exist, |
| 534 | * then by construction the shared fences must be later |
| 535 | * than the exclusive fence. If we successfully wait for |
| 536 | * all the shared fences, we know that the exclusive fence |
| 537 | * must all be signaled. If all the shared fences are |
| 538 | * signaled, we can prune the array and recover the |
| 539 | * floating references on the fences/requests. |
| 540 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 541 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 542 | } else { |
| 543 | excl = reservation_object_get_excl_rcu(resv); |
| 544 | } |
| 545 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 546 | if (excl && timeout >= 0) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 547 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
| 548 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 549 | |
| 550 | dma_fence_put(excl); |
| 551 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 552 | /* |
| 553 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 554 | * signaled and that the reservation object has not been changed (i.e. |
| 555 | * no new fences have been added). |
| 556 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 557 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 558 | if (reservation_object_trylock(resv)) { |
| 559 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 560 | reservation_object_add_excl_fence(resv, NULL); |
| 561 | reservation_object_unlock(resv); |
| 562 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 565 | return timeout; |
| 566 | } |
| 567 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 568 | static void __fence_set_priority(struct dma_fence *fence, |
| 569 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 570 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 571 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 572 | struct intel_engine_cs *engine; |
| 573 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 574 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 575 | return; |
| 576 | |
| 577 | rq = to_request(fence); |
| 578 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 579 | |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 580 | rcu_read_lock(); |
| 581 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 582 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 583 | rcu_read_unlock(); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 586 | static void fence_set_priority(struct dma_fence *fence, |
| 587 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 588 | { |
| 589 | /* Recurse once into a fence-array */ |
| 590 | if (dma_fence_is_array(fence)) { |
| 591 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 592 | int i; |
| 593 | |
| 594 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 595 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 596 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 597 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
| 601 | int |
| 602 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 603 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 604 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 605 | { |
| 606 | struct dma_fence *excl; |
| 607 | |
| 608 | if (flags & I915_WAIT_ALL) { |
| 609 | struct dma_fence **shared; |
| 610 | unsigned int count, i; |
| 611 | int ret; |
| 612 | |
| 613 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 614 | &excl, &count, &shared); |
| 615 | if (ret) |
| 616 | return ret; |
| 617 | |
| 618 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 619 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 620 | dma_fence_put(shared[i]); |
| 621 | } |
| 622 | |
| 623 | kfree(shared); |
| 624 | } else { |
| 625 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 626 | } |
| 627 | |
| 628 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 629 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 630 | dma_fence_put(excl); |
| 631 | } |
| 632 | return 0; |
| 633 | } |
| 634 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 635 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 636 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 637 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 638 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 639 | * @timeout: how long to wait |
Chris Wilson | a0a8b1c | 2017-11-09 14:06:44 +0000 | [diff] [blame] | 640 | * @rps_client: client (user process) to charge for any waitboosting |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 641 | */ |
| 642 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 643 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 644 | unsigned int flags, |
| 645 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 646 | struct intel_rps_client *rps_client) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 647 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 648 | might_sleep(); |
| 649 | #if IS_ENABLED(CONFIG_LOCKDEP) |
| 650 | GEM_BUG_ON(debug_locks && |
| 651 | !!lockdep_is_held(&obj->base.dev->struct_mutex) != |
| 652 | !!(flags & I915_WAIT_LOCKED)); |
| 653 | #endif |
| 654 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 655 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 656 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 657 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 658 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 659 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 663 | { |
| 664 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 665 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 666 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 667 | } |
| 668 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 669 | static int |
| 670 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 671 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 672 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 673 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 674 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 675 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 676 | |
| 677 | /* We manually control the domain here and pretend that it |
| 678 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 679 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 680 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 681 | if (copy_from_user(vaddr, user_data, args->size)) |
| 682 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 683 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 684 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 685 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 686 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 687 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 688 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 691 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 692 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 693 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 697 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 698 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 699 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 702 | static int |
| 703 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 704 | struct drm_i915_private *dev_priv, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 705 | uint64_t size, |
| 706 | uint32_t *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 707 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 708 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 709 | int ret; |
| 710 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 711 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 712 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 713 | if (size == 0) |
| 714 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 715 | |
| 716 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 717 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 718 | if (IS_ERR(obj)) |
| 719 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 720 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 721 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 722 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 723 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 724 | if (ret) |
| 725 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 726 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 727 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 728 | return 0; |
| 729 | } |
| 730 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 731 | int |
| 732 | i915_gem_dumb_create(struct drm_file *file, |
| 733 | struct drm_device *dev, |
| 734 | struct drm_mode_create_dumb *args) |
| 735 | { |
| 736 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 737 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 738 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 739 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 740 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 741 | } |
| 742 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 743 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 744 | { |
| 745 | return !(obj->cache_level == I915_CACHE_NONE || |
| 746 | obj->cache_level == I915_CACHE_WT); |
| 747 | } |
| 748 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 749 | /** |
| 750 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 751 | * @dev: drm device pointer |
| 752 | * @data: ioctl data blob |
| 753 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 754 | */ |
| 755 | int |
| 756 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 757 | struct drm_file *file) |
| 758 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 759 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 760 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 761 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 762 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 763 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 764 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 765 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 766 | } |
| 767 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 768 | static inline enum fb_op_origin |
| 769 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 770 | { |
| 771 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 772 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 773 | } |
| 774 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 775 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 776 | { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 777 | /* |
| 778 | * No actual flushing is required for the GTT write domain for reads |
| 779 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 780 | * as far as we know, so there's no chipset flush. It also doesn't |
| 781 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 782 | * |
| 783 | * However, we do have to enforce the order so that all writes through |
| 784 | * the GTT land before any writes to the device, such as updates to |
| 785 | * the GATT itself. |
| 786 | * |
| 787 | * We also have to wait a bit for the writes to land from the GTT. |
| 788 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 789 | * timing. This issue has only been observed when switching quickly |
| 790 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 791 | * 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] | 792 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 793 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 794 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 795 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 796 | wmb(); |
| 797 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 798 | intel_runtime_pm_get(dev_priv); |
| 799 | spin_lock_irq(&dev_priv->uncore.lock); |
| 800 | |
| 801 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
| 802 | |
| 803 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 804 | intel_runtime_pm_put(dev_priv); |
| 805 | } |
| 806 | |
| 807 | static void |
| 808 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 809 | { |
| 810 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 811 | struct i915_vma *vma; |
| 812 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 813 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 814 | return; |
| 815 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 816 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 817 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 818 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 819 | |
| 820 | intel_fb_obj_flush(obj, |
| 821 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 822 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 823 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 824 | if (vma->iomap) |
| 825 | continue; |
| 826 | |
| 827 | i915_vma_unset_ggtt_write(vma); |
| 828 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 829 | break; |
| 830 | |
| 831 | case I915_GEM_DOMAIN_CPU: |
| 832 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 833 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 834 | |
| 835 | case I915_GEM_DOMAIN_RENDER: |
| 836 | if (gpu_write_needs_clflush(obj)) |
| 837 | obj->cache_dirty = true; |
| 838 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 839 | } |
| 840 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 841 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 844 | static inline int |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 845 | __copy_to_user_swizzled(char __user *cpu_vaddr, |
| 846 | const char *gpu_vaddr, int gpu_offset, |
| 847 | int length) |
| 848 | { |
| 849 | int ret, cpu_offset = 0; |
| 850 | |
| 851 | while (length > 0) { |
| 852 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 853 | int this_length = min(cacheline_end - gpu_offset, length); |
| 854 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 855 | |
| 856 | ret = __copy_to_user(cpu_vaddr + cpu_offset, |
| 857 | gpu_vaddr + swizzled_gpu_offset, |
| 858 | this_length); |
| 859 | if (ret) |
| 860 | return ret + length; |
| 861 | |
| 862 | cpu_offset += this_length; |
| 863 | gpu_offset += this_length; |
| 864 | length -= this_length; |
| 865 | } |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | static inline int |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 871 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
| 872 | const char __user *cpu_vaddr, |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 873 | int length) |
| 874 | { |
| 875 | int ret, cpu_offset = 0; |
| 876 | |
| 877 | while (length > 0) { |
| 878 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 879 | int this_length = min(cacheline_end - gpu_offset, length); |
| 880 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 881 | |
| 882 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, |
| 883 | cpu_vaddr + cpu_offset, |
| 884 | this_length); |
| 885 | if (ret) |
| 886 | return ret + length; |
| 887 | |
| 888 | cpu_offset += this_length; |
| 889 | gpu_offset += this_length; |
| 890 | length -= this_length; |
| 891 | } |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 896 | /* |
| 897 | * Pins the specified object's pages and synchronizes the object with |
| 898 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 899 | * flush the object from the CPU cache. |
| 900 | */ |
| 901 | 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] | 902 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 903 | { |
| 904 | int ret; |
| 905 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 906 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 907 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 908 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 909 | if (!i915_gem_object_has_struct_page(obj)) |
| 910 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 911 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 912 | ret = i915_gem_object_wait(obj, |
| 913 | I915_WAIT_INTERRUPTIBLE | |
| 914 | I915_WAIT_LOCKED, |
| 915 | MAX_SCHEDULE_TIMEOUT, |
| 916 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 917 | if (ret) |
| 918 | return ret; |
| 919 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 920 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 921 | if (ret) |
| 922 | return ret; |
| 923 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 924 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 925 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 926 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 927 | if (ret) |
| 928 | goto err_unpin; |
| 929 | else |
| 930 | goto out; |
| 931 | } |
| 932 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 933 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 934 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 935 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 936 | * read domain and manually flush cachelines (if required). This |
| 937 | * optimizes for the case when the gpu will dirty the data |
| 938 | * anyway again before the next pread happens. |
| 939 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 940 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 941 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 942 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 943 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 944 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 945 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 946 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 947 | |
| 948 | err_unpin: |
| 949 | i915_gem_object_unpin_pages(obj); |
| 950 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 954 | unsigned int *needs_clflush) |
| 955 | { |
| 956 | int ret; |
| 957 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 958 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 959 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 960 | *needs_clflush = 0; |
| 961 | if (!i915_gem_object_has_struct_page(obj)) |
| 962 | return -ENODEV; |
| 963 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 964 | ret = i915_gem_object_wait(obj, |
| 965 | I915_WAIT_INTERRUPTIBLE | |
| 966 | I915_WAIT_LOCKED | |
| 967 | I915_WAIT_ALL, |
| 968 | MAX_SCHEDULE_TIMEOUT, |
| 969 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 970 | if (ret) |
| 971 | return ret; |
| 972 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 973 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 974 | if (ret) |
| 975 | return ret; |
| 976 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 977 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 978 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 979 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 980 | if (ret) |
| 981 | goto err_unpin; |
| 982 | else |
| 983 | goto out; |
| 984 | } |
| 985 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 986 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 987 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 988 | /* If we're not in the cpu write domain, set ourself into the |
| 989 | * gtt write domain and manually flush cachelines (as required). |
| 990 | * This optimizes for the case when the gpu will use the data |
| 991 | * right away and we therefore have to clflush anyway. |
| 992 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 993 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 994 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 995 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 996 | /* |
| 997 | * Same trick applies to invalidate partially written |
| 998 | * cachelines read before writing. |
| 999 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 1000 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 1001 | *needs_clflush |= CLFLUSH_BEFORE; |
| 1002 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1003 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 1004 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1005 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 1006 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 1007 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1008 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 1009 | |
| 1010 | err_unpin: |
| 1011 | i915_gem_object_unpin_pages(obj); |
| 1012 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1015 | static void |
| 1016 | shmem_clflush_swizzled_range(char *addr, unsigned long length, |
| 1017 | bool swizzled) |
| 1018 | { |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 1019 | if (unlikely(swizzled)) { |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1020 | unsigned long start = (unsigned long) addr; |
| 1021 | unsigned long end = (unsigned long) addr + length; |
| 1022 | |
| 1023 | /* For swizzling simply ensure that we always flush both |
| 1024 | * channels. Lame, but simple and it works. Swizzled |
| 1025 | * pwrite/pread is far from a hotpath - current userspace |
| 1026 | * doesn't use it at all. */ |
| 1027 | start = round_down(start, 128); |
| 1028 | end = round_up(end, 128); |
| 1029 | |
| 1030 | drm_clflush_virt_range((void *)start, end - start); |
| 1031 | } else { |
| 1032 | drm_clflush_virt_range(addr, length); |
| 1033 | } |
| 1034 | |
| 1035 | } |
| 1036 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1037 | /* Only difference to the fast-path function is that this can handle bit17 |
| 1038 | * and uses non-atomic copy and kmap functions. */ |
| 1039 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1040 | shmem_pread_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1041 | char __user *user_data, |
| 1042 | bool page_do_bit17_swizzling, bool needs_clflush) |
| 1043 | { |
| 1044 | char *vaddr; |
| 1045 | int ret; |
| 1046 | |
| 1047 | vaddr = kmap(page); |
| 1048 | if (needs_clflush) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1049 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1050 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1051 | |
| 1052 | if (page_do_bit17_swizzling) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1053 | ret = __copy_to_user_swizzled(user_data, vaddr, offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1054 | else |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1055 | ret = __copy_to_user(user_data, vaddr + offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1056 | kunmap(page); |
| 1057 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1058 | return ret ? - EFAULT : 0; |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1059 | } |
| 1060 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1061 | static int |
| 1062 | shmem_pread(struct page *page, int offset, int length, char __user *user_data, |
| 1063 | bool page_do_bit17_swizzling, bool needs_clflush) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1064 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1065 | int ret; |
| 1066 | |
| 1067 | ret = -ENODEV; |
| 1068 | if (!page_do_bit17_swizzling) { |
| 1069 | char *vaddr = kmap_atomic(page); |
| 1070 | |
| 1071 | if (needs_clflush) |
| 1072 | drm_clflush_virt_range(vaddr + offset, length); |
| 1073 | ret = __copy_to_user_inatomic(user_data, vaddr + offset, length); |
| 1074 | kunmap_atomic(vaddr); |
| 1075 | } |
| 1076 | if (ret == 0) |
| 1077 | return 0; |
| 1078 | |
| 1079 | return shmem_pread_slow(page, offset, length, user_data, |
| 1080 | page_do_bit17_swizzling, needs_clflush); |
| 1081 | } |
| 1082 | |
| 1083 | static int |
| 1084 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 1085 | struct drm_i915_gem_pread *args) |
| 1086 | { |
| 1087 | char __user *user_data; |
| 1088 | u64 remain; |
| 1089 | unsigned int obj_do_bit17_swizzling; |
| 1090 | unsigned int needs_clflush; |
| 1091 | unsigned int idx, offset; |
| 1092 | int ret; |
| 1093 | |
| 1094 | obj_do_bit17_swizzling = 0; |
| 1095 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 1096 | obj_do_bit17_swizzling = BIT(17); |
| 1097 | |
| 1098 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 1099 | if (ret) |
| 1100 | return ret; |
| 1101 | |
| 1102 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 1103 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 1104 | if (ret) |
| 1105 | return ret; |
| 1106 | |
| 1107 | remain = args->size; |
| 1108 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1109 | offset = offset_in_page(args->offset); |
| 1110 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1111 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1112 | int length; |
| 1113 | |
| 1114 | length = remain; |
| 1115 | if (offset + length > PAGE_SIZE) |
| 1116 | length = PAGE_SIZE - offset; |
| 1117 | |
| 1118 | ret = shmem_pread(page, offset, length, user_data, |
| 1119 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1120 | needs_clflush); |
| 1121 | if (ret) |
| 1122 | break; |
| 1123 | |
| 1124 | remain -= length; |
| 1125 | user_data += length; |
| 1126 | offset = 0; |
| 1127 | } |
| 1128 | |
| 1129 | i915_gem_obj_finish_shmem_access(obj); |
| 1130 | return ret; |
| 1131 | } |
| 1132 | |
| 1133 | static inline bool |
| 1134 | gtt_user_read(struct io_mapping *mapping, |
| 1135 | loff_t base, int offset, |
| 1136 | char __user *user_data, int length) |
| 1137 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1138 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1139 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1140 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1141 | /* 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] | 1142 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1143 | unwritten = __copy_to_user_inatomic(user_data, |
| 1144 | (void __force *)vaddr + offset, |
| 1145 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1146 | io_mapping_unmap_atomic(vaddr); |
| 1147 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1148 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1149 | unwritten = copy_to_user(user_data, |
| 1150 | (void __force *)vaddr + offset, |
| 1151 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1152 | io_mapping_unmap(vaddr); |
| 1153 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1154 | return unwritten; |
| 1155 | } |
| 1156 | |
| 1157 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1158 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1159 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1160 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1161 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1162 | struct i915_ggtt *ggtt = &i915->ggtt; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1163 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1164 | struct i915_vma *vma; |
| 1165 | void __user *user_data; |
| 1166 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1167 | int ret; |
| 1168 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1169 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1170 | if (ret) |
| 1171 | return ret; |
| 1172 | |
| 1173 | intel_runtime_pm_get(i915); |
| 1174 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1175 | PIN_MAPPABLE | |
| 1176 | PIN_NONFAULT | |
| 1177 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1178 | if (!IS_ERR(vma)) { |
| 1179 | node.start = i915_ggtt_offset(vma); |
| 1180 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1181 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1182 | if (ret) { |
| 1183 | i915_vma_unpin(vma); |
| 1184 | vma = ERR_PTR(ret); |
| 1185 | } |
| 1186 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1187 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1188 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1189 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1190 | goto out_unlock; |
| 1191 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1195 | if (ret) |
| 1196 | goto out_unpin; |
| 1197 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1198 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1199 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1200 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1201 | remain = args->size; |
| 1202 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1203 | |
| 1204 | while (remain > 0) { |
| 1205 | /* Operation in this page |
| 1206 | * |
| 1207 | * page_base = page offset within aperture |
| 1208 | * page_offset = offset within page |
| 1209 | * page_length = bytes to copy for this page |
| 1210 | */ |
| 1211 | u32 page_base = node.start; |
| 1212 | unsigned page_offset = offset_in_page(offset); |
| 1213 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1214 | page_length = remain < page_length ? remain : page_length; |
| 1215 | if (node.allocated) { |
| 1216 | wmb(); |
| 1217 | ggtt->base.insert_page(&ggtt->base, |
| 1218 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1219 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1220 | wmb(); |
| 1221 | } else { |
| 1222 | page_base += offset & PAGE_MASK; |
| 1223 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1224 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1225 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1226 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1227 | ret = -EFAULT; |
| 1228 | break; |
| 1229 | } |
| 1230 | |
| 1231 | remain -= page_length; |
| 1232 | user_data += page_length; |
| 1233 | offset += page_length; |
| 1234 | } |
| 1235 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1236 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1237 | out_unpin: |
| 1238 | if (node.allocated) { |
| 1239 | wmb(); |
| 1240 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1241 | node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1242 | remove_mappable_node(&node); |
| 1243 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1244 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1245 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1246 | out_unlock: |
| 1247 | intel_runtime_pm_put(i915); |
| 1248 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1249 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1250 | return ret; |
| 1251 | } |
| 1252 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1253 | /** |
| 1254 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1255 | * @dev: drm device pointer |
| 1256 | * @data: ioctl data blob |
| 1257 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1258 | * |
| 1259 | * On error, the contents of *data are undefined. |
| 1260 | */ |
| 1261 | int |
| 1262 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1263 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1264 | { |
| 1265 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1266 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1267 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1268 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1269 | if (args->size == 0) |
| 1270 | return 0; |
| 1271 | |
| 1272 | if (!access_ok(VERIFY_WRITE, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1273 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1274 | args->size)) |
| 1275 | return -EFAULT; |
| 1276 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1277 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1278 | if (!obj) |
| 1279 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1280 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1281 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1282 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1283 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1284 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1287 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1288 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1289 | ret = i915_gem_object_wait(obj, |
| 1290 | I915_WAIT_INTERRUPTIBLE, |
| 1291 | MAX_SCHEDULE_TIMEOUT, |
| 1292 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1293 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1294 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1295 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1296 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1297 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1298 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1299 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1300 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1301 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1302 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1303 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1304 | i915_gem_object_unpin_pages(obj); |
| 1305 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1306 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1307 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1310 | /* This is the fast write path which cannot handle |
| 1311 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1312 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1313 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1314 | static inline bool |
| 1315 | ggtt_write(struct io_mapping *mapping, |
| 1316 | loff_t base, int offset, |
| 1317 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1318 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1319 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1320 | unsigned long unwritten; |
| 1321 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1322 | /* 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] | 1323 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1324 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1325 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1326 | io_mapping_unmap_atomic(vaddr); |
| 1327 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1328 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1329 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1330 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1331 | io_mapping_unmap(vaddr); |
| 1332 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1333 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1334 | return unwritten; |
| 1335 | } |
| 1336 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1337 | /** |
| 1338 | * This is the fast pwrite path, where we copy the data directly from the |
| 1339 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1340 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1341 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1342 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1343 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1344 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1345 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1346 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1347 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1348 | struct i915_ggtt *ggtt = &i915->ggtt; |
| 1349 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1350 | struct i915_vma *vma; |
| 1351 | u64 remain, offset; |
| 1352 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1353 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1354 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1355 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1356 | if (ret) |
| 1357 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1358 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1359 | if (i915_gem_object_has_struct_page(obj)) { |
| 1360 | /* |
| 1361 | * Avoid waking the device up if we can fallback, as |
| 1362 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1363 | * depending on PCI sleeps and our own resume time). |
| 1364 | * This easily dwarfs any performance advantage from |
| 1365 | * using the cache bypass of indirect GGTT access. |
| 1366 | */ |
| 1367 | if (!intel_runtime_pm_get_if_in_use(i915)) { |
| 1368 | ret = -EFAULT; |
| 1369 | goto out_unlock; |
| 1370 | } |
| 1371 | } else { |
| 1372 | /* No backing pages, no fallback, we must force GGTT access */ |
| 1373 | intel_runtime_pm_get(i915); |
| 1374 | } |
| 1375 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1376 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1377 | PIN_MAPPABLE | |
| 1378 | PIN_NONFAULT | |
| 1379 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1380 | if (!IS_ERR(vma)) { |
| 1381 | node.start = i915_ggtt_offset(vma); |
| 1382 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1383 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1384 | if (ret) { |
| 1385 | i915_vma_unpin(vma); |
| 1386 | vma = ERR_PTR(ret); |
| 1387 | } |
| 1388 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1389 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1390 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1391 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1392 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1393 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1394 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1395 | |
| 1396 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1397 | if (ret) |
| 1398 | goto out_unpin; |
| 1399 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1400 | mutex_unlock(&i915->drm.struct_mutex); |
| 1401 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1402 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1403 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1404 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1405 | offset = args->offset; |
| 1406 | remain = args->size; |
| 1407 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1408 | /* Operation in this page |
| 1409 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1410 | * page_base = page offset within aperture |
| 1411 | * page_offset = offset within page |
| 1412 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1413 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1414 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1415 | unsigned int page_offset = offset_in_page(offset); |
| 1416 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1417 | page_length = remain < page_length ? remain : page_length; |
| 1418 | if (node.allocated) { |
| 1419 | wmb(); /* flush the write before we modify the GGTT */ |
| 1420 | ggtt->base.insert_page(&ggtt->base, |
| 1421 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1422 | node.start, I915_CACHE_NONE, 0); |
| 1423 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1424 | } else { |
| 1425 | page_base += offset & PAGE_MASK; |
| 1426 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1427 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1428 | * source page isn't available. Return the error and we'll |
| 1429 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1430 | * If the object is non-shmem backed, we retry again with the |
| 1431 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1432 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1433 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1434 | user_data, page_length)) { |
| 1435 | ret = -EFAULT; |
| 1436 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1437 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1438 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1439 | remain -= page_length; |
| 1440 | user_data += page_length; |
| 1441 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1442 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1443 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1444 | |
| 1445 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1446 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1447 | if (node.allocated) { |
| 1448 | wmb(); |
| 1449 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1450 | node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1451 | remove_mappable_node(&node); |
| 1452 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1453 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1454 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1455 | out_rpm: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1456 | intel_runtime_pm_put(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1457 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1458 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1459 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1462 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1463 | shmem_pwrite_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1464 | char __user *user_data, |
| 1465 | bool page_do_bit17_swizzling, |
| 1466 | bool needs_clflush_before, |
| 1467 | bool needs_clflush_after) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1468 | { |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1469 | char *vaddr; |
| 1470 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1471 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1472 | vaddr = kmap(page); |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 1473 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1474 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1475 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1476 | if (page_do_bit17_swizzling) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1477 | ret = __copy_from_user_swizzled(vaddr, offset, user_data, |
| 1478 | length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1479 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1480 | ret = __copy_from_user(vaddr + offset, user_data, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1481 | if (needs_clflush_after) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1482 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1483 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1484 | kunmap(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1485 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 1486 | return ret ? -EFAULT : 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1489 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1490 | * Flushes invalid cachelines before writing to the target if |
| 1491 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1492 | * writing if needs_clflush is set. |
| 1493 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1494 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1495 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
| 1496 | bool page_do_bit17_swizzling, |
| 1497 | bool needs_clflush_before, |
| 1498 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1499 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1500 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1501 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1502 | ret = -ENODEV; |
| 1503 | if (!page_do_bit17_swizzling) { |
| 1504 | char *vaddr = kmap_atomic(page); |
| 1505 | |
| 1506 | if (needs_clflush_before) |
| 1507 | drm_clflush_virt_range(vaddr + offset, len); |
| 1508 | ret = __copy_from_user_inatomic(vaddr + offset, user_data, len); |
| 1509 | if (needs_clflush_after) |
| 1510 | drm_clflush_virt_range(vaddr + offset, len); |
| 1511 | |
| 1512 | kunmap_atomic(vaddr); |
| 1513 | } |
| 1514 | if (ret == 0) |
| 1515 | return ret; |
| 1516 | |
| 1517 | return shmem_pwrite_slow(page, offset, len, user_data, |
| 1518 | page_do_bit17_swizzling, |
| 1519 | needs_clflush_before, |
| 1520 | needs_clflush_after); |
| 1521 | } |
| 1522 | |
| 1523 | static int |
| 1524 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1525 | const struct drm_i915_gem_pwrite *args) |
| 1526 | { |
| 1527 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1528 | void __user *user_data; |
| 1529 | u64 remain; |
| 1530 | unsigned int obj_do_bit17_swizzling; |
| 1531 | unsigned int partial_cacheline_write; |
| 1532 | unsigned int needs_clflush; |
| 1533 | unsigned int offset, idx; |
| 1534 | int ret; |
| 1535 | |
| 1536 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1537 | if (ret) |
| 1538 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1539 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1540 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1541 | mutex_unlock(&i915->drm.struct_mutex); |
| 1542 | if (ret) |
| 1543 | return ret; |
| 1544 | |
| 1545 | obj_do_bit17_swizzling = 0; |
| 1546 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 1547 | obj_do_bit17_swizzling = BIT(17); |
| 1548 | |
| 1549 | /* If we don't overwrite a cacheline completely we need to be |
| 1550 | * careful to have up-to-date data by first clflushing. Don't |
| 1551 | * overcomplicate things and flush the entire patch. |
| 1552 | */ |
| 1553 | partial_cacheline_write = 0; |
| 1554 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1555 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1556 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1557 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1558 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1559 | offset = offset_in_page(args->offset); |
| 1560 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1561 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1562 | int length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1563 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1564 | length = remain; |
| 1565 | if (offset + length > PAGE_SIZE) |
| 1566 | length = PAGE_SIZE - offset; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1567 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1568 | ret = shmem_pwrite(page, offset, length, user_data, |
| 1569 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1570 | (offset | length) & partial_cacheline_write, |
| 1571 | needs_clflush & CLFLUSH_AFTER); |
| 1572 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1573 | break; |
| 1574 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1575 | remain -= length; |
| 1576 | user_data += length; |
| 1577 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1580 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1581 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1582 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | /** |
| 1586 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1587 | * @dev: drm device |
| 1588 | * @data: ioctl data blob |
| 1589 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1590 | * |
| 1591 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1592 | */ |
| 1593 | int |
| 1594 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1595 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1596 | { |
| 1597 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1598 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1599 | int ret; |
| 1600 | |
| 1601 | if (args->size == 0) |
| 1602 | return 0; |
| 1603 | |
| 1604 | if (!access_ok(VERIFY_READ, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1605 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1606 | args->size)) |
| 1607 | return -EFAULT; |
| 1608 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1609 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1610 | if (!obj) |
| 1611 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1612 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1613 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1614 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1615 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1616 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1617 | } |
| 1618 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1619 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1620 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1621 | ret = -ENODEV; |
| 1622 | if (obj->ops->pwrite) |
| 1623 | ret = obj->ops->pwrite(obj, args); |
| 1624 | if (ret != -ENODEV) |
| 1625 | goto err; |
| 1626 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1627 | ret = i915_gem_object_wait(obj, |
| 1628 | I915_WAIT_INTERRUPTIBLE | |
| 1629 | I915_WAIT_ALL, |
| 1630 | MAX_SCHEDULE_TIMEOUT, |
| 1631 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1632 | if (ret) |
| 1633 | goto err; |
| 1634 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1635 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1636 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1637 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1638 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1639 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1640 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1641 | * it would end up going through the fenced access, and we'll get |
| 1642 | * different detiling behavior between reading and writing. |
| 1643 | * pread/pwrite currently are reading and writing from the CPU |
| 1644 | * perspective, requiring manual detiling by the client. |
| 1645 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1646 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1647 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1648 | /* Note that the gtt paths might fail with non-page-backed user |
| 1649 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1650 | * textures). Fallback to the shmem path in that case. |
| 1651 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1652 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1653 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1654 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1655 | if (obj->phys_handle) |
| 1656 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1657 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1658 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1659 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1660 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1661 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1662 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1663 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1664 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1665 | } |
| 1666 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1667 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1668 | { |
| 1669 | struct drm_i915_private *i915; |
| 1670 | struct list_head *list; |
| 1671 | struct i915_vma *vma; |
| 1672 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1673 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1674 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1675 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1676 | if (i915_vma_is_active(vma)) |
| 1677 | continue; |
| 1678 | |
| 1679 | if (!drm_mm_node_allocated(&vma->node)) |
| 1680 | continue; |
| 1681 | |
| 1682 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 1683 | } |
| 1684 | |
| 1685 | i915 = to_i915(obj->base.dev); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1686 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1687 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1688 | list_move_tail(&obj->mm.link, list); |
| 1689 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1690 | } |
| 1691 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1692 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1693 | * Called when user space prepares to use an object with the CPU, either |
| 1694 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1695 | * @dev: drm device |
| 1696 | * @data: ioctl data blob |
| 1697 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1698 | */ |
| 1699 | int |
| 1700 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1701 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1702 | { |
| 1703 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1704 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1705 | uint32_t read_domains = args->read_domains; |
| 1706 | uint32_t write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1707 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1708 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1709 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1710 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1711 | return -EINVAL; |
| 1712 | |
| 1713 | /* Having something in the write domain implies it's in the read |
| 1714 | * domain, and only that read domain. Enforce that in the request. |
| 1715 | */ |
| 1716 | if (write_domain != 0 && read_domains != write_domain) |
| 1717 | return -EINVAL; |
| 1718 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1719 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1720 | if (!obj) |
| 1721 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1722 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1723 | /* Try to flush the object off the GPU without holding the lock. |
| 1724 | * We will repeat the flush holding the lock in the normal manner |
| 1725 | * to catch cases where we are gazumped. |
| 1726 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1727 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1728 | I915_WAIT_INTERRUPTIBLE | |
| 1729 | (write_domain ? I915_WAIT_ALL : 0), |
| 1730 | MAX_SCHEDULE_TIMEOUT, |
| 1731 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1732 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1733 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1734 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1735 | /* |
| 1736 | * Proxy objects do not control access to the backing storage, ergo |
| 1737 | * they cannot be used as a means to manipulate the cache domain |
| 1738 | * tracking for that backing storage. The proxy object is always |
| 1739 | * considered to be outside of any cache domain. |
| 1740 | */ |
| 1741 | if (i915_gem_object_is_proxy(obj)) { |
| 1742 | err = -ENXIO; |
| 1743 | goto out; |
| 1744 | } |
| 1745 | |
| 1746 | /* |
| 1747 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1748 | * direct access in memory with previous cached writes through |
| 1749 | * shmemfs and that our cache domain tracking remains valid. |
| 1750 | * For example, if the obj->filp was moved to swap without us |
| 1751 | * being notified and releasing the pages, we would mistakenly |
| 1752 | * continue to assume that the obj remained out of the CPU cached |
| 1753 | * domain. |
| 1754 | */ |
| 1755 | err = i915_gem_object_pin_pages(obj); |
| 1756 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1757 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1758 | |
| 1759 | err = i915_mutex_lock_interruptible(dev); |
| 1760 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1761 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1762 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1763 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1764 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1765 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1766 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1767 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1768 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1769 | |
| 1770 | /* And bump the LRU for this access */ |
| 1771 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1772 | |
| 1773 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1774 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1775 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1776 | intel_fb_obj_invalidate(obj, |
| 1777 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1778 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1779 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1780 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1781 | out: |
| 1782 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1783 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | /** |
| 1787 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1788 | * @dev: drm device |
| 1789 | * @data: ioctl data blob |
| 1790 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1791 | */ |
| 1792 | int |
| 1793 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1794 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1795 | { |
| 1796 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1797 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1798 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1799 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1800 | if (!obj) |
| 1801 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1802 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1803 | /* |
| 1804 | * Proxy objects are barred from CPU access, so there is no |
| 1805 | * need to ban sw_finish as it is a nop. |
| 1806 | */ |
| 1807 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1808 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1809 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1810 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1811 | |
| 1812 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1816 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1817 | * it is mapped to. |
| 1818 | * @dev: drm device |
| 1819 | * @data: ioctl data blob |
| 1820 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1821 | * |
| 1822 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1823 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1824 | * |
| 1825 | * IMPORTANT: |
| 1826 | * |
| 1827 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1828 | * mmap support, please don't implement mmap support like here. The modern way |
| 1829 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1830 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1831 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1832 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1833 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1834 | */ |
| 1835 | int |
| 1836 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1837 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1838 | { |
| 1839 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1840 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1841 | unsigned long addr; |
| 1842 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1843 | if (args->flags & ~(I915_MMAP_WC)) |
| 1844 | return -EINVAL; |
| 1845 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1846 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1847 | return -ENODEV; |
| 1848 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1849 | obj = i915_gem_object_lookup(file, args->handle); |
| 1850 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1851 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1852 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1853 | /* prime objects have no backing filp to GEM mmap |
| 1854 | * pages from. |
| 1855 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1856 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1857 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1858 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1859 | } |
| 1860 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1861 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1862 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1863 | args->offset); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1864 | if (args->flags & I915_MMAP_WC) { |
| 1865 | struct mm_struct *mm = current->mm; |
| 1866 | struct vm_area_struct *vma; |
| 1867 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1868 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1869 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1870 | return -EINTR; |
| 1871 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1872 | vma = find_vma(mm, addr); |
| 1873 | if (vma) |
| 1874 | vma->vm_page_prot = |
| 1875 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1876 | else |
| 1877 | addr = -ENOMEM; |
| 1878 | up_write(&mm->mmap_sem); |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1879 | |
| 1880 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1881 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1882 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1883 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1884 | if (IS_ERR((void *)addr)) |
| 1885 | return addr; |
| 1886 | |
| 1887 | args->addr_ptr = (uint64_t) addr; |
| 1888 | |
| 1889 | return 0; |
| 1890 | } |
| 1891 | |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1892 | static unsigned int tile_row_pages(struct drm_i915_gem_object *obj) |
| 1893 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1894 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1897 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1898 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1899 | * |
| 1900 | * A history of the GTT mmap interface: |
| 1901 | * |
| 1902 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1903 | * aligned and suitable for fencing, and still fit into the available |
| 1904 | * mappable space left by the pinned display objects. A classic problem |
| 1905 | * we called the page-fault-of-doom where we would ping-pong between |
| 1906 | * two objects that could not fit inside the GTT and so the memcpy |
| 1907 | * would page one object in at the expense of the other between every |
| 1908 | * single byte. |
| 1909 | * |
| 1910 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1911 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1912 | * object is too large for the available space (or simply too large |
| 1913 | * for the mappable aperture!), a view is created instead and faulted |
| 1914 | * into userspace. (This view is aligned and sized appropriately for |
| 1915 | * fenced access.) |
| 1916 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1917 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1918 | * delayed writes via GTT before performing direct access via WC. |
| 1919 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1920 | * Restrictions: |
| 1921 | * |
| 1922 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1923 | * hangs on some architectures, corruption on others. An attempt to service |
| 1924 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1925 | * |
| 1926 | * * the object must be able to fit into RAM (physical memory, though no |
| 1927 | * limited to the mappable aperture). |
| 1928 | * |
| 1929 | * |
| 1930 | * Caveats: |
| 1931 | * |
| 1932 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1933 | * all data to system memory. Subsequent access will not be synchronized. |
| 1934 | * |
| 1935 | * * all mappings are revoked on runtime device suspend. |
| 1936 | * |
| 1937 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1938 | * (older machines require fence register for display and blitter access |
| 1939 | * as well). Contention of the fence registers will cause the previous users |
| 1940 | * to be unmapped and any new access will generate new page faults. |
| 1941 | * |
| 1942 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1943 | * rather than the expected SIGSEGV. |
| 1944 | */ |
| 1945 | int i915_gem_mmap_gtt_version(void) |
| 1946 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1947 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1950 | static inline struct i915_ggtt_view |
| 1951 | compute_partial_view(struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1952 | pgoff_t page_offset, |
| 1953 | unsigned int chunk) |
| 1954 | { |
| 1955 | struct i915_ggtt_view view; |
| 1956 | |
| 1957 | if (i915_gem_object_is_tiled(obj)) |
| 1958 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1959 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1960 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1961 | view.partial.offset = rounddown(page_offset, chunk); |
| 1962 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1963 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1964 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1965 | |
| 1966 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1967 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1968 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1969 | |
| 1970 | return view; |
| 1971 | } |
| 1972 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1973 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1974 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1975 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1976 | * |
| 1977 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1978 | * from userspace. The fault handler takes care of binding the object to |
| 1979 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1980 | * only if needed based on whether the old reg is still valid or the object |
| 1981 | * is tiled) and inserting a new PTE into the faulting process. |
| 1982 | * |
| 1983 | * Note that the faulting process may involve evicting existing objects |
| 1984 | * from the GTT and/or fence registers to make room. So performance may |
| 1985 | * suffer if the GTT working set is large or there are few fence registers |
| 1986 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1987 | * |
| 1988 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1989 | * 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] | 1990 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1991 | int i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1992 | { |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1993 | #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1994 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1995 | 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] | 1996 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1997 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1998 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1999 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2000 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2001 | pgoff_t page_offset; |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 2002 | unsigned int flags; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2003 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2004 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2005 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 2006 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2007 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2008 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 2009 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 2010 | /* 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] | 2011 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 2012 | * repeat the flush holding the lock in the normal manner to catch cases |
| 2013 | * where we are gazumped. |
| 2014 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2015 | ret = i915_gem_object_wait(obj, |
| 2016 | I915_WAIT_INTERRUPTIBLE, |
| 2017 | MAX_SCHEDULE_TIMEOUT, |
| 2018 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 2019 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2020 | goto err; |
| 2021 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 2022 | ret = i915_gem_object_pin_pages(obj); |
| 2023 | if (ret) |
| 2024 | goto err; |
| 2025 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2026 | intel_runtime_pm_get(dev_priv); |
| 2027 | |
| 2028 | ret = i915_mutex_lock_interruptible(dev); |
| 2029 | if (ret) |
| 2030 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 2031 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 2032 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 2033 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 2034 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2035 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 2038 | /* If the object is smaller than a couple of partial vma, it is |
| 2039 | * not worth only creating a single partial vma - we may as well |
| 2040 | * clear enough space for the full object. |
| 2041 | */ |
| 2042 | flags = PIN_MAPPABLE; |
| 2043 | if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT) |
| 2044 | flags |= PIN_NONBLOCK | PIN_NONFAULT; |
| 2045 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2046 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 2047 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2048 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2049 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 2050 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 2051 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 2052 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 2053 | /* Userspace is now writing through an untracked VMA, abandon |
| 2054 | * all hope that the hardware is able to track future writes. |
| 2055 | */ |
| 2056 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 2057 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2058 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); |
| 2059 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2060 | if (IS_ERR(vma)) { |
| 2061 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2062 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2063 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2064 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 2065 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 2066 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2067 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 2068 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 2069 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 2070 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2071 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 2072 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 2073 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 2074 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 2075 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 2076 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 2077 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 2078 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2079 | if (ret) |
| 2080 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2081 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2082 | /* Mark as being mmapped into userspace for later revocation */ |
| 2083 | assert_rpm_wakelock_held(dev_priv); |
| 2084 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 2085 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 2086 | GEM_BUG_ON(!obj->userfault_count); |
| 2087 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 2088 | i915_vma_set_ggtt_write(vma); |
| 2089 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2090 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 2091 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2092 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2093 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2094 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2095 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2096 | err_rpm: |
| 2097 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 2098 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2099 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2100 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2101 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 2102 | /* |
| 2103 | * We eat errors when the gpu is terminally wedged to avoid |
| 2104 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 2105 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 2106 | * and so needs to be reported. |
| 2107 | */ |
| 2108 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2109 | ret = VM_FAULT_SIGBUS; |
| 2110 | break; |
| 2111 | } |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 2112 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 2113 | /* |
| 2114 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 2115 | * handler to reset everything when re-faulting in |
| 2116 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2117 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 2118 | case 0: |
| 2119 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 2120 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 2121 | case -EBUSY: |
| 2122 | /* |
| 2123 | * EBUSY is ok: this just means that another thread |
| 2124 | * already did the job. |
| 2125 | */ |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2126 | ret = VM_FAULT_NOPAGE; |
| 2127 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2128 | case -ENOMEM: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2129 | ret = VM_FAULT_OOM; |
| 2130 | break; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2131 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2132 | case -EFAULT: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2133 | ret = VM_FAULT_SIGBUS; |
| 2134 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2135 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2136 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2137 | ret = VM_FAULT_SIGBUS; |
| 2138 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2139 | } |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2140 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2141 | } |
| 2142 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2143 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2144 | { |
| 2145 | struct i915_vma *vma; |
| 2146 | |
| 2147 | GEM_BUG_ON(!obj->userfault_count); |
| 2148 | |
| 2149 | obj->userfault_count = 0; |
| 2150 | list_del(&obj->userfault_link); |
| 2151 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2152 | obj->base.dev->anon_inode->i_mapping); |
| 2153 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 2154 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2155 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2156 | } |
| 2157 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2158 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2159 | * i915_gem_release_mmap - remove physical page mappings |
| 2160 | * @obj: obj in question |
| 2161 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2162 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2163 | * relinquish ownership of the pages back to the system. |
| 2164 | * |
| 2165 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2166 | * object through the GTT and then lose the fence register due to |
| 2167 | * resource pressure. Similarly if the object has been moved out of the |
| 2168 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2169 | * mapping will then trigger a page fault on the next user access, allowing |
| 2170 | * fixup by i915_gem_fault(). |
| 2171 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2172 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2173 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2174 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2175 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2176 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2177 | /* Serialisation between user GTT access and our code depends upon |
| 2178 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2179 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2180 | * |
| 2181 | * Note that RPM complicates somewhat by adding an additional |
| 2182 | * requirement that operations to the GGTT be made holding the RPM |
| 2183 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2184 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2185 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2186 | intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2187 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2188 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2189 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2190 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2191 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2192 | |
| 2193 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2194 | * memory transactions from userspace before we return. The TLB |
| 2195 | * flushing implied above by changing the PTE above *should* be |
| 2196 | * sufficient, an extra barrier here just provides us with a bit |
| 2197 | * of paranoid documentation about our requirement to serialise |
| 2198 | * memory writes before touching registers / GSM. |
| 2199 | */ |
| 2200 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2201 | |
| 2202 | out: |
| 2203 | intel_runtime_pm_put(i915); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2204 | } |
| 2205 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2206 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2207 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2208 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2209 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2210 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2211 | /* |
| 2212 | * Only called during RPM suspend. All users of the userfault_list |
| 2213 | * must be holding an RPM wakeref to ensure that this can not |
| 2214 | * run concurrently with themselves (and use the struct_mutex for |
| 2215 | * protection between themselves). |
| 2216 | */ |
| 2217 | |
| 2218 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2219 | &dev_priv->mm.userfault_list, userfault_link) |
| 2220 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2221 | |
| 2222 | /* The fence will be lost when the device powers down. If any were |
| 2223 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2224 | * down! All other fences will be reacquired by the user upon waking. |
| 2225 | */ |
| 2226 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2227 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2228 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2229 | /* Ideally we want to assert that the fence register is not |
| 2230 | * live at this point (i.e. that no piece of code will be |
| 2231 | * trying to write through fence + GTT, as that both violates |
| 2232 | * our tracking of activity and associated locking/barriers, |
| 2233 | * but also is illegal given that the hw is powered down). |
| 2234 | * |
| 2235 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2236 | * That is not sufficient, and we need a more fine-grained |
| 2237 | * tool if we want to have a sanity check here. |
| 2238 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2239 | |
| 2240 | if (!reg->vma) |
| 2241 | continue; |
| 2242 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2243 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2244 | reg->dirty = true; |
| 2245 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2248 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2249 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2250 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2251 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2252 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2253 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2254 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2255 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2256 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2257 | /* Attempt to reap some mmap space from dead objects */ |
| 2258 | do { |
| 2259 | err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE); |
| 2260 | if (err) |
| 2261 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2262 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2263 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2264 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2265 | if (!err) |
| 2266 | break; |
| 2267 | |
| 2268 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2269 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2270 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2274 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2275 | drm_gem_free_mmap_offset(&obj->base); |
| 2276 | } |
| 2277 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2278 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2279 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2280 | struct drm_device *dev, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2281 | uint32_t handle, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2282 | uint64_t *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2283 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2284 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2285 | int ret; |
| 2286 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2287 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2288 | if (!obj) |
| 2289 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2290 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2291 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2292 | if (ret == 0) |
| 2293 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2294 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2295 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2296 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2297 | } |
| 2298 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2299 | /** |
| 2300 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2301 | * @dev: DRM device |
| 2302 | * @data: GTT mapping ioctl data |
| 2303 | * @file: GEM object info |
| 2304 | * |
| 2305 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2306 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2307 | * up so we can get faults in the handler above. |
| 2308 | * |
| 2309 | * The fault handler will take care of binding the object into the GTT |
| 2310 | * (since it may have been evicted to make room for something), allocating |
| 2311 | * a fence register, and mapping the appropriate aperture address into |
| 2312 | * userspace. |
| 2313 | */ |
| 2314 | int |
| 2315 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2316 | struct drm_file *file) |
| 2317 | { |
| 2318 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2319 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2320 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2321 | } |
| 2322 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2323 | /* Immediately discard the backing storage */ |
| 2324 | static void |
| 2325 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2326 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2327 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2328 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2329 | if (obj->base.filp == NULL) |
| 2330 | return; |
| 2331 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2332 | /* Our goal here is to return as much of the memory as |
| 2333 | * is possible back to the system as we are called from OOM. |
| 2334 | * To do this we must instruct the shmfs to drop all of its |
| 2335 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2336 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2337 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2338 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2339 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2340 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2341 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2342 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2343 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2344 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2345 | struct address_space *mapping; |
| 2346 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2347 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2348 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2349 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2350 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2351 | case I915_MADV_DONTNEED: |
| 2352 | i915_gem_object_truncate(obj); |
| 2353 | case __I915_MADV_PURGED: |
| 2354 | return; |
| 2355 | } |
| 2356 | |
| 2357 | if (obj->base.filp == NULL) |
| 2358 | return; |
| 2359 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2360 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2361 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2362 | } |
| 2363 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2364 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2365 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2366 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2367 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2368 | struct sgt_iter sgt_iter; |
| 2369 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2370 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2371 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2372 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2373 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2374 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2375 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2376 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2377 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2378 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2379 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2380 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2381 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2382 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2383 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2384 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2385 | put_page(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2386 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2387 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2388 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2389 | sg_free_table(pages); |
| 2390 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2391 | } |
| 2392 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2393 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2394 | { |
| 2395 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2396 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2397 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2398 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2399 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2400 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2401 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2402 | } |
| 2403 | |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2404 | void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2405 | enum i915_mm_subclass subclass) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2406 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2407 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2408 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2409 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2410 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2411 | return; |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 2412 | |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 2413 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2414 | if (!i915_gem_object_has_pages(obj)) |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2415 | return; |
| 2416 | |
| 2417 | /* May be called by shrinker from within get_pages() (on another bo) */ |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2418 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2419 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) |
| 2420 | goto unlock; |
Ben Widawsky | 3e12302 | 2013-07-31 17:00:04 -0700 | [diff] [blame] | 2421 | |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2422 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
| 2423 | * array, hence protect them from being reaped by removing them from gtt |
| 2424 | * lists early. */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2425 | pages = fetch_and_zero(&obj->mm.pages); |
| 2426 | GEM_BUG_ON(!pages); |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2427 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2428 | spin_lock(&i915->mm.obj_lock); |
| 2429 | list_del(&obj->mm.link); |
| 2430 | spin_unlock(&i915->mm.obj_lock); |
| 2431 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2432 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2433 | void *ptr; |
| 2434 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2435 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2436 | if (is_vmalloc_addr(ptr)) |
| 2437 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2438 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2439 | kunmap(kmap_to_page(ptr)); |
| 2440 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2441 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2442 | } |
| 2443 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2444 | __i915_gem_object_reset_page_iter(obj); |
| 2445 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2446 | if (!IS_ERR(pages)) |
| 2447 | obj->ops->put_pages(obj, pages); |
| 2448 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2449 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
| 2450 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2451 | unlock: |
| 2452 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2453 | } |
| 2454 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2455 | static bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2456 | { |
| 2457 | struct sg_table new_st; |
| 2458 | struct scatterlist *sg, *new_sg; |
| 2459 | unsigned int i; |
| 2460 | |
| 2461 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2462 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2463 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2464 | 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] | 2465 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2466 | |
| 2467 | new_sg = new_st.sgl; |
| 2468 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2469 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
| 2470 | /* called before being DMA mapped, no need to copy sg->dma_* */ |
| 2471 | new_sg = sg_next(new_sg); |
| 2472 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2473 | 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] | 2474 | |
| 2475 | sg_free_table(orig_st); |
| 2476 | |
| 2477 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2478 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2481 | 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] | 2482 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2483 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2484 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2485 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2486 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2487 | struct sg_table *st; |
| 2488 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2489 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2490 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2491 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2492 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2493 | unsigned int sg_page_sizes; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2494 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2495 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2496 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2497 | /* Assert that the object is not currently in any GPU domain. As it |
| 2498 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2499 | * a GPU cache |
| 2500 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2501 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2502 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2503 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2504 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2505 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2506 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2507 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2508 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2509 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2510 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2511 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | /* Get the list of pages out of our struct file. They'll be pinned |
| 2515 | * at this point until we release them. |
| 2516 | * |
| 2517 | * Fail silently without starting the shrinker |
| 2518 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2519 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2520 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2521 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2522 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2523 | sg = st->sgl; |
| 2524 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2525 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2526 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2527 | const unsigned int shrink[] = { |
| 2528 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2529 | 0, |
| 2530 | }, *s = shrink; |
| 2531 | gfp_t gfp = noreclaim; |
| 2532 | |
| 2533 | do { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2534 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2535 | if (likely(!IS_ERR(page))) |
| 2536 | break; |
| 2537 | |
| 2538 | if (!*s) { |
| 2539 | ret = PTR_ERR(page); |
| 2540 | goto err_sg; |
| 2541 | } |
| 2542 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2543 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2544 | cond_resched(); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2545 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2546 | /* We've tried hard to allocate the memory by reaping |
| 2547 | * our own buffer, now let the real VM do its job and |
| 2548 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2549 | * |
| 2550 | * However, since graphics tend to be disposable, |
| 2551 | * defer the oom here by reporting the ENOMEM back |
| 2552 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2553 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2554 | if (!*s) { |
| 2555 | /* reclaim and warn, but no oom */ |
| 2556 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2557 | |
| 2558 | /* Our bo are always dirty and so we require |
| 2559 | * kswapd to reclaim our pages (direct reclaim |
| 2560 | * does not effectively begin pageout of our |
| 2561 | * buffers on its own). However, direct reclaim |
| 2562 | * only waits for kswapd when under allocation |
| 2563 | * congestion. So as a result __GFP_RECLAIM is |
| 2564 | * unreliable and fails to actually reclaim our |
| 2565 | * dirty pages -- unless you try over and over |
| 2566 | * again with !__GFP_NORETRY. However, we still |
| 2567 | * want to fail this allocation rather than |
| 2568 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2569 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2570 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2571 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2572 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2573 | } while (1); |
| 2574 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2575 | if (!i || |
| 2576 | sg->length >= max_segment || |
| 2577 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2578 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2579 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2580 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2581 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2582 | st->nents++; |
| 2583 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2584 | } else { |
| 2585 | sg->length += PAGE_SIZE; |
| 2586 | } |
| 2587 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2588 | |
| 2589 | /* Check that the i965g/gm workaround works. */ |
| 2590 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2591 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2592 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2593 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2594 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2595 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2596 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2597 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2598 | i915_sg_trim(st); |
| 2599 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2600 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2601 | if (ret) { |
| 2602 | /* DMA remapping failed? One possible cause is that |
| 2603 | * it could not reserve enough large entries, asking |
| 2604 | * for PAGE_SIZE chunks instead may be helpful. |
| 2605 | */ |
| 2606 | if (max_segment > PAGE_SIZE) { |
| 2607 | for_each_sgt_page(page, sgt_iter, st) |
| 2608 | put_page(page); |
| 2609 | sg_free_table(st); |
| 2610 | |
| 2611 | max_segment = PAGE_SIZE; |
| 2612 | goto rebuild_st; |
| 2613 | } else { |
| 2614 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2615 | "Failed to DMA remap %lu pages\n", |
| 2616 | page_count); |
| 2617 | goto err_pages; |
| 2618 | } |
| 2619 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2620 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2621 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2622 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2623 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2624 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2625 | |
| 2626 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2627 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2628 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2629 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2630 | err_pages: |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2631 | for_each_sgt_page(page, sgt_iter, st) |
| 2632 | put_page(page); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2633 | sg_free_table(st); |
| 2634 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2635 | |
| 2636 | /* shmemfs first checks if there is enough memory to allocate the page |
| 2637 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2638 | * ENOMEM for a genuine allocation failure. |
| 2639 | * |
| 2640 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2641 | * space and so want to translate the error from shmemfs back to our |
| 2642 | * usual understanding of ENOMEM. |
| 2643 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2644 | if (ret == -ENOSPC) |
| 2645 | ret = -ENOMEM; |
| 2646 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2647 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2651 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2652 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2653 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2654 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2655 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2656 | int i; |
| 2657 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2658 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2659 | |
| 2660 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2661 | obj->mm.get_page.sg_idx = 0; |
| 2662 | |
| 2663 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2664 | |
| 2665 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2666 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2667 | GEM_BUG_ON(obj->mm.quirked); |
| 2668 | __i915_gem_object_pin_pages(obj); |
| 2669 | obj->mm.quirked = true; |
| 2670 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2671 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2672 | GEM_BUG_ON(!sg_page_sizes); |
| 2673 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2674 | |
| 2675 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2676 | * Calculate the supported page-sizes which fit into the given |
| 2677 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2678 | * to use opportunistically when later inserting into the GTT. For |
| 2679 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2680 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2681 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2682 | */ |
| 2683 | obj->mm.page_sizes.sg = 0; |
| 2684 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2685 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2686 | obj->mm.page_sizes.sg |= BIT(i); |
| 2687 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2688 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2689 | |
| 2690 | spin_lock(&i915->mm.obj_lock); |
| 2691 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2692 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2696 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2697 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2698 | |
| 2699 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2700 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2701 | return -EFAULT; |
| 2702 | } |
| 2703 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2704 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2705 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2706 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2707 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2708 | } |
| 2709 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2710 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2711 | * 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] | 2712 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2713 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2714 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2715 | * or as the object is itself released. |
| 2716 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2717 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2718 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2719 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2720 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2721 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2722 | if (err) |
| 2723 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2724 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2725 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2726 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2727 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2728 | err = ____i915_gem_object_get_pages(obj); |
| 2729 | if (err) |
| 2730 | goto unlock; |
| 2731 | |
| 2732 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2733 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2734 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2735 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2736 | unlock: |
| 2737 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2738 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2739 | } |
| 2740 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2741 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2742 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2743 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2744 | { |
| 2745 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2746 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2747 | struct sgt_iter sgt_iter; |
| 2748 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2749 | struct page *stack_pages[32]; |
| 2750 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2751 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2752 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2753 | void *addr; |
| 2754 | |
| 2755 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2756 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2757 | return kmap(sg_page(sgt->sgl)); |
| 2758 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2759 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2760 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2761 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2762 | if (!pages) |
| 2763 | return NULL; |
| 2764 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2765 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2766 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2767 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2768 | |
| 2769 | /* Check that we have the expected number of pages */ |
| 2770 | GEM_BUG_ON(i != n_pages); |
| 2771 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2772 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2773 | default: |
| 2774 | MISSING_CASE(type); |
| 2775 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2776 | case I915_MAP_WB: |
| 2777 | pgprot = PAGE_KERNEL; |
| 2778 | break; |
| 2779 | case I915_MAP_WC: |
| 2780 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2781 | break; |
| 2782 | } |
| 2783 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2784 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2785 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2786 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2787 | |
| 2788 | return addr; |
| 2789 | } |
| 2790 | |
| 2791 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2792 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2793 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2794 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2795 | enum i915_map_type has_type; |
| 2796 | bool pinned; |
| 2797 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2798 | int ret; |
| 2799 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2800 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2801 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2802 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2803 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2804 | if (ret) |
| 2805 | return ERR_PTR(ret); |
| 2806 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2807 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2808 | type &= ~I915_MAP_OVERRIDE; |
| 2809 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2810 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2811 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2812 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2813 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2814 | ret = ____i915_gem_object_get_pages(obj); |
| 2815 | if (ret) |
| 2816 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2817 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2818 | smp_mb__before_atomic(); |
| 2819 | } |
| 2820 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2821 | pinned = false; |
| 2822 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2823 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2824 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2825 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2826 | if (ptr && has_type != type) { |
| 2827 | if (pinned) { |
| 2828 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2829 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2830 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2831 | |
| 2832 | if (is_vmalloc_addr(ptr)) |
| 2833 | vunmap(ptr); |
| 2834 | else |
| 2835 | kunmap(kmap_to_page(ptr)); |
| 2836 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2837 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2838 | } |
| 2839 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2840 | if (!ptr) { |
| 2841 | ptr = i915_gem_object_map(obj, type); |
| 2842 | if (!ptr) { |
| 2843 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2844 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2845 | } |
| 2846 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2847 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2848 | } |
| 2849 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2850 | out_unlock: |
| 2851 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2852 | return ptr; |
| 2853 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2854 | err_unpin: |
| 2855 | atomic_dec(&obj->mm.pages_pin_count); |
| 2856 | err_unlock: |
| 2857 | ptr = ERR_PTR(ret); |
| 2858 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2859 | } |
| 2860 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2861 | static int |
| 2862 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2863 | const struct drm_i915_gem_pwrite *arg) |
| 2864 | { |
| 2865 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2866 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2867 | u64 remain, offset; |
| 2868 | unsigned int pg; |
| 2869 | |
| 2870 | /* Before we instantiate/pin the backing store for our use, we |
| 2871 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2872 | * the pagecache. We avoid the penalty of instantiating all the |
| 2873 | * pages, important if the user is just writing to a few and never |
| 2874 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2875 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2876 | * or clearing-before-use) before it is overwritten. |
| 2877 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2878 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2879 | return -ENODEV; |
| 2880 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2881 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2882 | return -EFAULT; |
| 2883 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2884 | /* Before the pages are instantiated the object is treated as being |
| 2885 | * in the CPU domain. The pages will be clflushed as required before |
| 2886 | * use, and we can freely write into the pages directly. If userspace |
| 2887 | * races pwrite with any other operation; corruption will ensue - |
| 2888 | * that is userspace's prerogative! |
| 2889 | */ |
| 2890 | |
| 2891 | remain = arg->size; |
| 2892 | offset = arg->offset; |
| 2893 | pg = offset_in_page(offset); |
| 2894 | |
| 2895 | do { |
| 2896 | unsigned int len, unwritten; |
| 2897 | struct page *page; |
| 2898 | void *data, *vaddr; |
| 2899 | int err; |
| 2900 | |
| 2901 | len = PAGE_SIZE - pg; |
| 2902 | if (len > remain) |
| 2903 | len = remain; |
| 2904 | |
| 2905 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2906 | offset, len, 0, |
| 2907 | &page, &data); |
| 2908 | if (err < 0) |
| 2909 | return err; |
| 2910 | |
| 2911 | vaddr = kmap(page); |
| 2912 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2913 | kunmap(page); |
| 2914 | |
| 2915 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2916 | offset, len, len - unwritten, |
| 2917 | page, data); |
| 2918 | if (err < 0) |
| 2919 | return err; |
| 2920 | |
| 2921 | if (unwritten) |
| 2922 | return -EFAULT; |
| 2923 | |
| 2924 | remain -= len; |
| 2925 | user_data += len; |
| 2926 | offset += len; |
| 2927 | pg = 0; |
| 2928 | } while (remain); |
| 2929 | |
| 2930 | return 0; |
| 2931 | } |
| 2932 | |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2933 | static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2934 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2935 | bool banned; |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2936 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2937 | atomic_inc(&ctx->guilty_count); |
| 2938 | |
Chris Wilson | 24eae08 | 2018-02-05 09:22:01 +0000 | [diff] [blame] | 2939 | banned = false; |
| 2940 | if (i915_gem_context_is_bannable(ctx)) { |
| 2941 | unsigned int score; |
| 2942 | |
| 2943 | score = atomic_add_return(CONTEXT_SCORE_GUILTY, |
| 2944 | &ctx->ban_score); |
| 2945 | banned = score >= CONTEXT_SCORE_BAN_THRESHOLD; |
| 2946 | |
| 2947 | DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n", |
| 2948 | ctx->name, score, yesno(banned)); |
| 2949 | } |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2950 | if (!banned) |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2951 | return; |
| 2952 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2953 | i915_gem_context_set_banned(ctx); |
| 2954 | if (!IS_ERR_OR_NULL(ctx->file_priv)) { |
| 2955 | atomic_inc(&ctx->file_priv->context_bans); |
| 2956 | DRM_DEBUG_DRIVER("client %s has had %d context banned\n", |
| 2957 | ctx->name, atomic_read(&ctx->file_priv->context_bans)); |
| 2958 | } |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) |
| 2962 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2963 | atomic_inc(&ctx->active_count); |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2964 | } |
| 2965 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2966 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2967 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2968 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2969 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2970 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2971 | |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2972 | /* We are called by the error capture and reset at a random |
| 2973 | * point in time. In particular, note that neither is crucially |
| 2974 | * ordered with an interrupt. After a hang, the GPU is dead and we |
| 2975 | * assume that no more writes can happen (we waited long enough for |
| 2976 | * all writes that were in transaction to be flushed) - adding an |
| 2977 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2978 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
| 2979 | */ |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2980 | spin_lock_irqsave(&engine->timeline->lock, flags); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 2981 | list_for_each_entry(request, &engine->timeline->requests, link) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2982 | if (__i915_request_completed(request, request->global_seqno)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2983 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2984 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2985 | GEM_BUG_ON(request->engine != engine); |
Chris Wilson | c00122f3 | 2017-02-12 17:19:58 +0000 | [diff] [blame] | 2986 | GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 2987 | &request->fence.flags)); |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2988 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2989 | active = request; |
| 2990 | break; |
| 2991 | } |
| 2992 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 2993 | |
| 2994 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2995 | } |
| 2996 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2997 | /* |
| 2998 | * Ensure irq handler finishes, and not run again. |
| 2999 | * Also return the active request so that we only search for it once. |
| 3000 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3001 | struct i915_request * |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3002 | i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) |
| 3003 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3004 | struct i915_request *request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3005 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3006 | /* |
| 3007 | * During the reset sequence, we must prevent the engine from |
| 3008 | * entering RC6. As the context state is undefined until we restart |
| 3009 | * the engine, if it does enter RC6 during the reset, the state |
| 3010 | * written to the powercontext is undefined and so we may lose |
| 3011 | * GPU state upon resume, i.e. fail to restart after a reset. |
| 3012 | */ |
| 3013 | intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); |
| 3014 | |
| 3015 | /* |
| 3016 | * Prevent the signaler thread from updating the request |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3017 | * state (by calling dma_fence_signal) as we are processing |
| 3018 | * the reset. The write from the GPU of the seqno is |
| 3019 | * asynchronous and the signaler thread may see a different |
| 3020 | * value to us and declare the request complete, even though |
| 3021 | * the reset routine have picked that request as the active |
| 3022 | * (incomplete) request. This conflict is not handled |
| 3023 | * gracefully! |
| 3024 | */ |
| 3025 | kthread_park(engine->breadcrumbs.signaler); |
| 3026 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3027 | /* |
| 3028 | * Prevent request submission to the hardware until we have |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3029 | * completed the reset in i915_gem_reset_finish(). If a request |
| 3030 | * is completed by one engine, it may then queue a request |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3031 | * to a second via its execlists->tasklet *just* as we are |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3032 | * calling engine->init_hw() and also writing the ELSP. |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3033 | * Turning off the execlists->tasklet until the reset is over |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3034 | * prevents the race. |
Chris Wilson | 68ad361 | 2018-03-07 13:42:26 +0000 | [diff] [blame] | 3035 | * |
| 3036 | * Note that this needs to be a single atomic operation on the |
| 3037 | * tasklet (flush existing tasks, prevent new tasks) to prevent |
| 3038 | * a race between reset and set-wedged. It is not, so we do the best |
| 3039 | * we can atm and make sure we don't lock the machine up in the more |
| 3040 | * common case of recursively being called from set-wedged from inside |
| 3041 | * i915_reset. |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3042 | */ |
Chris Wilson | 68ad361 | 2018-03-07 13:42:26 +0000 | [diff] [blame] | 3043 | if (!atomic_read(&engine->execlists.tasklet.count)) |
| 3044 | tasklet_kill(&engine->execlists.tasklet); |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3045 | tasklet_disable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3046 | |
Michał Winiarski | c41937f | 2017-10-26 15:35:58 +0200 | [diff] [blame] | 3047 | /* |
| 3048 | * We're using worker to queue preemption requests from the tasklet in |
| 3049 | * GuC submission mode. |
| 3050 | * Even though tasklet was disabled, we may still have a worker queued. |
| 3051 | * Let's make sure that all workers scheduled before disabling the |
| 3052 | * tasklet are completed before continuing with the reset. |
| 3053 | */ |
| 3054 | if (engine->i915->guc.preempt_wq) |
| 3055 | flush_workqueue(engine->i915->guc.preempt_wq); |
| 3056 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3057 | if (engine->irq_seqno_barrier) |
| 3058 | engine->irq_seqno_barrier(engine); |
| 3059 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3060 | request = i915_gem_find_active_request(engine); |
| 3061 | if (request && request->fence.error == -EIO) |
| 3062 | request = ERR_PTR(-EIO); /* Previous reset failed! */ |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3063 | |
| 3064 | return request; |
| 3065 | } |
| 3066 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3067 | int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3068 | { |
| 3069 | struct intel_engine_cs *engine; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3070 | struct i915_request *request; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3071 | enum intel_engine_id id; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3072 | int err = 0; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3073 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3074 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3075 | request = i915_gem_reset_prepare_engine(engine); |
| 3076 | if (IS_ERR(request)) { |
| 3077 | err = PTR_ERR(request); |
| 3078 | continue; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3079 | } |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3080 | |
| 3081 | engine->hangcheck.active_request = request; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3082 | } |
| 3083 | |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3084 | i915_gem_revoke_fences(dev_priv); |
Michal Wajdeczko | c37d572 | 2018-03-12 13:03:07 +0000 | [diff] [blame] | 3085 | intel_uc_sanitize(dev_priv); |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3086 | |
| 3087 | return err; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3088 | } |
| 3089 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3090 | static void skip_request(struct i915_request *request) |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 3091 | { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3092 | void *vaddr = request->ring->vaddr; |
| 3093 | u32 head; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 3094 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3095 | /* As this request likely depends on state from the lost |
| 3096 | * context, clear out all the user operations leaving the |
| 3097 | * breadcrumb at the end (so we get the fence notifications). |
| 3098 | */ |
| 3099 | head = request->head; |
| 3100 | if (request->postfix < head) { |
| 3101 | memset(vaddr + head, 0, request->ring->size - head); |
| 3102 | head = 0; |
| 3103 | } |
| 3104 | memset(vaddr + head, 0, request->postfix - head); |
Chris Wilson | c0d5f32 | 2017-01-10 17:22:43 +0000 | [diff] [blame] | 3105 | |
| 3106 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3109 | static void engine_skip_context(struct i915_request *request) |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3110 | { |
| 3111 | struct intel_engine_cs *engine = request->engine; |
| 3112 | struct i915_gem_context *hung_ctx = request->ctx; |
| 3113 | struct intel_timeline *timeline; |
| 3114 | unsigned long flags; |
| 3115 | |
| 3116 | timeline = i915_gem_context_lookup_timeline(hung_ctx, engine); |
| 3117 | |
| 3118 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3119 | spin_lock(&timeline->lock); |
| 3120 | |
| 3121 | list_for_each_entry_continue(request, &engine->timeline->requests, link) |
| 3122 | if (request->ctx == hung_ctx) |
| 3123 | skip_request(request); |
| 3124 | |
| 3125 | list_for_each_entry(request, &timeline->requests, link) |
| 3126 | skip_request(request); |
| 3127 | |
| 3128 | spin_unlock(&timeline->lock); |
| 3129 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 3130 | } |
| 3131 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3132 | /* Returns the request if it was guilty of the hang */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3133 | static struct i915_request * |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3134 | i915_gem_reset_request(struct intel_engine_cs *engine, |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3135 | struct i915_request *request, |
| 3136 | bool stalled) |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3137 | { |
Mika Kuoppala | 71895a0 | 2017-01-17 17:59:07 +0200 | [diff] [blame] | 3138 | /* The guilty request will get skipped on a hung engine. |
| 3139 | * |
| 3140 | * Users of client default contexts do not rely on logical |
| 3141 | * state preserved between batches so it is safe to execute |
| 3142 | * queued requests following the hang. Non default contexts |
| 3143 | * rely on preserved state, so skipping a batch loses the |
| 3144 | * evolution of the state and it needs to be considered corrupted. |
| 3145 | * Executing more queued batches on top of corrupted state is |
| 3146 | * risky. But we take the risk by trying to advance through |
| 3147 | * the queued requests in order to make the client behaviour |
| 3148 | * more predictable around resets, by not throwing away random |
| 3149 | * amount of batches it has prepared for execution. Sophisticated |
| 3150 | * clients can use gem_reset_stats_ioctl and dma fence status |
| 3151 | * (exported via sync_file info ioctl on explicit fences) to observe |
| 3152 | * when it loses the context state and should rebuild accordingly. |
| 3153 | * |
| 3154 | * The context ban, and ultimately the client ban, mechanism are safety |
| 3155 | * valves if client submission ends up resulting in nothing more than |
| 3156 | * subsequent hangs. |
| 3157 | */ |
| 3158 | |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3159 | if (i915_request_completed(request)) { |
| 3160 | GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n", |
| 3161 | engine->name, request->global_seqno, |
| 3162 | request->fence.context, request->fence.seqno, |
| 3163 | intel_engine_get_seqno(engine)); |
| 3164 | stalled = false; |
| 3165 | } |
| 3166 | |
| 3167 | if (stalled) { |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3168 | i915_gem_context_mark_guilty(request->ctx); |
| 3169 | skip_request(request); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3170 | |
| 3171 | /* If this context is now banned, skip all pending requests. */ |
| 3172 | if (i915_gem_context_is_banned(request->ctx)) |
| 3173 | engine_skip_context(request); |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3174 | } else { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3175 | /* |
| 3176 | * Since this is not the hung engine, it may have advanced |
| 3177 | * since the hang declaration. Double check by refinding |
| 3178 | * the active request at the time of the reset. |
| 3179 | */ |
| 3180 | request = i915_gem_find_active_request(engine); |
| 3181 | if (request) { |
| 3182 | i915_gem_context_mark_innocent(request->ctx); |
| 3183 | dma_fence_set_error(&request->fence, -EAGAIN); |
| 3184 | |
| 3185 | /* Rewind the engine to replay the incomplete rq */ |
| 3186 | spin_lock_irq(&engine->timeline->lock); |
| 3187 | request = list_prev_entry(request, link); |
| 3188 | if (&request->link == &engine->timeline->requests) |
| 3189 | request = NULL; |
| 3190 | spin_unlock_irq(&engine->timeline->lock); |
| 3191 | } |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3192 | } |
| 3193 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3194 | return request; |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3195 | } |
| 3196 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3197 | void i915_gem_reset_engine(struct intel_engine_cs *engine, |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3198 | struct i915_request *request, |
| 3199 | bool stalled) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3200 | { |
Chris Wilson | fcb1de5 | 2017-12-19 09:01:10 +0000 | [diff] [blame] | 3201 | /* |
| 3202 | * Make sure this write is visible before we re-enable the interrupt |
| 3203 | * handlers on another CPU, as tasklet_enable() resolves to just |
| 3204 | * a compiler barrier which is insufficient for our purpose here. |
| 3205 | */ |
| 3206 | smp_store_mb(engine->irq_posted, 0); |
Chris Wilson | ed454f2 | 2017-07-21 13:32:29 +0100 | [diff] [blame] | 3207 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3208 | if (request) |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3209 | request = i915_gem_reset_request(engine, request, stalled); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3210 | |
| 3211 | if (request) { |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3212 | DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n", |
| 3213 | engine->name, request->global_seqno); |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3214 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3215 | |
| 3216 | /* Setup the CS to resume from the breadcrumb of the hung request */ |
| 3217 | engine->reset_hw(engine, request); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3218 | } |
| 3219 | |
Chris Wilson | d0667e9 | 2018-04-06 23:03:54 +0100 | [diff] [blame] | 3220 | void i915_gem_reset(struct drm_i915_private *dev_priv, |
| 3221 | unsigned int stalled_mask) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3222 | { |
| 3223 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3224 | enum intel_engine_id id; |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3225 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3226 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 3227 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3228 | i915_retire_requests(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3229 | |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3230 | for_each_engine(engine, dev_priv, id) { |
| 3231 | struct i915_gem_context *ctx; |
| 3232 | |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3233 | i915_gem_reset_engine(engine, |
| 3234 | engine->hangcheck.active_request, |
Chris Wilson | d0667e9 | 2018-04-06 23:03:54 +0100 | [diff] [blame] | 3235 | stalled_mask & ENGINE_MASK(id)); |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3236 | ctx = fetch_and_zero(&engine->last_retired_context); |
| 3237 | if (ctx) |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 3238 | intel_context_unpin(ctx, engine); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3239 | |
| 3240 | /* |
| 3241 | * Ostensibily, we always want a context loaded for powersaving, |
| 3242 | * so if the engine is idle after the reset, send a request |
| 3243 | * to load our scratch kernel_context. |
| 3244 | * |
| 3245 | * More mysteriously, if we leave the engine idle after a reset, |
| 3246 | * the next userspace batch may hang, with what appears to be |
| 3247 | * an incoherent read by the CS (presumably stale TLB). An |
| 3248 | * empty request appears sufficient to paper over the glitch. |
| 3249 | */ |
Chris Wilson | 01b8fdc | 2018-02-05 15:24:31 +0000 | [diff] [blame] | 3250 | if (intel_engine_is_idle(engine)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3251 | struct i915_request *rq; |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3252 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3253 | rq = i915_request_alloc(engine, |
| 3254 | dev_priv->kernel_context); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3255 | if (!IS_ERR(rq)) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3256 | __i915_request_add(rq, false); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3257 | } |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3258 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3259 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 3260 | i915_gem_restore_fences(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3261 | } |
| 3262 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3263 | void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) |
| 3264 | { |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3265 | tasklet_enable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3266 | kthread_unpark(engine->breadcrumbs.signaler); |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3267 | |
| 3268 | intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3269 | } |
| 3270 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3271 | void i915_gem_reset_finish(struct drm_i915_private *dev_priv) |
| 3272 | { |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3273 | struct intel_engine_cs *engine; |
| 3274 | enum intel_engine_id id; |
| 3275 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3276 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3277 | |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3278 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3279 | engine->hangcheck.active_request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3280 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3281 | } |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3282 | } |
| 3283 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3284 | static void nop_submit_request(struct i915_request *request) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3285 | { |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3286 | GEM_TRACE("%s fence %llx:%d -> -EIO\n", |
| 3287 | request->engine->name, |
| 3288 | request->fence.context, request->fence.seqno); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3289 | dma_fence_set_error(&request->fence, -EIO); |
| 3290 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3291 | i915_request_submit(request); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3292 | } |
| 3293 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3294 | static void nop_complete_submit_request(struct i915_request *request) |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3295 | { |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3296 | unsigned long flags; |
| 3297 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3298 | GEM_TRACE("%s fence %llx:%d -> -EIO\n", |
| 3299 | request->engine->name, |
| 3300 | request->fence.context, request->fence.seqno); |
Chris Wilson | 3cd9442 | 2017-01-10 17:22:45 +0000 | [diff] [blame] | 3301 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3302 | |
| 3303 | spin_lock_irqsave(&request->engine->timeline->lock, flags); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3304 | __i915_request_submit(request); |
Chris Wilson | 3dcf93f7 | 2016-11-22 14:41:20 +0000 | [diff] [blame] | 3305 | intel_engine_init_global_seqno(request->engine, request->global_seqno); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3306 | spin_unlock_irqrestore(&request->engine->timeline->lock, flags); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3307 | } |
| 3308 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3309 | void i915_gem_set_wedged(struct drm_i915_private *i915) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3310 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 3311 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3312 | enum intel_engine_id id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3313 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3314 | GEM_TRACE("start\n"); |
| 3315 | |
Chris Wilson | 7f961d7 | 2018-04-26 11:32:19 +0100 | [diff] [blame] | 3316 | if (GEM_SHOW_DEBUG()) { |
Chris Wilson | 559e040 | 2018-02-05 09:21:59 +0000 | [diff] [blame] | 3317 | struct drm_printer p = drm_debug_printer(__func__); |
| 3318 | |
| 3319 | for_each_engine(engine, i915, id) |
| 3320 | intel_engine_dump(engine, &p, "%s\n", engine->name); |
| 3321 | } |
| 3322 | |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3323 | set_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3324 | smp_mb__after_atomic(); |
| 3325 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3326 | /* |
| 3327 | * First, stop submission to hw, but do not yet complete requests by |
| 3328 | * rolling the global seqno forward (since this would complete requests |
| 3329 | * for which we haven't set the fence error to EIO yet). |
| 3330 | */ |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3331 | for_each_engine(engine, i915, id) { |
| 3332 | i915_gem_reset_prepare_engine(engine); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3333 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3334 | engine->submit_request = nop_submit_request; |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3335 | engine->schedule = NULL; |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3336 | } |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3337 | i915->caps.scheduler = 0; |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3338 | |
Chris Wilson | ac697ae | 2018-03-15 15:10:15 +0000 | [diff] [blame] | 3339 | /* Even if the GPU reset fails, it should still stop the engines */ |
| 3340 | intel_gpu_reset(i915, ALL_ENGINES); |
| 3341 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3342 | /* |
| 3343 | * Make sure no one is running the old callback before we proceed with |
| 3344 | * cancelling requests and resetting the completion tracking. Otherwise |
| 3345 | * we might submit a request to the hardware which never completes. |
| 3346 | */ |
| 3347 | synchronize_rcu(); |
| 3348 | |
| 3349 | for_each_engine(engine, i915, id) { |
| 3350 | /* Mark all executing requests as skipped */ |
| 3351 | engine->cancel_requests(engine); |
| 3352 | |
| 3353 | /* |
| 3354 | * Only once we've force-cancelled all in-flight requests can we |
| 3355 | * start to complete all requests. |
| 3356 | */ |
| 3357 | engine->submit_request = nop_complete_submit_request; |
| 3358 | } |
| 3359 | |
| 3360 | /* |
| 3361 | * Make sure no request can slip through without getting completed by |
| 3362 | * either this call here to intel_engine_init_global_seqno, or the one |
| 3363 | * in nop_complete_submit_request. |
| 3364 | */ |
| 3365 | synchronize_rcu(); |
| 3366 | |
| 3367 | for_each_engine(engine, i915, id) { |
| 3368 | unsigned long flags; |
| 3369 | |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3370 | /* |
| 3371 | * Mark all pending requests as complete so that any concurrent |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3372 | * (lockless) lookup doesn't try and wait upon the request as we |
| 3373 | * reset it. |
| 3374 | */ |
| 3375 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3376 | intel_engine_init_global_seqno(engine, |
| 3377 | intel_engine_last_submit(engine)); |
| 3378 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3379 | |
| 3380 | i915_gem_reset_finish_engine(engine); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3381 | } |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3382 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3383 | GEM_TRACE("end\n"); |
| 3384 | |
Chris Wilson | 3d7adbb | 2017-07-21 13:32:27 +0100 | [diff] [blame] | 3385 | wake_up_all(&i915->gpu_error.reset_queue); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3386 | } |
| 3387 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3388 | bool i915_gem_unset_wedged(struct drm_i915_private *i915) |
| 3389 | { |
| 3390 | struct i915_gem_timeline *tl; |
| 3391 | int i; |
| 3392 | |
| 3393 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3394 | if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3395 | return true; |
| 3396 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3397 | GEM_TRACE("start\n"); |
| 3398 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3399 | /* |
| 3400 | * Before unwedging, make sure that all pending operations |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3401 | * are flushed and errored out - we may have requests waiting upon |
| 3402 | * third party fences. We marked all inflight requests as EIO, and |
| 3403 | * every execbuf since returned EIO, for consistency we want all |
| 3404 | * the currently pending requests to also be marked as EIO, which |
| 3405 | * is done inside our nop_submit_request - and so we must wait. |
| 3406 | * |
| 3407 | * No more can be submitted until we reset the wedged bit. |
| 3408 | */ |
| 3409 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3410 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3411 | struct i915_request *rq; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3412 | |
| 3413 | rq = i915_gem_active_peek(&tl->engine[i].last_request, |
| 3414 | &i915->drm.struct_mutex); |
| 3415 | if (!rq) |
| 3416 | continue; |
| 3417 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3418 | /* |
| 3419 | * We can't use our normal waiter as we want to |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3420 | * avoid recursively trying to handle the current |
| 3421 | * reset. The basic dma_fence_default_wait() installs |
| 3422 | * a callback for dma_fence_signal(), which is |
| 3423 | * triggered by our nop handler (indirectly, the |
| 3424 | * callback enables the signaler thread which is |
| 3425 | * woken by the nop_submit_request() advancing the seqno |
| 3426 | * and when the seqno passes the fence, the signaler |
| 3427 | * then signals the fence waking us up). |
| 3428 | */ |
| 3429 | if (dma_fence_default_wait(&rq->fence, true, |
| 3430 | MAX_SCHEDULE_TIMEOUT) < 0) |
| 3431 | return false; |
| 3432 | } |
| 3433 | } |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3434 | i915_retire_requests(i915); |
| 3435 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3436 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3437 | /* |
| 3438 | * Undo nop_submit_request. We prevent all new i915 requests from |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3439 | * being queued (by disallowing execbuf whilst wedged) so having |
| 3440 | * waited for all active requests above, we know the system is idle |
| 3441 | * and do not have to worry about a thread being inside |
| 3442 | * engine->submit_request() as we swap over. So unlike installing |
| 3443 | * the nop_submit_request on reset, we can do this from normal |
| 3444 | * context and do not require stop_machine(). |
| 3445 | */ |
| 3446 | intel_engines_reset_default_submission(i915); |
Chris Wilson | 36703e7 | 2017-06-22 11:56:25 +0100 | [diff] [blame] | 3447 | i915_gem_contexts_lost(i915); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3448 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3449 | GEM_TRACE("end\n"); |
| 3450 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3451 | smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ |
| 3452 | clear_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3453 | |
| 3454 | return true; |
| 3455 | } |
| 3456 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 3457 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3458 | i915_gem_retire_work_handler(struct work_struct *work) |
| 3459 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3460 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3461 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 3462 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3463 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3464 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3465 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3466 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3467 | mutex_unlock(&dev->struct_mutex); |
| 3468 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3469 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3470 | /* |
| 3471 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3472 | * We do not need to do this test under locking as in the worst-case |
| 3473 | * we queue the retire worker once too often. |
| 3474 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3475 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3476 | queue_delayed_work(dev_priv->wq, |
| 3477 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 3478 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3479 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3480 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3481 | static void shrink_caches(struct drm_i915_private *i915) |
| 3482 | { |
| 3483 | /* |
| 3484 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 3485 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 3486 | * with the aim of reducing fragmentation. |
| 3487 | */ |
| 3488 | kmem_cache_shrink(i915->priorities); |
| 3489 | kmem_cache_shrink(i915->dependencies); |
| 3490 | kmem_cache_shrink(i915->requests); |
| 3491 | kmem_cache_shrink(i915->luts); |
| 3492 | kmem_cache_shrink(i915->vmas); |
| 3493 | kmem_cache_shrink(i915->objects); |
| 3494 | } |
| 3495 | |
| 3496 | struct sleep_rcu_work { |
| 3497 | union { |
| 3498 | struct rcu_head rcu; |
| 3499 | struct work_struct work; |
| 3500 | }; |
| 3501 | struct drm_i915_private *i915; |
| 3502 | unsigned int epoch; |
| 3503 | }; |
| 3504 | |
| 3505 | static inline bool |
| 3506 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 3507 | { |
| 3508 | /* |
| 3509 | * There is a small chance that the epoch wrapped since we started |
| 3510 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 3511 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 3512 | */ |
| 3513 | return epoch == READ_ONCE(i915->gt.epoch); |
| 3514 | } |
| 3515 | |
| 3516 | static void __sleep_work(struct work_struct *work) |
| 3517 | { |
| 3518 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 3519 | struct drm_i915_private *i915 = s->i915; |
| 3520 | unsigned int epoch = s->epoch; |
| 3521 | |
| 3522 | kfree(s); |
| 3523 | if (same_epoch(i915, epoch)) |
| 3524 | shrink_caches(i915); |
| 3525 | } |
| 3526 | |
| 3527 | static void __sleep_rcu(struct rcu_head *rcu) |
| 3528 | { |
| 3529 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 3530 | struct drm_i915_private *i915 = s->i915; |
| 3531 | |
| 3532 | if (same_epoch(i915, s->epoch)) { |
| 3533 | INIT_WORK(&s->work, __sleep_work); |
| 3534 | queue_work(i915->wq, &s->work); |
| 3535 | } else { |
| 3536 | kfree(s); |
| 3537 | } |
| 3538 | } |
| 3539 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3540 | static inline bool |
| 3541 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 3542 | { |
| 3543 | return (READ_ONCE(i915->gt.active_requests) || |
| 3544 | work_pending(&i915->gt.idle_work.work)); |
| 3545 | } |
| 3546 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3547 | static void |
| 3548 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3549 | { |
| 3550 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3551 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3552 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3553 | bool rearm_hangcheck; |
| 3554 | |
| 3555 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3556 | return; |
| 3557 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3558 | /* |
| 3559 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3560 | * new request is submitted. As we don't trust the hardware, we |
| 3561 | * continue on if the wait times out. This is necessary to allow |
| 3562 | * the machine to suspend even if the hardware dies, and we will |
| 3563 | * try to recover in resume (after depriving the hardware of power, |
| 3564 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3565 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3566 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3567 | intel_engines_are_idle(dev_priv), |
| 3568 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3569 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3570 | |
| 3571 | rearm_hangcheck = |
| 3572 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3573 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3574 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3575 | /* Currently busy, come back later */ |
| 3576 | mod_delayed_work(dev_priv->wq, |
| 3577 | &dev_priv->gt.idle_work, |
| 3578 | msecs_to_jiffies(50)); |
| 3579 | goto out_rearm; |
| 3580 | } |
| 3581 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3582 | /* |
| 3583 | * New request retired after this work handler started, extend active |
| 3584 | * period until next instance of the work. |
| 3585 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3586 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3587 | goto out_unlock; |
| 3588 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 3589 | epoch = __i915_gem_park(dev_priv); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3590 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3591 | rearm_hangcheck = false; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3592 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3593 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3594 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3595 | out_rearm: |
| 3596 | if (rearm_hangcheck) { |
| 3597 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3598 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3599 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3600 | |
| 3601 | /* |
| 3602 | * When we are idle, it is an opportune time to reap our caches. |
| 3603 | * However, we have many objects that utilise RCU and the ordered |
| 3604 | * i915->wq that this work is executing on. To try and flush any |
| 3605 | * pending frees now we are idle, we first wait for an RCU grace |
| 3606 | * period, and then queue a task (that will run last on the wq) to |
| 3607 | * shrink and re-optimize the caches. |
| 3608 | */ |
| 3609 | if (same_epoch(dev_priv, epoch)) { |
| 3610 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3611 | if (s) { |
| 3612 | s->i915 = dev_priv; |
| 3613 | s->epoch = epoch; |
| 3614 | call_rcu(&s->rcu, __sleep_rcu); |
| 3615 | } |
| 3616 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3617 | } |
| 3618 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3619 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3620 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3621 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3622 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3623 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3624 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3625 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3626 | mutex_lock(&i915->drm.struct_mutex); |
| 3627 | |
| 3628 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3629 | struct i915_gem_context *ctx = lut->ctx; |
| 3630 | struct i915_vma *vma; |
| 3631 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3632 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3633 | if (ctx->file_priv != fpriv) |
| 3634 | continue; |
| 3635 | |
| 3636 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3637 | GEM_BUG_ON(vma->obj != obj); |
| 3638 | |
| 3639 | /* We allow the process to have multiple handles to the same |
| 3640 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3641 | */ |
| 3642 | GEM_BUG_ON(!vma->open_count); |
| 3643 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3644 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3645 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3646 | list_del(&lut->obj_link); |
| 3647 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3648 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3649 | kmem_cache_free(i915->luts, lut); |
| 3650 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3651 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3652 | |
| 3653 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3654 | } |
| 3655 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3656 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3657 | { |
| 3658 | if (timeout_ns < 0) |
| 3659 | return MAX_SCHEDULE_TIMEOUT; |
| 3660 | |
| 3661 | if (timeout_ns == 0) |
| 3662 | return 0; |
| 3663 | |
| 3664 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3665 | } |
| 3666 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3667 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3668 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3669 | * @dev: drm device pointer |
| 3670 | * @data: ioctl data blob |
| 3671 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3672 | * |
| 3673 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3674 | * the timeout parameter. |
| 3675 | * -ETIME: object is still busy after timeout |
| 3676 | * -ERESTARTSYS: signal interrupted the wait |
| 3677 | * -ENONENT: object doesn't exist |
| 3678 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3679 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3680 | * -ENOMEM: damn |
| 3681 | * -ENODEV: Internal IRQ fail |
| 3682 | * -E?: The add request failed |
| 3683 | * |
| 3684 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3685 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3686 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3687 | * without holding struct_mutex the object may become re-busied before this |
| 3688 | * function completes. A similar but shorter * race condition exists in the busy |
| 3689 | * ioctl |
| 3690 | */ |
| 3691 | int |
| 3692 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3693 | { |
| 3694 | struct drm_i915_gem_wait *args = data; |
| 3695 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3696 | ktime_t start; |
| 3697 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3698 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3699 | if (args->flags != 0) |
| 3700 | return -EINVAL; |
| 3701 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3702 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3703 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3704 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3705 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3706 | start = ktime_get(); |
| 3707 | |
| 3708 | ret = i915_gem_object_wait(obj, |
| 3709 | I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL, |
| 3710 | to_wait_timeout(args->timeout_ns), |
| 3711 | to_rps_client(file)); |
| 3712 | |
| 3713 | if (args->timeout_ns > 0) { |
| 3714 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3715 | if (args->timeout_ns < 0) |
| 3716 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3717 | |
| 3718 | /* |
| 3719 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3720 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3721 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3722 | * |
| 3723 | * This is a regression from the timespec->ktime conversion. |
| 3724 | */ |
| 3725 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3726 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3727 | |
| 3728 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3729 | if (ret == -ETIME && args->timeout_ns) |
| 3730 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3733 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3734 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3735 | } |
| 3736 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3737 | static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags) |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3738 | { |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3739 | int ret, i; |
| 3740 | |
| 3741 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
| 3742 | ret = i915_gem_active_wait(&tl->engine[i].last_request, flags); |
| 3743 | if (ret) |
| 3744 | return ret; |
| 3745 | } |
| 3746 | |
| 3747 | return 0; |
| 3748 | } |
| 3749 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3750 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3751 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3752 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3753 | dev_err(i915->drm.dev, |
| 3754 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3755 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3756 | i915_gem_set_wedged(i915); |
| 3757 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | return 0; |
| 3761 | } |
| 3762 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3763 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags) |
| 3764 | { |
Dave Gordon | b4ac5af | 2016-03-24 11:20:38 +0000 | [diff] [blame] | 3765 | int ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3766 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3767 | /* If the device is asleep, we have no requests outstanding */ |
| 3768 | if (!READ_ONCE(i915->gt.awake)) |
| 3769 | return 0; |
| 3770 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3771 | if (flags & I915_WAIT_LOCKED) { |
| 3772 | struct i915_gem_timeline *tl; |
| 3773 | |
| 3774 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3775 | |
| 3776 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3777 | ret = wait_for_timeline(tl, flags); |
| 3778 | if (ret) |
| 3779 | return ret; |
| 3780 | } |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3781 | i915_retire_requests(i915); |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3782 | |
| 3783 | ret = wait_for_engines(i915); |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3784 | } else { |
| 3785 | ret = wait_for_timeline(&i915->gt.global_timeline, flags); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 3786 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3787 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3788 | return ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3789 | } |
| 3790 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3791 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3792 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3793 | /* |
| 3794 | * We manually flush the CPU domain so that we can override and |
| 3795 | * force the flush for the display, and perform it asyncrhonously. |
| 3796 | */ |
| 3797 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3798 | if (obj->cache_dirty) |
| 3799 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3800 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3804 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3805 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3806 | return; |
| 3807 | |
| 3808 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3809 | __i915_gem_object_flush_for_display(obj); |
| 3810 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3811 | } |
| 3812 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3813 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3814 | * Moves a single object to the WC read, and possibly write domain. |
| 3815 | * @obj: object to act on |
| 3816 | * @write: ask for write access or read only |
| 3817 | * |
| 3818 | * This function returns when the move is complete, including waiting on |
| 3819 | * flushes to occur. |
| 3820 | */ |
| 3821 | int |
| 3822 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3823 | { |
| 3824 | int ret; |
| 3825 | |
| 3826 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3827 | |
| 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); |
| 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_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3838 | return 0; |
| 3839 | |
| 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 | */ |
| 3848 | ret = i915_gem_object_pin_pages(obj); |
| 3849 | if (ret) |
| 3850 | return ret; |
| 3851 | |
| 3852 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3853 | |
| 3854 | /* Serialise direct access to this object with the barriers for |
| 3855 | * coherent writes from the GPU, by effectively invalidating the |
| 3856 | * WC 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_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3859 | mb(); |
| 3860 | |
| 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_WC) != 0); |
| 3865 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [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_WC; |
| 3868 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3869 | obj->mm.dirty = true; |
| 3870 | } |
| 3871 | |
| 3872 | i915_gem_object_unpin_pages(obj); |
| 3873 | return 0; |
| 3874 | } |
| 3875 | |
| 3876 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3877 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3878 | * @obj: object to act on |
| 3879 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3880 | * |
| 3881 | * This function returns when the move is complete, including waiting on |
| 3882 | * flushes to occur. |
| 3883 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3884 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3885 | 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] | 3886 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3887 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3888 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3889 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3890 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3891 | ret = i915_gem_object_wait(obj, |
| 3892 | I915_WAIT_INTERRUPTIBLE | |
| 3893 | I915_WAIT_LOCKED | |
| 3894 | (write ? I915_WAIT_ALL : 0), |
| 3895 | MAX_SCHEDULE_TIMEOUT, |
| 3896 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3897 | if (ret) |
| 3898 | return ret; |
| 3899 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3900 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3901 | return 0; |
| 3902 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3903 | /* Flush and acquire obj->pages so that we are coherent through |
| 3904 | * direct access in memory with previous cached writes through |
| 3905 | * shmemfs and that our cache domain tracking remains valid. |
| 3906 | * For example, if the obj->filp was moved to swap without us |
| 3907 | * being notified and releasing the pages, we would mistakenly |
| 3908 | * continue to assume that the obj remained out of the CPU cached |
| 3909 | * domain. |
| 3910 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3911 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3912 | if (ret) |
| 3913 | return ret; |
| 3914 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3915 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3916 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3917 | /* Serialise direct access to this object with the barriers for |
| 3918 | * coherent writes from the GPU, by effectively invalidating the |
| 3919 | * GTT domain upon first access. |
| 3920 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3921 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3922 | mb(); |
| 3923 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3924 | /* It should now be out of any other write domains, and we can update |
| 3925 | * the domain values for our changes. |
| 3926 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3927 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3928 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3929 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3930 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3931 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3932 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3933 | } |
| 3934 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3935 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3936 | return 0; |
| 3937 | } |
| 3938 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3939 | /** |
| 3940 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3941 | * @obj: object to act on |
| 3942 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3943 | * |
| 3944 | * After this function returns, the object will be in the new cache-level |
| 3945 | * across all GTT and the contents of the backing storage will be coherent, |
| 3946 | * with respect to the new cache-level. In order to keep the backing storage |
| 3947 | * coherent for all users, we only allow a single cache level to be set |
| 3948 | * globally on the object and prevent it from being changed whilst the |
| 3949 | * hardware is reading from the object. That is if the object is currently |
| 3950 | * on the scanout it will be set to uncached (or equivalent display |
| 3951 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3952 | * that all direct access to the scanout remains coherent. |
| 3953 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3954 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3955 | enum i915_cache_level cache_level) |
| 3956 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3957 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3958 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3959 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3960 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3961 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3962 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3963 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3964 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3965 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3966 | * be invalid given the new cache-level. This is principally to |
| 3967 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3968 | * reading an invalid PTE on older architectures. |
| 3969 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3970 | restart: |
| 3971 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3972 | if (!drm_mm_node_allocated(&vma->node)) |
| 3973 | continue; |
| 3974 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3975 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3976 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3977 | return -EBUSY; |
| 3978 | } |
| 3979 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3980 | if (!i915_vma_is_closed(vma) && |
| 3981 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3982 | continue; |
| 3983 | |
| 3984 | ret = i915_vma_unbind(vma); |
| 3985 | if (ret) |
| 3986 | return ret; |
| 3987 | |
| 3988 | /* As unbinding may affect other elements in the |
| 3989 | * obj->vma_list (due to side-effects from retiring |
| 3990 | * an active vma), play safe and restart the iterator. |
| 3991 | */ |
| 3992 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3993 | } |
| 3994 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3995 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3996 | * cache-level on the PTE. We could simply unbind them all and |
| 3997 | * rebind with the correct cache-level on next use. However since |
| 3998 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3999 | * rewrite the PTE in the belief that doing so tramples upon less |
| 4000 | * state and so involves less work. |
| 4001 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 4002 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4003 | /* Before we change the PTE, the GPU must not be accessing it. |
| 4004 | * If we wait upon the object, we know that all the bound |
| 4005 | * VMA are no longer active. |
| 4006 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4007 | ret = i915_gem_object_wait(obj, |
| 4008 | I915_WAIT_INTERRUPTIBLE | |
| 4009 | I915_WAIT_LOCKED | |
| 4010 | I915_WAIT_ALL, |
| 4011 | MAX_SCHEDULE_TIMEOUT, |
| 4012 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4013 | if (ret) |
| 4014 | return ret; |
| 4015 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4016 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 4017 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4018 | /* Access to snoopable pages through the GTT is |
| 4019 | * incoherent and on some machines causes a hard |
| 4020 | * lockup. Relinquish the CPU mmaping to force |
| 4021 | * userspace to refault in the pages and we can |
| 4022 | * then double check if the GTT mapping is still |
| 4023 | * valid for that pointer access. |
| 4024 | */ |
| 4025 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4026 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4027 | /* As we no longer need a fence for GTT access, |
| 4028 | * we can relinquish it now (and so prevent having |
| 4029 | * to steal a fence from someone else on the next |
| 4030 | * fence request). Note GPU activity would have |
| 4031 | * dropped the fence as all snoopable access is |
| 4032 | * supposed to be linear. |
| 4033 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 4034 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 4035 | ret = i915_vma_put_fence(vma); |
| 4036 | if (ret) |
| 4037 | return ret; |
| 4038 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4039 | } else { |
| 4040 | /* We either have incoherent backing store and |
| 4041 | * so no GTT access or the architecture is fully |
| 4042 | * coherent. In such cases, existing GTT mmaps |
| 4043 | * ignore the cache bit in the PTE and we can |
| 4044 | * rewrite it without confusing the GPU or having |
| 4045 | * to force userspace to fault back in its mmaps. |
| 4046 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4047 | } |
| 4048 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 4049 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4050 | if (!drm_mm_node_allocated(&vma->node)) |
| 4051 | continue; |
| 4052 | |
| 4053 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 4054 | if (ret) |
| 4055 | return ret; |
| 4056 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4057 | } |
| 4058 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 4059 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 4060 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4061 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4062 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 4063 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4064 | return 0; |
| 4065 | } |
| 4066 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4067 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 4068 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4069 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4070 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4071 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4072 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4073 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4074 | rcu_read_lock(); |
| 4075 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 4076 | if (!obj) { |
| 4077 | err = -ENOENT; |
| 4078 | goto out; |
| 4079 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4080 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4081 | switch (obj->cache_level) { |
| 4082 | case I915_CACHE_LLC: |
| 4083 | case I915_CACHE_L3_LLC: |
| 4084 | args->caching = I915_CACHING_CACHED; |
| 4085 | break; |
| 4086 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4087 | case I915_CACHE_WT: |
| 4088 | args->caching = I915_CACHING_DISPLAY; |
| 4089 | break; |
| 4090 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4091 | default: |
| 4092 | args->caching = I915_CACHING_NONE; |
| 4093 | break; |
| 4094 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4095 | out: |
| 4096 | rcu_read_unlock(); |
| 4097 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4098 | } |
| 4099 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4100 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 4101 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4102 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4103 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4104 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4105 | struct drm_i915_gem_object *obj; |
| 4106 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4107 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4108 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4109 | switch (args->caching) { |
| 4110 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4111 | level = I915_CACHE_NONE; |
| 4112 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4113 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4114 | /* |
| 4115 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 4116 | * snooped mapping may leave stale data in a corresponding CPU |
| 4117 | * cacheline, whereas normally such cachelines would get |
| 4118 | * invalidated. |
| 4119 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4120 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4121 | return -ENODEV; |
| 4122 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4123 | level = I915_CACHE_LLC; |
| 4124 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4125 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4126 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4127 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4128 | default: |
| 4129 | return -EINVAL; |
| 4130 | } |
| 4131 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4132 | obj = i915_gem_object_lookup(file, args->handle); |
| 4133 | if (!obj) |
| 4134 | return -ENOENT; |
| 4135 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 4136 | /* |
| 4137 | * The caching mode of proxy object is handled by its generator, and |
| 4138 | * not allowed to be changed by userspace. |
| 4139 | */ |
| 4140 | if (i915_gem_object_is_proxy(obj)) { |
| 4141 | ret = -ENXIO; |
| 4142 | goto out; |
| 4143 | } |
| 4144 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4145 | if (obj->cache_level == level) |
| 4146 | goto out; |
| 4147 | |
| 4148 | ret = i915_gem_object_wait(obj, |
| 4149 | I915_WAIT_INTERRUPTIBLE, |
| 4150 | MAX_SCHEDULE_TIMEOUT, |
| 4151 | to_rps_client(file)); |
| 4152 | if (ret) |
| 4153 | goto out; |
| 4154 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 4155 | ret = i915_mutex_lock_interruptible(dev); |
| 4156 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4157 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4158 | |
| 4159 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4160 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4161 | |
| 4162 | out: |
| 4163 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4164 | return ret; |
| 4165 | } |
| 4166 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4167 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 4168 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 4169 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 4170 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 4171 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4172 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4173 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4174 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 4175 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4176 | const struct i915_ggtt_view *view, |
| 4177 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4178 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4179 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4180 | int ret; |
| 4181 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4182 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4183 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4184 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4185 | * display coherency whilst setting up the cache domains. |
| 4186 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4187 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4188 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4189 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 4190 | * a result, we make sure that the pinning that is about to occur is |
| 4191 | * done with uncached PTEs. This is lowest common denominator for all |
| 4192 | * chipsets. |
| 4193 | * |
| 4194 | * However for gen6+, we could do better by using the GFDT bit instead |
| 4195 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 4196 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 4197 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4198 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 4199 | HAS_WT(to_i915(obj->base.dev)) ? |
| 4200 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4201 | if (ret) { |
| 4202 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4203 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4204 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4205 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4206 | /* As the user may map the buffer once pinned in the display plane |
| 4207 | * (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] | 4208 | * always use map_and_fenceable for all scanout buffers. However, |
| 4209 | * it may simply be too big to fit into mappable, in which case |
| 4210 | * put it anyway and hope that userspace can cope (but always first |
| 4211 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4212 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4213 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4214 | if ((flags & PIN_MAPPABLE) == 0 && |
| 4215 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4216 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4217 | flags | |
| 4218 | PIN_MAPPABLE | |
| 4219 | PIN_NONBLOCK); |
| 4220 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 4221 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4222 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4223 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4224 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 4225 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 4226 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 4227 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 4228 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4229 | /* It should now be out of any other write domains, and we can update |
| 4230 | * the domain values for our changes. |
| 4231 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4232 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4233 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4234 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4235 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4236 | err_unpin_global: |
| 4237 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4238 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4242 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4243 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 4244 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4245 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4246 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 4247 | return; |
| 4248 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4249 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 4250 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 4251 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4252 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 4253 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4254 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4255 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4256 | } |
| 4257 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4258 | /** |
| 4259 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 4260 | * @obj: object to act on |
| 4261 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4262 | * |
| 4263 | * This function returns when the move is complete, including waiting on |
| 4264 | * flushes to occur. |
| 4265 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 4266 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 4267 | 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] | 4268 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4269 | int ret; |
| 4270 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4271 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4272 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4273 | ret = i915_gem_object_wait(obj, |
| 4274 | I915_WAIT_INTERRUPTIBLE | |
| 4275 | I915_WAIT_LOCKED | |
| 4276 | (write ? I915_WAIT_ALL : 0), |
| 4277 | MAX_SCHEDULE_TIMEOUT, |
| 4278 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 4279 | if (ret) |
| 4280 | return ret; |
| 4281 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 4282 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4283 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4284 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4285 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4286 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4287 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | /* It should now be out of any other write domains, and we can update |
| 4291 | * the domain values for our changes. |
| 4292 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4293 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4294 | |
| 4295 | /* If we're writing through the CPU, then the GPU read domains will |
| 4296 | * need to be invalidated at next use. |
| 4297 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4298 | if (write) |
| 4299 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 4300 | |
| 4301 | return 0; |
| 4302 | } |
| 4303 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4304 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 4305 | * emitted over 20 msec ago. |
| 4306 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4307 | * Note that if we were to use the current jiffies each time around the loop, |
| 4308 | * we wouldn't escape the function with any frames outstanding if the time to |
| 4309 | * render a frame was over 20ms. |
| 4310 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4311 | * This should get us reasonable parallelism between CPU and GPU but also |
| 4312 | * relatively low latency when blocking on a particular request to finish. |
| 4313 | */ |
| 4314 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4315 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4316 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4317 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4318 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 4319 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4320 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4321 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4322 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 4323 | /* ABI: return -EIO if already wedged */ |
| 4324 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 4325 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 4326 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4327 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4328 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4329 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 4330 | break; |
| 4331 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4332 | if (target) { |
| 4333 | list_del(&target->client_link); |
| 4334 | target->file_priv = NULL; |
| 4335 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 4336 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4337 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4338 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4339 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4340 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4341 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4342 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4343 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4344 | return 0; |
| 4345 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4346 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4347 | I915_WAIT_INTERRUPTIBLE, |
| 4348 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4349 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4350 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4351 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4352 | } |
| 4353 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4354 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4355 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 4356 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 4357 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 4358 | u64 alignment, |
| 4359 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4360 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4361 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 4362 | struct i915_address_space *vm = &dev_priv->ggtt.base; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4363 | struct i915_vma *vma; |
| 4364 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 4365 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4366 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4367 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 4368 | if (flags & PIN_MAPPABLE && |
| 4369 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4370 | /* If the required space is larger than the available |
| 4371 | * aperture, we will not able to find a slot for the |
| 4372 | * object and unbinding the object now will be in |
| 4373 | * vain. Worse, doing so may cause us to ping-pong |
| 4374 | * the object in and out of the Global GTT and |
| 4375 | * waste a lot of cycles under the mutex. |
| 4376 | */ |
| 4377 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 4378 | return ERR_PTR(-E2BIG); |
| 4379 | |
| 4380 | /* If NONBLOCK is set the caller is optimistically |
| 4381 | * trying to cache the full object within the mappable |
| 4382 | * aperture, and *must* have a fallback in place for |
| 4383 | * situations where we cannot bind the object. We |
| 4384 | * can be a little more lax here and use the fallback |
| 4385 | * more often to avoid costly migrations of ourselves |
| 4386 | * and other objects within the aperture. |
| 4387 | * |
| 4388 | * Half-the-aperture is used as a simple heuristic. |
| 4389 | * More interesting would to do search for a free |
| 4390 | * block prior to making the commitment to unbind. |
| 4391 | * That caters for the self-harm case, and with a |
| 4392 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 4393 | * we could try to minimise harm to others. |
| 4394 | */ |
| 4395 | if (flags & PIN_NONBLOCK && |
| 4396 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 4397 | return ERR_PTR(-ENOSPC); |
| 4398 | } |
| 4399 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 4400 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 4401 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4402 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4403 | |
| 4404 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4405 | if (flags & PIN_NONBLOCK) { |
| 4406 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 4407 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4408 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4409 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 4410 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4411 | return ERR_PTR(-ENOSPC); |
| 4412 | } |
| 4413 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4414 | WARN(i915_vma_is_pinned(vma), |
| 4415 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4416 | " offset=%08x, req.alignment=%llx," |
| 4417 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 4418 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4419 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4420 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4421 | ret = i915_vma_unbind(vma); |
| 4422 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4423 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4424 | } |
| 4425 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4426 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 4427 | if (ret) |
| 4428 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4429 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4430 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4431 | } |
| 4432 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4433 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4434 | { |
| 4435 | /* Note that we could alias engines in the execbuf API, but |
| 4436 | * that would be very unwise as it prevents userspace from |
| 4437 | * fine control over engine selection. Ahem. |
| 4438 | * |
| 4439 | * This should be something like EXEC_MAX_ENGINE instead of |
| 4440 | * I915_NUM_ENGINES. |
| 4441 | */ |
| 4442 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 4443 | return 0x10000 << id; |
| 4444 | } |
| 4445 | |
| 4446 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 4447 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 4448 | /* The uABI guarantees an active writer is also amongst the read |
| 4449 | * engines. This would be true if we accessed the activity tracking |
| 4450 | * under the lock, but as we perform the lookup of the object and |
| 4451 | * its activity locklessly we can not guarantee that the last_write |
| 4452 | * being active implies that we have set the same engine flag from |
| 4453 | * last_read - hence we always set both read and write busy for |
| 4454 | * last_write. |
| 4455 | */ |
| 4456 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4457 | } |
| 4458 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4459 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4460 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4461 | unsigned int (*flag)(unsigned int id)) |
| 4462 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4463 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4464 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4465 | /* We have to check the current hw status of the fence as the uABI |
| 4466 | * guarantees forward progress. We could rely on the idle worker |
| 4467 | * to eventually flush us, but to minimise latency just ask the |
| 4468 | * hardware. |
| 4469 | * |
| 4470 | * Note we only report on the status of native fences. |
| 4471 | */ |
| 4472 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4473 | return 0; |
| 4474 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4475 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4476 | rq = container_of(fence, struct i915_request, fence); |
| 4477 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4478 | return 0; |
| 4479 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4480 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4481 | } |
| 4482 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4483 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4484 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4485 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4486 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4487 | } |
| 4488 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4489 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4490 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4491 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4492 | if (!fence) |
| 4493 | return 0; |
| 4494 | |
| 4495 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4496 | } |
| 4497 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4498 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4499 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4500 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4501 | { |
| 4502 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4503 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4504 | struct reservation_object_list *list; |
| 4505 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4506 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4507 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4508 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4509 | rcu_read_lock(); |
| 4510 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4511 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4512 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4513 | |
| 4514 | /* A discrepancy here is that we do not report the status of |
| 4515 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4516 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4517 | * This also means that wait-ioctl may report an object as busy, |
| 4518 | * where busy-ioctl considers it idle. |
| 4519 | * |
| 4520 | * We trade the ability to warn of foreign fences to report on which |
| 4521 | * i915 engines are active for the object. |
| 4522 | * |
| 4523 | * Alternatively, we can trade that extra information on read/write |
| 4524 | * activity with |
| 4525 | * args->busy = |
| 4526 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4527 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4528 | * |
| 4529 | */ |
| 4530 | retry: |
| 4531 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4532 | |
| 4533 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4534 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4535 | |
| 4536 | /* Translate shared fences to READ set of engines */ |
| 4537 | list = rcu_dereference(obj->resv->fence); |
| 4538 | if (list) { |
| 4539 | unsigned int shared_count = list->shared_count, i; |
| 4540 | |
| 4541 | for (i = 0; i < shared_count; ++i) { |
| 4542 | struct dma_fence *fence = |
| 4543 | rcu_dereference(list->shared[i]); |
| 4544 | |
| 4545 | args->busy |= busy_check_reader(fence); |
| 4546 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4547 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4548 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4549 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4550 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4551 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4552 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4553 | out: |
| 4554 | rcu_read_unlock(); |
| 4555 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | int |
| 4559 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4560 | struct drm_file *file_priv) |
| 4561 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4562 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4563 | } |
| 4564 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4565 | int |
| 4566 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4567 | struct drm_file *file_priv) |
| 4568 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4569 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4570 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4571 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4572 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4573 | |
| 4574 | switch (args->madv) { |
| 4575 | case I915_MADV_DONTNEED: |
| 4576 | case I915_MADV_WILLNEED: |
| 4577 | break; |
| 4578 | default: |
| 4579 | return -EINVAL; |
| 4580 | } |
| 4581 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4582 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4583 | if (!obj) |
| 4584 | return -ENOENT; |
| 4585 | |
| 4586 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4587 | if (err) |
| 4588 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4589 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4590 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4591 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4592 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4593 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4594 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4595 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4596 | obj->mm.quirked = false; |
| 4597 | } |
| 4598 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4599 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4600 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4601 | obj->mm.quirked = true; |
| 4602 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4603 | } |
| 4604 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4605 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4606 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4607 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4608 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4609 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4610 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4611 | i915_gem_object_truncate(obj); |
| 4612 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4613 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4614 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4615 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4616 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4617 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4618 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4619 | } |
| 4620 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4621 | static void |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4622 | frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4623 | { |
| 4624 | struct drm_i915_gem_object *obj = |
| 4625 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4626 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4627 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4630 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4631 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4632 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4633 | mutex_init(&obj->mm.lock); |
| 4634 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4635 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4636 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4637 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4638 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4639 | obj->ops = ops; |
| 4640 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4641 | reservation_object_init(&obj->__builtin_resv); |
| 4642 | obj->resv = &obj->__builtin_resv; |
| 4643 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4644 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4645 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4646 | |
| 4647 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4648 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4649 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4650 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4651 | 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] | 4652 | } |
| 4653 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4654 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4655 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4656 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4657 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4658 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4659 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4660 | |
| 4661 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4662 | }; |
| 4663 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4664 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4665 | struct drm_gem_object *obj, |
| 4666 | size_t size) |
| 4667 | { |
| 4668 | struct drm_i915_private *i915 = to_i915(dev); |
| 4669 | unsigned long flags = VM_NORESERVE; |
| 4670 | struct file *filp; |
| 4671 | |
| 4672 | drm_gem_private_object_init(dev, obj, size); |
| 4673 | |
| 4674 | if (i915->mm.gemfs) |
| 4675 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4676 | flags); |
| 4677 | else |
| 4678 | filp = shmem_file_setup("i915", size, flags); |
| 4679 | |
| 4680 | if (IS_ERR(filp)) |
| 4681 | return PTR_ERR(filp); |
| 4682 | |
| 4683 | obj->filp = filp; |
| 4684 | |
| 4685 | return 0; |
| 4686 | } |
| 4687 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4688 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4689 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4690 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4691 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4692 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4693 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4694 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4695 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4696 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4697 | /* There is a prevalence of the assumption that we fit the object's |
| 4698 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4699 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4700 | * such a local variable, please consider fixing! |
| 4701 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4702 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4703 | return ERR_PTR(-E2BIG); |
| 4704 | |
| 4705 | if (overflows_type(size, obj->base.size)) |
| 4706 | return ERR_PTR(-E2BIG); |
| 4707 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4708 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4709 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4710 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4711 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4712 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4713 | if (ret) |
| 4714 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4715 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4716 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4717 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4718 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4719 | mask &= ~__GFP_HIGHMEM; |
| 4720 | mask |= __GFP_DMA32; |
| 4721 | } |
| 4722 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4723 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4724 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4725 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4726 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4727 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4728 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4729 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4730 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4731 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4732 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4733 | /* 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] | 4734 | * cache) for about a 10% performance improvement |
| 4735 | * compared to uncached. Graphics requests other than |
| 4736 | * display scanout are coherent with the CPU in |
| 4737 | * accessing this cache. This means in this mode we |
| 4738 | * don't need to clflush on the CPU side, and on the |
| 4739 | * GPU side we only need to flush internal caches to |
| 4740 | * get data visible to the CPU. |
| 4741 | * |
| 4742 | * However, we maintain the display planes as UC, and so |
| 4743 | * need to rebind when first used as such. |
| 4744 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4745 | cache_level = I915_CACHE_LLC; |
| 4746 | else |
| 4747 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4748 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4749 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4750 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4751 | trace_i915_gem_object_create(obj); |
| 4752 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4753 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4754 | |
| 4755 | fail: |
| 4756 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4757 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4760 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4761 | { |
| 4762 | /* If we are the last user of the backing storage (be it shmemfs |
| 4763 | * pages or stolen etc), we know that the pages are going to be |
| 4764 | * immediately released. In this case, we can then skip copying |
| 4765 | * back the contents from the GPU. |
| 4766 | */ |
| 4767 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4768 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4769 | return false; |
| 4770 | |
| 4771 | if (obj->base.filp == NULL) |
| 4772 | return true; |
| 4773 | |
| 4774 | /* At first glance, this looks racy, but then again so would be |
| 4775 | * userspace racing mmap against close. However, the first external |
| 4776 | * reference to the filp can only be obtained through the |
| 4777 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4778 | * acquiring such a reference whilst we are in the middle of |
| 4779 | * freeing the object. |
| 4780 | */ |
| 4781 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4782 | } |
| 4783 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4784 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4785 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4786 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4787 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4788 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4789 | intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4790 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4791 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4792 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4793 | trace_i915_gem_object_destroy(obj); |
| 4794 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4795 | mutex_lock(&i915->drm.struct_mutex); |
| 4796 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4797 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
| 4798 | list_for_each_entry_safe(vma, vn, |
| 4799 | &obj->vma_list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4800 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4801 | vma->flags &= ~I915_VMA_PIN_MASK; |
| 4802 | i915_vma_close(vma); |
| 4803 | } |
Chris Wilson | db6c2b4 | 2016-11-01 11:54:00 +0000 | [diff] [blame] | 4804 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
| 4805 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4806 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4807 | /* This serializes freeing with the shrinker. Since the free |
| 4808 | * is delayed, first by RCU then by the workqueue, we want the |
| 4809 | * shrinker to be able to free pages of unreferenced objects, |
| 4810 | * or else we may oom whilst there are plenty of deferred |
| 4811 | * freed objects. |
| 4812 | */ |
| 4813 | if (i915_gem_object_has_pages(obj)) { |
| 4814 | spin_lock(&i915->mm.obj_lock); |
| 4815 | list_del_init(&obj->mm.link); |
| 4816 | spin_unlock(&i915->mm.obj_lock); |
| 4817 | } |
| 4818 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4819 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4820 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4821 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4822 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4823 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4824 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4825 | |
| 4826 | if (obj->ops->release) |
| 4827 | obj->ops->release(obj); |
| 4828 | |
| 4829 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4830 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4831 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4832 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4833 | |
| 4834 | if (obj->base.import_attach) |
| 4835 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4836 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4837 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4838 | drm_gem_object_release(&obj->base); |
| 4839 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4840 | |
| 4841 | kfree(obj->bit_17); |
| 4842 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4843 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4844 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4845 | atomic_dec(&i915->mm.free_count); |
| 4846 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4847 | if (on) |
| 4848 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4849 | } |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4850 | intel_runtime_pm_put(i915); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4854 | { |
| 4855 | struct llist_node *freed; |
| 4856 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4857 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4858 | freed = NULL; |
| 4859 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4860 | /* Only one consumer of llist_del_first() allowed */ |
| 4861 | spin_lock(&i915->mm.free_lock); |
| 4862 | freed = llist_del_first(&i915->mm.free_list); |
| 4863 | spin_unlock(&i915->mm.free_lock); |
| 4864 | } |
| 4865 | if (unlikely(freed)) { |
| 4866 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4867 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4868 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | static void __i915_gem_free_work(struct work_struct *work) |
| 4872 | { |
| 4873 | struct drm_i915_private *i915 = |
| 4874 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4875 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4876 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4877 | /* |
| 4878 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4879 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4880 | * However, the object may also be bound into the global GTT (e.g. |
| 4881 | * older GPUs without per-process support, or for direct access through |
| 4882 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4883 | * unbound now. |
| 4884 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4885 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4886 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4887 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4888 | spin_unlock(&i915->mm.free_lock); |
| 4889 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4890 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4891 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4892 | return; |
| 4893 | |
| 4894 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4895 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4896 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4900 | { |
| 4901 | struct drm_i915_gem_object *obj = |
| 4902 | container_of(head, typeof(*obj), rcu); |
| 4903 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4904 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4905 | /* |
| 4906 | * Since we require blocking on struct_mutex to unbind the freed |
| 4907 | * object from the GPU before releasing resources back to the |
| 4908 | * system, we can not do that directly from the RCU callback (which may |
| 4909 | * be a softirq context), but must instead then defer that work onto a |
| 4910 | * kthread. We use the RCU callback rather than move the freed object |
| 4911 | * directly onto the work queue so that we can mix between using the |
| 4912 | * worker and performing frees directly from subsequent allocations for |
| 4913 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4914 | */ |
| 4915 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4916 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4920 | { |
| 4921 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4922 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4923 | if (obj->mm.quirked) |
| 4924 | __i915_gem_object_unpin_pages(obj); |
| 4925 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4926 | if (discard_backing_storage(obj)) |
| 4927 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4928 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4929 | /* |
| 4930 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4931 | * read-side critical sections are complete, e.g. |
| 4932 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4933 | * lookup see i915_gem_object_lookup_rcu(). |
| 4934 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4935 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4936 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4937 | } |
| 4938 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4939 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4940 | { |
| 4941 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4942 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4943 | if (!i915_gem_object_has_active_reference(obj) && |
| 4944 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4945 | i915_gem_object_set_active_reference(obj); |
| 4946 | else |
| 4947 | i915_gem_object_put(obj); |
| 4948 | } |
| 4949 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4950 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4951 | { |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4952 | struct i915_gem_context *kernel_context = i915->kernel_context; |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4953 | struct intel_engine_cs *engine; |
| 4954 | enum intel_engine_id id; |
| 4955 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4956 | for_each_engine(engine, i915, id) { |
| 4957 | GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request)); |
| 4958 | GEM_BUG_ON(engine->last_retired_context != kernel_context); |
| 4959 | } |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4960 | } |
| 4961 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4962 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4963 | { |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4964 | if (i915_terminally_wedged(&i915->gpu_error)) { |
| 4965 | mutex_lock(&i915->drm.struct_mutex); |
| 4966 | i915_gem_unset_wedged(i915); |
| 4967 | mutex_unlock(&i915->drm.struct_mutex); |
| 4968 | } |
| 4969 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4970 | /* |
| 4971 | * If we inherit context state from the BIOS or earlier occupants |
| 4972 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4973 | * try to take over. The only way to remove the earlier state |
| 4974 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4975 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4976 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4977 | */ |
Daniele Ceraolo Spurio | ce1599a | 2018-02-07 13:24:40 -0800 | [diff] [blame] | 4978 | if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915)) |
| 4979 | WARN_ON(intel_gpu_reset(i915, ALL_ENGINES)); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4982 | int i915_gem_suspend(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4983 | { |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4984 | struct drm_device *dev = &dev_priv->drm; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4985 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4986 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4987 | intel_runtime_pm_get(dev_priv); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4988 | intel_suspend_gt_powersave(dev_priv); |
| 4989 | |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4990 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4991 | |
| 4992 | /* We have to flush all the executing contexts to main memory so |
| 4993 | * that they can saved in the hibernation image. To ensure the last |
| 4994 | * context image is coherent, we have to switch away from it. That |
| 4995 | * leaves the dev_priv->kernel_context still active when |
| 4996 | * we actually suspend, and its image in memory may not match the GPU |
| 4997 | * state. Fortunately, the kernel_context is disposable and we do |
| 4998 | * not rely on its state. |
| 4999 | */ |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 5000 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5001 | ret = i915_gem_switch_to_kernel_context(dev_priv); |
| 5002 | if (ret) |
| 5003 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5004 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 5005 | ret = i915_gem_wait_for_idle(dev_priv, |
| 5006 | I915_WAIT_INTERRUPTIBLE | |
| 5007 | I915_WAIT_LOCKED); |
| 5008 | if (ret && ret != -EIO) |
| 5009 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 5010 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 5011 | assert_kernel_context_is_current(dev_priv); |
| 5012 | } |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5013 | i915_gem_contexts_lost(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 5014 | mutex_unlock(&dev->struct_mutex); |
| 5015 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 5016 | intel_uc_suspend(dev_priv); |
Sagar Arun Kamble | 63987bf | 2017-04-05 15:51:50 +0530 | [diff] [blame] | 5017 | |
Chris Wilson | 737b150 | 2015-01-26 18:03:03 +0200 | [diff] [blame] | 5018 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5019 | cancel_delayed_work_sync(&dev_priv->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 5020 | |
| 5021 | /* As the idle_work is rearming if it detects a race, play safe and |
| 5022 | * repeat the flush until it is definitely idle. |
| 5023 | */ |
Chris Wilson | 7c26240 | 2017-10-06 11:40:38 +0100 | [diff] [blame] | 5024 | drain_delayed_work(&dev_priv->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 5025 | |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 5026 | /* Assert that we sucessfully flushed all the work and |
| 5027 | * reset the GPU back to its idle, low power state. |
| 5028 | */ |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5029 | WARN_ON(dev_priv->gt.awake); |
Chris Wilson | fc692bd | 2017-08-26 12:09:35 +0100 | [diff] [blame] | 5030 | if (WARN_ON(!intel_engines_are_idle(dev_priv))) |
| 5031 | i915_gem_set_wedged(dev_priv); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 5032 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 5033 | /* |
| 5034 | * Neither the BIOS, ourselves or any other kernel |
| 5035 | * expects the system to be in execlists mode on startup, |
| 5036 | * so we need to reset the GPU back to legacy mode. And the only |
| 5037 | * known way to disable logical contexts is through a GPU reset. |
| 5038 | * |
| 5039 | * So in order to leave the system in a known default configuration, |
| 5040 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 5041 | * clean up the GEM state tracking, flushing off the requests and |
| 5042 | * leaving the system in a known idle state. |
| 5043 | * |
| 5044 | * Note that is of the upmost importance that the GPU is idle and |
| 5045 | * all stray writes are flushed *before* we dismantle the backing |
| 5046 | * storage for the pinned objects. |
| 5047 | * |
| 5048 | * However, since we are uncertain that resetting the GPU on older |
| 5049 | * machines is a good idea, we don't - just in case it leaves the |
| 5050 | * machine in an unusable condition. |
| 5051 | */ |
Michal Wajdeczko | c37d572 | 2018-03-12 13:03:07 +0000 | [diff] [blame] | 5052 | intel_uc_sanitize(dev_priv); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5053 | i915_gem_sanitize(dev_priv); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 5054 | |
| 5055 | intel_runtime_pm_put(dev_priv); |
| 5056 | return 0; |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 5057 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 5058 | err_unlock: |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 5059 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 5060 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 5061 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5062 | } |
| 5063 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5064 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5065 | { |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5066 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5067 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5068 | mutex_lock(&i915->drm.struct_mutex); |
| 5069 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 5070 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5071 | i915_gem_restore_gtt_mappings(i915); |
| 5072 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5073 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5074 | /* |
| 5075 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5076 | * guarantee that the context image is complete. So let's just reset |
| 5077 | * it and start again. |
| 5078 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5079 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5080 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5081 | if (i915_gem_init_hw(i915)) |
| 5082 | goto err_wedged; |
| 5083 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 5084 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 5085 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5086 | /* Always reload a context for powersaving. */ |
| 5087 | if (i915_gem_switch_to_kernel_context(i915)) |
| 5088 | goto err_wedged; |
| 5089 | |
| 5090 | out_unlock: |
| 5091 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 5092 | mutex_unlock(&i915->drm.struct_mutex); |
| 5093 | return; |
| 5094 | |
| 5095 | err_wedged: |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5096 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 5097 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 5098 | i915_gem_set_wedged(i915); |
| 5099 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5100 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5101 | } |
| 5102 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5103 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5104 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5105 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5106 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 5107 | return; |
| 5108 | |
| 5109 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 5110 | DISP_TILE_SURFACE_SWIZZLING); |
| 5111 | |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5112 | if (IS_GEN5(dev_priv)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 5113 | return; |
| 5114 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5115 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5116 | if (IS_GEN6(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5117 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5118 | else if (IS_GEN7(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5119 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5120 | else if (IS_GEN8(dev_priv)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 5121 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 5122 | else |
| 5123 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5124 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 5125 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5126 | 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] | 5127 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5128 | I915_WRITE(RING_CTL(base), 0); |
| 5129 | I915_WRITE(RING_HEAD(base), 0); |
| 5130 | I915_WRITE(RING_TAIL(base), 0); |
| 5131 | I915_WRITE(RING_START(base), 0); |
| 5132 | } |
| 5133 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5134 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5135 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5136 | if (IS_I830(dev_priv)) { |
| 5137 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5138 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5139 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5140 | init_unused_ring(dev_priv, SRB2_BASE); |
| 5141 | init_unused_ring(dev_priv, SRB3_BASE); |
| 5142 | } else if (IS_GEN2(dev_priv)) { |
| 5143 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5144 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5145 | } else if (IS_GEN3(dev_priv)) { |
| 5146 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5147 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5148 | } |
| 5149 | } |
| 5150 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5151 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5152 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5153 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5154 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5155 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5156 | int err; |
| 5157 | |
| 5158 | for_each_engine(engine, i915, id) { |
| 5159 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5160 | if (err) { |
| 5161 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 5162 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5163 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5164 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | return 0; |
| 5168 | } |
| 5169 | |
| 5170 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 5171 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 5172 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5173 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 5174 | dev_priv->gt.last_init_time = ktime_get(); |
| 5175 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5176 | /* Double layer security blanket, see i915_gem_init() */ |
| 5177 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5178 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 5179 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 5180 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5181 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 5182 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5183 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 5184 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 5185 | |
Tvrtko Ursulin | 6e26695 | 2016-10-13 11:02:53 +0100 | [diff] [blame] | 5186 | if (HAS_PCH_NOP(dev_priv)) { |
Tvrtko Ursulin | fd6b8f4 | 2016-10-14 10:13:06 +0100 | [diff] [blame] | 5187 | if (IS_IVYBRIDGE(dev_priv)) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 5188 | u32 temp = I915_READ(GEN7_MSG_CTL); |
| 5189 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); |
| 5190 | I915_WRITE(GEN7_MSG_CTL, temp); |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5191 | } else if (INTEL_GEN(dev_priv) >= 7) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 5192 | u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); |
| 5193 | temp &= ~RESET_PCH_HANDSHAKE_ENABLE; |
| 5194 | I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); |
| 5195 | } |
Ben Widawsky | 88a2b2a | 2013-04-05 13:12:43 -0700 | [diff] [blame] | 5196 | } |
| 5197 | |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 5198 | intel_gt_workarounds_apply(dev_priv); |
| 5199 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5200 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5201 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5202 | /* |
| 5203 | * At least 830 can leave some of the unused rings |
| 5204 | * "active" (ie. head != tail) after resume which |
| 5205 | * will prevent c3 entry. Makes sure all unused rings |
| 5206 | * are totally idle. |
| 5207 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5208 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5209 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 5210 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5211 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5212 | ret = -EIO; |
| 5213 | goto out; |
| 5214 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 5215 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5216 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5217 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5218 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5219 | goto out; |
| 5220 | } |
| 5221 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 5222 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 5223 | if (ret) { |
| 5224 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 5225 | goto out; |
| 5226 | } |
| 5227 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5228 | /* We can't enable contexts until all firmware is loaded */ |
| 5229 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5230 | if (ret) { |
| 5231 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5232 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5233 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5234 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5235 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 5236 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 5237 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 5238 | ret = __i915_gem_restart_engines(dev_priv); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5239 | out: |
| 5240 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5241 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5242 | } |
| 5243 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5244 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 5245 | { |
| 5246 | struct i915_gem_context *ctx; |
| 5247 | struct intel_engine_cs *engine; |
| 5248 | enum intel_engine_id id; |
| 5249 | int err; |
| 5250 | |
| 5251 | /* |
| 5252 | * As we reset the gpu during very early sanitisation, the current |
| 5253 | * register state on the GPU should reflect its defaults values. |
| 5254 | * We load a context onto the hw (with restore-inhibit), then switch |
| 5255 | * over to a second context to save that default register state. We |
| 5256 | * can then prime every new context with that state so they all start |
| 5257 | * from the same default HW values. |
| 5258 | */ |
| 5259 | |
| 5260 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 5261 | if (IS_ERR(ctx)) |
| 5262 | return PTR_ERR(ctx); |
| 5263 | |
| 5264 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5265 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5266 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5267 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5268 | if (IS_ERR(rq)) { |
| 5269 | err = PTR_ERR(rq); |
| 5270 | goto out_ctx; |
| 5271 | } |
| 5272 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 5273 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5274 | if (engine->init_context) |
| 5275 | err = engine->init_context(rq); |
| 5276 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5277 | __i915_request_add(rq, true); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5278 | if (err) |
| 5279 | goto err_active; |
| 5280 | } |
| 5281 | |
| 5282 | err = i915_gem_switch_to_kernel_context(i915); |
| 5283 | if (err) |
| 5284 | goto err_active; |
| 5285 | |
| 5286 | err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED); |
| 5287 | if (err) |
| 5288 | goto err_active; |
| 5289 | |
| 5290 | assert_kernel_context_is_current(i915); |
| 5291 | |
| 5292 | for_each_engine(engine, i915, id) { |
| 5293 | struct i915_vma *state; |
| 5294 | |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 5295 | state = to_intel_context(ctx, engine)->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5296 | if (!state) |
| 5297 | continue; |
| 5298 | |
| 5299 | /* |
| 5300 | * As we will hold a reference to the logical state, it will |
| 5301 | * not be torn down with the context, and importantly the |
| 5302 | * object will hold onto its vma (making it possible for a |
| 5303 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 5304 | * from the GTT to prevent such accidents and reclaim the |
| 5305 | * space. |
| 5306 | */ |
| 5307 | err = i915_vma_unbind(state); |
| 5308 | if (err) |
| 5309 | goto err_active; |
| 5310 | |
| 5311 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 5312 | if (err) |
| 5313 | goto err_active; |
| 5314 | |
| 5315 | engine->default_state = i915_gem_object_get(state->obj); |
| 5316 | } |
| 5317 | |
| 5318 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 5319 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 5320 | |
| 5321 | /* |
| 5322 | * Make sure that classes with multiple engine instances all |
| 5323 | * share the same basic configuration. |
| 5324 | */ |
| 5325 | for_each_engine(engine, i915, id) { |
| 5326 | unsigned int bit = BIT(engine->uabi_class); |
| 5327 | unsigned int expected = engine->default_state ? bit : 0; |
| 5328 | |
| 5329 | if ((found & bit) != expected) { |
| 5330 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 5331 | engine->uabi_class, engine->name); |
| 5332 | } |
| 5333 | } |
| 5334 | } |
| 5335 | |
| 5336 | out_ctx: |
| 5337 | i915_gem_context_set_closed(ctx); |
| 5338 | i915_gem_context_put(ctx); |
| 5339 | return err; |
| 5340 | |
| 5341 | err_active: |
| 5342 | /* |
| 5343 | * If we have to abandon now, we expect the engines to be idle |
| 5344 | * and ready to be torn-down. First try to flush any remaining |
| 5345 | * request, ensure we are pointing at the kernel context and |
| 5346 | * then remove it. |
| 5347 | */ |
| 5348 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 5349 | goto out_ctx; |
| 5350 | |
| 5351 | if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED))) |
| 5352 | goto out_ctx; |
| 5353 | |
| 5354 | i915_gem_contexts_lost(i915); |
| 5355 | goto out_ctx; |
| 5356 | } |
| 5357 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5358 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5359 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5360 | int ret; |
| 5361 | |
Matthew Auld | da9fe3f3 | 2017-10-06 23:18:31 +0100 | [diff] [blame] | 5362 | /* |
| 5363 | * We need to fallback to 4K pages since gvt gtt handling doesn't |
| 5364 | * support huge page entries - we will need to check either hypervisor |
| 5365 | * mm can support huge guest page or just do emulation in gvt. |
| 5366 | */ |
| 5367 | if (intel_vgpu_active(dev_priv)) |
| 5368 | mkwrite_device_info(dev_priv)->page_sizes = |
| 5369 | I915_GTT_PAGE_SIZE_4K; |
| 5370 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 5371 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 5372 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5373 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 5374 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5375 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5376 | } else { |
| 5377 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5378 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5379 | } |
| 5380 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5381 | ret = i915_gem_init_userptr(dev_priv); |
| 5382 | if (ret) |
| 5383 | return ret; |
| 5384 | |
Jackie Li | 6b0478f | 2018-03-13 17:32:50 -0700 | [diff] [blame] | 5385 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 5386 | if (ret) |
| 5387 | return ret; |
| 5388 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5389 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 5390 | if (ret) |
| 5391 | return ret; |
| 5392 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5393 | /* This is just a security blanket to placate dragons. |
| 5394 | * On some systems, we very sporadically observe that the first TLBs |
| 5395 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5396 | * we hold the forcewake during initialisation these problems |
| 5397 | * just magically go away. |
| 5398 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5399 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5400 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5401 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5402 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5403 | if (ret) { |
| 5404 | GEM_BUG_ON(ret == -EIO); |
| 5405 | goto err_unlock; |
| 5406 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5407 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5408 | ret = i915_gem_contexts_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5409 | if (ret) { |
| 5410 | GEM_BUG_ON(ret == -EIO); |
| 5411 | goto err_ggtt; |
| 5412 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5413 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5414 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5415 | if (ret) { |
| 5416 | GEM_BUG_ON(ret == -EIO); |
| 5417 | goto err_context; |
| 5418 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5419 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5420 | intel_init_gt_powersave(dev_priv); |
| 5421 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5422 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5423 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5424 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5425 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5426 | ret = i915_gem_init_hw(dev_priv); |
| 5427 | if (ret) |
| 5428 | goto err_uc_init; |
| 5429 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5430 | /* |
| 5431 | * Despite its name intel_init_clock_gating applies both display |
| 5432 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5433 | * GT power context workaround. Worse, sometimes it includes a context |
| 5434 | * register workaround which we need to apply before we record the |
| 5435 | * default HW state for all contexts. |
| 5436 | * |
| 5437 | * FIXME: break up the workarounds and apply them at the right time! |
| 5438 | */ |
| 5439 | intel_init_clock_gating(dev_priv); |
| 5440 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5441 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5442 | if (ret) |
| 5443 | goto err_init_hw; |
| 5444 | |
| 5445 | if (i915_inject_load_failure()) { |
| 5446 | ret = -ENODEV; |
| 5447 | goto err_init_hw; |
| 5448 | } |
| 5449 | |
| 5450 | if (i915_inject_load_failure()) { |
| 5451 | ret = -EIO; |
| 5452 | goto err_init_hw; |
| 5453 | } |
| 5454 | |
| 5455 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5456 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5457 | |
| 5458 | return 0; |
| 5459 | |
| 5460 | /* |
| 5461 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5462 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5463 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5464 | * driver doesn't explode during runtime. |
| 5465 | */ |
| 5466 | err_init_hw: |
| 5467 | i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED); |
| 5468 | i915_gem_contexts_lost(dev_priv); |
| 5469 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5470 | err_uc_init: |
| 5471 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5472 | err_pm: |
| 5473 | if (ret != -EIO) { |
| 5474 | intel_cleanup_gt_powersave(dev_priv); |
| 5475 | i915_gem_cleanup_engines(dev_priv); |
| 5476 | } |
| 5477 | err_context: |
| 5478 | if (ret != -EIO) |
| 5479 | i915_gem_contexts_fini(dev_priv); |
| 5480 | err_ggtt: |
| 5481 | err_unlock: |
| 5482 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5483 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5484 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5485 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5486 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5487 | if (ret != -EIO) |
| 5488 | i915_gem_cleanup_userptr(dev_priv); |
| 5489 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5490 | if (ret == -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5491 | /* |
| 5492 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5493 | * wedged. But we only want to do this where the GPU is angry, |
| 5494 | * for all other failure, such as an allocation failure, bail. |
| 5495 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5496 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5497 | DRM_ERROR("Failed to initialize GPU, declaring it wedged\n"); |
| 5498 | i915_gem_set_wedged(dev_priv); |
| 5499 | } |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5500 | ret = 0; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5501 | } |
| 5502 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5503 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5504 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5505 | } |
| 5506 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5507 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5508 | { |
| 5509 | i915_gem_sanitize(i915); |
| 5510 | } |
| 5511 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5512 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5513 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5514 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5515 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5516 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5517 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5518 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5519 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5520 | } |
| 5521 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5522 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5523 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5524 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5525 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5526 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5527 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5528 | !IS_CHERRYVIEW(dev_priv)) |
| 5529 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5530 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5531 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5532 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5533 | dev_priv->num_fence_regs = 16; |
| 5534 | else |
| 5535 | dev_priv->num_fence_regs = 8; |
| 5536 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5537 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5538 | dev_priv->num_fence_regs = |
| 5539 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5540 | |
| 5541 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5542 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5543 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5544 | |
| 5545 | fence->i915 = dev_priv; |
| 5546 | fence->id = i; |
| 5547 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5548 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5549 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5550 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5551 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5552 | } |
| 5553 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5554 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5555 | { |
| 5556 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5557 | spin_lock_init(&i915->mm.obj_lock); |
| 5558 | spin_lock_init(&i915->mm.free_lock); |
| 5559 | |
| 5560 | init_llist_head(&i915->mm.free_list); |
| 5561 | |
| 5562 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5563 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5564 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5565 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5566 | |
| 5567 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5568 | } |
| 5569 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5570 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5571 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5572 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5573 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5574 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5575 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5576 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5577 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5578 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5579 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5580 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5581 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5582 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5583 | if (!dev_priv->luts) |
| 5584 | goto err_vmas; |
| 5585 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5586 | dev_priv->requests = KMEM_CACHE(i915_request, |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5587 | SLAB_HWCACHE_ALIGN | |
| 5588 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5589 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5590 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5591 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5592 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5593 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5594 | SLAB_HWCACHE_ALIGN | |
| 5595 | SLAB_RECLAIM_ACCOUNT); |
| 5596 | if (!dev_priv->dependencies) |
| 5597 | goto err_requests; |
| 5598 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5599 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5600 | if (!dev_priv->priorities) |
| 5601 | goto err_dependencies; |
| 5602 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5603 | INIT_LIST_HEAD(&dev_priv->gt.timelines); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame^] | 5604 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
| 5605 | |
| 5606 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | bb89485 | 2016-11-14 20:40:57 +0000 | [diff] [blame] | 5607 | err = i915_gem_timeline_init__global(dev_priv); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5608 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5609 | if (err) |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5610 | goto err_priorities; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5611 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5612 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5613 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5614 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5615 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5616 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5617 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5618 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5619 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5620 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5621 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5622 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5623 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5624 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5625 | err = i915_gemfs_init(dev_priv); |
| 5626 | if (err) |
| 5627 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5628 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5629 | return 0; |
| 5630 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5631 | err_priorities: |
| 5632 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5633 | err_dependencies: |
| 5634 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5635 | err_requests: |
| 5636 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5637 | err_luts: |
| 5638 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5639 | err_vmas: |
| 5640 | kmem_cache_destroy(dev_priv->vmas); |
| 5641 | err_objects: |
| 5642 | kmem_cache_destroy(dev_priv->objects); |
| 5643 | err_out: |
| 5644 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5645 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5646 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5647 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5648 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5649 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5650 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5651 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5652 | WARN_ON(dev_priv->mm.object_count); |
Chris Wilson | 7d5d59e | 2016-11-01 08:48:41 +0000 | [diff] [blame] | 5653 | |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5654 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5655 | i915_gem_timeline_fini(&dev_priv->gt.global_timeline); |
| 5656 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); |
| 5657 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5658 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5659 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5660 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5661 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5662 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5663 | kmem_cache_destroy(dev_priv->vmas); |
| 5664 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5665 | |
| 5666 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5667 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5668 | |
| 5669 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5670 | } |
| 5671 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5672 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5673 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5674 | /* Discard all purgeable objects, let userspace recover those as |
| 5675 | * required after resuming. |
| 5676 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5677 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5678 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5679 | return 0; |
| 5680 | } |
| 5681 | |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5682 | int i915_gem_freeze_late(struct drm_i915_private *dev_priv) |
| 5683 | { |
| 5684 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5685 | struct list_head *phases[] = { |
| 5686 | &dev_priv->mm.unbound_list, |
| 5687 | &dev_priv->mm.bound_list, |
| 5688 | NULL |
| 5689 | }, **p; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5690 | |
| 5691 | /* Called just before we write the hibernation image. |
| 5692 | * |
| 5693 | * We need to update the domain tracking to reflect that the CPU |
| 5694 | * will be accessing all the pages to create and restore from the |
| 5695 | * hibernation, and so upon restoration those pages will be in the |
| 5696 | * CPU domain. |
| 5697 | * |
| 5698 | * To make sure the hibernation image contains the latest state, |
| 5699 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5700 | * |
| 5701 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5702 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5703 | */ |
| 5704 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 5705 | i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND); |
Chris Wilson | 17b93c4 | 2017-04-07 11:25:50 +0100 | [diff] [blame] | 5706 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5707 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5708 | spin_lock(&dev_priv->mm.obj_lock); |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5709 | for (p = phases; *p; p++) { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5710 | list_for_each_entry(obj, *p, mm.link) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 5711 | __start_cpu_write(obj); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5712 | } |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5713 | spin_unlock(&dev_priv->mm.obj_lock); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5714 | |
| 5715 | return 0; |
| 5716 | } |
| 5717 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5718 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5719 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5720 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5721 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5722 | |
| 5723 | /* Clean up our request list when the client is going away, so that |
| 5724 | * later retire_requests won't dereference our soon-to-be-gone |
| 5725 | * file_priv. |
| 5726 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5727 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5728 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5729 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5730 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5731 | } |
| 5732 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5733 | 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] | 5734 | { |
| 5735 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5736 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5737 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5738 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5739 | |
| 5740 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5741 | if (!file_priv) |
| 5742 | return -ENOMEM; |
| 5743 | |
| 5744 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5745 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5746 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5747 | |
| 5748 | spin_lock_init(&file_priv->mm.lock); |
| 5749 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5750 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5751 | file_priv->bsd_engine = -1; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5752 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5753 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5754 | if (ret) |
| 5755 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5756 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5757 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5758 | } |
| 5759 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5760 | /** |
| 5761 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5762 | * @old: current GEM buffer for the frontbuffer slots |
| 5763 | * @new: new GEM buffer for the frontbuffer slots |
| 5764 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5765 | * |
| 5766 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5767 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5768 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5769 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5770 | struct drm_i915_gem_object *new, |
| 5771 | unsigned frontbuffer_bits) |
| 5772 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5773 | /* Control of individual bits within the mask are guarded by |
| 5774 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5775 | * manipulation of individual bits. But since the bitfield as a whole |
| 5776 | * is updated using RMW, we need to use atomics in order to update |
| 5777 | * the bits. |
| 5778 | */ |
| 5779 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
| 5780 | sizeof(atomic_t) * BITS_PER_BYTE); |
| 5781 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5782 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5783 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5784 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5788 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5789 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5790 | } |
| 5791 | } |
| 5792 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5793 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5794 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5795 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5796 | const void *data, size_t size) |
| 5797 | { |
| 5798 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5799 | struct file *file; |
| 5800 | size_t offset; |
| 5801 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5802 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5803 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5804 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5805 | return obj; |
| 5806 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5807 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5808 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5809 | file = obj->base.filp; |
| 5810 | offset = 0; |
| 5811 | do { |
| 5812 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5813 | struct page *page; |
| 5814 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5815 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5816 | err = pagecache_write_begin(file, file->f_mapping, |
| 5817 | offset, len, 0, |
| 5818 | &page, &pgdata); |
| 5819 | if (err < 0) |
| 5820 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5821 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5822 | vaddr = kmap(page); |
| 5823 | memcpy(vaddr, data, len); |
| 5824 | kunmap(page); |
| 5825 | |
| 5826 | err = pagecache_write_end(file, file->f_mapping, |
| 5827 | offset, len, len, |
| 5828 | page, pgdata); |
| 5829 | if (err < 0) |
| 5830 | goto fail; |
| 5831 | |
| 5832 | size -= len; |
| 5833 | data += len; |
| 5834 | offset += len; |
| 5835 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5836 | |
| 5837 | return obj; |
| 5838 | |
| 5839 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5840 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5841 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5842 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5843 | |
| 5844 | struct scatterlist * |
| 5845 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5846 | unsigned int n, |
| 5847 | unsigned int *offset) |
| 5848 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5849 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5850 | struct scatterlist *sg; |
| 5851 | unsigned int idx, count; |
| 5852 | |
| 5853 | might_sleep(); |
| 5854 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5855 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5856 | |
| 5857 | /* As we iterate forward through the sg, we record each entry in a |
| 5858 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5859 | * this index previously, we will have an entry for it. |
| 5860 | * |
| 5861 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5862 | * sequential page access (where each new request is consecutive |
| 5863 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5864 | * i.e. O(1) with a large constant! |
| 5865 | */ |
| 5866 | if (n < READ_ONCE(iter->sg_idx)) |
| 5867 | goto lookup; |
| 5868 | |
| 5869 | mutex_lock(&iter->lock); |
| 5870 | |
| 5871 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5872 | * (or the subsequent) sg are fast - comparing against the last |
| 5873 | * sg is faster than going through the radixtree. |
| 5874 | */ |
| 5875 | |
| 5876 | sg = iter->sg_pos; |
| 5877 | idx = iter->sg_idx; |
| 5878 | count = __sg_page_count(sg); |
| 5879 | |
| 5880 | while (idx + count <= n) { |
| 5881 | unsigned long exception, i; |
| 5882 | int ret; |
| 5883 | |
| 5884 | /* If we cannot allocate and insert this entry, or the |
| 5885 | * individual pages from this range, cancel updating the |
| 5886 | * sg_idx so that on this lookup we are forced to linearly |
| 5887 | * scan onwards, but on future lookups we will try the |
| 5888 | * insertion again (in which case we need to be careful of |
| 5889 | * the error return reporting that we have already inserted |
| 5890 | * this index). |
| 5891 | */ |
| 5892 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5893 | if (ret && ret != -EEXIST) |
| 5894 | goto scan; |
| 5895 | |
| 5896 | exception = |
| 5897 | RADIX_TREE_EXCEPTIONAL_ENTRY | |
| 5898 | idx << RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5899 | for (i = 1; i < count; i++) { |
| 5900 | ret = radix_tree_insert(&iter->radix, idx + i, |
| 5901 | (void *)exception); |
| 5902 | if (ret && ret != -EEXIST) |
| 5903 | goto scan; |
| 5904 | } |
| 5905 | |
| 5906 | idx += count; |
| 5907 | sg = ____sg_next(sg); |
| 5908 | count = __sg_page_count(sg); |
| 5909 | } |
| 5910 | |
| 5911 | scan: |
| 5912 | iter->sg_pos = sg; |
| 5913 | iter->sg_idx = idx; |
| 5914 | |
| 5915 | mutex_unlock(&iter->lock); |
| 5916 | |
| 5917 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5918 | goto lookup; |
| 5919 | |
| 5920 | /* In case we failed to insert the entry into the radixtree, we need |
| 5921 | * to look beyond the current sg. |
| 5922 | */ |
| 5923 | while (idx + count <= n) { |
| 5924 | idx += count; |
| 5925 | sg = ____sg_next(sg); |
| 5926 | count = __sg_page_count(sg); |
| 5927 | } |
| 5928 | |
| 5929 | *offset = n - idx; |
| 5930 | return sg; |
| 5931 | |
| 5932 | lookup: |
| 5933 | rcu_read_lock(); |
| 5934 | |
| 5935 | sg = radix_tree_lookup(&iter->radix, n); |
| 5936 | GEM_BUG_ON(!sg); |
| 5937 | |
| 5938 | /* If this index is in the middle of multi-page sg entry, |
| 5939 | * the radixtree will contain an exceptional entry that points |
| 5940 | * to the start of that range. We will return the pointer to |
| 5941 | * the base page and the offset of this page within the |
| 5942 | * sg entry's range. |
| 5943 | */ |
| 5944 | *offset = 0; |
| 5945 | if (unlikely(radix_tree_exception(sg))) { |
| 5946 | unsigned long base = |
| 5947 | (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5948 | |
| 5949 | sg = radix_tree_lookup(&iter->radix, base); |
| 5950 | GEM_BUG_ON(!sg); |
| 5951 | |
| 5952 | *offset = n - base; |
| 5953 | } |
| 5954 | |
| 5955 | rcu_read_unlock(); |
| 5956 | |
| 5957 | return sg; |
| 5958 | } |
| 5959 | |
| 5960 | struct page * |
| 5961 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5962 | { |
| 5963 | struct scatterlist *sg; |
| 5964 | unsigned int offset; |
| 5965 | |
| 5966 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5967 | |
| 5968 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5969 | return nth_page(sg_page(sg), offset); |
| 5970 | } |
| 5971 | |
| 5972 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5973 | struct page * |
| 5974 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5975 | unsigned int n) |
| 5976 | { |
| 5977 | struct page *page; |
| 5978 | |
| 5979 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5980 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5981 | set_page_dirty(page); |
| 5982 | |
| 5983 | return page; |
| 5984 | } |
| 5985 | |
| 5986 | dma_addr_t |
| 5987 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5988 | unsigned long n) |
| 5989 | { |
| 5990 | struct scatterlist *sg; |
| 5991 | unsigned int offset; |
| 5992 | |
| 5993 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5994 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5995 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5996 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5997 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5998 | { |
| 5999 | struct sg_table *pages; |
| 6000 | int err; |
| 6001 | |
| 6002 | if (align > obj->base.size) |
| 6003 | return -EINVAL; |
| 6004 | |
| 6005 | if (obj->ops == &i915_gem_phys_ops) |
| 6006 | return 0; |
| 6007 | |
| 6008 | if (obj->ops != &i915_gem_object_ops) |
| 6009 | return -EINVAL; |
| 6010 | |
| 6011 | err = i915_gem_object_unbind(obj); |
| 6012 | if (err) |
| 6013 | return err; |
| 6014 | |
| 6015 | mutex_lock(&obj->mm.lock); |
| 6016 | |
| 6017 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 6018 | err = -EFAULT; |
| 6019 | goto err_unlock; |
| 6020 | } |
| 6021 | |
| 6022 | if (obj->mm.quirked) { |
| 6023 | err = -EFAULT; |
| 6024 | goto err_unlock; |
| 6025 | } |
| 6026 | |
| 6027 | if (obj->mm.mapping) { |
| 6028 | err = -EBUSY; |
| 6029 | goto err_unlock; |
| 6030 | } |
| 6031 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 6032 | pages = fetch_and_zero(&obj->mm.pages); |
| 6033 | if (pages) { |
| 6034 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 6035 | |
| 6036 | __i915_gem_object_reset_page_iter(obj); |
| 6037 | |
| 6038 | spin_lock(&i915->mm.obj_lock); |
| 6039 | list_del(&obj->mm.link); |
| 6040 | spin_unlock(&i915->mm.obj_lock); |
| 6041 | } |
| 6042 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6043 | obj->ops = &i915_gem_phys_ops; |
| 6044 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 6045 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6046 | if (err) |
| 6047 | goto err_xfer; |
| 6048 | |
| 6049 | /* Perma-pin (until release) the physical set of pages */ |
| 6050 | __i915_gem_object_pin_pages(obj); |
| 6051 | |
| 6052 | if (!IS_ERR_OR_NULL(pages)) |
| 6053 | i915_gem_object_ops.put_pages(obj, pages); |
| 6054 | mutex_unlock(&obj->mm.lock); |
| 6055 | return 0; |
| 6056 | |
| 6057 | err_xfer: |
| 6058 | obj->ops = &i915_gem_object_ops; |
| 6059 | obj->mm.pages = pages; |
| 6060 | err_unlock: |
| 6061 | mutex_unlock(&obj->mm.lock); |
| 6062 | return err; |
| 6063 | } |
| 6064 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6065 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 6066 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 6067 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 6068 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 6069 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 6070 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 6071 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6072 | #endif |