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); |
| 144 | |
| 145 | if (!i915->gt.awake) |
| 146 | return I915_EPOCH_INVALID; |
| 147 | |
| 148 | GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); |
| 149 | |
| 150 | /* |
| 151 | * Be paranoid and flush a concurrent interrupt to make sure |
| 152 | * we don't reactivate any irq tasklets after parking. |
| 153 | * |
| 154 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 155 | * there may still be an in-flight interrupt even though the CSB |
| 156 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 157 | * is completed before we continue, but it doesn't prevent the HW from |
| 158 | * raising a spurious interrupt later. To complete the shield we should |
| 159 | * coordinate disabling the CS irq with flushing the interrupts. |
| 160 | */ |
| 161 | synchronize_irq(i915->drm.irq); |
| 162 | |
| 163 | intel_engines_park(i915); |
| 164 | i915_gem_timelines_park(i915); |
| 165 | |
| 166 | i915_pmu_gt_parked(i915); |
| 167 | |
| 168 | i915->gt.awake = false; |
| 169 | |
| 170 | if (INTEL_GEN(i915) >= 6) |
| 171 | gen6_rps_idle(i915); |
| 172 | |
| 173 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ); |
| 174 | |
| 175 | intel_runtime_pm_put(i915); |
| 176 | |
| 177 | return i915->gt.epoch; |
| 178 | } |
| 179 | |
| 180 | void i915_gem_park(struct drm_i915_private *i915) |
| 181 | { |
| 182 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 183 | GEM_BUG_ON(i915->gt.active_requests); |
| 184 | |
| 185 | if (!i915->gt.awake) |
| 186 | return; |
| 187 | |
| 188 | /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ |
| 189 | mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); |
| 190 | } |
| 191 | |
| 192 | void i915_gem_unpark(struct drm_i915_private *i915) |
| 193 | { |
| 194 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 195 | GEM_BUG_ON(!i915->gt.active_requests); |
| 196 | |
| 197 | if (i915->gt.awake) |
| 198 | return; |
| 199 | |
| 200 | intel_runtime_pm_get_noresume(i915); |
| 201 | |
| 202 | /* |
| 203 | * It seems that the DMC likes to transition between the DC states a lot |
| 204 | * when there are no connected displays (no active power domains) during |
| 205 | * command submission. |
| 206 | * |
| 207 | * This activity has negative impact on the performance of the chip with |
| 208 | * huge latencies observed in the interrupt handler and elsewhere. |
| 209 | * |
| 210 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 211 | * GT activity, preventing any DC state transitions. |
| 212 | */ |
| 213 | intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 214 | |
| 215 | i915->gt.awake = true; |
| 216 | if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ |
| 217 | i915->gt.epoch = 1; |
| 218 | |
| 219 | intel_enable_gt_powersave(i915); |
| 220 | i915_update_gfx_val(i915); |
| 221 | if (INTEL_GEN(i915) >= 6) |
| 222 | gen6_rps_busy(i915); |
| 223 | i915_pmu_gt_unparked(i915); |
| 224 | |
| 225 | intel_engines_unpark(i915); |
| 226 | |
| 227 | i915_queue_hangcheck(i915); |
| 228 | |
| 229 | queue_delayed_work(i915->wq, |
| 230 | &i915->gt.retire_work, |
| 231 | round_jiffies_up_relative(HZ)); |
| 232 | } |
| 233 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 234 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 235 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 236 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 237 | { |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 238 | struct drm_i915_private *dev_priv = to_i915(dev); |
Joonas Lahtinen | 62106b4 | 2016-03-18 10:42:57 +0200 | [diff] [blame] | 239 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 240 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 241 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 242 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 243 | |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 244 | pinned = ggtt->base.reserved; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 245 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 246 | list_for_each_entry(vma, &ggtt->base.active_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 247 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 248 | pinned += vma->node.size; |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 249 | list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 250 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 251 | pinned += vma->node.size; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 252 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 253 | |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 254 | args->aper_size = ggtt->base.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 255 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 256 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 260 | 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] | 261 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 262 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 263 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 264 | struct sg_table *st; |
| 265 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 266 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 267 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 268 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 269 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 270 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 271 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 272 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 273 | /* Always aligning to the object size, allows a single allocation |
| 274 | * to handle all possible callers, and given typical object sizes, |
| 275 | * the alignment of the buddy allocation will naturally match. |
| 276 | */ |
| 277 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 278 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 279 | roundup_pow_of_two(obj->base.size)); |
| 280 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 281 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 282 | |
| 283 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 284 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 285 | struct page *page; |
| 286 | char *src; |
| 287 | |
| 288 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 289 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 290 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 291 | goto err_phys; |
| 292 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 293 | |
| 294 | src = kmap_atomic(page); |
| 295 | memcpy(vaddr, src, PAGE_SIZE); |
| 296 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 297 | kunmap_atomic(src); |
| 298 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 299 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 300 | vaddr += PAGE_SIZE; |
| 301 | } |
| 302 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 303 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 304 | |
| 305 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 306 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 307 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 308 | goto err_phys; |
| 309 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 310 | |
| 311 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 312 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 313 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 314 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | sg = st->sgl; |
| 318 | sg->offset = 0; |
| 319 | sg->length = obj->base.size; |
| 320 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 321 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 322 | sg_dma_len(sg) = obj->base.size; |
| 323 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 324 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 325 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 326 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 327 | |
| 328 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 329 | |
| 330 | err_phys: |
| 331 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 332 | |
| 333 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 336 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 337 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 338 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 339 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 340 | if (cpu_write_needs_clflush(obj)) |
| 341 | obj->cache_dirty = true; |
| 342 | } |
| 343 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 344 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 345 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 346 | struct sg_table *pages, |
| 347 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 348 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 349 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 350 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 351 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 352 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 353 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 354 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 355 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 356 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 357 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 358 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 359 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void |
| 363 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 364 | struct sg_table *pages) |
| 365 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 366 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 367 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 368 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 369 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 370 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 371 | int i; |
| 372 | |
| 373 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 374 | struct page *page; |
| 375 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 376 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 377 | page = shmem_read_mapping_page(mapping, i); |
| 378 | if (IS_ERR(page)) |
| 379 | continue; |
| 380 | |
| 381 | dst = kmap_atomic(page); |
| 382 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 383 | memcpy(dst, vaddr, PAGE_SIZE); |
| 384 | kunmap_atomic(dst); |
| 385 | |
| 386 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 387 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 388 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 389 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 390 | vaddr += PAGE_SIZE; |
| 391 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 392 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 395 | sg_free_table(pages); |
| 396 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 397 | |
| 398 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void |
| 402 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 403 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 404 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 408 | .get_pages = i915_gem_object_get_pages_phys, |
| 409 | .put_pages = i915_gem_object_put_pages_phys, |
| 410 | .release = i915_gem_object_release_phys, |
| 411 | }; |
| 412 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 413 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 414 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 415 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 416 | { |
| 417 | struct i915_vma *vma; |
| 418 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 419 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 420 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 421 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 422 | |
| 423 | /* Closed vma are removed from the obj->vma_list - but they may |
| 424 | * still have an active binding on the object. To remove those we |
| 425 | * must wait for all rendering to complete to the object (as unbinding |
| 426 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 427 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 428 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 429 | if (ret) |
| 430 | return ret; |
| 431 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 432 | while ((vma = list_first_entry_or_null(&obj->vma_list, |
| 433 | struct i915_vma, |
| 434 | obj_link))) { |
| 435 | list_move_tail(&vma->obj_link, &still_in_list); |
| 436 | ret = i915_vma_unbind(vma); |
| 437 | if (ret) |
| 438 | break; |
| 439 | } |
| 440 | list_splice(&still_in_list, &obj->vma_list); |
| 441 | |
| 442 | return ret; |
| 443 | } |
| 444 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 445 | static long |
| 446 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 447 | unsigned int flags, |
| 448 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 449 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 450 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 451 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 452 | |
| 453 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 454 | |
| 455 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 456 | return timeout; |
| 457 | |
| 458 | if (!dma_fence_is_i915(fence)) |
| 459 | return dma_fence_wait_timeout(fence, |
| 460 | flags & I915_WAIT_INTERRUPTIBLE, |
| 461 | timeout); |
| 462 | |
| 463 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 464 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 465 | goto out; |
| 466 | |
Chris Wilson | e9af4ea | 2018-01-18 13:16:09 +0000 | [diff] [blame] | 467 | /* |
| 468 | * 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] | 469 | * this is undesirable and limits the throughput of the system, as |
| 470 | * many clients cannot continue processing user input/output whilst |
| 471 | * blocked. RPS autotuning may take tens of milliseconds to respond |
| 472 | * to the GPU load and thus incurs additional latency for the client. |
| 473 | * We can circumvent that by promoting the GPU frequency to maximum |
| 474 | * before we wait. This makes the GPU throttle up much more quickly |
| 475 | * (good for benchmarks and user experience, e.g. window animations), |
| 476 | * but at a cost of spending more power processing the workload |
| 477 | * (bad for battery). Not all clients even want their results |
| 478 | * immediately and for them we should just let the GPU select its own |
| 479 | * frequency to maximise efficiency. To prevent a single client from |
| 480 | * forcing the clocks too high for the whole system, we only allow |
| 481 | * each client to waitboost once in a busy period. |
| 482 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 483 | if (rps_client && !i915_request_started(rq)) { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 484 | if (INTEL_GEN(rq->i915) >= 6) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 485 | gen6_rps_boost(rq, rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 488 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 489 | |
| 490 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 491 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 492 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 493 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 494 | return timeout; |
| 495 | } |
| 496 | |
| 497 | static long |
| 498 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 499 | unsigned int flags, |
| 500 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 501 | struct intel_rps_client *rps_client) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 502 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 503 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 504 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 505 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 506 | |
| 507 | if (flags & I915_WAIT_ALL) { |
| 508 | struct dma_fence **shared; |
| 509 | unsigned int count, i; |
| 510 | int ret; |
| 511 | |
| 512 | ret = reservation_object_get_fences_rcu(resv, |
| 513 | &excl, &count, &shared); |
| 514 | if (ret) |
| 515 | return ret; |
| 516 | |
| 517 | for (i = 0; i < count; i++) { |
| 518 | timeout = i915_gem_object_wait_fence(shared[i], |
| 519 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 520 | rps_client); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 521 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 522 | break; |
| 523 | |
| 524 | dma_fence_put(shared[i]); |
| 525 | } |
| 526 | |
| 527 | for (; i < count; i++) |
| 528 | dma_fence_put(shared[i]); |
| 529 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 530 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 531 | /* |
| 532 | * If both shared fences and an exclusive fence exist, |
| 533 | * then by construction the shared fences must be later |
| 534 | * than the exclusive fence. If we successfully wait for |
| 535 | * all the shared fences, we know that the exclusive fence |
| 536 | * must all be signaled. If all the shared fences are |
| 537 | * signaled, we can prune the array and recover the |
| 538 | * floating references on the fences/requests. |
| 539 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 540 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 541 | } else { |
| 542 | excl = reservation_object_get_excl_rcu(resv); |
| 543 | } |
| 544 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 545 | if (excl && timeout >= 0) |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 546 | timeout = i915_gem_object_wait_fence(excl, flags, timeout, |
| 547 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 548 | |
| 549 | dma_fence_put(excl); |
| 550 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 551 | /* |
| 552 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 553 | * signaled and that the reservation object has not been changed (i.e. |
| 554 | * no new fences have been added). |
| 555 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 556 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 557 | if (reservation_object_trylock(resv)) { |
| 558 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 559 | reservation_object_add_excl_fence(resv, NULL); |
| 560 | reservation_object_unlock(resv); |
| 561 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 564 | return timeout; |
| 565 | } |
| 566 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 567 | static void __fence_set_priority(struct dma_fence *fence, |
| 568 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 569 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 570 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 571 | struct intel_engine_cs *engine; |
| 572 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 573 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 574 | return; |
| 575 | |
| 576 | rq = to_request(fence); |
| 577 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 578 | |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 579 | rcu_read_lock(); |
| 580 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 581 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 582 | rcu_read_unlock(); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 585 | static void fence_set_priority(struct dma_fence *fence, |
| 586 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 587 | { |
| 588 | /* Recurse once into a fence-array */ |
| 589 | if (dma_fence_is_array(fence)) { |
| 590 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 591 | int i; |
| 592 | |
| 593 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 594 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 595 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 596 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | int |
| 601 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 602 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 603 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 604 | { |
| 605 | struct dma_fence *excl; |
| 606 | |
| 607 | if (flags & I915_WAIT_ALL) { |
| 608 | struct dma_fence **shared; |
| 609 | unsigned int count, i; |
| 610 | int ret; |
| 611 | |
| 612 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 613 | &excl, &count, &shared); |
| 614 | if (ret) |
| 615 | return ret; |
| 616 | |
| 617 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 618 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 619 | dma_fence_put(shared[i]); |
| 620 | } |
| 621 | |
| 622 | kfree(shared); |
| 623 | } else { |
| 624 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 625 | } |
| 626 | |
| 627 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 628 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 629 | dma_fence_put(excl); |
| 630 | } |
| 631 | return 0; |
| 632 | } |
| 633 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 634 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 635 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 636 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 637 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 638 | * @timeout: how long to wait |
Chris Wilson | a0a8b1c | 2017-11-09 14:06:44 +0000 | [diff] [blame] | 639 | * @rps_client: client (user process) to charge for any waitboosting |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 640 | */ |
| 641 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 642 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 643 | unsigned int flags, |
| 644 | long timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 645 | struct intel_rps_client *rps_client) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 646 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 647 | might_sleep(); |
| 648 | #if IS_ENABLED(CONFIG_LOCKDEP) |
| 649 | GEM_BUG_ON(debug_locks && |
| 650 | !!lockdep_is_held(&obj->base.dev->struct_mutex) != |
| 651 | !!(flags & I915_WAIT_LOCKED)); |
| 652 | #endif |
| 653 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 654 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 655 | timeout = i915_gem_object_wait_reservation(obj->resv, |
| 656 | flags, timeout, |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 657 | rps_client); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 658 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static struct intel_rps_client *to_rps_client(struct drm_file *file) |
| 662 | { |
| 663 | struct drm_i915_file_private *fpriv = file->driver_priv; |
| 664 | |
Sagar Arun Kamble | 562d9ba | 2017-10-10 22:30:06 +0100 | [diff] [blame] | 665 | return &fpriv->rps_client; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 668 | static int |
| 669 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 670 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 671 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 672 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 673 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 674 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 675 | |
| 676 | /* We manually control the domain here and pretend that it |
| 677 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 678 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 679 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 680 | if (copy_from_user(vaddr, user_data, args->size)) |
| 681 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 682 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 683 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 684 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 685 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 686 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 687 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 688 | } |
| 689 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 690 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 691 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 692 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 696 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 697 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 698 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 701 | static int |
| 702 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 703 | struct drm_i915_private *dev_priv, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 704 | uint64_t size, |
| 705 | uint32_t *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 706 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 707 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 708 | int ret; |
| 709 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 710 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 711 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 712 | if (size == 0) |
| 713 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 714 | |
| 715 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 716 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 717 | if (IS_ERR(obj)) |
| 718 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 719 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 720 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 721 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 722 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 723 | if (ret) |
| 724 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 725 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 726 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 727 | return 0; |
| 728 | } |
| 729 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 730 | int |
| 731 | i915_gem_dumb_create(struct drm_file *file, |
| 732 | struct drm_device *dev, |
| 733 | struct drm_mode_create_dumb *args) |
| 734 | { |
| 735 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 736 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 737 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 738 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 739 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 740 | } |
| 741 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 742 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 743 | { |
| 744 | return !(obj->cache_level == I915_CACHE_NONE || |
| 745 | obj->cache_level == I915_CACHE_WT); |
| 746 | } |
| 747 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 748 | /** |
| 749 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 750 | * @dev: drm device pointer |
| 751 | * @data: ioctl data blob |
| 752 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 753 | */ |
| 754 | int |
| 755 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 756 | struct drm_file *file) |
| 757 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 758 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 759 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 760 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 761 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 762 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 763 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 764 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 765 | } |
| 766 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 767 | static inline enum fb_op_origin |
| 768 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 769 | { |
| 770 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 771 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 772 | } |
| 773 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 774 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 775 | { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 776 | /* |
| 777 | * No actual flushing is required for the GTT write domain for reads |
| 778 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 779 | * as far as we know, so there's no chipset flush. It also doesn't |
| 780 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 781 | * |
| 782 | * However, we do have to enforce the order so that all writes through |
| 783 | * the GTT land before any writes to the device, such as updates to |
| 784 | * the GATT itself. |
| 785 | * |
| 786 | * We also have to wait a bit for the writes to land from the GTT. |
| 787 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 788 | * timing. This issue has only been observed when switching quickly |
| 789 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 790 | * 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] | 791 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 792 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 793 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 794 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 795 | wmb(); |
| 796 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 797 | intel_runtime_pm_get(dev_priv); |
| 798 | spin_lock_irq(&dev_priv->uncore.lock); |
| 799 | |
| 800 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
| 801 | |
| 802 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 803 | intel_runtime_pm_put(dev_priv); |
| 804 | } |
| 805 | |
| 806 | static void |
| 807 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 808 | { |
| 809 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 810 | struct i915_vma *vma; |
| 811 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 812 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 813 | return; |
| 814 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 815 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 816 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 817 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 818 | |
| 819 | intel_fb_obj_flush(obj, |
| 820 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 821 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 822 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 823 | if (vma->iomap) |
| 824 | continue; |
| 825 | |
| 826 | i915_vma_unset_ggtt_write(vma); |
| 827 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 828 | break; |
| 829 | |
| 830 | case I915_GEM_DOMAIN_CPU: |
| 831 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 832 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 833 | |
| 834 | case I915_GEM_DOMAIN_RENDER: |
| 835 | if (gpu_write_needs_clflush(obj)) |
| 836 | obj->cache_dirty = true; |
| 837 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 838 | } |
| 839 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 840 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 843 | static inline int |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 844 | __copy_to_user_swizzled(char __user *cpu_vaddr, |
| 845 | const char *gpu_vaddr, int gpu_offset, |
| 846 | int length) |
| 847 | { |
| 848 | int ret, cpu_offset = 0; |
| 849 | |
| 850 | while (length > 0) { |
| 851 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 852 | int this_length = min(cacheline_end - gpu_offset, length); |
| 853 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 854 | |
| 855 | ret = __copy_to_user(cpu_vaddr + cpu_offset, |
| 856 | gpu_vaddr + swizzled_gpu_offset, |
| 857 | this_length); |
| 858 | if (ret) |
| 859 | return ret + length; |
| 860 | |
| 861 | cpu_offset += this_length; |
| 862 | gpu_offset += this_length; |
| 863 | length -= this_length; |
| 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static inline int |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 870 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
| 871 | const char __user *cpu_vaddr, |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 872 | int length) |
| 873 | { |
| 874 | int ret, cpu_offset = 0; |
| 875 | |
| 876 | while (length > 0) { |
| 877 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 878 | int this_length = min(cacheline_end - gpu_offset, length); |
| 879 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 880 | |
| 881 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, |
| 882 | cpu_vaddr + cpu_offset, |
| 883 | this_length); |
| 884 | if (ret) |
| 885 | return ret + length; |
| 886 | |
| 887 | cpu_offset += this_length; |
| 888 | gpu_offset += this_length; |
| 889 | length -= this_length; |
| 890 | } |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 895 | /* |
| 896 | * Pins the specified object's pages and synchronizes the object with |
| 897 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 898 | * flush the object from the CPU cache. |
| 899 | */ |
| 900 | 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] | 901 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 902 | { |
| 903 | int ret; |
| 904 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 905 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 906 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 907 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 908 | if (!i915_gem_object_has_struct_page(obj)) |
| 909 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 910 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 911 | ret = i915_gem_object_wait(obj, |
| 912 | I915_WAIT_INTERRUPTIBLE | |
| 913 | I915_WAIT_LOCKED, |
| 914 | MAX_SCHEDULE_TIMEOUT, |
| 915 | NULL); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 916 | if (ret) |
| 917 | return ret; |
| 918 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 919 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 920 | if (ret) |
| 921 | return ret; |
| 922 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 923 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 924 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 925 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 926 | if (ret) |
| 927 | goto err_unpin; |
| 928 | else |
| 929 | goto out; |
| 930 | } |
| 931 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 932 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 933 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 934 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 935 | * read domain and manually flush cachelines (if required). This |
| 936 | * optimizes for the case when the gpu will dirty the data |
| 937 | * anyway again before the next pread happens. |
| 938 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 939 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 940 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 941 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 942 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 943 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 944 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 945 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 946 | |
| 947 | err_unpin: |
| 948 | i915_gem_object_unpin_pages(obj); |
| 949 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 953 | unsigned int *needs_clflush) |
| 954 | { |
| 955 | int ret; |
| 956 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 957 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 958 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 959 | *needs_clflush = 0; |
| 960 | if (!i915_gem_object_has_struct_page(obj)) |
| 961 | return -ENODEV; |
| 962 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 963 | ret = i915_gem_object_wait(obj, |
| 964 | I915_WAIT_INTERRUPTIBLE | |
| 965 | I915_WAIT_LOCKED | |
| 966 | I915_WAIT_ALL, |
| 967 | MAX_SCHEDULE_TIMEOUT, |
| 968 | NULL); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 969 | if (ret) |
| 970 | return ret; |
| 971 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 972 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 973 | if (ret) |
| 974 | return ret; |
| 975 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 976 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 977 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 978 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 979 | if (ret) |
| 980 | goto err_unpin; |
| 981 | else |
| 982 | goto out; |
| 983 | } |
| 984 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 985 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 986 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 987 | /* If we're not in the cpu write domain, set ourself into the |
| 988 | * gtt write domain and manually flush cachelines (as required). |
| 989 | * This optimizes for the case when the gpu will use the data |
| 990 | * right away and we therefore have to clflush anyway. |
| 991 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 992 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 993 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 994 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 995 | /* |
| 996 | * Same trick applies to invalidate partially written |
| 997 | * cachelines read before writing. |
| 998 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 999 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 1000 | *needs_clflush |= CLFLUSH_BEFORE; |
| 1001 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1002 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 1003 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1004 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 1005 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 1006 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1007 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 1008 | |
| 1009 | err_unpin: |
| 1010 | i915_gem_object_unpin_pages(obj); |
| 1011 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1014 | static void |
| 1015 | shmem_clflush_swizzled_range(char *addr, unsigned long length, |
| 1016 | bool swizzled) |
| 1017 | { |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 1018 | if (unlikely(swizzled)) { |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1019 | unsigned long start = (unsigned long) addr; |
| 1020 | unsigned long end = (unsigned long) addr + length; |
| 1021 | |
| 1022 | /* For swizzling simply ensure that we always flush both |
| 1023 | * channels. Lame, but simple and it works. Swizzled |
| 1024 | * pwrite/pread is far from a hotpath - current userspace |
| 1025 | * doesn't use it at all. */ |
| 1026 | start = round_down(start, 128); |
| 1027 | end = round_up(end, 128); |
| 1028 | |
| 1029 | drm_clflush_virt_range((void *)start, end - start); |
| 1030 | } else { |
| 1031 | drm_clflush_virt_range(addr, length); |
| 1032 | } |
| 1033 | |
| 1034 | } |
| 1035 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1036 | /* Only difference to the fast-path function is that this can handle bit17 |
| 1037 | * and uses non-atomic copy and kmap functions. */ |
| 1038 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1039 | shmem_pread_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1040 | char __user *user_data, |
| 1041 | bool page_do_bit17_swizzling, bool needs_clflush) |
| 1042 | { |
| 1043 | char *vaddr; |
| 1044 | int ret; |
| 1045 | |
| 1046 | vaddr = kmap(page); |
| 1047 | if (needs_clflush) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1048 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1049 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1050 | |
| 1051 | if (page_do_bit17_swizzling) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1052 | ret = __copy_to_user_swizzled(user_data, vaddr, offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1053 | else |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1054 | ret = __copy_to_user(user_data, vaddr + offset, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1055 | kunmap(page); |
| 1056 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1057 | return ret ? - EFAULT : 0; |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1060 | static int |
| 1061 | shmem_pread(struct page *page, int offset, int length, char __user *user_data, |
| 1062 | bool page_do_bit17_swizzling, bool needs_clflush) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1063 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1064 | int ret; |
| 1065 | |
| 1066 | ret = -ENODEV; |
| 1067 | if (!page_do_bit17_swizzling) { |
| 1068 | char *vaddr = kmap_atomic(page); |
| 1069 | |
| 1070 | if (needs_clflush) |
| 1071 | drm_clflush_virt_range(vaddr + offset, length); |
| 1072 | ret = __copy_to_user_inatomic(user_data, vaddr + offset, length); |
| 1073 | kunmap_atomic(vaddr); |
| 1074 | } |
| 1075 | if (ret == 0) |
| 1076 | return 0; |
| 1077 | |
| 1078 | return shmem_pread_slow(page, offset, length, user_data, |
| 1079 | page_do_bit17_swizzling, needs_clflush); |
| 1080 | } |
| 1081 | |
| 1082 | static int |
| 1083 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 1084 | struct drm_i915_gem_pread *args) |
| 1085 | { |
| 1086 | char __user *user_data; |
| 1087 | u64 remain; |
| 1088 | unsigned int obj_do_bit17_swizzling; |
| 1089 | unsigned int needs_clflush; |
| 1090 | unsigned int idx, offset; |
| 1091 | int ret; |
| 1092 | |
| 1093 | obj_do_bit17_swizzling = 0; |
| 1094 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 1095 | obj_do_bit17_swizzling = BIT(17); |
| 1096 | |
| 1097 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 1098 | if (ret) |
| 1099 | return ret; |
| 1100 | |
| 1101 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 1102 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 1103 | if (ret) |
| 1104 | return ret; |
| 1105 | |
| 1106 | remain = args->size; |
| 1107 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1108 | offset = offset_in_page(args->offset); |
| 1109 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1110 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1111 | int length; |
| 1112 | |
| 1113 | length = remain; |
| 1114 | if (offset + length > PAGE_SIZE) |
| 1115 | length = PAGE_SIZE - offset; |
| 1116 | |
| 1117 | ret = shmem_pread(page, offset, length, user_data, |
| 1118 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1119 | needs_clflush); |
| 1120 | if (ret) |
| 1121 | break; |
| 1122 | |
| 1123 | remain -= length; |
| 1124 | user_data += length; |
| 1125 | offset = 0; |
| 1126 | } |
| 1127 | |
| 1128 | i915_gem_obj_finish_shmem_access(obj); |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
| 1132 | static inline bool |
| 1133 | gtt_user_read(struct io_mapping *mapping, |
| 1134 | loff_t base, int offset, |
| 1135 | char __user *user_data, int length) |
| 1136 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1137 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1138 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1139 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1140 | /* 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] | 1141 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1142 | unwritten = __copy_to_user_inatomic(user_data, |
| 1143 | (void __force *)vaddr + offset, |
| 1144 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1145 | io_mapping_unmap_atomic(vaddr); |
| 1146 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1147 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1148 | unwritten = copy_to_user(user_data, |
| 1149 | (void __force *)vaddr + offset, |
| 1150 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1151 | io_mapping_unmap(vaddr); |
| 1152 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1153 | return unwritten; |
| 1154 | } |
| 1155 | |
| 1156 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1157 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 1158 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1159 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1160 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1161 | struct i915_ggtt *ggtt = &i915->ggtt; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1162 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1163 | struct i915_vma *vma; |
| 1164 | void __user *user_data; |
| 1165 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1166 | int ret; |
| 1167 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1168 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1169 | if (ret) |
| 1170 | return ret; |
| 1171 | |
| 1172 | intel_runtime_pm_get(i915); |
| 1173 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1174 | PIN_MAPPABLE | |
| 1175 | PIN_NONFAULT | |
| 1176 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1177 | if (!IS_ERR(vma)) { |
| 1178 | node.start = i915_ggtt_offset(vma); |
| 1179 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1180 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1181 | if (ret) { |
| 1182 | i915_vma_unpin(vma); |
| 1183 | vma = ERR_PTR(ret); |
| 1184 | } |
| 1185 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1186 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1187 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1188 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1189 | goto out_unlock; |
| 1190 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1194 | if (ret) |
| 1195 | goto out_unpin; |
| 1196 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1197 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1198 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1199 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1200 | remain = args->size; |
| 1201 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1202 | |
| 1203 | while (remain > 0) { |
| 1204 | /* Operation in this page |
| 1205 | * |
| 1206 | * page_base = page offset within aperture |
| 1207 | * page_offset = offset within page |
| 1208 | * page_length = bytes to copy for this page |
| 1209 | */ |
| 1210 | u32 page_base = node.start; |
| 1211 | unsigned page_offset = offset_in_page(offset); |
| 1212 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1213 | page_length = remain < page_length ? remain : page_length; |
| 1214 | if (node.allocated) { |
| 1215 | wmb(); |
| 1216 | ggtt->base.insert_page(&ggtt->base, |
| 1217 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1218 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1219 | wmb(); |
| 1220 | } else { |
| 1221 | page_base += offset & PAGE_MASK; |
| 1222 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1223 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1224 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1225 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1226 | ret = -EFAULT; |
| 1227 | break; |
| 1228 | } |
| 1229 | |
| 1230 | remain -= page_length; |
| 1231 | user_data += page_length; |
| 1232 | offset += page_length; |
| 1233 | } |
| 1234 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1235 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1236 | out_unpin: |
| 1237 | if (node.allocated) { |
| 1238 | wmb(); |
| 1239 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1240 | node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1241 | remove_mappable_node(&node); |
| 1242 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1243 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1244 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1245 | out_unlock: |
| 1246 | intel_runtime_pm_put(i915); |
| 1247 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1248 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1249 | return ret; |
| 1250 | } |
| 1251 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1252 | /** |
| 1253 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1254 | * @dev: drm device pointer |
| 1255 | * @data: ioctl data blob |
| 1256 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1257 | * |
| 1258 | * On error, the contents of *data are undefined. |
| 1259 | */ |
| 1260 | int |
| 1261 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1262 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1263 | { |
| 1264 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1265 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1266 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1267 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1268 | if (args->size == 0) |
| 1269 | return 0; |
| 1270 | |
| 1271 | if (!access_ok(VERIFY_WRITE, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1272 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1273 | args->size)) |
| 1274 | return -EFAULT; |
| 1275 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1276 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1277 | if (!obj) |
| 1278 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1279 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1280 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1281 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1282 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1283 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1286 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1287 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1288 | ret = i915_gem_object_wait(obj, |
| 1289 | I915_WAIT_INTERRUPTIBLE, |
| 1290 | MAX_SCHEDULE_TIMEOUT, |
| 1291 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1292 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1293 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1294 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1295 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1296 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1297 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1298 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1299 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1300 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1301 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1302 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1303 | i915_gem_object_unpin_pages(obj); |
| 1304 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1305 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1306 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1309 | /* This is the fast write path which cannot handle |
| 1310 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1311 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1312 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1313 | static inline bool |
| 1314 | ggtt_write(struct io_mapping *mapping, |
| 1315 | loff_t base, int offset, |
| 1316 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1317 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1318 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1319 | unsigned long unwritten; |
| 1320 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1321 | /* 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] | 1322 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1323 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1324 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1325 | io_mapping_unmap_atomic(vaddr); |
| 1326 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1327 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1328 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1329 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1330 | io_mapping_unmap(vaddr); |
| 1331 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1332 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1333 | return unwritten; |
| 1334 | } |
| 1335 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1336 | /** |
| 1337 | * This is the fast pwrite path, where we copy the data directly from the |
| 1338 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1339 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1340 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1341 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1342 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1343 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1344 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1345 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1346 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1347 | struct i915_ggtt *ggtt = &i915->ggtt; |
| 1348 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1349 | struct i915_vma *vma; |
| 1350 | u64 remain, offset; |
| 1351 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1352 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1353 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1354 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1355 | if (ret) |
| 1356 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1357 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1358 | if (i915_gem_object_has_struct_page(obj)) { |
| 1359 | /* |
| 1360 | * Avoid waking the device up if we can fallback, as |
| 1361 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1362 | * depending on PCI sleeps and our own resume time). |
| 1363 | * This easily dwarfs any performance advantage from |
| 1364 | * using the cache bypass of indirect GGTT access. |
| 1365 | */ |
| 1366 | if (!intel_runtime_pm_get_if_in_use(i915)) { |
| 1367 | ret = -EFAULT; |
| 1368 | goto out_unlock; |
| 1369 | } |
| 1370 | } else { |
| 1371 | /* No backing pages, no fallback, we must force GGTT access */ |
| 1372 | intel_runtime_pm_get(i915); |
| 1373 | } |
| 1374 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1375 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1376 | PIN_MAPPABLE | |
| 1377 | PIN_NONFAULT | |
| 1378 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1379 | if (!IS_ERR(vma)) { |
| 1380 | node.start = i915_ggtt_offset(vma); |
| 1381 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1382 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1383 | if (ret) { |
| 1384 | i915_vma_unpin(vma); |
| 1385 | vma = ERR_PTR(ret); |
| 1386 | } |
| 1387 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1388 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1389 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1390 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1391 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1392 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1393 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1394 | |
| 1395 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1396 | if (ret) |
| 1397 | goto out_unpin; |
| 1398 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1399 | mutex_unlock(&i915->drm.struct_mutex); |
| 1400 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1401 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1402 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1403 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1404 | offset = args->offset; |
| 1405 | remain = args->size; |
| 1406 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1407 | /* Operation in this page |
| 1408 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1409 | * page_base = page offset within aperture |
| 1410 | * page_offset = offset within page |
| 1411 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1412 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1413 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1414 | unsigned int page_offset = offset_in_page(offset); |
| 1415 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1416 | page_length = remain < page_length ? remain : page_length; |
| 1417 | if (node.allocated) { |
| 1418 | wmb(); /* flush the write before we modify the GGTT */ |
| 1419 | ggtt->base.insert_page(&ggtt->base, |
| 1420 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1421 | node.start, I915_CACHE_NONE, 0); |
| 1422 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1423 | } else { |
| 1424 | page_base += offset & PAGE_MASK; |
| 1425 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1426 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1427 | * source page isn't available. Return the error and we'll |
| 1428 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1429 | * If the object is non-shmem backed, we retry again with the |
| 1430 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1431 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1432 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1433 | user_data, page_length)) { |
| 1434 | ret = -EFAULT; |
| 1435 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1436 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1437 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1438 | remain -= page_length; |
| 1439 | user_data += page_length; |
| 1440 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1441 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1442 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1443 | |
| 1444 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1445 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1446 | if (node.allocated) { |
| 1447 | wmb(); |
| 1448 | ggtt->base.clear_range(&ggtt->base, |
Michał Winiarski | 4fb84d9 | 2016-10-13 14:02:40 +0200 | [diff] [blame] | 1449 | node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1450 | remove_mappable_node(&node); |
| 1451 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1452 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1453 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1454 | out_rpm: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1455 | intel_runtime_pm_put(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1456 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1457 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1458 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1461 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1462 | shmem_pwrite_slow(struct page *page, int offset, int length, |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1463 | char __user *user_data, |
| 1464 | bool page_do_bit17_swizzling, |
| 1465 | bool needs_clflush_before, |
| 1466 | bool needs_clflush_after) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1467 | { |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1468 | char *vaddr; |
| 1469 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1470 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1471 | vaddr = kmap(page); |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 1472 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1473 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1474 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1475 | if (page_do_bit17_swizzling) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1476 | ret = __copy_from_user_swizzled(vaddr, offset, user_data, |
| 1477 | length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1478 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1479 | ret = __copy_from_user(vaddr + offset, user_data, length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1480 | if (needs_clflush_after) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1481 | shmem_clflush_swizzled_range(vaddr + offset, length, |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 1482 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 1483 | kunmap(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1484 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 1485 | return ret ? -EFAULT : 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1488 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1489 | * Flushes invalid cachelines before writing to the target if |
| 1490 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1491 | * writing if needs_clflush is set. |
| 1492 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1493 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1494 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
| 1495 | bool page_do_bit17_swizzling, |
| 1496 | bool needs_clflush_before, |
| 1497 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1498 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1499 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1500 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1501 | ret = -ENODEV; |
| 1502 | if (!page_do_bit17_swizzling) { |
| 1503 | char *vaddr = kmap_atomic(page); |
| 1504 | |
| 1505 | if (needs_clflush_before) |
| 1506 | drm_clflush_virt_range(vaddr + offset, len); |
| 1507 | ret = __copy_from_user_inatomic(vaddr + offset, user_data, len); |
| 1508 | if (needs_clflush_after) |
| 1509 | drm_clflush_virt_range(vaddr + offset, len); |
| 1510 | |
| 1511 | kunmap_atomic(vaddr); |
| 1512 | } |
| 1513 | if (ret == 0) |
| 1514 | return ret; |
| 1515 | |
| 1516 | return shmem_pwrite_slow(page, offset, len, user_data, |
| 1517 | page_do_bit17_swizzling, |
| 1518 | needs_clflush_before, |
| 1519 | needs_clflush_after); |
| 1520 | } |
| 1521 | |
| 1522 | static int |
| 1523 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1524 | const struct drm_i915_gem_pwrite *args) |
| 1525 | { |
| 1526 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1527 | void __user *user_data; |
| 1528 | u64 remain; |
| 1529 | unsigned int obj_do_bit17_swizzling; |
| 1530 | unsigned int partial_cacheline_write; |
| 1531 | unsigned int needs_clflush; |
| 1532 | unsigned int offset, idx; |
| 1533 | int ret; |
| 1534 | |
| 1535 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1536 | if (ret) |
| 1537 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1538 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1539 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1540 | mutex_unlock(&i915->drm.struct_mutex); |
| 1541 | if (ret) |
| 1542 | return ret; |
| 1543 | |
| 1544 | obj_do_bit17_swizzling = 0; |
| 1545 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 1546 | obj_do_bit17_swizzling = BIT(17); |
| 1547 | |
| 1548 | /* If we don't overwrite a cacheline completely we need to be |
| 1549 | * careful to have up-to-date data by first clflushing. Don't |
| 1550 | * overcomplicate things and flush the entire patch. |
| 1551 | */ |
| 1552 | partial_cacheline_write = 0; |
| 1553 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1554 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1555 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1556 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1557 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1558 | offset = offset_in_page(args->offset); |
| 1559 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1560 | struct page *page = i915_gem_object_get_page(obj, idx); |
| 1561 | int length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1562 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1563 | length = remain; |
| 1564 | if (offset + length > PAGE_SIZE) |
| 1565 | length = PAGE_SIZE - offset; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1566 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1567 | ret = shmem_pwrite(page, offset, length, user_data, |
| 1568 | page_to_phys(page) & obj_do_bit17_swizzling, |
| 1569 | (offset | length) & partial_cacheline_write, |
| 1570 | needs_clflush & CLFLUSH_AFTER); |
| 1571 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1572 | break; |
| 1573 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1574 | remain -= length; |
| 1575 | user_data += length; |
| 1576 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1579 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1580 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1581 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /** |
| 1585 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1586 | * @dev: drm device |
| 1587 | * @data: ioctl data blob |
| 1588 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1589 | * |
| 1590 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1591 | */ |
| 1592 | int |
| 1593 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1594 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1595 | { |
| 1596 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1597 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1598 | int ret; |
| 1599 | |
| 1600 | if (args->size == 0) |
| 1601 | return 0; |
| 1602 | |
| 1603 | if (!access_ok(VERIFY_READ, |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 1604 | u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1605 | args->size)) |
| 1606 | return -EFAULT; |
| 1607 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1608 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1609 | if (!obj) |
| 1610 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1611 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1612 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1613 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1614 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1615 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1616 | } |
| 1617 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1618 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1619 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1620 | ret = -ENODEV; |
| 1621 | if (obj->ops->pwrite) |
| 1622 | ret = obj->ops->pwrite(obj, args); |
| 1623 | if (ret != -ENODEV) |
| 1624 | goto err; |
| 1625 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1626 | ret = i915_gem_object_wait(obj, |
| 1627 | I915_WAIT_INTERRUPTIBLE | |
| 1628 | I915_WAIT_ALL, |
| 1629 | MAX_SCHEDULE_TIMEOUT, |
| 1630 | to_rps_client(file)); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1631 | if (ret) |
| 1632 | goto err; |
| 1633 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1634 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1635 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1636 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1637 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1638 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1639 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1640 | * it would end up going through the fenced access, and we'll get |
| 1641 | * different detiling behavior between reading and writing. |
| 1642 | * pread/pwrite currently are reading and writing from the CPU |
| 1643 | * perspective, requiring manual detiling by the client. |
| 1644 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1645 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1646 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1647 | /* Note that the gtt paths might fail with non-page-backed user |
| 1648 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1649 | * textures). Fallback to the shmem path in that case. |
| 1650 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1651 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1652 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1653 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1654 | if (obj->phys_handle) |
| 1655 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1656 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1657 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1658 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1659 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1660 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1661 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1662 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1663 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1666 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1667 | { |
| 1668 | struct drm_i915_private *i915; |
| 1669 | struct list_head *list; |
| 1670 | struct i915_vma *vma; |
| 1671 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1672 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1673 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1674 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1675 | if (i915_vma_is_active(vma)) |
| 1676 | continue; |
| 1677 | |
| 1678 | if (!drm_mm_node_allocated(&vma->node)) |
| 1679 | continue; |
| 1680 | |
| 1681 | list_move_tail(&vma->vm_link, &vma->vm->inactive_list); |
| 1682 | } |
| 1683 | |
| 1684 | i915 = to_i915(obj->base.dev); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1685 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1686 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1687 | list_move_tail(&obj->mm.link, list); |
| 1688 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1689 | } |
| 1690 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1691 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1692 | * Called when user space prepares to use an object with the CPU, either |
| 1693 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1694 | * @dev: drm device |
| 1695 | * @data: ioctl data blob |
| 1696 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1697 | */ |
| 1698 | int |
| 1699 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1700 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1701 | { |
| 1702 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1703 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1704 | uint32_t read_domains = args->read_domains; |
| 1705 | uint32_t write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1706 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1707 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1708 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1709 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1710 | return -EINVAL; |
| 1711 | |
| 1712 | /* Having something in the write domain implies it's in the read |
| 1713 | * domain, and only that read domain. Enforce that in the request. |
| 1714 | */ |
| 1715 | if (write_domain != 0 && read_domains != write_domain) |
| 1716 | return -EINVAL; |
| 1717 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1718 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1719 | if (!obj) |
| 1720 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1721 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1722 | /* Try to flush the object off the GPU without holding the lock. |
| 1723 | * We will repeat the flush holding the lock in the normal manner |
| 1724 | * to catch cases where we are gazumped. |
| 1725 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1726 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1727 | I915_WAIT_INTERRUPTIBLE | |
| 1728 | (write_domain ? I915_WAIT_ALL : 0), |
| 1729 | MAX_SCHEDULE_TIMEOUT, |
| 1730 | to_rps_client(file)); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1731 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1732 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1733 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1734 | /* |
| 1735 | * Proxy objects do not control access to the backing storage, ergo |
| 1736 | * they cannot be used as a means to manipulate the cache domain |
| 1737 | * tracking for that backing storage. The proxy object is always |
| 1738 | * considered to be outside of any cache domain. |
| 1739 | */ |
| 1740 | if (i915_gem_object_is_proxy(obj)) { |
| 1741 | err = -ENXIO; |
| 1742 | goto out; |
| 1743 | } |
| 1744 | |
| 1745 | /* |
| 1746 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1747 | * direct access in memory with previous cached writes through |
| 1748 | * shmemfs and that our cache domain tracking remains valid. |
| 1749 | * For example, if the obj->filp was moved to swap without us |
| 1750 | * being notified and releasing the pages, we would mistakenly |
| 1751 | * continue to assume that the obj remained out of the CPU cached |
| 1752 | * domain. |
| 1753 | */ |
| 1754 | err = i915_gem_object_pin_pages(obj); |
| 1755 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1756 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1757 | |
| 1758 | err = i915_mutex_lock_interruptible(dev); |
| 1759 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1760 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1761 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1762 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1763 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1764 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1765 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1766 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1767 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1768 | |
| 1769 | /* And bump the LRU for this access */ |
| 1770 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1771 | |
| 1772 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1773 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1774 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1775 | intel_fb_obj_invalidate(obj, |
| 1776 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1777 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1778 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1779 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1780 | out: |
| 1781 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1782 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | /** |
| 1786 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1787 | * @dev: drm device |
| 1788 | * @data: ioctl data blob |
| 1789 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1790 | */ |
| 1791 | int |
| 1792 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1793 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1794 | { |
| 1795 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1796 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1797 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1798 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1799 | if (!obj) |
| 1800 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1801 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1802 | /* |
| 1803 | * Proxy objects are barred from CPU access, so there is no |
| 1804 | * need to ban sw_finish as it is a nop. |
| 1805 | */ |
| 1806 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1807 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1808 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1809 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1810 | |
| 1811 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1815 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1816 | * it is mapped to. |
| 1817 | * @dev: drm device |
| 1818 | * @data: ioctl data blob |
| 1819 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1820 | * |
| 1821 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1822 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1823 | * |
| 1824 | * IMPORTANT: |
| 1825 | * |
| 1826 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1827 | * mmap support, please don't implement mmap support like here. The modern way |
| 1828 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1829 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1830 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1831 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1832 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1833 | */ |
| 1834 | int |
| 1835 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1836 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1837 | { |
| 1838 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1839 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1840 | unsigned long addr; |
| 1841 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1842 | if (args->flags & ~(I915_MMAP_WC)) |
| 1843 | return -EINVAL; |
| 1844 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1845 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1846 | return -ENODEV; |
| 1847 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1848 | obj = i915_gem_object_lookup(file, args->handle); |
| 1849 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1850 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1851 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1852 | /* prime objects have no backing filp to GEM mmap |
| 1853 | * pages from. |
| 1854 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1855 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1856 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1857 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1860 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1861 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1862 | args->offset); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1863 | if (args->flags & I915_MMAP_WC) { |
| 1864 | struct mm_struct *mm = current->mm; |
| 1865 | struct vm_area_struct *vma; |
| 1866 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1867 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1868 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1869 | return -EINTR; |
| 1870 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1871 | vma = find_vma(mm, addr); |
| 1872 | if (vma) |
| 1873 | vma->vm_page_prot = |
| 1874 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1875 | else |
| 1876 | addr = -ENOMEM; |
| 1877 | up_write(&mm->mmap_sem); |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1878 | |
| 1879 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1880 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1881 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1882 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1883 | if (IS_ERR((void *)addr)) |
| 1884 | return addr; |
| 1885 | |
| 1886 | args->addr_ptr = (uint64_t) addr; |
| 1887 | |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1891 | static unsigned int tile_row_pages(struct drm_i915_gem_object *obj) |
| 1892 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1893 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1894 | } |
| 1895 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1896 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1897 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1898 | * |
| 1899 | * A history of the GTT mmap interface: |
| 1900 | * |
| 1901 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1902 | * aligned and suitable for fencing, and still fit into the available |
| 1903 | * mappable space left by the pinned display objects. A classic problem |
| 1904 | * we called the page-fault-of-doom where we would ping-pong between |
| 1905 | * two objects that could not fit inside the GTT and so the memcpy |
| 1906 | * would page one object in at the expense of the other between every |
| 1907 | * single byte. |
| 1908 | * |
| 1909 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1910 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1911 | * object is too large for the available space (or simply too large |
| 1912 | * for the mappable aperture!), a view is created instead and faulted |
| 1913 | * into userspace. (This view is aligned and sized appropriately for |
| 1914 | * fenced access.) |
| 1915 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1916 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1917 | * delayed writes via GTT before performing direct access via WC. |
| 1918 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1919 | * Restrictions: |
| 1920 | * |
| 1921 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1922 | * hangs on some architectures, corruption on others. An attempt to service |
| 1923 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1924 | * |
| 1925 | * * the object must be able to fit into RAM (physical memory, though no |
| 1926 | * limited to the mappable aperture). |
| 1927 | * |
| 1928 | * |
| 1929 | * Caveats: |
| 1930 | * |
| 1931 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1932 | * all data to system memory. Subsequent access will not be synchronized. |
| 1933 | * |
| 1934 | * * all mappings are revoked on runtime device suspend. |
| 1935 | * |
| 1936 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1937 | * (older machines require fence register for display and blitter access |
| 1938 | * as well). Contention of the fence registers will cause the previous users |
| 1939 | * to be unmapped and any new access will generate new page faults. |
| 1940 | * |
| 1941 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1942 | * rather than the expected SIGSEGV. |
| 1943 | */ |
| 1944 | int i915_gem_mmap_gtt_version(void) |
| 1945 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1946 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1947 | } |
| 1948 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1949 | static inline struct i915_ggtt_view |
| 1950 | compute_partial_view(struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1951 | pgoff_t page_offset, |
| 1952 | unsigned int chunk) |
| 1953 | { |
| 1954 | struct i915_ggtt_view view; |
| 1955 | |
| 1956 | if (i915_gem_object_is_tiled(obj)) |
| 1957 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1958 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1959 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1960 | view.partial.offset = rounddown(page_offset, chunk); |
| 1961 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1962 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1963 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1964 | |
| 1965 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1966 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1967 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1968 | |
| 1969 | return view; |
| 1970 | } |
| 1971 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1972 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1973 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1974 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1975 | * |
| 1976 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1977 | * from userspace. The fault handler takes care of binding the object to |
| 1978 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1979 | * only if needed based on whether the old reg is still valid or the object |
| 1980 | * is tiled) and inserting a new PTE into the faulting process. |
| 1981 | * |
| 1982 | * Note that the faulting process may involve evicting existing objects |
| 1983 | * from the GTT and/or fence registers to make room. So performance may |
| 1984 | * suffer if the GTT working set is large or there are few fence registers |
| 1985 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1986 | * |
| 1987 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1988 | * 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] | 1989 | */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1990 | int i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1991 | { |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1992 | #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */ |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1993 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1994 | 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] | 1995 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1996 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1997 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1998 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1999 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2000 | pgoff_t page_offset; |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 2001 | unsigned int flags; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2002 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2003 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2004 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 2005 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2006 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2007 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 2008 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 2009 | /* 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] | 2010 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 2011 | * repeat the flush holding the lock in the normal manner to catch cases |
| 2012 | * where we are gazumped. |
| 2013 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 2014 | ret = i915_gem_object_wait(obj, |
| 2015 | I915_WAIT_INTERRUPTIBLE, |
| 2016 | MAX_SCHEDULE_TIMEOUT, |
| 2017 | NULL); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 2018 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2019 | goto err; |
| 2020 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 2021 | ret = i915_gem_object_pin_pages(obj); |
| 2022 | if (ret) |
| 2023 | goto err; |
| 2024 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2025 | intel_runtime_pm_get(dev_priv); |
| 2026 | |
| 2027 | ret = i915_mutex_lock_interruptible(dev); |
| 2028 | if (ret) |
| 2029 | goto err_rpm; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 2030 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 2031 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 2032 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 2033 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2034 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 2037 | /* If the object is smaller than a couple of partial vma, it is |
| 2038 | * not worth only creating a single partial vma - we may as well |
| 2039 | * clear enough space for the full object. |
| 2040 | */ |
| 2041 | flags = PIN_MAPPABLE; |
| 2042 | if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT) |
| 2043 | flags |= PIN_NONBLOCK | PIN_NONFAULT; |
| 2044 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2045 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 8211887 | 2016-08-18 17:17:05 +0100 | [diff] [blame] | 2046 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2047 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2048 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 2049 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 2050 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 2051 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 2052 | /* Userspace is now writing through an untracked VMA, abandon |
| 2053 | * all hope that the hardware is able to track future writes. |
| 2054 | */ |
| 2055 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 2056 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2057 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE); |
| 2058 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2059 | if (IS_ERR(vma)) { |
| 2060 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2061 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2062 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2063 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 2064 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 2065 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2066 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 2067 | |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 2068 | ret = i915_vma_pin_fence(vma); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 2069 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2070 | goto err_unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 2071 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 2072 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 2073 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 2074 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 2075 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 2076 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 2077 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2078 | if (ret) |
| 2079 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 2080 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2081 | /* Mark as being mmapped into userspace for later revocation */ |
| 2082 | assert_rpm_wakelock_held(dev_priv); |
| 2083 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 2084 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 2085 | GEM_BUG_ON(!obj->userfault_count); |
| 2086 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 2087 | i915_vma_set_ggtt_write(vma); |
| 2088 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2089 | err_fence: |
Chris Wilson | 3bd4073 | 2017-10-09 09:43:56 +0100 | [diff] [blame] | 2090 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2091 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 2092 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2093 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2094 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2095 | err_rpm: |
| 2096 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 2097 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 2098 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2099 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2100 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 2101 | /* |
| 2102 | * We eat errors when the gpu is terminally wedged to avoid |
| 2103 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 2104 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 2105 | * and so needs to be reported. |
| 2106 | */ |
| 2107 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2108 | ret = VM_FAULT_SIGBUS; |
| 2109 | break; |
| 2110 | } |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 2111 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 2112 | /* |
| 2113 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 2114 | * handler to reset everything when re-faulting in |
| 2115 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 2116 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 2117 | case 0: |
| 2118 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 2119 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 2120 | case -EBUSY: |
| 2121 | /* |
| 2122 | * EBUSY is ok: this just means that another thread |
| 2123 | * already did the job. |
| 2124 | */ |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2125 | ret = VM_FAULT_NOPAGE; |
| 2126 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2127 | case -ENOMEM: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2128 | ret = VM_FAULT_OOM; |
| 2129 | break; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2130 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 2131 | case -EFAULT: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2132 | ret = VM_FAULT_SIGBUS; |
| 2133 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2134 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 2135 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2136 | ret = VM_FAULT_SIGBUS; |
| 2137 | break; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2138 | } |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 2139 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2140 | } |
| 2141 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2142 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 2143 | { |
| 2144 | struct i915_vma *vma; |
| 2145 | |
| 2146 | GEM_BUG_ON(!obj->userfault_count); |
| 2147 | |
| 2148 | obj->userfault_count = 0; |
| 2149 | list_del(&obj->userfault_link); |
| 2150 | drm_vma_node_unmap(&obj->base.vma_node, |
| 2151 | obj->base.dev->anon_inode->i_mapping); |
| 2152 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 2153 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2154 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2157 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2158 | * i915_gem_release_mmap - remove physical page mappings |
| 2159 | * @obj: obj in question |
| 2160 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 2161 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2162 | * relinquish ownership of the pages back to the system. |
| 2163 | * |
| 2164 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 2165 | * object through the GTT and then lose the fence register due to |
| 2166 | * resource pressure. Similarly if the object has been moved out of the |
| 2167 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 2168 | * mapping will then trigger a page fault on the next user access, allowing |
| 2169 | * fixup by i915_gem_fault(). |
| 2170 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 2171 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2172 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2173 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2174 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2175 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2176 | /* Serialisation between user GTT access and our code depends upon |
| 2177 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2178 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2179 | * |
| 2180 | * Note that RPM complicates somewhat by adding an additional |
| 2181 | * requirement that operations to the GGTT be made holding the RPM |
| 2182 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2183 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2184 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2185 | intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2186 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2187 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2188 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2189 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2190 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2191 | |
| 2192 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2193 | * memory transactions from userspace before we return. The TLB |
| 2194 | * flushing implied above by changing the PTE above *should* be |
| 2195 | * sufficient, an extra barrier here just provides us with a bit |
| 2196 | * of paranoid documentation about our requirement to serialise |
| 2197 | * memory writes before touching registers / GSM. |
| 2198 | */ |
| 2199 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2200 | |
| 2201 | out: |
| 2202 | intel_runtime_pm_put(i915); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2203 | } |
| 2204 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2205 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2206 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2207 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2208 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2209 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2210 | /* |
| 2211 | * Only called during RPM suspend. All users of the userfault_list |
| 2212 | * must be holding an RPM wakeref to ensure that this can not |
| 2213 | * run concurrently with themselves (and use the struct_mutex for |
| 2214 | * protection between themselves). |
| 2215 | */ |
| 2216 | |
| 2217 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2218 | &dev_priv->mm.userfault_list, userfault_link) |
| 2219 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2220 | |
| 2221 | /* The fence will be lost when the device powers down. If any were |
| 2222 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2223 | * down! All other fences will be reacquired by the user upon waking. |
| 2224 | */ |
| 2225 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2226 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2227 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2228 | /* Ideally we want to assert that the fence register is not |
| 2229 | * live at this point (i.e. that no piece of code will be |
| 2230 | * trying to write through fence + GTT, as that both violates |
| 2231 | * our tracking of activity and associated locking/barriers, |
| 2232 | * but also is illegal given that the hw is powered down). |
| 2233 | * |
| 2234 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2235 | * That is not sufficient, and we need a more fine-grained |
| 2236 | * tool if we want to have a sanity check here. |
| 2237 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2238 | |
| 2239 | if (!reg->vma) |
| 2240 | continue; |
| 2241 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2242 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2243 | reg->dirty = true; |
| 2244 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2245 | } |
| 2246 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2247 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2248 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2249 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2250 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2251 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2252 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2253 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2254 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2255 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2256 | /* Attempt to reap some mmap space from dead objects */ |
| 2257 | do { |
| 2258 | err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE); |
| 2259 | if (err) |
| 2260 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2261 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2262 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2263 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2264 | if (!err) |
| 2265 | break; |
| 2266 | |
| 2267 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2268 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2269 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2273 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2274 | drm_gem_free_mmap_offset(&obj->base); |
| 2275 | } |
| 2276 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2277 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2278 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2279 | struct drm_device *dev, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2280 | uint32_t handle, |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2281 | uint64_t *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2282 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2283 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2284 | int ret; |
| 2285 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2286 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2287 | if (!obj) |
| 2288 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2289 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2290 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2291 | if (ret == 0) |
| 2292 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2293 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2294 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2295 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2296 | } |
| 2297 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2298 | /** |
| 2299 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2300 | * @dev: DRM device |
| 2301 | * @data: GTT mapping ioctl data |
| 2302 | * @file: GEM object info |
| 2303 | * |
| 2304 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2305 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2306 | * up so we can get faults in the handler above. |
| 2307 | * |
| 2308 | * The fault handler will take care of binding the object into the GTT |
| 2309 | * (since it may have been evicted to make room for something), allocating |
| 2310 | * a fence register, and mapping the appropriate aperture address into |
| 2311 | * userspace. |
| 2312 | */ |
| 2313 | int |
| 2314 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2315 | struct drm_file *file) |
| 2316 | { |
| 2317 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2318 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2319 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2320 | } |
| 2321 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2322 | /* Immediately discard the backing storage */ |
| 2323 | static void |
| 2324 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2325 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2326 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2327 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2328 | if (obj->base.filp == NULL) |
| 2329 | return; |
| 2330 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2331 | /* Our goal here is to return as much of the memory as |
| 2332 | * is possible back to the system as we are called from OOM. |
| 2333 | * To do this we must instruct the shmfs to drop all of its |
| 2334 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2335 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2336 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2337 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2338 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2339 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2340 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2341 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2342 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2343 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2344 | struct address_space *mapping; |
| 2345 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2346 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2347 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2348 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2349 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2350 | case I915_MADV_DONTNEED: |
| 2351 | i915_gem_object_truncate(obj); |
| 2352 | case __I915_MADV_PURGED: |
| 2353 | return; |
| 2354 | } |
| 2355 | |
| 2356 | if (obj->base.filp == NULL) |
| 2357 | return; |
| 2358 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2359 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2360 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2361 | } |
| 2362 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2363 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2364 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2365 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2366 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2367 | struct sgt_iter sgt_iter; |
| 2368 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2369 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2370 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2371 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2372 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2373 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2374 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2375 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2376 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2377 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2378 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2379 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2380 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2381 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2382 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2383 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2384 | put_page(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2385 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2386 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2387 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2388 | sg_free_table(pages); |
| 2389 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2390 | } |
| 2391 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2392 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2393 | { |
| 2394 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2395 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2396 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2397 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2398 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2399 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2400 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2401 | } |
| 2402 | |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2403 | void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2404 | enum i915_mm_subclass subclass) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2405 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2406 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2407 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2408 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2409 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2410 | return; |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 2411 | |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 2412 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2413 | if (!i915_gem_object_has_pages(obj)) |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2414 | return; |
| 2415 | |
| 2416 | /* May be called by shrinker from within get_pages() (on another bo) */ |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 2417 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2418 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) |
| 2419 | goto unlock; |
Ben Widawsky | 3e12302 | 2013-07-31 17:00:04 -0700 | [diff] [blame] | 2420 | |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2421 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
| 2422 | * array, hence protect them from being reaped by removing them from gtt |
| 2423 | * lists early. */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2424 | pages = fetch_and_zero(&obj->mm.pages); |
| 2425 | GEM_BUG_ON(!pages); |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2426 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2427 | spin_lock(&i915->mm.obj_lock); |
| 2428 | list_del(&obj->mm.link); |
| 2429 | spin_unlock(&i915->mm.obj_lock); |
| 2430 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2431 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2432 | void *ptr; |
| 2433 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2434 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2435 | if (is_vmalloc_addr(ptr)) |
| 2436 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2437 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2438 | kunmap(kmap_to_page(ptr)); |
| 2439 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2440 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2441 | } |
| 2442 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2443 | __i915_gem_object_reset_page_iter(obj); |
| 2444 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2445 | if (!IS_ERR(pages)) |
| 2446 | obj->ops->put_pages(obj, pages); |
| 2447 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2448 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
| 2449 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2450 | unlock: |
| 2451 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2452 | } |
| 2453 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2454 | static bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2455 | { |
| 2456 | struct sg_table new_st; |
| 2457 | struct scatterlist *sg, *new_sg; |
| 2458 | unsigned int i; |
| 2459 | |
| 2460 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2461 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2462 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2463 | 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] | 2464 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2465 | |
| 2466 | new_sg = new_st.sgl; |
| 2467 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2468 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
| 2469 | /* called before being DMA mapped, no need to copy sg->dma_* */ |
| 2470 | new_sg = sg_next(new_sg); |
| 2471 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2472 | 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] | 2473 | |
| 2474 | sg_free_table(orig_st); |
| 2475 | |
| 2476 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2477 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2480 | 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] | 2481 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2482 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2483 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2484 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2485 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2486 | struct sg_table *st; |
| 2487 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2488 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2489 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2490 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2491 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2492 | unsigned int sg_page_sizes; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2493 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2494 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2495 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2496 | /* Assert that the object is not currently in any GPU domain. As it |
| 2497 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2498 | * a GPU cache |
| 2499 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2500 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2501 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2502 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2503 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2504 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2505 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2506 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2507 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2508 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2509 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2510 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | /* Get the list of pages out of our struct file. They'll be pinned |
| 2514 | * at this point until we release them. |
| 2515 | * |
| 2516 | * Fail silently without starting the shrinker |
| 2517 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2518 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2519 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2520 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2521 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2522 | sg = st->sgl; |
| 2523 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2524 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2525 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2526 | const unsigned int shrink[] = { |
| 2527 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2528 | 0, |
| 2529 | }, *s = shrink; |
| 2530 | gfp_t gfp = noreclaim; |
| 2531 | |
| 2532 | do { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2533 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2534 | if (likely(!IS_ERR(page))) |
| 2535 | break; |
| 2536 | |
| 2537 | if (!*s) { |
| 2538 | ret = PTR_ERR(page); |
| 2539 | goto err_sg; |
| 2540 | } |
| 2541 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2542 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2543 | cond_resched(); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2544 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2545 | /* We've tried hard to allocate the memory by reaping |
| 2546 | * our own buffer, now let the real VM do its job and |
| 2547 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2548 | * |
| 2549 | * However, since graphics tend to be disposable, |
| 2550 | * defer the oom here by reporting the ENOMEM back |
| 2551 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2552 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2553 | if (!*s) { |
| 2554 | /* reclaim and warn, but no oom */ |
| 2555 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2556 | |
| 2557 | /* Our bo are always dirty and so we require |
| 2558 | * kswapd to reclaim our pages (direct reclaim |
| 2559 | * does not effectively begin pageout of our |
| 2560 | * buffers on its own). However, direct reclaim |
| 2561 | * only waits for kswapd when under allocation |
| 2562 | * congestion. So as a result __GFP_RECLAIM is |
| 2563 | * unreliable and fails to actually reclaim our |
| 2564 | * dirty pages -- unless you try over and over |
| 2565 | * again with !__GFP_NORETRY. However, we still |
| 2566 | * want to fail this allocation rather than |
| 2567 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2568 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2569 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2570 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2571 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2572 | } while (1); |
| 2573 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2574 | if (!i || |
| 2575 | sg->length >= max_segment || |
| 2576 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2577 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2578 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2579 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2580 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2581 | st->nents++; |
| 2582 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2583 | } else { |
| 2584 | sg->length += PAGE_SIZE; |
| 2585 | } |
| 2586 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2587 | |
| 2588 | /* Check that the i965g/gm workaround works. */ |
| 2589 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2590 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2591 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2592 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2593 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2594 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2595 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2596 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2597 | i915_sg_trim(st); |
| 2598 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2599 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2600 | if (ret) { |
| 2601 | /* DMA remapping failed? One possible cause is that |
| 2602 | * it could not reserve enough large entries, asking |
| 2603 | * for PAGE_SIZE chunks instead may be helpful. |
| 2604 | */ |
| 2605 | if (max_segment > PAGE_SIZE) { |
| 2606 | for_each_sgt_page(page, sgt_iter, st) |
| 2607 | put_page(page); |
| 2608 | sg_free_table(st); |
| 2609 | |
| 2610 | max_segment = PAGE_SIZE; |
| 2611 | goto rebuild_st; |
| 2612 | } else { |
| 2613 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2614 | "Failed to DMA remap %lu pages\n", |
| 2615 | page_count); |
| 2616 | goto err_pages; |
| 2617 | } |
| 2618 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2619 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2620 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2621 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2622 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2623 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2624 | |
| 2625 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2626 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2627 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2628 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2629 | err_pages: |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2630 | for_each_sgt_page(page, sgt_iter, st) |
| 2631 | put_page(page); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2632 | sg_free_table(st); |
| 2633 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2634 | |
| 2635 | /* shmemfs first checks if there is enough memory to allocate the page |
| 2636 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2637 | * ENOMEM for a genuine allocation failure. |
| 2638 | * |
| 2639 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2640 | * space and so want to translate the error from shmemfs back to our |
| 2641 | * usual understanding of ENOMEM. |
| 2642 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2643 | if (ret == -ENOSPC) |
| 2644 | ret = -ENOMEM; |
| 2645 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2646 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2650 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2651 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2652 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2653 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2654 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2655 | int i; |
| 2656 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2657 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2658 | |
| 2659 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2660 | obj->mm.get_page.sg_idx = 0; |
| 2661 | |
| 2662 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2663 | |
| 2664 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2665 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2666 | GEM_BUG_ON(obj->mm.quirked); |
| 2667 | __i915_gem_object_pin_pages(obj); |
| 2668 | obj->mm.quirked = true; |
| 2669 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2670 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2671 | GEM_BUG_ON(!sg_page_sizes); |
| 2672 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2673 | |
| 2674 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2675 | * Calculate the supported page-sizes which fit into the given |
| 2676 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2677 | * to use opportunistically when later inserting into the GTT. For |
| 2678 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2679 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2680 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2681 | */ |
| 2682 | obj->mm.page_sizes.sg = 0; |
| 2683 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2684 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2685 | obj->mm.page_sizes.sg |= BIT(i); |
| 2686 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2687 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2688 | |
| 2689 | spin_lock(&i915->mm.obj_lock); |
| 2690 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2691 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2695 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2696 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2697 | |
| 2698 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2699 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2700 | return -EFAULT; |
| 2701 | } |
| 2702 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2703 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2704 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2705 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2706 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2707 | } |
| 2708 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2709 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2710 | * 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] | 2711 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2712 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2713 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2714 | * or as the object is itself released. |
| 2715 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2716 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2717 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2718 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2719 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2720 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2721 | if (err) |
| 2722 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2723 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2724 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2725 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2726 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2727 | err = ____i915_gem_object_get_pages(obj); |
| 2728 | if (err) |
| 2729 | goto unlock; |
| 2730 | |
| 2731 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2732 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2733 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2734 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2735 | unlock: |
| 2736 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2737 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2740 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2741 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2742 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2743 | { |
| 2744 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2745 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2746 | struct sgt_iter sgt_iter; |
| 2747 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2748 | struct page *stack_pages[32]; |
| 2749 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2750 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2751 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2752 | void *addr; |
| 2753 | |
| 2754 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2755 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2756 | return kmap(sg_page(sgt->sgl)); |
| 2757 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2758 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2759 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2760 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2761 | if (!pages) |
| 2762 | return NULL; |
| 2763 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2764 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2765 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2766 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2767 | |
| 2768 | /* Check that we have the expected number of pages */ |
| 2769 | GEM_BUG_ON(i != n_pages); |
| 2770 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2771 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2772 | default: |
| 2773 | MISSING_CASE(type); |
| 2774 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2775 | case I915_MAP_WB: |
| 2776 | pgprot = PAGE_KERNEL; |
| 2777 | break; |
| 2778 | case I915_MAP_WC: |
| 2779 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2780 | break; |
| 2781 | } |
| 2782 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2783 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2784 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2785 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2786 | |
| 2787 | return addr; |
| 2788 | } |
| 2789 | |
| 2790 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2791 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2792 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2793 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2794 | enum i915_map_type has_type; |
| 2795 | bool pinned; |
| 2796 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2797 | int ret; |
| 2798 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2799 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2800 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2801 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2802 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2803 | if (ret) |
| 2804 | return ERR_PTR(ret); |
| 2805 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2806 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2807 | type &= ~I915_MAP_OVERRIDE; |
| 2808 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2809 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2810 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2811 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2812 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2813 | ret = ____i915_gem_object_get_pages(obj); |
| 2814 | if (ret) |
| 2815 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2816 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2817 | smp_mb__before_atomic(); |
| 2818 | } |
| 2819 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2820 | pinned = false; |
| 2821 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2822 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2823 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2824 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2825 | if (ptr && has_type != type) { |
| 2826 | if (pinned) { |
| 2827 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2828 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2829 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2830 | |
| 2831 | if (is_vmalloc_addr(ptr)) |
| 2832 | vunmap(ptr); |
| 2833 | else |
| 2834 | kunmap(kmap_to_page(ptr)); |
| 2835 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2836 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2837 | } |
| 2838 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2839 | if (!ptr) { |
| 2840 | ptr = i915_gem_object_map(obj, type); |
| 2841 | if (!ptr) { |
| 2842 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2843 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2844 | } |
| 2845 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2846 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2847 | } |
| 2848 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2849 | out_unlock: |
| 2850 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2851 | return ptr; |
| 2852 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2853 | err_unpin: |
| 2854 | atomic_dec(&obj->mm.pages_pin_count); |
| 2855 | err_unlock: |
| 2856 | ptr = ERR_PTR(ret); |
| 2857 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2858 | } |
| 2859 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2860 | static int |
| 2861 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2862 | const struct drm_i915_gem_pwrite *arg) |
| 2863 | { |
| 2864 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2865 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2866 | u64 remain, offset; |
| 2867 | unsigned int pg; |
| 2868 | |
| 2869 | /* Before we instantiate/pin the backing store for our use, we |
| 2870 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2871 | * the pagecache. We avoid the penalty of instantiating all the |
| 2872 | * pages, important if the user is just writing to a few and never |
| 2873 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2874 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2875 | * or clearing-before-use) before it is overwritten. |
| 2876 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2877 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2878 | return -ENODEV; |
| 2879 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2880 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2881 | return -EFAULT; |
| 2882 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2883 | /* Before the pages are instantiated the object is treated as being |
| 2884 | * in the CPU domain. The pages will be clflushed as required before |
| 2885 | * use, and we can freely write into the pages directly. If userspace |
| 2886 | * races pwrite with any other operation; corruption will ensue - |
| 2887 | * that is userspace's prerogative! |
| 2888 | */ |
| 2889 | |
| 2890 | remain = arg->size; |
| 2891 | offset = arg->offset; |
| 2892 | pg = offset_in_page(offset); |
| 2893 | |
| 2894 | do { |
| 2895 | unsigned int len, unwritten; |
| 2896 | struct page *page; |
| 2897 | void *data, *vaddr; |
| 2898 | int err; |
| 2899 | |
| 2900 | len = PAGE_SIZE - pg; |
| 2901 | if (len > remain) |
| 2902 | len = remain; |
| 2903 | |
| 2904 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2905 | offset, len, 0, |
| 2906 | &page, &data); |
| 2907 | if (err < 0) |
| 2908 | return err; |
| 2909 | |
| 2910 | vaddr = kmap(page); |
| 2911 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2912 | kunmap(page); |
| 2913 | |
| 2914 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2915 | offset, len, len - unwritten, |
| 2916 | page, data); |
| 2917 | if (err < 0) |
| 2918 | return err; |
| 2919 | |
| 2920 | if (unwritten) |
| 2921 | return -EFAULT; |
| 2922 | |
| 2923 | remain -= len; |
| 2924 | user_data += len; |
| 2925 | offset += len; |
| 2926 | pg = 0; |
| 2927 | } while (remain); |
| 2928 | |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2932 | static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx) |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2933 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2934 | bool banned; |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2935 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2936 | atomic_inc(&ctx->guilty_count); |
| 2937 | |
Chris Wilson | 24eae08 | 2018-02-05 09:22:01 +0000 | [diff] [blame] | 2938 | banned = false; |
| 2939 | if (i915_gem_context_is_bannable(ctx)) { |
| 2940 | unsigned int score; |
| 2941 | |
| 2942 | score = atomic_add_return(CONTEXT_SCORE_GUILTY, |
| 2943 | &ctx->ban_score); |
| 2944 | banned = score >= CONTEXT_SCORE_BAN_THRESHOLD; |
| 2945 | |
| 2946 | DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n", |
| 2947 | ctx->name, score, yesno(banned)); |
| 2948 | } |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2949 | if (!banned) |
Mika Kuoppala | b083a08 | 2016-11-18 15:10:47 +0200 | [diff] [blame] | 2950 | return; |
| 2951 | |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2952 | i915_gem_context_set_banned(ctx); |
| 2953 | if (!IS_ERR_OR_NULL(ctx->file_priv)) { |
| 2954 | atomic_inc(&ctx->file_priv->context_bans); |
| 2955 | DRM_DEBUG_DRIVER("client %s has had %d context banned\n", |
| 2956 | ctx->name, atomic_read(&ctx->file_priv->context_bans)); |
| 2957 | } |
Mika Kuoppala | e5e1fc4 | 2016-11-16 17:20:31 +0200 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx) |
| 2961 | { |
Chris Wilson | 77b25a9 | 2017-07-21 13:32:30 +0100 | [diff] [blame] | 2962 | atomic_inc(&ctx->active_count); |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2963 | } |
| 2964 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2965 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2966 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2967 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2968 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2969 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2970 | |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2971 | /* We are called by the error capture and reset at a random |
| 2972 | * point in time. In particular, note that neither is crucially |
| 2973 | * ordered with an interrupt. After a hang, the GPU is dead and we |
| 2974 | * assume that no more writes can happen (we waited long enough for |
| 2975 | * all writes that were in transaction to be flushed) - adding an |
| 2976 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2977 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
| 2978 | */ |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2979 | spin_lock_irqsave(&engine->timeline->lock, flags); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 2980 | list_for_each_entry(request, &engine->timeline->requests, link) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2981 | if (__i915_request_completed(request, request->global_seqno)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2982 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2983 | |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 2984 | GEM_BUG_ON(request->engine != engine); |
Chris Wilson | c00122f3 | 2017-02-12 17:19:58 +0000 | [diff] [blame] | 2985 | GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 2986 | &request->fence.flags)); |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2987 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2988 | active = request; |
| 2989 | break; |
| 2990 | } |
| 2991 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 2992 | |
| 2993 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2994 | } |
| 2995 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 2996 | /* |
| 2997 | * Ensure irq handler finishes, and not run again. |
| 2998 | * Also return the active request so that we only search for it once. |
| 2999 | */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3000 | struct i915_request * |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3001 | i915_gem_reset_prepare_engine(struct intel_engine_cs *engine) |
| 3002 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3003 | struct i915_request *request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3004 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3005 | /* |
| 3006 | * During the reset sequence, we must prevent the engine from |
| 3007 | * entering RC6. As the context state is undefined until we restart |
| 3008 | * the engine, if it does enter RC6 during the reset, the state |
| 3009 | * written to the powercontext is undefined and so we may lose |
| 3010 | * GPU state upon resume, i.e. fail to restart after a reset. |
| 3011 | */ |
| 3012 | intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL); |
| 3013 | |
| 3014 | /* |
| 3015 | * Prevent the signaler thread from updating the request |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3016 | * state (by calling dma_fence_signal) as we are processing |
| 3017 | * the reset. The write from the GPU of the seqno is |
| 3018 | * asynchronous and the signaler thread may see a different |
| 3019 | * value to us and declare the request complete, even though |
| 3020 | * the reset routine have picked that request as the active |
| 3021 | * (incomplete) request. This conflict is not handled |
| 3022 | * gracefully! |
| 3023 | */ |
| 3024 | kthread_park(engine->breadcrumbs.signaler); |
| 3025 | |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3026 | /* |
| 3027 | * Prevent request submission to the hardware until we have |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3028 | * completed the reset in i915_gem_reset_finish(). If a request |
| 3029 | * is completed by one engine, it may then queue a request |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3030 | * to a second via its execlists->tasklet *just* as we are |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3031 | * calling engine->init_hw() and also writing the ELSP. |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3032 | * Turning off the execlists->tasklet until the reset is over |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3033 | * prevents the race. |
Chris Wilson | 68ad361 | 2018-03-07 13:42:26 +0000 | [diff] [blame] | 3034 | * |
| 3035 | * Note that this needs to be a single atomic operation on the |
| 3036 | * tasklet (flush existing tasks, prevent new tasks) to prevent |
| 3037 | * a race between reset and set-wedged. It is not, so we do the best |
| 3038 | * we can atm and make sure we don't lock the machine up in the more |
| 3039 | * common case of recursively being called from set-wedged from inside |
| 3040 | * i915_reset. |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3041 | */ |
Chris Wilson | 68ad361 | 2018-03-07 13:42:26 +0000 | [diff] [blame] | 3042 | if (!atomic_read(&engine->execlists.tasklet.count)) |
| 3043 | tasklet_kill(&engine->execlists.tasklet); |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3044 | tasklet_disable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3045 | |
Michał Winiarski | c41937f | 2017-10-26 15:35:58 +0200 | [diff] [blame] | 3046 | /* |
| 3047 | * We're using worker to queue preemption requests from the tasklet in |
| 3048 | * GuC submission mode. |
| 3049 | * Even though tasklet was disabled, we may still have a worker queued. |
| 3050 | * Let's make sure that all workers scheduled before disabling the |
| 3051 | * tasklet are completed before continuing with the reset. |
| 3052 | */ |
| 3053 | if (engine->i915->guc.preempt_wq) |
| 3054 | flush_workqueue(engine->i915->guc.preempt_wq); |
| 3055 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3056 | if (engine->irq_seqno_barrier) |
| 3057 | engine->irq_seqno_barrier(engine); |
| 3058 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3059 | request = i915_gem_find_active_request(engine); |
| 3060 | if (request && request->fence.error == -EIO) |
| 3061 | request = ERR_PTR(-EIO); /* Previous reset failed! */ |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3062 | |
| 3063 | return request; |
| 3064 | } |
| 3065 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3066 | int i915_gem_reset_prepare(struct drm_i915_private *dev_priv) |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3067 | { |
| 3068 | struct intel_engine_cs *engine; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3069 | struct i915_request *request; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3070 | enum intel_engine_id id; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3071 | int err = 0; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3072 | |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3073 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3074 | request = i915_gem_reset_prepare_engine(engine); |
| 3075 | if (IS_ERR(request)) { |
| 3076 | err = PTR_ERR(request); |
| 3077 | continue; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3078 | } |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3079 | |
| 3080 | engine->hangcheck.active_request = request; |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3081 | } |
| 3082 | |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3083 | i915_gem_revoke_fences(dev_priv); |
Michal Wajdeczko | c37d572 | 2018-03-12 13:03:07 +0000 | [diff] [blame] | 3084 | intel_uc_sanitize(dev_priv); |
Chris Wilson | 0e178ae | 2017-01-17 17:59:06 +0200 | [diff] [blame] | 3085 | |
| 3086 | return err; |
Chris Wilson | 4c96554 | 2017-01-17 17:59:01 +0200 | [diff] [blame] | 3087 | } |
| 3088 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3089 | static void skip_request(struct i915_request *request) |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 3090 | { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3091 | void *vaddr = request->ring->vaddr; |
| 3092 | u32 head; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 3093 | |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3094 | /* As this request likely depends on state from the lost |
| 3095 | * context, clear out all the user operations leaving the |
| 3096 | * breadcrumb at the end (so we get the fence notifications). |
| 3097 | */ |
| 3098 | head = request->head; |
| 3099 | if (request->postfix < head) { |
| 3100 | memset(vaddr + head, 0, request->ring->size - head); |
| 3101 | head = 0; |
| 3102 | } |
| 3103 | memset(vaddr + head, 0, request->postfix - head); |
Chris Wilson | c0d5f32 | 2017-01-10 17:22:43 +0000 | [diff] [blame] | 3104 | |
| 3105 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3108 | static void engine_skip_context(struct i915_request *request) |
Mika Kuoppala | 36193ac | 2017-01-17 17:59:02 +0200 | [diff] [blame] | 3109 | { |
| 3110 | struct intel_engine_cs *engine = request->engine; |
| 3111 | struct i915_gem_context *hung_ctx = request->ctx; |
| 3112 | struct intel_timeline *timeline; |
| 3113 | unsigned long flags; |
| 3114 | |
| 3115 | timeline = i915_gem_context_lookup_timeline(hung_ctx, engine); |
| 3116 | |
| 3117 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3118 | spin_lock(&timeline->lock); |
| 3119 | |
| 3120 | list_for_each_entry_continue(request, &engine->timeline->requests, link) |
| 3121 | if (request->ctx == hung_ctx) |
| 3122 | skip_request(request); |
| 3123 | |
| 3124 | list_for_each_entry(request, &timeline->requests, link) |
| 3125 | skip_request(request); |
| 3126 | |
| 3127 | spin_unlock(&timeline->lock); |
| 3128 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
| 3129 | } |
| 3130 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3131 | /* Returns the request if it was guilty of the hang */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3132 | static struct i915_request * |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3133 | i915_gem_reset_request(struct intel_engine_cs *engine, |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3134 | struct i915_request *request, |
| 3135 | bool stalled) |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3136 | { |
Mika Kuoppala | 71895a0 | 2017-01-17 17:59:07 +0200 | [diff] [blame] | 3137 | /* The guilty request will get skipped on a hung engine. |
| 3138 | * |
| 3139 | * Users of client default contexts do not rely on logical |
| 3140 | * state preserved between batches so it is safe to execute |
| 3141 | * queued requests following the hang. Non default contexts |
| 3142 | * rely on preserved state, so skipping a batch loses the |
| 3143 | * evolution of the state and it needs to be considered corrupted. |
| 3144 | * Executing more queued batches on top of corrupted state is |
| 3145 | * risky. But we take the risk by trying to advance through |
| 3146 | * the queued requests in order to make the client behaviour |
| 3147 | * more predictable around resets, by not throwing away random |
| 3148 | * amount of batches it has prepared for execution. Sophisticated |
| 3149 | * clients can use gem_reset_stats_ioctl and dma fence status |
| 3150 | * (exported via sync_file info ioctl on explicit fences) to observe |
| 3151 | * when it loses the context state and should rebuild accordingly. |
| 3152 | * |
| 3153 | * The context ban, and ultimately the client ban, mechanism are safety |
| 3154 | * valves if client submission ends up resulting in nothing more than |
| 3155 | * subsequent hangs. |
| 3156 | */ |
| 3157 | |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3158 | if (i915_request_completed(request)) { |
| 3159 | GEM_TRACE("%s pardoned global=%d (fence %llx:%d), current %d\n", |
| 3160 | engine->name, request->global_seqno, |
| 3161 | request->fence.context, request->fence.seqno, |
| 3162 | intel_engine_get_seqno(engine)); |
| 3163 | stalled = false; |
| 3164 | } |
| 3165 | |
| 3166 | if (stalled) { |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3167 | i915_gem_context_mark_guilty(request->ctx); |
| 3168 | skip_request(request); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3169 | |
| 3170 | /* If this context is now banned, skip all pending requests. */ |
| 3171 | if (i915_gem_context_is_banned(request->ctx)) |
| 3172 | engine_skip_context(request); |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3173 | } else { |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3174 | /* |
| 3175 | * Since this is not the hung engine, it may have advanced |
| 3176 | * since the hang declaration. Double check by refinding |
| 3177 | * the active request at the time of the reset. |
| 3178 | */ |
| 3179 | request = i915_gem_find_active_request(engine); |
| 3180 | if (request) { |
| 3181 | i915_gem_context_mark_innocent(request->ctx); |
| 3182 | dma_fence_set_error(&request->fence, -EAGAIN); |
| 3183 | |
| 3184 | /* Rewind the engine to replay the incomplete rq */ |
| 3185 | spin_lock_irq(&engine->timeline->lock); |
| 3186 | request = list_prev_entry(request, link); |
| 3187 | if (&request->link == &engine->timeline->requests) |
| 3188 | request = NULL; |
| 3189 | spin_unlock_irq(&engine->timeline->lock); |
| 3190 | } |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3191 | } |
| 3192 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3193 | return request; |
Mika Kuoppala | 61da536 | 2017-01-17 17:59:05 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3196 | void i915_gem_reset_engine(struct intel_engine_cs *engine, |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3197 | struct i915_request *request, |
| 3198 | bool stalled) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 3199 | { |
Chris Wilson | fcb1de5 | 2017-12-19 09:01:10 +0000 | [diff] [blame] | 3200 | /* |
| 3201 | * Make sure this write is visible before we re-enable the interrupt |
| 3202 | * handlers on another CPU, as tasklet_enable() resolves to just |
| 3203 | * a compiler barrier which is insufficient for our purpose here. |
| 3204 | */ |
| 3205 | smp_store_mb(engine->irq_posted, 0); |
Chris Wilson | ed454f2 | 2017-07-21 13:32:29 +0100 | [diff] [blame] | 3206 | |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3207 | if (request) |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3208 | request = i915_gem_reset_request(engine, request, stalled); |
Chris Wilson | d1d1ebf4 | 2017-07-21 13:32:33 +0100 | [diff] [blame] | 3209 | |
| 3210 | if (request) { |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3211 | DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n", |
| 3212 | engine->name, request->global_seqno); |
Chris Wilson | c0dcb20 | 2017-02-07 15:24:37 +0000 | [diff] [blame] | 3213 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3214 | |
| 3215 | /* Setup the CS to resume from the breadcrumb of the hung request */ |
| 3216 | engine->reset_hw(engine, request); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3217 | } |
| 3218 | |
Chris Wilson | d0667e9 | 2018-04-06 23:03:54 +0100 | [diff] [blame] | 3219 | void i915_gem_reset(struct drm_i915_private *dev_priv, |
| 3220 | unsigned int stalled_mask) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3221 | { |
| 3222 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3223 | enum intel_engine_id id; |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3224 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3225 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
| 3226 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3227 | i915_retire_requests(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3228 | |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3229 | for_each_engine(engine, dev_priv, id) { |
| 3230 | struct i915_gem_context *ctx; |
| 3231 | |
Chris Wilson | bba0869 | 2018-04-06 23:03:53 +0100 | [diff] [blame] | 3232 | i915_gem_reset_engine(engine, |
| 3233 | engine->hangcheck.active_request, |
Chris Wilson | d0667e9 | 2018-04-06 23:03:54 +0100 | [diff] [blame] | 3234 | stalled_mask & ENGINE_MASK(id)); |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3235 | ctx = fetch_and_zero(&engine->last_retired_context); |
| 3236 | if (ctx) |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 3237 | intel_context_unpin(ctx, engine); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3238 | |
| 3239 | /* |
| 3240 | * Ostensibily, we always want a context loaded for powersaving, |
| 3241 | * so if the engine is idle after the reset, send a request |
| 3242 | * to load our scratch kernel_context. |
| 3243 | * |
| 3244 | * More mysteriously, if we leave the engine idle after a reset, |
| 3245 | * the next userspace batch may hang, with what appears to be |
| 3246 | * an incoherent read by the CS (presumably stale TLB). An |
| 3247 | * empty request appears sufficient to paper over the glitch. |
| 3248 | */ |
Chris Wilson | 01b8fdc | 2018-02-05 15:24:31 +0000 | [diff] [blame] | 3249 | if (intel_engine_is_idle(engine)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3250 | struct i915_request *rq; |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3251 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3252 | rq = i915_request_alloc(engine, |
| 3253 | dev_priv->kernel_context); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3254 | if (!IS_ERR(rq)) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3255 | __i915_request_add(rq, false); |
Chris Wilson | 7b6da81 | 2017-12-16 00:03:34 +0000 | [diff] [blame] | 3256 | } |
Chris Wilson | 2ae5573 | 2017-02-12 17:20:02 +0000 | [diff] [blame] | 3257 | } |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3258 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 3259 | i915_gem_restore_fences(dev_priv); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3260 | } |
| 3261 | |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3262 | void i915_gem_reset_finish_engine(struct intel_engine_cs *engine) |
| 3263 | { |
Sagar Arun Kamble | c6dce8f | 2017-11-16 19:02:37 +0530 | [diff] [blame] | 3264 | tasklet_enable(&engine->execlists.tasklet); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3265 | kthread_unpark(engine->breadcrumbs.signaler); |
Chris Wilson | 1749d90 | 2017-10-09 12:02:59 +0100 | [diff] [blame] | 3266 | |
| 3267 | intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL); |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3268 | } |
| 3269 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3270 | void i915_gem_reset_finish(struct drm_i915_private *dev_priv) |
| 3271 | { |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3272 | struct intel_engine_cs *engine; |
| 3273 | enum intel_engine_id id; |
| 3274 | |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3275 | lockdep_assert_held(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1f7b847 | 2017-02-08 14:30:33 +0000 | [diff] [blame] | 3276 | |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3277 | for_each_engine(engine, dev_priv, id) { |
Michel Thierry | c64992e | 2017-06-20 10:57:44 +0100 | [diff] [blame] | 3278 | engine->hangcheck.active_request = NULL; |
Michel Thierry | a1ef70e | 2017-06-20 10:57:47 +0100 | [diff] [blame] | 3279 | i915_gem_reset_finish_engine(engine); |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 3280 | } |
Chris Wilson | d802709 | 2017-02-08 14:30:32 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3283 | static void nop_submit_request(struct i915_request *request) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3284 | { |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3285 | GEM_TRACE("%s fence %llx:%d -> -EIO\n", |
| 3286 | request->engine->name, |
| 3287 | request->fence.context, request->fence.seqno); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3288 | dma_fence_set_error(&request->fence, -EIO); |
| 3289 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3290 | i915_request_submit(request); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3291 | } |
| 3292 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3293 | static void nop_complete_submit_request(struct i915_request *request) |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3294 | { |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3295 | unsigned long flags; |
| 3296 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3297 | GEM_TRACE("%s fence %llx:%d -> -EIO\n", |
| 3298 | request->engine->name, |
| 3299 | request->fence.context, request->fence.seqno); |
Chris Wilson | 3cd9442 | 2017-01-10 17:22:45 +0000 | [diff] [blame] | 3300 | dma_fence_set_error(&request->fence, -EIO); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3301 | |
| 3302 | spin_lock_irqsave(&request->engine->timeline->lock, flags); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3303 | __i915_request_submit(request); |
Chris Wilson | 3dcf93f7 | 2016-11-22 14:41:20 +0000 | [diff] [blame] | 3304 | intel_engine_init_global_seqno(request->engine, request->global_seqno); |
Chris Wilson | 8d55082 | 2017-10-06 12:56:17 +0100 | [diff] [blame] | 3305 | spin_unlock_irqrestore(&request->engine->timeline->lock, flags); |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3306 | } |
| 3307 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3308 | void i915_gem_set_wedged(struct drm_i915_private *i915) |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 3309 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 3310 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 3311 | enum intel_engine_id id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3312 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3313 | GEM_TRACE("start\n"); |
| 3314 | |
Chris Wilson | 7f961d7 | 2018-04-26 11:32:19 +0100 | [diff] [blame] | 3315 | if (GEM_SHOW_DEBUG()) { |
Chris Wilson | 559e040 | 2018-02-05 09:21:59 +0000 | [diff] [blame] | 3316 | struct drm_printer p = drm_debug_printer(__func__); |
| 3317 | |
| 3318 | for_each_engine(engine, i915, id) |
| 3319 | intel_engine_dump(engine, &p, "%s\n", engine->name); |
| 3320 | } |
| 3321 | |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3322 | set_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3323 | smp_mb__after_atomic(); |
| 3324 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3325 | /* |
| 3326 | * First, stop submission to hw, but do not yet complete requests by |
| 3327 | * rolling the global seqno forward (since this would complete requests |
| 3328 | * for which we haven't set the fence error to EIO yet). |
| 3329 | */ |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3330 | for_each_engine(engine, i915, id) { |
| 3331 | i915_gem_reset_prepare_engine(engine); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3332 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3333 | engine->submit_request = nop_submit_request; |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3334 | engine->schedule = NULL; |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3335 | } |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 3336 | i915->caps.scheduler = 0; |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3337 | |
Chris Wilson | ac697ae | 2018-03-15 15:10:15 +0000 | [diff] [blame] | 3338 | /* Even if the GPU reset fails, it should still stop the engines */ |
| 3339 | intel_gpu_reset(i915, ALL_ENGINES); |
| 3340 | |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3341 | /* |
| 3342 | * Make sure no one is running the old callback before we proceed with |
| 3343 | * cancelling requests and resetting the completion tracking. Otherwise |
| 3344 | * we might submit a request to the hardware which never completes. |
| 3345 | */ |
| 3346 | synchronize_rcu(); |
| 3347 | |
| 3348 | for_each_engine(engine, i915, id) { |
| 3349 | /* Mark all executing requests as skipped */ |
| 3350 | engine->cancel_requests(engine); |
| 3351 | |
| 3352 | /* |
| 3353 | * Only once we've force-cancelled all in-flight requests can we |
| 3354 | * start to complete all requests. |
| 3355 | */ |
| 3356 | engine->submit_request = nop_complete_submit_request; |
| 3357 | } |
| 3358 | |
| 3359 | /* |
| 3360 | * Make sure no request can slip through without getting completed by |
| 3361 | * either this call here to intel_engine_init_global_seqno, or the one |
| 3362 | * in nop_complete_submit_request. |
| 3363 | */ |
| 3364 | synchronize_rcu(); |
| 3365 | |
| 3366 | for_each_engine(engine, i915, id) { |
| 3367 | unsigned long flags; |
| 3368 | |
Chris Wilson | 0d73e7a | 2018-02-07 15:13:50 +0000 | [diff] [blame] | 3369 | /* |
| 3370 | * Mark all pending requests as complete so that any concurrent |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3371 | * (lockless) lookup doesn't try and wait upon the request as we |
| 3372 | * reset it. |
| 3373 | */ |
| 3374 | spin_lock_irqsave(&engine->timeline->lock, flags); |
| 3375 | intel_engine_init_global_seqno(engine, |
| 3376 | intel_engine_last_submit(engine)); |
| 3377 | spin_unlock_irqrestore(&engine->timeline->lock, flags); |
Chris Wilson | 963ddd6 | 2018-03-02 11:33:24 +0000 | [diff] [blame] | 3378 | |
| 3379 | i915_gem_reset_finish_engine(engine); |
Daniel Vetter | af7a8ff | 2017-10-11 11:10:19 +0200 | [diff] [blame] | 3380 | } |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 3381 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3382 | GEM_TRACE("end\n"); |
| 3383 | |
Chris Wilson | 3d7adbb | 2017-07-21 13:32:27 +0100 | [diff] [blame] | 3384 | wake_up_all(&i915->gpu_error.reset_queue); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3385 | } |
| 3386 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3387 | bool i915_gem_unset_wedged(struct drm_i915_private *i915) |
| 3388 | { |
| 3389 | struct i915_gem_timeline *tl; |
| 3390 | int i; |
| 3391 | |
| 3392 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3393 | if (!test_bit(I915_WEDGED, &i915->gpu_error.flags)) |
| 3394 | return true; |
| 3395 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3396 | GEM_TRACE("start\n"); |
| 3397 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3398 | /* |
| 3399 | * Before unwedging, make sure that all pending operations |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3400 | * are flushed and errored out - we may have requests waiting upon |
| 3401 | * third party fences. We marked all inflight requests as EIO, and |
| 3402 | * every execbuf since returned EIO, for consistency we want all |
| 3403 | * the currently pending requests to also be marked as EIO, which |
| 3404 | * is done inside our nop_submit_request - and so we must wait. |
| 3405 | * |
| 3406 | * No more can be submitted until we reset the wedged bit. |
| 3407 | */ |
| 3408 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3409 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3410 | struct i915_request *rq; |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3411 | |
| 3412 | rq = i915_gem_active_peek(&tl->engine[i].last_request, |
| 3413 | &i915->drm.struct_mutex); |
| 3414 | if (!rq) |
| 3415 | continue; |
| 3416 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3417 | /* |
| 3418 | * We can't use our normal waiter as we want to |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3419 | * avoid recursively trying to handle the current |
| 3420 | * reset. The basic dma_fence_default_wait() installs |
| 3421 | * a callback for dma_fence_signal(), which is |
| 3422 | * triggered by our nop handler (indirectly, the |
| 3423 | * callback enables the signaler thread which is |
| 3424 | * woken by the nop_submit_request() advancing the seqno |
| 3425 | * and when the seqno passes the fence, the signaler |
| 3426 | * then signals the fence waking us up). |
| 3427 | */ |
| 3428 | if (dma_fence_default_wait(&rq->fence, true, |
| 3429 | MAX_SCHEDULE_TIMEOUT) < 0) |
| 3430 | return false; |
| 3431 | } |
| 3432 | } |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3433 | i915_retire_requests(i915); |
| 3434 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3435 | |
Chris Wilson | 2d4ecac | 2018-03-07 13:42:21 +0000 | [diff] [blame] | 3436 | /* |
| 3437 | * Undo nop_submit_request. We prevent all new i915 requests from |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3438 | * being queued (by disallowing execbuf whilst wedged) so having |
| 3439 | * waited for all active requests above, we know the system is idle |
| 3440 | * and do not have to worry about a thread being inside |
| 3441 | * engine->submit_request() as we swap over. So unlike installing |
| 3442 | * the nop_submit_request on reset, we can do this from normal |
| 3443 | * context and do not require stop_machine(). |
| 3444 | */ |
| 3445 | intel_engines_reset_default_submission(i915); |
Chris Wilson | 36703e7 | 2017-06-22 11:56:25 +0100 | [diff] [blame] | 3446 | i915_gem_contexts_lost(i915); |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3447 | |
Chris Wilson | d9b13c4 | 2018-03-15 13:14:50 +0000 | [diff] [blame] | 3448 | GEM_TRACE("end\n"); |
| 3449 | |
Chris Wilson | 2e8f9d3 | 2017-03-16 17:13:04 +0000 | [diff] [blame] | 3450 | smp_mb__before_atomic(); /* complete takeover before enabling execbuf */ |
| 3451 | clear_bit(I915_WEDGED, &i915->gpu_error.flags); |
| 3452 | |
| 3453 | return true; |
| 3454 | } |
| 3455 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 3456 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3457 | i915_gem_retire_work_handler(struct work_struct *work) |
| 3458 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3459 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3460 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 3461 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3462 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3463 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3464 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3465 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3466 | mutex_unlock(&dev->struct_mutex); |
| 3467 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3468 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3469 | /* |
| 3470 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3471 | * We do not need to do this test under locking as in the worst-case |
| 3472 | * we queue the retire worker once too often. |
| 3473 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 3474 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3475 | queue_delayed_work(dev_priv->wq, |
| 3476 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 3477 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3478 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 3479 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3480 | static void shrink_caches(struct drm_i915_private *i915) |
| 3481 | { |
| 3482 | /* |
| 3483 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 3484 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 3485 | * with the aim of reducing fragmentation. |
| 3486 | */ |
| 3487 | kmem_cache_shrink(i915->priorities); |
| 3488 | kmem_cache_shrink(i915->dependencies); |
| 3489 | kmem_cache_shrink(i915->requests); |
| 3490 | kmem_cache_shrink(i915->luts); |
| 3491 | kmem_cache_shrink(i915->vmas); |
| 3492 | kmem_cache_shrink(i915->objects); |
| 3493 | } |
| 3494 | |
| 3495 | struct sleep_rcu_work { |
| 3496 | union { |
| 3497 | struct rcu_head rcu; |
| 3498 | struct work_struct work; |
| 3499 | }; |
| 3500 | struct drm_i915_private *i915; |
| 3501 | unsigned int epoch; |
| 3502 | }; |
| 3503 | |
| 3504 | static inline bool |
| 3505 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 3506 | { |
| 3507 | /* |
| 3508 | * There is a small chance that the epoch wrapped since we started |
| 3509 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 3510 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 3511 | */ |
| 3512 | return epoch == READ_ONCE(i915->gt.epoch); |
| 3513 | } |
| 3514 | |
| 3515 | static void __sleep_work(struct work_struct *work) |
| 3516 | { |
| 3517 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 3518 | struct drm_i915_private *i915 = s->i915; |
| 3519 | unsigned int epoch = s->epoch; |
| 3520 | |
| 3521 | kfree(s); |
| 3522 | if (same_epoch(i915, epoch)) |
| 3523 | shrink_caches(i915); |
| 3524 | } |
| 3525 | |
| 3526 | static void __sleep_rcu(struct rcu_head *rcu) |
| 3527 | { |
| 3528 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 3529 | struct drm_i915_private *i915 = s->i915; |
| 3530 | |
| 3531 | if (same_epoch(i915, s->epoch)) { |
| 3532 | INIT_WORK(&s->work, __sleep_work); |
| 3533 | queue_work(i915->wq, &s->work); |
| 3534 | } else { |
| 3535 | kfree(s); |
| 3536 | } |
| 3537 | } |
| 3538 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3539 | static inline bool |
| 3540 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 3541 | { |
| 3542 | return (READ_ONCE(i915->gt.active_requests) || |
| 3543 | work_pending(&i915->gt.idle_work.work)); |
| 3544 | } |
| 3545 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 3546 | static void |
| 3547 | i915_gem_idle_work_handler(struct work_struct *work) |
| 3548 | { |
| 3549 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3550 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3551 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3552 | bool rearm_hangcheck; |
| 3553 | |
| 3554 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 3555 | return; |
| 3556 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3557 | /* |
| 3558 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3559 | * new request is submitted. As we don't trust the hardware, we |
| 3560 | * continue on if the wait times out. This is necessary to allow |
| 3561 | * the machine to suspend even if the hardware dies, and we will |
| 3562 | * try to recover in resume (after depriving the hardware of power, |
| 3563 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3564 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3565 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3566 | intel_engines_are_idle(dev_priv), |
| 3567 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3568 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3569 | |
| 3570 | rearm_hangcheck = |
| 3571 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3572 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3573 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3574 | /* Currently busy, come back later */ |
| 3575 | mod_delayed_work(dev_priv->wq, |
| 3576 | &dev_priv->gt.idle_work, |
| 3577 | msecs_to_jiffies(50)); |
| 3578 | goto out_rearm; |
| 3579 | } |
| 3580 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3581 | /* |
| 3582 | * New request retired after this work handler started, extend active |
| 3583 | * period until next instance of the work. |
| 3584 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3585 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3586 | goto out_unlock; |
| 3587 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 3588 | epoch = __i915_gem_park(dev_priv); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3589 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3590 | rearm_hangcheck = false; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3591 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3592 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3593 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3594 | out_rearm: |
| 3595 | if (rearm_hangcheck) { |
| 3596 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3597 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3598 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3599 | |
| 3600 | /* |
| 3601 | * When we are idle, it is an opportune time to reap our caches. |
| 3602 | * However, we have many objects that utilise RCU and the ordered |
| 3603 | * i915->wq that this work is executing on. To try and flush any |
| 3604 | * pending frees now we are idle, we first wait for an RCU grace |
| 3605 | * period, and then queue a task (that will run last on the wq) to |
| 3606 | * shrink and re-optimize the caches. |
| 3607 | */ |
| 3608 | if (same_epoch(dev_priv, epoch)) { |
| 3609 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3610 | if (s) { |
| 3611 | s->i915 = dev_priv; |
| 3612 | s->epoch = epoch; |
| 3613 | call_rcu(&s->rcu, __sleep_rcu); |
| 3614 | } |
| 3615 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3616 | } |
| 3617 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3618 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3619 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3620 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3621 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3622 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3623 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3624 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3625 | mutex_lock(&i915->drm.struct_mutex); |
| 3626 | |
| 3627 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3628 | struct i915_gem_context *ctx = lut->ctx; |
| 3629 | struct i915_vma *vma; |
| 3630 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3631 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3632 | if (ctx->file_priv != fpriv) |
| 3633 | continue; |
| 3634 | |
| 3635 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3636 | GEM_BUG_ON(vma->obj != obj); |
| 3637 | |
| 3638 | /* We allow the process to have multiple handles to the same |
| 3639 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3640 | */ |
| 3641 | GEM_BUG_ON(!vma->open_count); |
| 3642 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3643 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3644 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3645 | list_del(&lut->obj_link); |
| 3646 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3647 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3648 | kmem_cache_free(i915->luts, lut); |
| 3649 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3650 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3651 | |
| 3652 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3653 | } |
| 3654 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3655 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3656 | { |
| 3657 | if (timeout_ns < 0) |
| 3658 | return MAX_SCHEDULE_TIMEOUT; |
| 3659 | |
| 3660 | if (timeout_ns == 0) |
| 3661 | return 0; |
| 3662 | |
| 3663 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3664 | } |
| 3665 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3666 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3667 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3668 | * @dev: drm device pointer |
| 3669 | * @data: ioctl data blob |
| 3670 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3671 | * |
| 3672 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3673 | * the timeout parameter. |
| 3674 | * -ETIME: object is still busy after timeout |
| 3675 | * -ERESTARTSYS: signal interrupted the wait |
| 3676 | * -ENONENT: object doesn't exist |
| 3677 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3678 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3679 | * -ENOMEM: damn |
| 3680 | * -ENODEV: Internal IRQ fail |
| 3681 | * -E?: The add request failed |
| 3682 | * |
| 3683 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3684 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3685 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3686 | * without holding struct_mutex the object may become re-busied before this |
| 3687 | * function completes. A similar but shorter * race condition exists in the busy |
| 3688 | * ioctl |
| 3689 | */ |
| 3690 | int |
| 3691 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3692 | { |
| 3693 | struct drm_i915_gem_wait *args = data; |
| 3694 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3695 | ktime_t start; |
| 3696 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3697 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3698 | if (args->flags != 0) |
| 3699 | return -EINVAL; |
| 3700 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3701 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3702 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3703 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3704 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3705 | start = ktime_get(); |
| 3706 | |
| 3707 | ret = i915_gem_object_wait(obj, |
| 3708 | I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL, |
| 3709 | to_wait_timeout(args->timeout_ns), |
| 3710 | to_rps_client(file)); |
| 3711 | |
| 3712 | if (args->timeout_ns > 0) { |
| 3713 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3714 | if (args->timeout_ns < 0) |
| 3715 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3716 | |
| 3717 | /* |
| 3718 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3719 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3720 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3721 | * |
| 3722 | * This is a regression from the timespec->ktime conversion. |
| 3723 | */ |
| 3724 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3725 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3726 | |
| 3727 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3728 | if (ret == -ETIME && args->timeout_ns) |
| 3729 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3730 | } |
| 3731 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3732 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3733 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3734 | } |
| 3735 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3736 | 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] | 3737 | { |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3738 | int ret, i; |
| 3739 | |
| 3740 | for (i = 0; i < ARRAY_SIZE(tl->engine); i++) { |
| 3741 | ret = i915_gem_active_wait(&tl->engine[i].last_request, flags); |
| 3742 | if (ret) |
| 3743 | return ret; |
| 3744 | } |
| 3745 | |
| 3746 | return 0; |
| 3747 | } |
| 3748 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3749 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3750 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3751 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3752 | dev_err(i915->drm.dev, |
| 3753 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3754 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3755 | i915_gem_set_wedged(i915); |
| 3756 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | return 0; |
| 3760 | } |
| 3761 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3762 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags) |
| 3763 | { |
Dave Gordon | b4ac5af | 2016-03-24 11:20:38 +0000 | [diff] [blame] | 3764 | int ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3765 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3766 | /* If the device is asleep, we have no requests outstanding */ |
| 3767 | if (!READ_ONCE(i915->gt.awake)) |
| 3768 | return 0; |
| 3769 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3770 | if (flags & I915_WAIT_LOCKED) { |
| 3771 | struct i915_gem_timeline *tl; |
| 3772 | |
| 3773 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3774 | |
| 3775 | list_for_each_entry(tl, &i915->gt.timelines, link) { |
| 3776 | ret = wait_for_timeline(tl, flags); |
| 3777 | if (ret) |
| 3778 | return ret; |
| 3779 | } |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3780 | i915_retire_requests(i915); |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3781 | |
| 3782 | ret = wait_for_engines(i915); |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3783 | } else { |
| 3784 | ret = wait_for_timeline(&i915->gt.global_timeline, flags); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 3785 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3786 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3787 | return ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3788 | } |
| 3789 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3790 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3791 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3792 | /* |
| 3793 | * We manually flush the CPU domain so that we can override and |
| 3794 | * force the flush for the display, and perform it asyncrhonously. |
| 3795 | */ |
| 3796 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3797 | if (obj->cache_dirty) |
| 3798 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3799 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3803 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3804 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3805 | return; |
| 3806 | |
| 3807 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3808 | __i915_gem_object_flush_for_display(obj); |
| 3809 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3810 | } |
| 3811 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3812 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3813 | * Moves a single object to the WC read, and possibly write domain. |
| 3814 | * @obj: object to act on |
| 3815 | * @write: ask for write access or read only |
| 3816 | * |
| 3817 | * This function returns when the move is complete, including waiting on |
| 3818 | * flushes to occur. |
| 3819 | */ |
| 3820 | int |
| 3821 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3822 | { |
| 3823 | int ret; |
| 3824 | |
| 3825 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3826 | |
| 3827 | ret = i915_gem_object_wait(obj, |
| 3828 | I915_WAIT_INTERRUPTIBLE | |
| 3829 | I915_WAIT_LOCKED | |
| 3830 | (write ? I915_WAIT_ALL : 0), |
| 3831 | MAX_SCHEDULE_TIMEOUT, |
| 3832 | NULL); |
| 3833 | if (ret) |
| 3834 | return ret; |
| 3835 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3836 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3837 | return 0; |
| 3838 | |
| 3839 | /* Flush and acquire obj->pages so that we are coherent through |
| 3840 | * direct access in memory with previous cached writes through |
| 3841 | * shmemfs and that our cache domain tracking remains valid. |
| 3842 | * For example, if the obj->filp was moved to swap without us |
| 3843 | * being notified and releasing the pages, we would mistakenly |
| 3844 | * continue to assume that the obj remained out of the CPU cached |
| 3845 | * domain. |
| 3846 | */ |
| 3847 | ret = i915_gem_object_pin_pages(obj); |
| 3848 | if (ret) |
| 3849 | return ret; |
| 3850 | |
| 3851 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3852 | |
| 3853 | /* Serialise direct access to this object with the barriers for |
| 3854 | * coherent writes from the GPU, by effectively invalidating the |
| 3855 | * WC domain upon first access. |
| 3856 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3857 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3858 | mb(); |
| 3859 | |
| 3860 | /* It should now be out of any other write domains, and we can update |
| 3861 | * the domain values for our changes. |
| 3862 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3863 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3864 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3865 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3866 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3867 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3868 | obj->mm.dirty = true; |
| 3869 | } |
| 3870 | |
| 3871 | i915_gem_object_unpin_pages(obj); |
| 3872 | return 0; |
| 3873 | } |
| 3874 | |
| 3875 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3876 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3877 | * @obj: object to act on |
| 3878 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3879 | * |
| 3880 | * This function returns when the move is complete, including waiting on |
| 3881 | * flushes to occur. |
| 3882 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3883 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3884 | 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] | 3885 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3886 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3887 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3888 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3889 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3890 | ret = i915_gem_object_wait(obj, |
| 3891 | I915_WAIT_INTERRUPTIBLE | |
| 3892 | I915_WAIT_LOCKED | |
| 3893 | (write ? I915_WAIT_ALL : 0), |
| 3894 | MAX_SCHEDULE_TIMEOUT, |
| 3895 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3896 | if (ret) |
| 3897 | return ret; |
| 3898 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3899 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3900 | return 0; |
| 3901 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3902 | /* Flush and acquire obj->pages so that we are coherent through |
| 3903 | * direct access in memory with previous cached writes through |
| 3904 | * shmemfs and that our cache domain tracking remains valid. |
| 3905 | * For example, if the obj->filp was moved to swap without us |
| 3906 | * being notified and releasing the pages, we would mistakenly |
| 3907 | * continue to assume that the obj remained out of the CPU cached |
| 3908 | * domain. |
| 3909 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3910 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3911 | if (ret) |
| 3912 | return ret; |
| 3913 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3914 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3915 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3916 | /* Serialise direct access to this object with the barriers for |
| 3917 | * coherent writes from the GPU, by effectively invalidating the |
| 3918 | * GTT domain upon first access. |
| 3919 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3920 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3921 | mb(); |
| 3922 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3923 | /* It should now be out of any other write domains, and we can update |
| 3924 | * the domain values for our changes. |
| 3925 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3926 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3927 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3928 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3929 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3930 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3931 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3932 | } |
| 3933 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3934 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3935 | return 0; |
| 3936 | } |
| 3937 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3938 | /** |
| 3939 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3940 | * @obj: object to act on |
| 3941 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3942 | * |
| 3943 | * After this function returns, the object will be in the new cache-level |
| 3944 | * across all GTT and the contents of the backing storage will be coherent, |
| 3945 | * with respect to the new cache-level. In order to keep the backing storage |
| 3946 | * coherent for all users, we only allow a single cache level to be set |
| 3947 | * globally on the object and prevent it from being changed whilst the |
| 3948 | * hardware is reading from the object. That is if the object is currently |
| 3949 | * on the scanout it will be set to uncached (or equivalent display |
| 3950 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3951 | * that all direct access to the scanout remains coherent. |
| 3952 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3953 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3954 | enum i915_cache_level cache_level) |
| 3955 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3956 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3957 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3958 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3959 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3960 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3961 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3962 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3963 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3964 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3965 | * be invalid given the new cache-level. This is principally to |
| 3966 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3967 | * reading an invalid PTE on older architectures. |
| 3968 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3969 | restart: |
| 3970 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3971 | if (!drm_mm_node_allocated(&vma->node)) |
| 3972 | continue; |
| 3973 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3974 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3975 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3976 | return -EBUSY; |
| 3977 | } |
| 3978 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3979 | if (!i915_vma_is_closed(vma) && |
| 3980 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3981 | continue; |
| 3982 | |
| 3983 | ret = i915_vma_unbind(vma); |
| 3984 | if (ret) |
| 3985 | return ret; |
| 3986 | |
| 3987 | /* As unbinding may affect other elements in the |
| 3988 | * obj->vma_list (due to side-effects from retiring |
| 3989 | * an active vma), play safe and restart the iterator. |
| 3990 | */ |
| 3991 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3992 | } |
| 3993 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3994 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3995 | * cache-level on the PTE. We could simply unbind them all and |
| 3996 | * rebind with the correct cache-level on next use. However since |
| 3997 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3998 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3999 | * state and so involves less work. |
| 4000 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 4001 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4002 | /* Before we change the PTE, the GPU must not be accessing it. |
| 4003 | * If we wait upon the object, we know that all the bound |
| 4004 | * VMA are no longer active. |
| 4005 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4006 | ret = i915_gem_object_wait(obj, |
| 4007 | I915_WAIT_INTERRUPTIBLE | |
| 4008 | I915_WAIT_LOCKED | |
| 4009 | I915_WAIT_ALL, |
| 4010 | MAX_SCHEDULE_TIMEOUT, |
| 4011 | NULL); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4012 | if (ret) |
| 4013 | return ret; |
| 4014 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4015 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 4016 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4017 | /* Access to snoopable pages through the GTT is |
| 4018 | * incoherent and on some machines causes a hard |
| 4019 | * lockup. Relinquish the CPU mmaping to force |
| 4020 | * userspace to refault in the pages and we can |
| 4021 | * then double check if the GTT mapping is still |
| 4022 | * valid for that pointer access. |
| 4023 | */ |
| 4024 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4025 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4026 | /* As we no longer need a fence for GTT access, |
| 4027 | * we can relinquish it now (and so prevent having |
| 4028 | * to steal a fence from someone else on the next |
| 4029 | * fence request). Note GPU activity would have |
| 4030 | * dropped the fence as all snoopable access is |
| 4031 | * supposed to be linear. |
| 4032 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 4033 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 4034 | ret = i915_vma_put_fence(vma); |
| 4035 | if (ret) |
| 4036 | return ret; |
| 4037 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4038 | } else { |
| 4039 | /* We either have incoherent backing store and |
| 4040 | * so no GTT access or the architecture is fully |
| 4041 | * coherent. In such cases, existing GTT mmaps |
| 4042 | * ignore the cache bit in the PTE and we can |
| 4043 | * rewrite it without confusing the GPU or having |
| 4044 | * to force userspace to fault back in its mmaps. |
| 4045 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4046 | } |
| 4047 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 4048 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 4049 | if (!drm_mm_node_allocated(&vma->node)) |
| 4050 | continue; |
| 4051 | |
| 4052 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 4053 | if (ret) |
| 4054 | return ret; |
| 4055 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4056 | } |
| 4057 | |
Chris Wilson | 1c7f4bc | 2016-02-26 11:03:19 +0000 | [diff] [blame] | 4058 | list_for_each_entry(vma, &obj->vma_list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 4059 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4060 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4061 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 4062 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 4063 | return 0; |
| 4064 | } |
| 4065 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4066 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 4067 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4068 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4069 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4070 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4071 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4072 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4073 | rcu_read_lock(); |
| 4074 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 4075 | if (!obj) { |
| 4076 | err = -ENOENT; |
| 4077 | goto out; |
| 4078 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4079 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4080 | switch (obj->cache_level) { |
| 4081 | case I915_CACHE_LLC: |
| 4082 | case I915_CACHE_L3_LLC: |
| 4083 | args->caching = I915_CACHING_CACHED; |
| 4084 | break; |
| 4085 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4086 | case I915_CACHE_WT: |
| 4087 | args->caching = I915_CACHING_DISPLAY; |
| 4088 | break; |
| 4089 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4090 | default: |
| 4091 | args->caching = I915_CACHING_NONE; |
| 4092 | break; |
| 4093 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4094 | out: |
| 4095 | rcu_read_unlock(); |
| 4096 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4097 | } |
| 4098 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4099 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 4100 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4101 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4102 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4103 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4104 | struct drm_i915_gem_object *obj; |
| 4105 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4106 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4107 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4108 | switch (args->caching) { |
| 4109 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4110 | level = I915_CACHE_NONE; |
| 4111 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 4112 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4113 | /* |
| 4114 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 4115 | * snooped mapping may leave stale data in a corresponding CPU |
| 4116 | * cacheline, whereas normally such cachelines would get |
| 4117 | * invalidated. |
| 4118 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4119 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 4120 | return -ENODEV; |
| 4121 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4122 | level = I915_CACHE_LLC; |
| 4123 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4124 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 4125 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 4126 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4127 | default: |
| 4128 | return -EINVAL; |
| 4129 | } |
| 4130 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4131 | obj = i915_gem_object_lookup(file, args->handle); |
| 4132 | if (!obj) |
| 4133 | return -ENOENT; |
| 4134 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 4135 | /* |
| 4136 | * The caching mode of proxy object is handled by its generator, and |
| 4137 | * not allowed to be changed by userspace. |
| 4138 | */ |
| 4139 | if (i915_gem_object_is_proxy(obj)) { |
| 4140 | ret = -ENXIO; |
| 4141 | goto out; |
| 4142 | } |
| 4143 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4144 | if (obj->cache_level == level) |
| 4145 | goto out; |
| 4146 | |
| 4147 | ret = i915_gem_object_wait(obj, |
| 4148 | I915_WAIT_INTERRUPTIBLE, |
| 4149 | MAX_SCHEDULE_TIMEOUT, |
| 4150 | to_rps_client(file)); |
| 4151 | if (ret) |
| 4152 | goto out; |
| 4153 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 4154 | ret = i915_mutex_lock_interruptible(dev); |
| 4155 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4156 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4157 | |
| 4158 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4159 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 4160 | |
| 4161 | out: |
| 4162 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 4163 | return ret; |
| 4164 | } |
| 4165 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4166 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 4167 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 4168 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 4169 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 4170 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4171 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4172 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4173 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 4174 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4175 | const struct i915_ggtt_view *view, |
| 4176 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4177 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4178 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4179 | int ret; |
| 4180 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4181 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4182 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4183 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4184 | * display coherency whilst setting up the cache domains. |
| 4185 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4186 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4187 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4188 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 4189 | * a result, we make sure that the pinning that is about to occur is |
| 4190 | * done with uncached PTEs. This is lowest common denominator for all |
| 4191 | * chipsets. |
| 4192 | * |
| 4193 | * However for gen6+, we could do better by using the GFDT bit instead |
| 4194 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 4195 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 4196 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 4197 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 4198 | HAS_WT(to_i915(obj->base.dev)) ? |
| 4199 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4200 | if (ret) { |
| 4201 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4202 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4203 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 4204 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4205 | /* As the user may map the buffer once pinned in the display plane |
| 4206 | * (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] | 4207 | * always use map_and_fenceable for all scanout buffers. However, |
| 4208 | * it may simply be too big to fit into mappable, in which case |
| 4209 | * put it anyway and hope that userspace can cope (but always first |
| 4210 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4211 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4212 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4213 | if ((flags & PIN_MAPPABLE) == 0 && |
| 4214 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 4215 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 4216 | flags | |
| 4217 | PIN_MAPPABLE | |
| 4218 | PIN_NONBLOCK); |
| 4219 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 4220 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4221 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4222 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4223 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 4224 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 4225 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 4226 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 4227 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 4228 | /* It should now be out of any other write domains, and we can update |
| 4229 | * the domain values for our changes. |
| 4230 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4231 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4232 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4233 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4234 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4235 | err_unpin_global: |
| 4236 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4237 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4238 | } |
| 4239 | |
| 4240 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4241 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 4242 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 4243 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4244 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4245 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 4246 | return; |
| 4247 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 4248 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 4249 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 4250 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4251 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 4252 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 4253 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4254 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 4255 | } |
| 4256 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4257 | /** |
| 4258 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 4259 | * @obj: object to act on |
| 4260 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4261 | * |
| 4262 | * This function returns when the move is complete, including waiting on |
| 4263 | * flushes to occur. |
| 4264 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 4265 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 4266 | 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] | 4267 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4268 | int ret; |
| 4269 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4270 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4271 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4272 | ret = i915_gem_object_wait(obj, |
| 4273 | I915_WAIT_INTERRUPTIBLE | |
| 4274 | I915_WAIT_LOCKED | |
| 4275 | (write ? I915_WAIT_ALL : 0), |
| 4276 | MAX_SCHEDULE_TIMEOUT, |
| 4277 | NULL); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 4278 | if (ret) |
| 4279 | return ret; |
| 4280 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 4281 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4282 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4283 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4284 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4285 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4286 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | /* It should now be out of any other write domains, and we can update |
| 4290 | * the domain values for our changes. |
| 4291 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4292 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4293 | |
| 4294 | /* If we're writing through the CPU, then the GPU read domains will |
| 4295 | * need to be invalidated at next use. |
| 4296 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4297 | if (write) |
| 4298 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 4299 | |
| 4300 | return 0; |
| 4301 | } |
| 4302 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4303 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 4304 | * emitted over 20 msec ago. |
| 4305 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4306 | * Note that if we were to use the current jiffies each time around the loop, |
| 4307 | * we wouldn't escape the function with any frames outstanding if the time to |
| 4308 | * render a frame was over 20ms. |
| 4309 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4310 | * This should get us reasonable parallelism between CPU and GPU but also |
| 4311 | * relatively low latency when blocking on a particular request to finish. |
| 4312 | */ |
| 4313 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4314 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4315 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4316 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4317 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 4318 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4319 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4320 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4321 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 4322 | /* ABI: return -EIO if already wedged */ |
| 4323 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
| 4324 | return -EIO; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 4325 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4326 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4327 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4328 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 4329 | break; |
| 4330 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 4331 | if (target) { |
| 4332 | list_del(&target->client_link); |
| 4333 | target->file_priv = NULL; |
| 4334 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 4335 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4336 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4337 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4338 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4339 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4340 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4341 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 4342 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4343 | return 0; |
| 4344 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4345 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4346 | I915_WAIT_INTERRUPTIBLE, |
| 4347 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4348 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 4349 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 4350 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4351 | } |
| 4352 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4353 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4354 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 4355 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 4356 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 4357 | u64 alignment, |
| 4358 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4359 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4360 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 4361 | struct i915_address_space *vm = &dev_priv->ggtt.base; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4362 | struct i915_vma *vma; |
| 4363 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 4364 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 4365 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4366 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 4367 | if (flags & PIN_MAPPABLE && |
| 4368 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4369 | /* If the required space is larger than the available |
| 4370 | * aperture, we will not able to find a slot for the |
| 4371 | * object and unbinding the object now will be in |
| 4372 | * vain. Worse, doing so may cause us to ping-pong |
| 4373 | * the object in and out of the Global GTT and |
| 4374 | * waste a lot of cycles under the mutex. |
| 4375 | */ |
| 4376 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 4377 | return ERR_PTR(-E2BIG); |
| 4378 | |
| 4379 | /* If NONBLOCK is set the caller is optimistically |
| 4380 | * trying to cache the full object within the mappable |
| 4381 | * aperture, and *must* have a fallback in place for |
| 4382 | * situations where we cannot bind the object. We |
| 4383 | * can be a little more lax here and use the fallback |
| 4384 | * more often to avoid costly migrations of ourselves |
| 4385 | * and other objects within the aperture. |
| 4386 | * |
| 4387 | * Half-the-aperture is used as a simple heuristic. |
| 4388 | * More interesting would to do search for a free |
| 4389 | * block prior to making the commitment to unbind. |
| 4390 | * That caters for the self-harm case, and with a |
| 4391 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 4392 | * we could try to minimise harm to others. |
| 4393 | */ |
| 4394 | if (flags & PIN_NONBLOCK && |
| 4395 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 4396 | return ERR_PTR(-ENOSPC); |
| 4397 | } |
| 4398 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 4399 | vma = i915_vma_instance(obj, vm, view); |
Chris Wilson | e0216b7 | 2017-01-19 19:26:57 +0000 | [diff] [blame] | 4400 | if (unlikely(IS_ERR(vma))) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4401 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4402 | |
| 4403 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4404 | if (flags & PIN_NONBLOCK) { |
| 4405 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 4406 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4407 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 4408 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 4409 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 4410 | return ERR_PTR(-ENOSPC); |
| 4411 | } |
| 4412 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4413 | WARN(i915_vma_is_pinned(vma), |
| 4414 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4415 | " offset=%08x, req.alignment=%llx," |
| 4416 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 4417 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4418 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 4419 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4420 | ret = i915_vma_unbind(vma); |
| 4421 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4422 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 4423 | } |
| 4424 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4425 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 4426 | if (ret) |
| 4427 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 4428 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 4429 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4430 | } |
| 4431 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4432 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4433 | { |
| 4434 | /* Note that we could alias engines in the execbuf API, but |
| 4435 | * that would be very unwise as it prevents userspace from |
| 4436 | * fine control over engine selection. Ahem. |
| 4437 | * |
| 4438 | * This should be something like EXEC_MAX_ENGINE instead of |
| 4439 | * I915_NUM_ENGINES. |
| 4440 | */ |
| 4441 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 4442 | return 0x10000 << id; |
| 4443 | } |
| 4444 | |
| 4445 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 4446 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 4447 | /* The uABI guarantees an active writer is also amongst the read |
| 4448 | * engines. This would be true if we accessed the activity tracking |
| 4449 | * under the lock, but as we perform the lookup of the object and |
| 4450 | * its activity locklessly we can not guarantee that the last_write |
| 4451 | * being active implies that we have set the same engine flag from |
| 4452 | * last_read - hence we always set both read and write busy for |
| 4453 | * last_write. |
| 4454 | */ |
| 4455 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4456 | } |
| 4457 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4458 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4459 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4460 | unsigned int (*flag)(unsigned int id)) |
| 4461 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4462 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4463 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4464 | /* We have to check the current hw status of the fence as the uABI |
| 4465 | * guarantees forward progress. We could rely on the idle worker |
| 4466 | * to eventually flush us, but to minimise latency just ask the |
| 4467 | * hardware. |
| 4468 | * |
| 4469 | * Note we only report on the status of native fences. |
| 4470 | */ |
| 4471 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 4472 | return 0; |
| 4473 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4474 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4475 | rq = container_of(fence, struct i915_request, fence); |
| 4476 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4477 | return 0; |
| 4478 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 4479 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4480 | } |
| 4481 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4482 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4483 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4484 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4485 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4486 | } |
| 4487 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 4488 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4489 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4490 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4491 | if (!fence) |
| 4492 | return 0; |
| 4493 | |
| 4494 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 4495 | } |
| 4496 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4497 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4498 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4499 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4500 | { |
| 4501 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4502 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4503 | struct reservation_object_list *list; |
| 4504 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4505 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4506 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4507 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4508 | rcu_read_lock(); |
| 4509 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4510 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4511 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4512 | |
| 4513 | /* A discrepancy here is that we do not report the status of |
| 4514 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4515 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4516 | * This also means that wait-ioctl may report an object as busy, |
| 4517 | * where busy-ioctl considers it idle. |
| 4518 | * |
| 4519 | * We trade the ability to warn of foreign fences to report on which |
| 4520 | * i915 engines are active for the object. |
| 4521 | * |
| 4522 | * Alternatively, we can trade that extra information on read/write |
| 4523 | * activity with |
| 4524 | * args->busy = |
| 4525 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4526 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4527 | * |
| 4528 | */ |
| 4529 | retry: |
| 4530 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4531 | |
| 4532 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4533 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4534 | |
| 4535 | /* Translate shared fences to READ set of engines */ |
| 4536 | list = rcu_dereference(obj->resv->fence); |
| 4537 | if (list) { |
| 4538 | unsigned int shared_count = list->shared_count, i; |
| 4539 | |
| 4540 | for (i = 0; i < shared_count; ++i) { |
| 4541 | struct dma_fence *fence = |
| 4542 | rcu_dereference(list->shared[i]); |
| 4543 | |
| 4544 | args->busy |= busy_check_reader(fence); |
| 4545 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4546 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4547 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4548 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4549 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4550 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4551 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4552 | out: |
| 4553 | rcu_read_unlock(); |
| 4554 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | int |
| 4558 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4559 | struct drm_file *file_priv) |
| 4560 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4561 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4562 | } |
| 4563 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4564 | int |
| 4565 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4566 | struct drm_file *file_priv) |
| 4567 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4568 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4569 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4570 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4571 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4572 | |
| 4573 | switch (args->madv) { |
| 4574 | case I915_MADV_DONTNEED: |
| 4575 | case I915_MADV_WILLNEED: |
| 4576 | break; |
| 4577 | default: |
| 4578 | return -EINVAL; |
| 4579 | } |
| 4580 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4581 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4582 | if (!obj) |
| 4583 | return -ENOENT; |
| 4584 | |
| 4585 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4586 | if (err) |
| 4587 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4588 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4589 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4590 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4591 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4592 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4593 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4594 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4595 | obj->mm.quirked = false; |
| 4596 | } |
| 4597 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4598 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4599 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4600 | obj->mm.quirked = true; |
| 4601 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4602 | } |
| 4603 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4604 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4605 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4606 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4607 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4608 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4609 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4610 | i915_gem_object_truncate(obj); |
| 4611 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4612 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4613 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4614 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4615 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4616 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4617 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4618 | } |
| 4619 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4620 | static void |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4621 | frontbuffer_retire(struct i915_gem_active *active, struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4622 | { |
| 4623 | struct drm_i915_gem_object *obj = |
| 4624 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4625 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4626 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4629 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4630 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4631 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4632 | mutex_init(&obj->mm.lock); |
| 4633 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4634 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4635 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4636 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4637 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4638 | obj->ops = ops; |
| 4639 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4640 | reservation_object_init(&obj->__builtin_resv); |
| 4641 | obj->resv = &obj->__builtin_resv; |
| 4642 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4643 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4644 | init_request_active(&obj->frontbuffer_write, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4645 | |
| 4646 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4647 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4648 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4649 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4650 | 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] | 4651 | } |
| 4652 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4653 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4654 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4655 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4656 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4657 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4658 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4659 | |
| 4660 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4661 | }; |
| 4662 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4663 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4664 | struct drm_gem_object *obj, |
| 4665 | size_t size) |
| 4666 | { |
| 4667 | struct drm_i915_private *i915 = to_i915(dev); |
| 4668 | unsigned long flags = VM_NORESERVE; |
| 4669 | struct file *filp; |
| 4670 | |
| 4671 | drm_gem_private_object_init(dev, obj, size); |
| 4672 | |
| 4673 | if (i915->mm.gemfs) |
| 4674 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4675 | flags); |
| 4676 | else |
| 4677 | filp = shmem_file_setup("i915", size, flags); |
| 4678 | |
| 4679 | if (IS_ERR(filp)) |
| 4680 | return PTR_ERR(filp); |
| 4681 | |
| 4682 | obj->filp = filp; |
| 4683 | |
| 4684 | return 0; |
| 4685 | } |
| 4686 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4687 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4688 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4689 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4690 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4691 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4692 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4693 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4694 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4695 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4696 | /* There is a prevalence of the assumption that we fit the object's |
| 4697 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4698 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4699 | * such a local variable, please consider fixing! |
| 4700 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4701 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4702 | return ERR_PTR(-E2BIG); |
| 4703 | |
| 4704 | if (overflows_type(size, obj->base.size)) |
| 4705 | return ERR_PTR(-E2BIG); |
| 4706 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4707 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4708 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4709 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4710 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4711 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4712 | if (ret) |
| 4713 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4714 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4715 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4716 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4717 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4718 | mask &= ~__GFP_HIGHMEM; |
| 4719 | mask |= __GFP_DMA32; |
| 4720 | } |
| 4721 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4722 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4723 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4724 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4725 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4726 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4727 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4728 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4729 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4730 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4731 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4732 | /* 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] | 4733 | * cache) for about a 10% performance improvement |
| 4734 | * compared to uncached. Graphics requests other than |
| 4735 | * display scanout are coherent with the CPU in |
| 4736 | * accessing this cache. This means in this mode we |
| 4737 | * don't need to clflush on the CPU side, and on the |
| 4738 | * GPU side we only need to flush internal caches to |
| 4739 | * get data visible to the CPU. |
| 4740 | * |
| 4741 | * However, we maintain the display planes as UC, and so |
| 4742 | * need to rebind when first used as such. |
| 4743 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4744 | cache_level = I915_CACHE_LLC; |
| 4745 | else |
| 4746 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4747 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4748 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4749 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4750 | trace_i915_gem_object_create(obj); |
| 4751 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4752 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4753 | |
| 4754 | fail: |
| 4755 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4756 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4759 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4760 | { |
| 4761 | /* If we are the last user of the backing storage (be it shmemfs |
| 4762 | * pages or stolen etc), we know that the pages are going to be |
| 4763 | * immediately released. In this case, we can then skip copying |
| 4764 | * back the contents from the GPU. |
| 4765 | */ |
| 4766 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4767 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4768 | return false; |
| 4769 | |
| 4770 | if (obj->base.filp == NULL) |
| 4771 | return true; |
| 4772 | |
| 4773 | /* At first glance, this looks racy, but then again so would be |
| 4774 | * userspace racing mmap against close. However, the first external |
| 4775 | * reference to the filp can only be obtained through the |
| 4776 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4777 | * acquiring such a reference whilst we are in the middle of |
| 4778 | * freeing the object. |
| 4779 | */ |
| 4780 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4781 | } |
| 4782 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4783 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4784 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4785 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4786 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4787 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4788 | intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4789 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4790 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4791 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4792 | trace_i915_gem_object_destroy(obj); |
| 4793 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4794 | mutex_lock(&i915->drm.struct_mutex); |
| 4795 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4796 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
| 4797 | list_for_each_entry_safe(vma, vn, |
| 4798 | &obj->vma_list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4799 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4800 | vma->flags &= ~I915_VMA_PIN_MASK; |
| 4801 | i915_vma_close(vma); |
| 4802 | } |
Chris Wilson | db6c2b4 | 2016-11-01 11:54:00 +0000 | [diff] [blame] | 4803 | GEM_BUG_ON(!list_empty(&obj->vma_list)); |
| 4804 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4805 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4806 | /* This serializes freeing with the shrinker. Since the free |
| 4807 | * is delayed, first by RCU then by the workqueue, we want the |
| 4808 | * shrinker to be able to free pages of unreferenced objects, |
| 4809 | * or else we may oom whilst there are plenty of deferred |
| 4810 | * freed objects. |
| 4811 | */ |
| 4812 | if (i915_gem_object_has_pages(obj)) { |
| 4813 | spin_lock(&i915->mm.obj_lock); |
| 4814 | list_del_init(&obj->mm.link); |
| 4815 | spin_unlock(&i915->mm.obj_lock); |
| 4816 | } |
| 4817 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4818 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4819 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4820 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4821 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4822 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4823 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4824 | |
| 4825 | if (obj->ops->release) |
| 4826 | obj->ops->release(obj); |
| 4827 | |
| 4828 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4829 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4830 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4831 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4832 | |
| 4833 | if (obj->base.import_attach) |
| 4834 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4835 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4836 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4837 | drm_gem_object_release(&obj->base); |
| 4838 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4839 | |
| 4840 | kfree(obj->bit_17); |
| 4841 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4842 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4843 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4844 | atomic_dec(&i915->mm.free_count); |
| 4845 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4846 | if (on) |
| 4847 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4848 | } |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4849 | intel_runtime_pm_put(i915); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4853 | { |
| 4854 | struct llist_node *freed; |
| 4855 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4856 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4857 | freed = NULL; |
| 4858 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4859 | /* Only one consumer of llist_del_first() allowed */ |
| 4860 | spin_lock(&i915->mm.free_lock); |
| 4861 | freed = llist_del_first(&i915->mm.free_list); |
| 4862 | spin_unlock(&i915->mm.free_lock); |
| 4863 | } |
| 4864 | if (unlikely(freed)) { |
| 4865 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4866 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4867 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | static void __i915_gem_free_work(struct work_struct *work) |
| 4871 | { |
| 4872 | struct drm_i915_private *i915 = |
| 4873 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4874 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4875 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4876 | /* |
| 4877 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4878 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4879 | * However, the object may also be bound into the global GTT (e.g. |
| 4880 | * older GPUs without per-process support, or for direct access through |
| 4881 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4882 | * unbound now. |
| 4883 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4884 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4885 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4886 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4887 | spin_unlock(&i915->mm.free_lock); |
| 4888 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4889 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4890 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4891 | return; |
| 4892 | |
| 4893 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4894 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4895 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4899 | { |
| 4900 | struct drm_i915_gem_object *obj = |
| 4901 | container_of(head, typeof(*obj), rcu); |
| 4902 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4903 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4904 | /* |
| 4905 | * Since we require blocking on struct_mutex to unbind the freed |
| 4906 | * object from the GPU before releasing resources back to the |
| 4907 | * system, we can not do that directly from the RCU callback (which may |
| 4908 | * be a softirq context), but must instead then defer that work onto a |
| 4909 | * kthread. We use the RCU callback rather than move the freed object |
| 4910 | * directly onto the work queue so that we can mix between using the |
| 4911 | * worker and performing frees directly from subsequent allocations for |
| 4912 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4913 | */ |
| 4914 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4915 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4919 | { |
| 4920 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4921 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4922 | if (obj->mm.quirked) |
| 4923 | __i915_gem_object_unpin_pages(obj); |
| 4924 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4925 | if (discard_backing_storage(obj)) |
| 4926 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4927 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4928 | /* |
| 4929 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4930 | * read-side critical sections are complete, e.g. |
| 4931 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4932 | * lookup see i915_gem_object_lookup_rcu(). |
| 4933 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4934 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4935 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4936 | } |
| 4937 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4938 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4939 | { |
| 4940 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4941 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4942 | if (!i915_gem_object_has_active_reference(obj) && |
| 4943 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4944 | i915_gem_object_set_active_reference(obj); |
| 4945 | else |
| 4946 | i915_gem_object_put(obj); |
| 4947 | } |
| 4948 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4949 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4950 | { |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4951 | struct i915_gem_context *kernel_context = i915->kernel_context; |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4952 | struct intel_engine_cs *engine; |
| 4953 | enum intel_engine_id id; |
| 4954 | |
Chris Wilson | ae6c457 | 2017-11-10 14:26:28 +0000 | [diff] [blame] | 4955 | for_each_engine(engine, i915, id) { |
| 4956 | GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request)); |
| 4957 | GEM_BUG_ON(engine->last_retired_context != kernel_context); |
| 4958 | } |
Chris Wilson | 3033aca | 2016-10-28 13:58:47 +0100 | [diff] [blame] | 4959 | } |
| 4960 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4961 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4962 | { |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4963 | if (i915_terminally_wedged(&i915->gpu_error)) { |
| 4964 | mutex_lock(&i915->drm.struct_mutex); |
| 4965 | i915_gem_unset_wedged(i915); |
| 4966 | mutex_unlock(&i915->drm.struct_mutex); |
| 4967 | } |
| 4968 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4969 | /* |
| 4970 | * If we inherit context state from the BIOS or earlier occupants |
| 4971 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4972 | * try to take over. The only way to remove the earlier state |
| 4973 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4974 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4975 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4976 | */ |
Daniele Ceraolo Spurio | ce1599a | 2018-02-07 13:24:40 -0800 | [diff] [blame] | 4977 | if (INTEL_GEN(i915) >= 5 && intel_has_gpu_reset(i915)) |
| 4978 | WARN_ON(intel_gpu_reset(i915, ALL_ENGINES)); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4981 | int i915_gem_suspend(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4982 | { |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4983 | struct drm_device *dev = &dev_priv->drm; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4984 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4985 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 4986 | intel_runtime_pm_get(dev_priv); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4987 | intel_suspend_gt_powersave(dev_priv); |
| 4988 | |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4989 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4990 | |
| 4991 | /* We have to flush all the executing contexts to main memory so |
| 4992 | * that they can saved in the hibernation image. To ensure the last |
| 4993 | * context image is coherent, we have to switch away from it. That |
| 4994 | * leaves the dev_priv->kernel_context still active when |
| 4995 | * we actually suspend, and its image in memory may not match the GPU |
| 4996 | * state. Fortunately, the kernel_context is disposable and we do |
| 4997 | * not rely on its state. |
| 4998 | */ |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4999 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5000 | ret = i915_gem_switch_to_kernel_context(dev_priv); |
| 5001 | if (ret) |
| 5002 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5003 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 5004 | ret = i915_gem_wait_for_idle(dev_priv, |
| 5005 | I915_WAIT_INTERRUPTIBLE | |
| 5006 | I915_WAIT_LOCKED); |
| 5007 | if (ret && ret != -EIO) |
| 5008 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 5009 | |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 5010 | assert_kernel_context_is_current(dev_priv); |
| 5011 | } |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5012 | i915_gem_contexts_lost(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 5013 | mutex_unlock(&dev->struct_mutex); |
| 5014 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 5015 | intel_uc_suspend(dev_priv); |
Sagar Arun Kamble | 63987bf | 2017-04-05 15:51:50 +0530 | [diff] [blame] | 5016 | |
Chris Wilson | 737b150 | 2015-01-26 18:03:03 +0200 | [diff] [blame] | 5017 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5018 | cancel_delayed_work_sync(&dev_priv->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 5019 | |
| 5020 | /* As the idle_work is rearming if it detects a race, play safe and |
| 5021 | * repeat the flush until it is definitely idle. |
| 5022 | */ |
Chris Wilson | 7c26240 | 2017-10-06 11:40:38 +0100 | [diff] [blame] | 5023 | drain_delayed_work(&dev_priv->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 5024 | |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 5025 | /* Assert that we sucessfully flushed all the work and |
| 5026 | * reset the GPU back to its idle, low power state. |
| 5027 | */ |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5028 | WARN_ON(dev_priv->gt.awake); |
Chris Wilson | fc692bd | 2017-08-26 12:09:35 +0100 | [diff] [blame] | 5029 | if (WARN_ON(!intel_engines_are_idle(dev_priv))) |
| 5030 | i915_gem_set_wedged(dev_priv); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 5031 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 5032 | /* |
| 5033 | * Neither the BIOS, ourselves or any other kernel |
| 5034 | * expects the system to be in execlists mode on startup, |
| 5035 | * so we need to reset the GPU back to legacy mode. And the only |
| 5036 | * known way to disable logical contexts is through a GPU reset. |
| 5037 | * |
| 5038 | * So in order to leave the system in a known default configuration, |
| 5039 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 5040 | * clean up the GEM state tracking, flushing off the requests and |
| 5041 | * leaving the system in a known idle state. |
| 5042 | * |
| 5043 | * Note that is of the upmost importance that the GPU is idle and |
| 5044 | * all stray writes are flushed *before* we dismantle the backing |
| 5045 | * storage for the pinned objects. |
| 5046 | * |
| 5047 | * However, since we are uncertain that resetting the GPU on older |
| 5048 | * machines is a good idea, we don't - just in case it leaves the |
| 5049 | * machine in an unusable condition. |
| 5050 | */ |
Michal Wajdeczko | c37d572 | 2018-03-12 13:03:07 +0000 | [diff] [blame] | 5051 | intel_uc_sanitize(dev_priv); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5052 | i915_gem_sanitize(dev_priv); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 5053 | |
| 5054 | intel_runtime_pm_put(dev_priv); |
| 5055 | return 0; |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 5056 | |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 5057 | err_unlock: |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 5058 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | c998e8a | 2017-03-02 08:30:29 +0000 | [diff] [blame] | 5059 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 5060 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5061 | } |
| 5062 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5063 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5064 | { |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5065 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5066 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5067 | mutex_lock(&i915->drm.struct_mutex); |
| 5068 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 5069 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5070 | i915_gem_restore_gtt_mappings(i915); |
| 5071 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5072 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5073 | /* |
| 5074 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5075 | * guarantee that the context image is complete. So let's just reset |
| 5076 | * it and start again. |
| 5077 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5078 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5079 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5080 | if (i915_gem_init_hw(i915)) |
| 5081 | goto err_wedged; |
| 5082 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 5083 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 5084 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5085 | /* Always reload a context for powersaving. */ |
| 5086 | if (i915_gem_switch_to_kernel_context(i915)) |
| 5087 | goto err_wedged; |
| 5088 | |
| 5089 | out_unlock: |
| 5090 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 5091 | mutex_unlock(&i915->drm.struct_mutex); |
| 5092 | return; |
| 5093 | |
| 5094 | err_wedged: |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5095 | if (!i915_terminally_wedged(&i915->gpu_error)) { |
| 5096 | DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n"); |
| 5097 | i915_gem_set_wedged(i915); |
| 5098 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 5099 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 5100 | } |
| 5101 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5102 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5103 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5104 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5105 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 5106 | return; |
| 5107 | |
| 5108 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 5109 | DISP_TILE_SURFACE_SWIZZLING); |
| 5110 | |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5111 | if (IS_GEN5(dev_priv)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 5112 | return; |
| 5113 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5114 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5115 | if (IS_GEN6(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5116 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5117 | else if (IS_GEN7(dev_priv)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 5118 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Tvrtko Ursulin | 5db9401 | 2016-10-13 11:03:10 +0100 | [diff] [blame] | 5119 | else if (IS_GEN8(dev_priv)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 5120 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 5121 | else |
| 5122 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 5123 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 5124 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5125 | 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] | 5126 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5127 | I915_WRITE(RING_CTL(base), 0); |
| 5128 | I915_WRITE(RING_HEAD(base), 0); |
| 5129 | I915_WRITE(RING_TAIL(base), 0); |
| 5130 | I915_WRITE(RING_START(base), 0); |
| 5131 | } |
| 5132 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5133 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5134 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5135 | if (IS_I830(dev_priv)) { |
| 5136 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5137 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5138 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5139 | init_unused_ring(dev_priv, SRB2_BASE); |
| 5140 | init_unused_ring(dev_priv, SRB3_BASE); |
| 5141 | } else if (IS_GEN2(dev_priv)) { |
| 5142 | init_unused_ring(dev_priv, SRB0_BASE); |
| 5143 | init_unused_ring(dev_priv, SRB1_BASE); |
| 5144 | } else if (IS_GEN3(dev_priv)) { |
| 5145 | init_unused_ring(dev_priv, PRB1_BASE); |
| 5146 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 5147 | } |
| 5148 | } |
| 5149 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5150 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5151 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5152 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5153 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5154 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5155 | int err; |
| 5156 | |
| 5157 | for_each_engine(engine, i915, id) { |
| 5158 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5159 | if (err) { |
| 5160 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 5161 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5162 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5163 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | return 0; |
| 5167 | } |
| 5168 | |
| 5169 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 5170 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 5171 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5172 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 5173 | dev_priv->gt.last_init_time = ktime_get(); |
| 5174 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5175 | /* Double layer security blanket, see i915_gem_init() */ |
| 5176 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5177 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 5178 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 5179 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5180 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 5181 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5182 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 5183 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 5184 | |
Tvrtko Ursulin | 6e26695 | 2016-10-13 11:02:53 +0100 | [diff] [blame] | 5185 | if (HAS_PCH_NOP(dev_priv)) { |
Tvrtko Ursulin | fd6b8f4 | 2016-10-14 10:13:06 +0100 | [diff] [blame] | 5186 | if (IS_IVYBRIDGE(dev_priv)) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 5187 | u32 temp = I915_READ(GEN7_MSG_CTL); |
| 5188 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); |
| 5189 | I915_WRITE(GEN7_MSG_CTL, temp); |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5190 | } else if (INTEL_GEN(dev_priv) >= 7) { |
Daniel Vetter | 6ba844b | 2014-01-22 23:39:30 +0100 | [diff] [blame] | 5191 | u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT); |
| 5192 | temp &= ~RESET_PCH_HANDSHAKE_ENABLE; |
| 5193 | I915_WRITE(HSW_NDE_RSTWRN_OPT, temp); |
| 5194 | } |
Ben Widawsky | 88a2b2a | 2013-04-05 13:12:43 -0700 | [diff] [blame] | 5195 | } |
| 5196 | |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 5197 | intel_gt_workarounds_apply(dev_priv); |
| 5198 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5199 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 5200 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5201 | /* |
| 5202 | * At least 830 can leave some of the unused rings |
| 5203 | * "active" (ie. head != tail) after resume which |
| 5204 | * will prevent c3 entry. Makes sure all unused rings |
| 5205 | * are totally idle. |
| 5206 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 5207 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 5208 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 5209 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5210 | if (i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5211 | ret = -EIO; |
| 5212 | goto out; |
| 5213 | } |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 5214 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 5215 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5216 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5217 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 5218 | goto out; |
| 5219 | } |
| 5220 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 5221 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 5222 | if (ret) { |
| 5223 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 5224 | goto out; |
| 5225 | } |
| 5226 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5227 | /* We can't enable contexts until all firmware is loaded */ |
| 5228 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5229 | if (ret) { |
| 5230 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5231 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 5232 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 5233 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5234 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 5235 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 5236 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 5237 | ret = __i915_gem_restart_engines(dev_priv); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5238 | out: |
| 5239 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5240 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5241 | } |
| 5242 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5243 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 5244 | { |
| 5245 | struct i915_gem_context *ctx; |
| 5246 | struct intel_engine_cs *engine; |
| 5247 | enum intel_engine_id id; |
| 5248 | int err; |
| 5249 | |
| 5250 | /* |
| 5251 | * As we reset the gpu during very early sanitisation, the current |
| 5252 | * register state on the GPU should reflect its defaults values. |
| 5253 | * We load a context onto the hw (with restore-inhibit), then switch |
| 5254 | * over to a second context to save that default register state. We |
| 5255 | * can then prime every new context with that state so they all start |
| 5256 | * from the same default HW values. |
| 5257 | */ |
| 5258 | |
| 5259 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 5260 | if (IS_ERR(ctx)) |
| 5261 | return PTR_ERR(ctx); |
| 5262 | |
| 5263 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5264 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5265 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5266 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5267 | if (IS_ERR(rq)) { |
| 5268 | err = PTR_ERR(rq); |
| 5269 | goto out_ctx; |
| 5270 | } |
| 5271 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 5272 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5273 | if (engine->init_context) |
| 5274 | err = engine->init_context(rq); |
| 5275 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5276 | __i915_request_add(rq, true); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5277 | if (err) |
| 5278 | goto err_active; |
| 5279 | } |
| 5280 | |
| 5281 | err = i915_gem_switch_to_kernel_context(i915); |
| 5282 | if (err) |
| 5283 | goto err_active; |
| 5284 | |
| 5285 | err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED); |
| 5286 | if (err) |
| 5287 | goto err_active; |
| 5288 | |
| 5289 | assert_kernel_context_is_current(i915); |
| 5290 | |
| 5291 | for_each_engine(engine, i915, id) { |
| 5292 | struct i915_vma *state; |
| 5293 | |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 5294 | state = to_intel_context(ctx, engine)->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5295 | if (!state) |
| 5296 | continue; |
| 5297 | |
| 5298 | /* |
| 5299 | * As we will hold a reference to the logical state, it will |
| 5300 | * not be torn down with the context, and importantly the |
| 5301 | * object will hold onto its vma (making it possible for a |
| 5302 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 5303 | * from the GTT to prevent such accidents and reclaim the |
| 5304 | * space. |
| 5305 | */ |
| 5306 | err = i915_vma_unbind(state); |
| 5307 | if (err) |
| 5308 | goto err_active; |
| 5309 | |
| 5310 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 5311 | if (err) |
| 5312 | goto err_active; |
| 5313 | |
| 5314 | engine->default_state = i915_gem_object_get(state->obj); |
| 5315 | } |
| 5316 | |
| 5317 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 5318 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 5319 | |
| 5320 | /* |
| 5321 | * Make sure that classes with multiple engine instances all |
| 5322 | * share the same basic configuration. |
| 5323 | */ |
| 5324 | for_each_engine(engine, i915, id) { |
| 5325 | unsigned int bit = BIT(engine->uabi_class); |
| 5326 | unsigned int expected = engine->default_state ? bit : 0; |
| 5327 | |
| 5328 | if ((found & bit) != expected) { |
| 5329 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 5330 | engine->uabi_class, engine->name); |
| 5331 | } |
| 5332 | } |
| 5333 | } |
| 5334 | |
| 5335 | out_ctx: |
| 5336 | i915_gem_context_set_closed(ctx); |
| 5337 | i915_gem_context_put(ctx); |
| 5338 | return err; |
| 5339 | |
| 5340 | err_active: |
| 5341 | /* |
| 5342 | * If we have to abandon now, we expect the engines to be idle |
| 5343 | * and ready to be torn-down. First try to flush any remaining |
| 5344 | * request, ensure we are pointing at the kernel context and |
| 5345 | * then remove it. |
| 5346 | */ |
| 5347 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 5348 | goto out_ctx; |
| 5349 | |
| 5350 | if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED))) |
| 5351 | goto out_ctx; |
| 5352 | |
| 5353 | i915_gem_contexts_lost(i915); |
| 5354 | goto out_ctx; |
| 5355 | } |
| 5356 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5357 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5358 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5359 | int ret; |
| 5360 | |
Matthew Auld | da9fe3f3 | 2017-10-06 23:18:31 +0100 | [diff] [blame] | 5361 | /* |
| 5362 | * We need to fallback to 4K pages since gvt gtt handling doesn't |
| 5363 | * support huge page entries - we will need to check either hypervisor |
| 5364 | * mm can support huge guest page or just do emulation in gvt. |
| 5365 | */ |
| 5366 | if (intel_vgpu_active(dev_priv)) |
| 5367 | mkwrite_device_info(dev_priv)->page_sizes = |
| 5368 | I915_GTT_PAGE_SIZE_4K; |
| 5369 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 5370 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 5371 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5372 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 5373 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5374 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 5375 | } else { |
| 5376 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 5377 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 5378 | } |
| 5379 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5380 | ret = i915_gem_init_userptr(dev_priv); |
| 5381 | if (ret) |
| 5382 | return ret; |
| 5383 | |
Jackie Li | 6b0478f | 2018-03-13 17:32:50 -0700 | [diff] [blame] | 5384 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 5385 | if (ret) |
| 5386 | return ret; |
| 5387 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5388 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 5389 | if (ret) |
| 5390 | return ret; |
| 5391 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5392 | /* This is just a security blanket to placate dragons. |
| 5393 | * On some systems, we very sporadically observe that the first TLBs |
| 5394 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 5395 | * we hold the forcewake during initialisation these problems |
| 5396 | * just magically go away. |
| 5397 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 5398 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 5399 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5400 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5401 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5402 | if (ret) { |
| 5403 | GEM_BUG_ON(ret == -EIO); |
| 5404 | goto err_unlock; |
| 5405 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5406 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5407 | ret = i915_gem_contexts_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5408 | if (ret) { |
| 5409 | GEM_BUG_ON(ret == -EIO); |
| 5410 | goto err_ggtt; |
| 5411 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5412 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5413 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5414 | if (ret) { |
| 5415 | GEM_BUG_ON(ret == -EIO); |
| 5416 | goto err_context; |
| 5417 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5418 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5419 | intel_init_gt_powersave(dev_priv); |
| 5420 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5421 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5422 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5423 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5424 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5425 | ret = i915_gem_init_hw(dev_priv); |
| 5426 | if (ret) |
| 5427 | goto err_uc_init; |
| 5428 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5429 | /* |
| 5430 | * Despite its name intel_init_clock_gating applies both display |
| 5431 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5432 | * GT power context workaround. Worse, sometimes it includes a context |
| 5433 | * register workaround which we need to apply before we record the |
| 5434 | * default HW state for all contexts. |
| 5435 | * |
| 5436 | * FIXME: break up the workarounds and apply them at the right time! |
| 5437 | */ |
| 5438 | intel_init_clock_gating(dev_priv); |
| 5439 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5440 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5441 | if (ret) |
| 5442 | goto err_init_hw; |
| 5443 | |
| 5444 | if (i915_inject_load_failure()) { |
| 5445 | ret = -ENODEV; |
| 5446 | goto err_init_hw; |
| 5447 | } |
| 5448 | |
| 5449 | if (i915_inject_load_failure()) { |
| 5450 | ret = -EIO; |
| 5451 | goto err_init_hw; |
| 5452 | } |
| 5453 | |
| 5454 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5455 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5456 | |
| 5457 | return 0; |
| 5458 | |
| 5459 | /* |
| 5460 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5461 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5462 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5463 | * driver doesn't explode during runtime. |
| 5464 | */ |
| 5465 | err_init_hw: |
| 5466 | i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED); |
| 5467 | i915_gem_contexts_lost(dev_priv); |
| 5468 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5469 | err_uc_init: |
| 5470 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5471 | err_pm: |
| 5472 | if (ret != -EIO) { |
| 5473 | intel_cleanup_gt_powersave(dev_priv); |
| 5474 | i915_gem_cleanup_engines(dev_priv); |
| 5475 | } |
| 5476 | err_context: |
| 5477 | if (ret != -EIO) |
| 5478 | i915_gem_contexts_fini(dev_priv); |
| 5479 | err_ggtt: |
| 5480 | err_unlock: |
| 5481 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5482 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5483 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5484 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5485 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5486 | if (ret != -EIO) |
| 5487 | i915_gem_cleanup_userptr(dev_priv); |
| 5488 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5489 | if (ret == -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5490 | /* |
| 5491 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5492 | * wedged. But we only want to do this where the GPU is angry, |
| 5493 | * for all other failure, such as an allocation failure, bail. |
| 5494 | */ |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5495 | if (!i915_terminally_wedged(&dev_priv->gpu_error)) { |
| 5496 | DRM_ERROR("Failed to initialize GPU, declaring it wedged\n"); |
| 5497 | i915_gem_set_wedged(dev_priv); |
| 5498 | } |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5499 | ret = 0; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5500 | } |
| 5501 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5502 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5503 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5504 | } |
| 5505 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5506 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5507 | { |
| 5508 | i915_gem_sanitize(i915); |
| 5509 | } |
| 5510 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5511 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5512 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5513 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5514 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5515 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5516 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5517 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5518 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5519 | } |
| 5520 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5521 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5522 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5523 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5524 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5525 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5526 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5527 | !IS_CHERRYVIEW(dev_priv)) |
| 5528 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5529 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5530 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5531 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5532 | dev_priv->num_fence_regs = 16; |
| 5533 | else |
| 5534 | dev_priv->num_fence_regs = 8; |
| 5535 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5536 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5537 | dev_priv->num_fence_regs = |
| 5538 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5539 | |
| 5540 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5541 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5542 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5543 | |
| 5544 | fence->i915 = dev_priv; |
| 5545 | fence->id = i; |
| 5546 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5547 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5548 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5549 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5550 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5551 | } |
| 5552 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5553 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5554 | { |
| 5555 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5556 | spin_lock_init(&i915->mm.obj_lock); |
| 5557 | spin_lock_init(&i915->mm.free_lock); |
| 5558 | |
| 5559 | init_llist_head(&i915->mm.free_list); |
| 5560 | |
| 5561 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5562 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5563 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5564 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5565 | |
| 5566 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5567 | } |
| 5568 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5569 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5570 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5571 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5572 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5573 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5574 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5575 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5576 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5577 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5578 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5579 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5580 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5581 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5582 | if (!dev_priv->luts) |
| 5583 | goto err_vmas; |
| 5584 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5585 | dev_priv->requests = KMEM_CACHE(i915_request, |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5586 | SLAB_HWCACHE_ALIGN | |
| 5587 | SLAB_RECLAIM_ACCOUNT | |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 5588 | SLAB_TYPESAFE_BY_RCU); |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5589 | if (!dev_priv->requests) |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5590 | goto err_luts; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5591 | |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5592 | dev_priv->dependencies = KMEM_CACHE(i915_dependency, |
| 5593 | SLAB_HWCACHE_ALIGN | |
| 5594 | SLAB_RECLAIM_ACCOUNT); |
| 5595 | if (!dev_priv->dependencies) |
| 5596 | goto err_requests; |
| 5597 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5598 | dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN); |
| 5599 | if (!dev_priv->priorities) |
| 5600 | goto err_dependencies; |
| 5601 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5602 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | b887d61 | 2018-04-30 14:15:02 +0100 | [diff] [blame^] | 5603 | INIT_LIST_HEAD(&dev_priv->gt.rings); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5604 | INIT_LIST_HEAD(&dev_priv->gt.timelines); |
Chris Wilson | bb89485 | 2016-11-14 20:40:57 +0000 | [diff] [blame] | 5605 | err = i915_gem_timeline_init__global(dev_priv); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5606 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5607 | if (err) |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5608 | goto err_priorities; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5609 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5610 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5611 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5612 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5613 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5614 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5615 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5616 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5617 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5618 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5619 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5620 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5621 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5622 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5623 | err = i915_gemfs_init(dev_priv); |
| 5624 | if (err) |
| 5625 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5626 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5627 | return 0; |
| 5628 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5629 | err_priorities: |
| 5630 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5631 | err_dependencies: |
| 5632 | kmem_cache_destroy(dev_priv->dependencies); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5633 | err_requests: |
| 5634 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5635 | err_luts: |
| 5636 | kmem_cache_destroy(dev_priv->luts); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5637 | err_vmas: |
| 5638 | kmem_cache_destroy(dev_priv->vmas); |
| 5639 | err_objects: |
| 5640 | kmem_cache_destroy(dev_priv->objects); |
| 5641 | err_out: |
| 5642 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5643 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5644 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5645 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5646 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5647 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5648 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5649 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5650 | WARN_ON(dev_priv->mm.object_count); |
Chris Wilson | 7d5d59e | 2016-11-01 08:48:41 +0000 | [diff] [blame] | 5651 | |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5652 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5653 | i915_gem_timeline_fini(&dev_priv->gt.global_timeline); |
| 5654 | WARN_ON(!list_empty(&dev_priv->gt.timelines)); |
| 5655 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5656 | |
Chris Wilson | c5cf9a9 | 2017-05-17 13:10:04 +0100 | [diff] [blame] | 5657 | kmem_cache_destroy(dev_priv->priorities); |
Chris Wilson | 52e5420 | 2016-11-14 20:41:02 +0000 | [diff] [blame] | 5658 | kmem_cache_destroy(dev_priv->dependencies); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5659 | kmem_cache_destroy(dev_priv->requests); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5660 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5661 | kmem_cache_destroy(dev_priv->vmas); |
| 5662 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5663 | |
| 5664 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5665 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5666 | |
| 5667 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5668 | } |
| 5669 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5670 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5671 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5672 | /* Discard all purgeable objects, let userspace recover those as |
| 5673 | * required after resuming. |
| 5674 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5675 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5676 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5677 | return 0; |
| 5678 | } |
| 5679 | |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5680 | int i915_gem_freeze_late(struct drm_i915_private *dev_priv) |
| 5681 | { |
| 5682 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5683 | struct list_head *phases[] = { |
| 5684 | &dev_priv->mm.unbound_list, |
| 5685 | &dev_priv->mm.bound_list, |
| 5686 | NULL |
| 5687 | }, **p; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5688 | |
| 5689 | /* Called just before we write the hibernation image. |
| 5690 | * |
| 5691 | * We need to update the domain tracking to reflect that the CPU |
| 5692 | * will be accessing all the pages to create and restore from the |
| 5693 | * hibernation, and so upon restoration those pages will be in the |
| 5694 | * CPU domain. |
| 5695 | * |
| 5696 | * To make sure the hibernation image contains the latest state, |
| 5697 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5698 | * |
| 5699 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5700 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5701 | */ |
| 5702 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 5703 | i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND); |
Chris Wilson | 17b93c4 | 2017-04-07 11:25:50 +0100 | [diff] [blame] | 5704 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5705 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5706 | spin_lock(&dev_priv->mm.obj_lock); |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5707 | for (p = phases; *p; p++) { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5708 | list_for_each_entry(obj, *p, mm.link) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 5709 | __start_cpu_write(obj); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5710 | } |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5711 | spin_unlock(&dev_priv->mm.obj_lock); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5712 | |
| 5713 | return 0; |
| 5714 | } |
| 5715 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5716 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5717 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5718 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5719 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5720 | |
| 5721 | /* Clean up our request list when the client is going away, so that |
| 5722 | * later retire_requests won't dereference our soon-to-be-gone |
| 5723 | * file_priv. |
| 5724 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5725 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5726 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5727 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5728 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5729 | } |
| 5730 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5731 | 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] | 5732 | { |
| 5733 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5734 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5735 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5736 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5737 | |
| 5738 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5739 | if (!file_priv) |
| 5740 | return -ENOMEM; |
| 5741 | |
| 5742 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5743 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5744 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5745 | |
| 5746 | spin_lock_init(&file_priv->mm.lock); |
| 5747 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5748 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5749 | file_priv->bsd_engine = -1; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5750 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5751 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5752 | if (ret) |
| 5753 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5754 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5755 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5756 | } |
| 5757 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5758 | /** |
| 5759 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5760 | * @old: current GEM buffer for the frontbuffer slots |
| 5761 | * @new: new GEM buffer for the frontbuffer slots |
| 5762 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5763 | * |
| 5764 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5765 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5766 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5767 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5768 | struct drm_i915_gem_object *new, |
| 5769 | unsigned frontbuffer_bits) |
| 5770 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5771 | /* Control of individual bits within the mask are guarded by |
| 5772 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5773 | * manipulation of individual bits. But since the bitfield as a whole |
| 5774 | * is updated using RMW, we need to use atomics in order to update |
| 5775 | * the bits. |
| 5776 | */ |
| 5777 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
| 5778 | sizeof(atomic_t) * BITS_PER_BYTE); |
| 5779 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5780 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5781 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5782 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5783 | } |
| 5784 | |
| 5785 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5786 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5787 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5788 | } |
| 5789 | } |
| 5790 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5791 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5792 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5793 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5794 | const void *data, size_t size) |
| 5795 | { |
| 5796 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5797 | struct file *file; |
| 5798 | size_t offset; |
| 5799 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5800 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5801 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5802 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5803 | return obj; |
| 5804 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5805 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5806 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5807 | file = obj->base.filp; |
| 5808 | offset = 0; |
| 5809 | do { |
| 5810 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5811 | struct page *page; |
| 5812 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5813 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5814 | err = pagecache_write_begin(file, file->f_mapping, |
| 5815 | offset, len, 0, |
| 5816 | &page, &pgdata); |
| 5817 | if (err < 0) |
| 5818 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5819 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5820 | vaddr = kmap(page); |
| 5821 | memcpy(vaddr, data, len); |
| 5822 | kunmap(page); |
| 5823 | |
| 5824 | err = pagecache_write_end(file, file->f_mapping, |
| 5825 | offset, len, len, |
| 5826 | page, pgdata); |
| 5827 | if (err < 0) |
| 5828 | goto fail; |
| 5829 | |
| 5830 | size -= len; |
| 5831 | data += len; |
| 5832 | offset += len; |
| 5833 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5834 | |
| 5835 | return obj; |
| 5836 | |
| 5837 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5838 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5839 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5840 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5841 | |
| 5842 | struct scatterlist * |
| 5843 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5844 | unsigned int n, |
| 5845 | unsigned int *offset) |
| 5846 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5847 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5848 | struct scatterlist *sg; |
| 5849 | unsigned int idx, count; |
| 5850 | |
| 5851 | might_sleep(); |
| 5852 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5853 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5854 | |
| 5855 | /* As we iterate forward through the sg, we record each entry in a |
| 5856 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5857 | * this index previously, we will have an entry for it. |
| 5858 | * |
| 5859 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5860 | * sequential page access (where each new request is consecutive |
| 5861 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5862 | * i.e. O(1) with a large constant! |
| 5863 | */ |
| 5864 | if (n < READ_ONCE(iter->sg_idx)) |
| 5865 | goto lookup; |
| 5866 | |
| 5867 | mutex_lock(&iter->lock); |
| 5868 | |
| 5869 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5870 | * (or the subsequent) sg are fast - comparing against the last |
| 5871 | * sg is faster than going through the radixtree. |
| 5872 | */ |
| 5873 | |
| 5874 | sg = iter->sg_pos; |
| 5875 | idx = iter->sg_idx; |
| 5876 | count = __sg_page_count(sg); |
| 5877 | |
| 5878 | while (idx + count <= n) { |
| 5879 | unsigned long exception, i; |
| 5880 | int ret; |
| 5881 | |
| 5882 | /* If we cannot allocate and insert this entry, or the |
| 5883 | * individual pages from this range, cancel updating the |
| 5884 | * sg_idx so that on this lookup we are forced to linearly |
| 5885 | * scan onwards, but on future lookups we will try the |
| 5886 | * insertion again (in which case we need to be careful of |
| 5887 | * the error return reporting that we have already inserted |
| 5888 | * this index). |
| 5889 | */ |
| 5890 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5891 | if (ret && ret != -EEXIST) |
| 5892 | goto scan; |
| 5893 | |
| 5894 | exception = |
| 5895 | RADIX_TREE_EXCEPTIONAL_ENTRY | |
| 5896 | idx << RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5897 | for (i = 1; i < count; i++) { |
| 5898 | ret = radix_tree_insert(&iter->radix, idx + i, |
| 5899 | (void *)exception); |
| 5900 | if (ret && ret != -EEXIST) |
| 5901 | goto scan; |
| 5902 | } |
| 5903 | |
| 5904 | idx += count; |
| 5905 | sg = ____sg_next(sg); |
| 5906 | count = __sg_page_count(sg); |
| 5907 | } |
| 5908 | |
| 5909 | scan: |
| 5910 | iter->sg_pos = sg; |
| 5911 | iter->sg_idx = idx; |
| 5912 | |
| 5913 | mutex_unlock(&iter->lock); |
| 5914 | |
| 5915 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5916 | goto lookup; |
| 5917 | |
| 5918 | /* In case we failed to insert the entry into the radixtree, we need |
| 5919 | * to look beyond the current sg. |
| 5920 | */ |
| 5921 | while (idx + count <= n) { |
| 5922 | idx += count; |
| 5923 | sg = ____sg_next(sg); |
| 5924 | count = __sg_page_count(sg); |
| 5925 | } |
| 5926 | |
| 5927 | *offset = n - idx; |
| 5928 | return sg; |
| 5929 | |
| 5930 | lookup: |
| 5931 | rcu_read_lock(); |
| 5932 | |
| 5933 | sg = radix_tree_lookup(&iter->radix, n); |
| 5934 | GEM_BUG_ON(!sg); |
| 5935 | |
| 5936 | /* If this index is in the middle of multi-page sg entry, |
| 5937 | * the radixtree will contain an exceptional entry that points |
| 5938 | * to the start of that range. We will return the pointer to |
| 5939 | * the base page and the offset of this page within the |
| 5940 | * sg entry's range. |
| 5941 | */ |
| 5942 | *offset = 0; |
| 5943 | if (unlikely(radix_tree_exception(sg))) { |
| 5944 | unsigned long base = |
| 5945 | (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT; |
| 5946 | |
| 5947 | sg = radix_tree_lookup(&iter->radix, base); |
| 5948 | GEM_BUG_ON(!sg); |
| 5949 | |
| 5950 | *offset = n - base; |
| 5951 | } |
| 5952 | |
| 5953 | rcu_read_unlock(); |
| 5954 | |
| 5955 | return sg; |
| 5956 | } |
| 5957 | |
| 5958 | struct page * |
| 5959 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5960 | { |
| 5961 | struct scatterlist *sg; |
| 5962 | unsigned int offset; |
| 5963 | |
| 5964 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5965 | |
| 5966 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5967 | return nth_page(sg_page(sg), offset); |
| 5968 | } |
| 5969 | |
| 5970 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5971 | struct page * |
| 5972 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5973 | unsigned int n) |
| 5974 | { |
| 5975 | struct page *page; |
| 5976 | |
| 5977 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5978 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5979 | set_page_dirty(page); |
| 5980 | |
| 5981 | return page; |
| 5982 | } |
| 5983 | |
| 5984 | dma_addr_t |
| 5985 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5986 | unsigned long n) |
| 5987 | { |
| 5988 | struct scatterlist *sg; |
| 5989 | unsigned int offset; |
| 5990 | |
| 5991 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5992 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5993 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5994 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5995 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5996 | { |
| 5997 | struct sg_table *pages; |
| 5998 | int err; |
| 5999 | |
| 6000 | if (align > obj->base.size) |
| 6001 | return -EINVAL; |
| 6002 | |
| 6003 | if (obj->ops == &i915_gem_phys_ops) |
| 6004 | return 0; |
| 6005 | |
| 6006 | if (obj->ops != &i915_gem_object_ops) |
| 6007 | return -EINVAL; |
| 6008 | |
| 6009 | err = i915_gem_object_unbind(obj); |
| 6010 | if (err) |
| 6011 | return err; |
| 6012 | |
| 6013 | mutex_lock(&obj->mm.lock); |
| 6014 | |
| 6015 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 6016 | err = -EFAULT; |
| 6017 | goto err_unlock; |
| 6018 | } |
| 6019 | |
| 6020 | if (obj->mm.quirked) { |
| 6021 | err = -EFAULT; |
| 6022 | goto err_unlock; |
| 6023 | } |
| 6024 | |
| 6025 | if (obj->mm.mapping) { |
| 6026 | err = -EBUSY; |
| 6027 | goto err_unlock; |
| 6028 | } |
| 6029 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 6030 | pages = fetch_and_zero(&obj->mm.pages); |
| 6031 | if (pages) { |
| 6032 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 6033 | |
| 6034 | __i915_gem_object_reset_page_iter(obj); |
| 6035 | |
| 6036 | spin_lock(&i915->mm.obj_lock); |
| 6037 | list_del(&obj->mm.link); |
| 6038 | spin_unlock(&i915->mm.obj_lock); |
| 6039 | } |
| 6040 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6041 | obj->ops = &i915_gem_phys_ops; |
| 6042 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 6043 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 6044 | if (err) |
| 6045 | goto err_xfer; |
| 6046 | |
| 6047 | /* Perma-pin (until release) the physical set of pages */ |
| 6048 | __i915_gem_object_pin_pages(obj); |
| 6049 | |
| 6050 | if (!IS_ERR_OR_NULL(pages)) |
| 6051 | i915_gem_object_ops.put_pages(obj, pages); |
| 6052 | mutex_unlock(&obj->mm.lock); |
| 6053 | return 0; |
| 6054 | |
| 6055 | err_xfer: |
| 6056 | obj->ops = &i915_gem_object_ops; |
| 6057 | obj->mm.pages = pages; |
| 6058 | err_unlock: |
| 6059 | mutex_unlock(&obj->mm.lock); |
| 6060 | return err; |
| 6061 | } |
| 6062 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6063 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 6064 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 6065 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 6066 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 6067 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 6068 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 6069 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 6070 | #endif |