Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
Daniel Vetter | be6a037 | 2015-03-18 10:46:04 +0100 | [diff] [blame] | 2 | * Copyright © 2008-2015 Intel Corporation |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 28 | #include <drm/drm_vma_manager.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 29 | #include <drm/drm_pci.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/i915_drm.h> |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 31 | #include <linux/dma-fence-array.h> |
Chris Wilson | fe3288b | 2017-02-12 17:20:01 +0000 | [diff] [blame] | 32 | #include <linux/kthread.h> |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 33 | #include <linux/reservation.h> |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 34 | #include <linux/shmem_fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Chris Wilson | 20e4933 | 2016-11-22 14:41:21 +0000 | [diff] [blame] | 36 | #include <linux/stop_machine.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 37 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 38 | #include <linux/pci.h> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 39 | #include <linux/dma-buf.h> |
Daniel Vetter | fcd70cd | 2019-01-17 22:03:34 +0100 | [diff] [blame] | 40 | #include <linux/mman.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 41 | |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 42 | #include "i915_drv.h" |
| 43 | #include "i915_gem_clflush.h" |
| 44 | #include "i915_gemfs.h" |
Chris Wilson | 32eb6bc | 2019-02-28 10:20:33 +0000 | [diff] [blame^] | 45 | #include "i915_globals.h" |
Chris Wilson | 9f58892 | 2019-01-16 15:33:04 +0000 | [diff] [blame] | 46 | #include "i915_reset.h" |
| 47 | #include "i915_trace.h" |
| 48 | #include "i915_vgpu.h" |
| 49 | |
| 50 | #include "intel_drv.h" |
| 51 | #include "intel_frontbuffer.h" |
| 52 | #include "intel_mocs.h" |
| 53 | #include "intel_workarounds.h" |
| 54 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 55 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 56 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 57 | static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 58 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 59 | if (obj->cache_dirty) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 60 | return false; |
| 61 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 62 | if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE)) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 63 | return true; |
| 64 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 65 | return obj->pin_global; /* currently in use by HW, keep flushed */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 68 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 69 | insert_mappable_node(struct i915_ggtt *ggtt, |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 70 | struct drm_mm_node *node, u32 size) |
| 71 | { |
| 72 | memset(node, 0, sizeof(*node)); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 73 | return drm_mm_insert_node_in_range(&ggtt->vm.mm, node, |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 74 | size, 0, I915_COLOR_UNEVICTABLE, |
| 75 | 0, ggtt->mappable_end, |
| 76 | DRM_MM_INSERT_LOW); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void |
| 80 | remove_mappable_node(struct drm_mm_node *node) |
| 81 | { |
| 82 | drm_mm_remove_node(node); |
| 83 | } |
| 84 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 85 | /* some bookkeeping */ |
| 86 | 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] | 87 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 88 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 89 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 90 | dev_priv->mm.object_count++; |
| 91 | dev_priv->mm.object_memory += size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 92 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | 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] | 96 | u64 size) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 97 | { |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 98 | spin_lock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 99 | dev_priv->mm.object_count--; |
| 100 | dev_priv->mm.object_memory -= size; |
Daniel Vetter | c20e835 | 2013-07-24 22:40:23 +0200 | [diff] [blame] | 101 | spin_unlock(&dev_priv->mm.object_stat_lock); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 104 | static u32 __i915_gem_park(struct drm_i915_private *i915) |
| 105 | { |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 106 | intel_wakeref_t wakeref; |
| 107 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 108 | GEM_TRACE("\n"); |
| 109 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 110 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 111 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 112 | GEM_BUG_ON(!list_empty(&i915->gt.active_rings)); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 113 | |
| 114 | if (!i915->gt.awake) |
| 115 | return I915_EPOCH_INVALID; |
| 116 | |
| 117 | GEM_BUG_ON(i915->gt.epoch == I915_EPOCH_INVALID); |
| 118 | |
| 119 | /* |
| 120 | * Be paranoid and flush a concurrent interrupt to make sure |
| 121 | * we don't reactivate any irq tasklets after parking. |
| 122 | * |
| 123 | * FIXME: Note that even though we have waited for execlists to be idle, |
| 124 | * there may still be an in-flight interrupt even though the CSB |
| 125 | * is now empty. synchronize_irq() makes sure that a residual interrupt |
| 126 | * is completed before we continue, but it doesn't prevent the HW from |
| 127 | * raising a spurious interrupt later. To complete the shield we should |
| 128 | * coordinate disabling the CS irq with flushing the interrupts. |
| 129 | */ |
| 130 | synchronize_irq(i915->drm.irq); |
| 131 | |
| 132 | intel_engines_park(i915); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 133 | i915_timelines_park(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 134 | |
| 135 | i915_pmu_gt_parked(i915); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 136 | i915_vma_parked(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 137 | |
Chris Wilson | 506d1f6 | 2019-01-14 14:21:11 +0000 | [diff] [blame] | 138 | wakeref = fetch_and_zero(&i915->gt.awake); |
| 139 | GEM_BUG_ON(!wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 140 | |
| 141 | if (INTEL_GEN(i915) >= 6) |
| 142 | gen6_rps_idle(i915); |
| 143 | |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 144 | intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 145 | |
| 146 | return i915->gt.epoch; |
| 147 | } |
| 148 | |
| 149 | void i915_gem_park(struct drm_i915_private *i915) |
| 150 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 151 | GEM_TRACE("\n"); |
| 152 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 153 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 154 | GEM_BUG_ON(i915->gt.active_requests); |
| 155 | |
| 156 | if (!i915->gt.awake) |
| 157 | return; |
| 158 | |
| 159 | /* Defer the actual call to __i915_gem_park() to prevent ping-pongs */ |
| 160 | mod_delayed_work(i915->wq, &i915->gt.idle_work, msecs_to_jiffies(100)); |
| 161 | } |
| 162 | |
| 163 | void i915_gem_unpark(struct drm_i915_private *i915) |
| 164 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 165 | GEM_TRACE("\n"); |
| 166 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 167 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 168 | GEM_BUG_ON(!i915->gt.active_requests); |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 169 | assert_rpm_wakelock_held(i915); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 170 | |
| 171 | if (i915->gt.awake) |
| 172 | return; |
| 173 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 174 | /* |
| 175 | * It seems that the DMC likes to transition between the DC states a lot |
| 176 | * when there are no connected displays (no active power domains) during |
| 177 | * command submission. |
| 178 | * |
| 179 | * This activity has negative impact on the performance of the chip with |
| 180 | * huge latencies observed in the interrupt handler and elsewhere. |
| 181 | * |
| 182 | * Work around it by grabbing a GT IRQ power domain whilst there is any |
| 183 | * GT activity, preventing any DC state transitions. |
| 184 | */ |
Chris Wilson | 8d761e7 | 2019-01-14 14:21:28 +0000 | [diff] [blame] | 185 | i915->gt.awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ); |
| 186 | GEM_BUG_ON(!i915->gt.awake); |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 187 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 188 | if (unlikely(++i915->gt.epoch == 0)) /* keep 0 as invalid */ |
| 189 | i915->gt.epoch = 1; |
| 190 | |
Chris Wilson | 32eb6bc | 2019-02-28 10:20:33 +0000 | [diff] [blame^] | 191 | i915_globals_unpark(); |
| 192 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 193 | intel_enable_gt_powersave(i915); |
| 194 | i915_update_gfx_val(i915); |
| 195 | if (INTEL_GEN(i915) >= 6) |
| 196 | gen6_rps_busy(i915); |
| 197 | i915_pmu_gt_unparked(i915); |
| 198 | |
| 199 | intel_engines_unpark(i915); |
| 200 | |
| 201 | i915_queue_hangcheck(i915); |
| 202 | |
| 203 | queue_delayed_work(i915->wq, |
| 204 | &i915->gt.retire_work, |
| 205 | round_jiffies_up_relative(HZ)); |
| 206 | } |
| 207 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 208 | int |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 209 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 210 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 211 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 212 | struct i915_ggtt *ggtt = &to_i915(dev)->ggtt; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 213 | struct drm_i915_gem_get_aperture *args = data; |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 214 | struct i915_vma *vma; |
Weinan Li | ff8f797 | 2017-05-31 10:35:52 +0800 | [diff] [blame] | 215 | u64 pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 216 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 217 | mutex_lock(&ggtt->vm.mutex); |
| 218 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 219 | pinned = ggtt->vm.reserved; |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 220 | list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 221 | if (i915_vma_is_pinned(vma)) |
Tvrtko Ursulin | ca1543b | 2015-07-01 11:51:10 +0100 | [diff] [blame] | 222 | pinned += vma->node.size; |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 223 | |
| 224 | mutex_unlock(&ggtt->vm.mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 225 | |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 226 | args->aper_size = ggtt->vm.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 227 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 228 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 232 | 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] | 233 | { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 234 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 235 | drm_dma_handle_t *phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 236 | struct sg_table *st; |
| 237 | struct scatterlist *sg; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 238 | char *vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 239 | int i; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 240 | int err; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 241 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 242 | if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj))) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 243 | return -EINVAL; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 244 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 245 | /* Always aligning to the object size, allows a single allocation |
| 246 | * to handle all possible callers, and given typical object sizes, |
| 247 | * the alignment of the buddy allocation will naturally match. |
| 248 | */ |
| 249 | phys = drm_pci_alloc(obj->base.dev, |
Ville Syrjälä | 750fae2 | 2017-09-07 17:32:03 +0300 | [diff] [blame] | 250 | roundup_pow_of_two(obj->base.size), |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 251 | roundup_pow_of_two(obj->base.size)); |
| 252 | if (!phys) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 253 | return -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 254 | |
| 255 | vaddr = phys->vaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 256 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
| 257 | struct page *page; |
| 258 | char *src; |
| 259 | |
| 260 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 261 | if (IS_ERR(page)) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 262 | err = PTR_ERR(page); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 263 | goto err_phys; |
| 264 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 265 | |
| 266 | src = kmap_atomic(page); |
| 267 | memcpy(vaddr, src, PAGE_SIZE); |
| 268 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 269 | kunmap_atomic(src); |
| 270 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 271 | put_page(page); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 272 | vaddr += PAGE_SIZE; |
| 273 | } |
| 274 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 275 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 276 | |
| 277 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 278 | if (!st) { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 279 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 280 | goto err_phys; |
| 281 | } |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 282 | |
| 283 | if (sg_alloc_table(st, 1, GFP_KERNEL)) { |
| 284 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 285 | err = -ENOMEM; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 286 | goto err_phys; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | sg = st->sgl; |
| 290 | sg->offset = 0; |
| 291 | sg->length = obj->base.size; |
| 292 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 293 | sg_dma_address(sg) = phys->busaddr; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 294 | sg_dma_len(sg) = obj->base.size; |
| 295 | |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 296 | obj->phys_handle = phys; |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 297 | |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 298 | __i915_gem_object_set_pages(obj, st, sg->length); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 299 | |
| 300 | return 0; |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 301 | |
| 302 | err_phys: |
| 303 | drm_pci_free(obj->base.dev, phys); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 304 | |
| 305 | return err; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 308 | static void __start_cpu_write(struct drm_i915_gem_object *obj) |
| 309 | { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 310 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 311 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 312 | if (cpu_write_needs_clflush(obj)) |
| 313 | obj->cache_dirty = true; |
| 314 | } |
| 315 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 316 | static void |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 317 | __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 318 | struct sg_table *pages, |
| 319 | bool needs_clflush) |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 320 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 321 | GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 322 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 323 | if (obj->mm.madv == I915_MADV_DONTNEED) |
| 324 | obj->mm.dirty = false; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 325 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 326 | if (needs_clflush && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 327 | (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0 && |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 328 | !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ)) |
Chris Wilson | 2b3c831 | 2016-11-11 14:58:09 +0000 | [diff] [blame] | 329 | drm_clflush_sg(pages); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 330 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 331 | __start_cpu_write(obj); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static void |
| 335 | i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj, |
| 336 | struct sg_table *pages) |
| 337 | { |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 338 | __i915_gem_object_release_shmem(obj, pages, false); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 339 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 340 | if (obj->mm.dirty) { |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 341 | struct address_space *mapping = obj->base.filp->f_mapping; |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 342 | char *vaddr = obj->phys_handle->vaddr; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 343 | int i; |
| 344 | |
| 345 | for (i = 0; i < obj->base.size / PAGE_SIZE; i++) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 346 | struct page *page; |
| 347 | char *dst; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 348 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 349 | page = shmem_read_mapping_page(mapping, i); |
| 350 | if (IS_ERR(page)) |
| 351 | continue; |
| 352 | |
| 353 | dst = kmap_atomic(page); |
| 354 | drm_clflush_virt_range(vaddr, PAGE_SIZE); |
| 355 | memcpy(dst, vaddr, PAGE_SIZE); |
| 356 | kunmap_atomic(dst); |
| 357 | |
| 358 | set_page_dirty(page); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 359 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 360 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 361 | put_page(page); |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 362 | vaddr += PAGE_SIZE; |
| 363 | } |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 364 | obj->mm.dirty = false; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 367 | sg_free_table(pages); |
| 368 | kfree(pages); |
Chris Wilson | dbb4351 | 2016-12-07 13:34:11 +0000 | [diff] [blame] | 369 | |
| 370 | drm_pci_free(obj->base.dev, obj->phys_handle); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static void |
| 374 | i915_gem_object_release_phys(struct drm_i915_gem_object *obj) |
| 375 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 376 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static const struct drm_i915_gem_object_ops i915_gem_phys_ops = { |
| 380 | .get_pages = i915_gem_object_get_pages_phys, |
| 381 | .put_pages = i915_gem_object_put_pages_phys, |
| 382 | .release = i915_gem_object_release_phys, |
| 383 | }; |
| 384 | |
Chris Wilson | 581ab1f | 2017-02-15 16:39:00 +0000 | [diff] [blame] | 385 | static const struct drm_i915_gem_object_ops i915_gem_object_ops; |
| 386 | |
Chris Wilson | 35a9611 | 2016-08-14 18:44:40 +0100 | [diff] [blame] | 387 | int i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 388 | { |
| 389 | struct i915_vma *vma; |
| 390 | LIST_HEAD(still_in_list); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 391 | int ret; |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 392 | |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 393 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 394 | |
| 395 | /* Closed vma are removed from the obj->vma_list - but they may |
| 396 | * still have an active binding on the object. To remove those we |
| 397 | * must wait for all rendering to complete to the object (as unbinding |
| 398 | * must anyway), and retire the requests. |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 399 | */ |
Chris Wilson | 5888fc9 | 2017-12-04 13:25:13 +0000 | [diff] [blame] | 400 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
Chris Wilson | 02bef8f | 2016-08-14 18:44:41 +0100 | [diff] [blame] | 401 | if (ret) |
| 402 | return ret; |
| 403 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 404 | spin_lock(&obj->vma.lock); |
| 405 | while (!ret && (vma = list_first_entry_or_null(&obj->vma.list, |
| 406 | struct i915_vma, |
| 407 | obj_link))) { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 408 | list_move_tail(&vma->obj_link, &still_in_list); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 409 | spin_unlock(&obj->vma.lock); |
| 410 | |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 411 | ret = i915_vma_unbind(vma); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 412 | |
| 413 | spin_lock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 414 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 415 | list_splice(&still_in_list, &obj->vma.list); |
| 416 | spin_unlock(&obj->vma.lock); |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 421 | static long |
| 422 | i915_gem_object_wait_fence(struct dma_fence *fence, |
| 423 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 424 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 425 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 426 | struct i915_request *rq; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 427 | |
| 428 | BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1); |
| 429 | |
| 430 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) |
| 431 | return timeout; |
| 432 | |
| 433 | if (!dma_fence_is_i915(fence)) |
| 434 | return dma_fence_wait_timeout(fence, |
| 435 | flags & I915_WAIT_INTERRUPTIBLE, |
| 436 | timeout); |
| 437 | |
| 438 | rq = to_request(fence); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 439 | if (i915_request_completed(rq)) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 440 | goto out; |
| 441 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 442 | timeout = i915_request_wait(rq, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 443 | |
| 444 | out: |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 445 | if (flags & I915_WAIT_LOCKED && i915_request_completed(rq)) |
| 446 | i915_request_retire_upto(rq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 447 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 448 | return timeout; |
| 449 | } |
| 450 | |
| 451 | static long |
| 452 | i915_gem_object_wait_reservation(struct reservation_object *resv, |
| 453 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 454 | long timeout) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 455 | { |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 456 | unsigned int seq = __read_seqcount_begin(&resv->seq); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 457 | struct dma_fence *excl; |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 458 | bool prune_fences = false; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 459 | |
| 460 | if (flags & I915_WAIT_ALL) { |
| 461 | struct dma_fence **shared; |
| 462 | unsigned int count, i; |
| 463 | int ret; |
| 464 | |
| 465 | ret = reservation_object_get_fences_rcu(resv, |
| 466 | &excl, &count, &shared); |
| 467 | if (ret) |
| 468 | return ret; |
| 469 | |
| 470 | for (i = 0; i < count; i++) { |
| 471 | timeout = i915_gem_object_wait_fence(shared[i], |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 472 | flags, timeout); |
Chris Wilson | d892e93 | 2017-02-12 21:53:43 +0000 | [diff] [blame] | 473 | if (timeout < 0) |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 474 | break; |
| 475 | |
| 476 | dma_fence_put(shared[i]); |
| 477 | } |
| 478 | |
| 479 | for (; i < count; i++) |
| 480 | dma_fence_put(shared[i]); |
| 481 | kfree(shared); |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 482 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 483 | /* |
| 484 | * If both shared fences and an exclusive fence exist, |
| 485 | * then by construction the shared fences must be later |
| 486 | * than the exclusive fence. If we successfully wait for |
| 487 | * all the shared fences, we know that the exclusive fence |
| 488 | * must all be signaled. If all the shared fences are |
| 489 | * signaled, we can prune the array and recover the |
| 490 | * floating references on the fences/requests. |
| 491 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 492 | prune_fences = count && timeout >= 0; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 493 | } else { |
| 494 | excl = reservation_object_get_excl_rcu(resv); |
| 495 | } |
| 496 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 497 | if (excl && timeout >= 0) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 498 | timeout = i915_gem_object_wait_fence(excl, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 499 | |
| 500 | dma_fence_put(excl); |
| 501 | |
Chris Wilson | fa73055 | 2018-03-07 17:13:03 +0000 | [diff] [blame] | 502 | /* |
| 503 | * Opportunistically prune the fences iff we know they have *all* been |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 504 | * signaled and that the reservation object has not been changed (i.e. |
| 505 | * no new fences have been added). |
| 506 | */ |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 507 | if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) { |
Chris Wilson | 03d1cac | 2017-03-08 13:26:28 +0000 | [diff] [blame] | 508 | if (reservation_object_trylock(resv)) { |
| 509 | if (!__read_seqcount_retry(&resv->seq, seq)) |
| 510 | reservation_object_add_excl_fence(resv, NULL); |
| 511 | reservation_object_unlock(resv); |
| 512 | } |
Chris Wilson | e54ca97 | 2017-02-17 15:13:04 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 515 | return timeout; |
| 516 | } |
| 517 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 518 | static void __fence_set_priority(struct dma_fence *fence, |
| 519 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 520 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 521 | struct i915_request *rq; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 522 | struct intel_engine_cs *engine; |
| 523 | |
Chris Wilson | c218ee0 | 2018-01-06 10:56:18 +0000 | [diff] [blame] | 524 | if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence)) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 525 | return; |
| 526 | |
| 527 | rq = to_request(fence); |
| 528 | engine = rq->engine; |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 529 | |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 530 | local_bh_disable(); |
| 531 | rcu_read_lock(); /* RCU serialisation for set-wedged protection */ |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 532 | if (engine->schedule) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 533 | engine->schedule(rq, attr); |
Chris Wilson | 47650db | 2018-03-07 13:42:25 +0000 | [diff] [blame] | 534 | rcu_read_unlock(); |
Chris Wilson | 4f6d8fc | 2018-05-07 14:57:25 +0100 | [diff] [blame] | 535 | local_bh_enable(); /* kick the tasklets if queues were reprioritised */ |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 538 | static void fence_set_priority(struct dma_fence *fence, |
| 539 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 540 | { |
| 541 | /* Recurse once into a fence-array */ |
| 542 | if (dma_fence_is_array(fence)) { |
| 543 | struct dma_fence_array *array = to_dma_fence_array(fence); |
| 544 | int i; |
| 545 | |
| 546 | for (i = 0; i < array->num_fences; i++) |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 547 | __fence_set_priority(array->fences[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 548 | } else { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 549 | __fence_set_priority(fence, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | int |
| 554 | i915_gem_object_wait_priority(struct drm_i915_gem_object *obj, |
| 555 | unsigned int flags, |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 556 | const struct i915_sched_attr *attr) |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 557 | { |
| 558 | struct dma_fence *excl; |
| 559 | |
| 560 | if (flags & I915_WAIT_ALL) { |
| 561 | struct dma_fence **shared; |
| 562 | unsigned int count, i; |
| 563 | int ret; |
| 564 | |
| 565 | ret = reservation_object_get_fences_rcu(obj->resv, |
| 566 | &excl, &count, &shared); |
| 567 | if (ret) |
| 568 | return ret; |
| 569 | |
| 570 | for (i = 0; i < count; i++) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 571 | fence_set_priority(shared[i], attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 572 | dma_fence_put(shared[i]); |
| 573 | } |
| 574 | |
| 575 | kfree(shared); |
| 576 | } else { |
| 577 | excl = reservation_object_get_excl_rcu(obj->resv); |
| 578 | } |
| 579 | |
| 580 | if (excl) { |
Chris Wilson | b7268c5 | 2018-04-18 19:40:52 +0100 | [diff] [blame] | 581 | fence_set_priority(excl, attr); |
Chris Wilson | 6b5e90f | 2016-11-14 20:41:05 +0000 | [diff] [blame] | 582 | dma_fence_put(excl); |
| 583 | } |
| 584 | return 0; |
| 585 | } |
| 586 | |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 587 | /** |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 588 | * Waits for rendering to the object to be completed |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 589 | * @obj: i915 gem object |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 590 | * @flags: how to wait (under a lock, for all rendering or just for writes etc) |
| 591 | * @timeout: how long to wait |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 592 | */ |
| 593 | int |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 594 | i915_gem_object_wait(struct drm_i915_gem_object *obj, |
| 595 | unsigned int flags, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 596 | long timeout) |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 597 | { |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 598 | might_sleep(); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 599 | GEM_BUG_ON(timeout < 0); |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 600 | |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 601 | timeout = i915_gem_object_wait_reservation(obj->resv, flags, timeout); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 602 | return timeout < 0 ? timeout : 0; |
Chris Wilson | 00e60f2 | 2016-08-04 16:32:40 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 605 | static int |
| 606 | i915_gem_phys_pwrite(struct drm_i915_gem_object *obj, |
| 607 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 608 | struct drm_file *file) |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 609 | { |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 610 | void *vaddr = obj->phys_handle->vaddr + args->offset; |
Gustavo Padovan | 3ed605b | 2016-04-26 12:32:27 -0300 | [diff] [blame] | 611 | char __user *user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 612 | |
| 613 | /* We manually control the domain here and pretend that it |
| 614 | * remains coherent i.e. in the GTT domain, like shmem_pwrite. |
| 615 | */ |
Rodrigo Vivi | 77a0d1c | 2015-06-18 11:43:24 -0700 | [diff] [blame] | 616 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 617 | if (copy_from_user(vaddr, user_data, args->size)) |
| 618 | return -EFAULT; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 619 | |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 620 | drm_clflush_virt_range(vaddr, args->size); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 621 | i915_gem_chipset_flush(to_i915(obj->base.dev)); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 622 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 623 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | 10466d2 | 2017-01-06 15:22:38 +0000 | [diff] [blame] | 624 | return 0; |
Chris Wilson | 0073115 | 2014-05-21 12:42:56 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 627 | void *i915_gem_object_alloc(struct drm_i915_private *dev_priv) |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 628 | { |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 629 | return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 633 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 634 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | efab6d8 | 2015-04-07 16:20:57 +0100 | [diff] [blame] | 635 | kmem_cache_free(dev_priv->objects, obj); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 638 | static int |
| 639 | i915_gem_create(struct drm_file *file, |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 640 | struct drm_i915_private *dev_priv, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 641 | u64 size, |
| 642 | u32 *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 643 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 644 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 645 | int ret; |
| 646 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 647 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 648 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 649 | if (size == 0) |
| 650 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 651 | |
| 652 | /* Allocate the new object */ |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 653 | obj = i915_gem_object_create(dev_priv, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 654 | if (IS_ERR(obj)) |
| 655 | return PTR_ERR(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 656 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 657 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 658 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 659 | i915_gem_object_put(obj); |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 660 | if (ret) |
| 661 | return ret; |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 662 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 663 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 667 | int |
| 668 | i915_gem_dumb_create(struct drm_file *file, |
| 669 | struct drm_device *dev, |
| 670 | struct drm_mode_create_dumb *args) |
| 671 | { |
| 672 | /* have to work out size/pitch and return them */ |
Paulo Zanoni | de45eaf | 2013-10-18 18:48:24 -0300 | [diff] [blame] | 673 | args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 674 | args->size = args->pitch * args->height; |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 675 | return i915_gem_create(file, to_i915(dev), |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 676 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 677 | } |
| 678 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 679 | static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj) |
| 680 | { |
| 681 | return !(obj->cache_level == I915_CACHE_NONE || |
| 682 | obj->cache_level == I915_CACHE_WT); |
| 683 | } |
| 684 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 685 | /** |
| 686 | * Creates a new mm object and returns a handle to it. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 687 | * @dev: drm device pointer |
| 688 | * @data: ioctl data blob |
| 689 | * @file: drm file pointer |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 690 | */ |
| 691 | int |
| 692 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 693 | struct drm_file *file) |
| 694 | { |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 695 | struct drm_i915_private *dev_priv = to_i915(dev); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 696 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 697 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 698 | i915_gem_flush_free_objects(dev_priv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 699 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 700 | return i915_gem_create(file, dev_priv, |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 701 | args->size, &args->handle); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 702 | } |
| 703 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 704 | static inline enum fb_op_origin |
| 705 | fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain) |
| 706 | { |
| 707 | return (domain == I915_GEM_DOMAIN_GTT ? |
| 708 | obj->frontbuffer_ggtt_origin : ORIGIN_CPU); |
| 709 | } |
| 710 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 711 | void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 712 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 713 | intel_wakeref_t wakeref; |
| 714 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 715 | /* |
| 716 | * No actual flushing is required for the GTT write domain for reads |
| 717 | * from the GTT domain. Writes to it "immediately" go to main memory |
| 718 | * as far as we know, so there's no chipset flush. It also doesn't |
| 719 | * land in the GPU render cache. |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 720 | * |
| 721 | * However, we do have to enforce the order so that all writes through |
| 722 | * the GTT land before any writes to the device, such as updates to |
| 723 | * the GATT itself. |
| 724 | * |
| 725 | * We also have to wait a bit for the writes to land from the GTT. |
| 726 | * An uncached read (i.e. mmio) seems to be ideal for the round-trip |
| 727 | * timing. This issue has only been observed when switching quickly |
| 728 | * between GTT writes and CPU reads from inside the kernel on recent hw, |
| 729 | * 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] | 730 | * system agents we cannot reproduce this behaviour, until Cannonlake |
| 731 | * that was!). |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 732 | */ |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 733 | |
Chris Wilson | 900ccf3 | 2018-07-20 11:19:10 +0100 | [diff] [blame] | 734 | wmb(); |
| 735 | |
| 736 | if (INTEL_INFO(dev_priv)->has_coherent_ggtt) |
| 737 | return; |
| 738 | |
Chris Wilson | a8bd3b8 | 2018-07-17 10:26:55 +0100 | [diff] [blame] | 739 | i915_gem_chipset_flush(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 740 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 741 | with_intel_runtime_pm(dev_priv, wakeref) { |
| 742 | spin_lock_irq(&dev_priv->uncore.lock); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 743 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 744 | POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 745 | |
Chris Wilson | d4225a5 | 2019-01-14 14:21:23 +0000 | [diff] [blame] | 746 | spin_unlock_irq(&dev_priv->uncore.lock); |
| 747 | } |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static void |
| 751 | flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains) |
| 752 | { |
| 753 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
| 754 | struct i915_vma *vma; |
| 755 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 756 | if (!(obj->write_domain & flush_domains)) |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 757 | return; |
| 758 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 759 | switch (obj->write_domain) { |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 760 | case I915_GEM_DOMAIN_GTT: |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 761 | i915_gem_flush_ggtt_writes(dev_priv); |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 762 | |
| 763 | intel_fb_obj_flush(obj, |
| 764 | fb_write_origin(obj, I915_GEM_DOMAIN_GTT)); |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 765 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 766 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 767 | if (vma->iomap) |
| 768 | continue; |
| 769 | |
| 770 | i915_vma_unset_ggtt_write(vma); |
| 771 | } |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 772 | break; |
| 773 | |
Chris Wilson | add00e6 | 2018-07-06 12:54:02 +0100 | [diff] [blame] | 774 | case I915_GEM_DOMAIN_WC: |
| 775 | wmb(); |
| 776 | break; |
| 777 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 778 | case I915_GEM_DOMAIN_CPU: |
| 779 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
| 780 | break; |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 781 | |
| 782 | case I915_GEM_DOMAIN_RENDER: |
| 783 | if (gpu_write_needs_clflush(obj)) |
| 784 | obj->cache_dirty = true; |
| 785 | break; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 788 | obj->write_domain = 0; |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 791 | /* |
| 792 | * Pins the specified object's pages and synchronizes the object with |
| 793 | * GPU accesses. Sets needs_clflush to non-zero if the caller should |
| 794 | * flush the object from the CPU cache. |
| 795 | */ |
| 796 | 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] | 797 | unsigned int *needs_clflush) |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 798 | { |
| 799 | int ret; |
| 800 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 801 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 802 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 803 | *needs_clflush = 0; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 804 | if (!i915_gem_object_has_struct_page(obj)) |
| 805 | return -ENODEV; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 806 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 807 | ret = i915_gem_object_wait(obj, |
| 808 | I915_WAIT_INTERRUPTIBLE | |
| 809 | I915_WAIT_LOCKED, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 810 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 811 | if (ret) |
| 812 | return ret; |
| 813 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 814 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 815 | if (ret) |
| 816 | return ret; |
| 817 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 818 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ || |
| 819 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 820 | ret = i915_gem_object_set_to_cpu_domain(obj, false); |
| 821 | if (ret) |
| 822 | goto err_unpin; |
| 823 | else |
| 824 | goto out; |
| 825 | } |
| 826 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 827 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 828 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 829 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 830 | * read domain and manually flush cachelines (if required). This |
| 831 | * optimizes for the case when the gpu will dirty the data |
| 832 | * anyway again before the next pread happens. |
| 833 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 834 | if (!obj->cache_dirty && |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 835 | !(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 836 | *needs_clflush = CLFLUSH_BEFORE; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 837 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 838 | out: |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 839 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 840 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 841 | |
| 842 | err_unpin: |
| 843 | i915_gem_object_unpin_pages(obj); |
| 844 | return ret; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj, |
| 848 | unsigned int *needs_clflush) |
| 849 | { |
| 850 | int ret; |
| 851 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 852 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 853 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 854 | *needs_clflush = 0; |
| 855 | if (!i915_gem_object_has_struct_page(obj)) |
| 856 | return -ENODEV; |
| 857 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 858 | ret = i915_gem_object_wait(obj, |
| 859 | I915_WAIT_INTERRUPTIBLE | |
| 860 | I915_WAIT_LOCKED | |
| 861 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 862 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 863 | if (ret) |
| 864 | return ret; |
| 865 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 866 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 867 | if (ret) |
| 868 | return ret; |
| 869 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 870 | if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE || |
| 871 | !static_cpu_has(X86_FEATURE_CLFLUSH)) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 872 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 873 | if (ret) |
| 874 | goto err_unpin; |
| 875 | else |
| 876 | goto out; |
| 877 | } |
| 878 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 879 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Chris Wilson | a314d5c | 2016-08-18 17:16:48 +0100 | [diff] [blame] | 880 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 881 | /* If we're not in the cpu write domain, set ourself into the |
| 882 | * gtt write domain and manually flush cachelines (as required). |
| 883 | * This optimizes for the case when the gpu will use the data |
| 884 | * right away and we therefore have to clflush anyway. |
| 885 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 886 | if (!obj->cache_dirty) { |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 887 | *needs_clflush |= CLFLUSH_AFTER; |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 888 | |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 889 | /* |
| 890 | * Same trick applies to invalidate partially written |
| 891 | * cachelines read before writing. |
| 892 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 893 | if (!(obj->read_domains & I915_GEM_DOMAIN_CPU)) |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 894 | *needs_clflush |= CLFLUSH_BEFORE; |
| 895 | } |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 896 | |
Chris Wilson | 7f5f95d | 2017-03-10 00:09:42 +0000 | [diff] [blame] | 897 | out: |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 898 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 899 | obj->mm.dirty = true; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 900 | /* return with the pages pinned */ |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 901 | return 0; |
Chris Wilson | 9764951 | 2016-08-18 17:16:50 +0100 | [diff] [blame] | 902 | |
| 903 | err_unpin: |
| 904 | i915_gem_object_unpin_pages(obj); |
| 905 | return ret; |
Brad Volkin | 4c914c0 | 2014-02-18 10:15:45 -0800 | [diff] [blame] | 906 | } |
| 907 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 908 | static int |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 909 | shmem_pread(struct page *page, int offset, int len, char __user *user_data, |
| 910 | bool needs_clflush) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 911 | { |
| 912 | char *vaddr; |
| 913 | int ret; |
| 914 | |
| 915 | vaddr = kmap(page); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 916 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 917 | if (needs_clflush) |
| 918 | drm_clflush_virt_range(vaddr + offset, len); |
| 919 | |
| 920 | ret = __copy_to_user(user_data, vaddr + offset, len); |
| 921 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 922 | kunmap(page); |
| 923 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 924 | return ret ? -EFAULT : 0; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static int |
| 928 | i915_gem_shmem_pread(struct drm_i915_gem_object *obj, |
| 929 | struct drm_i915_gem_pread *args) |
| 930 | { |
| 931 | char __user *user_data; |
| 932 | u64 remain; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 933 | unsigned int needs_clflush; |
| 934 | unsigned int idx, offset; |
| 935 | int ret; |
| 936 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 937 | ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex); |
| 938 | if (ret) |
| 939 | return ret; |
| 940 | |
| 941 | ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush); |
| 942 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 943 | if (ret) |
| 944 | return ret; |
| 945 | |
| 946 | remain = args->size; |
| 947 | user_data = u64_to_user_ptr(args->data_ptr); |
| 948 | offset = offset_in_page(args->offset); |
| 949 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 950 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 951 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 952 | |
| 953 | ret = shmem_pread(page, offset, length, user_data, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 954 | needs_clflush); |
| 955 | if (ret) |
| 956 | break; |
| 957 | |
| 958 | remain -= length; |
| 959 | user_data += length; |
| 960 | offset = 0; |
| 961 | } |
| 962 | |
| 963 | i915_gem_obj_finish_shmem_access(obj); |
| 964 | return ret; |
| 965 | } |
| 966 | |
| 967 | static inline bool |
| 968 | gtt_user_read(struct io_mapping *mapping, |
| 969 | loff_t base, int offset, |
| 970 | char __user *user_data, int length) |
| 971 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 972 | void __iomem *vaddr; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 973 | unsigned long unwritten; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 974 | |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 975 | /* 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] | 976 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 977 | unwritten = __copy_to_user_inatomic(user_data, |
| 978 | (void __force *)vaddr + offset, |
| 979 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 980 | io_mapping_unmap_atomic(vaddr); |
| 981 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 982 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 983 | unwritten = copy_to_user(user_data, |
| 984 | (void __force *)vaddr + offset, |
| 985 | length); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 986 | io_mapping_unmap(vaddr); |
| 987 | } |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 988 | return unwritten; |
| 989 | } |
| 990 | |
| 991 | static int |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 992 | i915_gem_gtt_pread(struct drm_i915_gem_object *obj, |
| 993 | const struct drm_i915_gem_pread *args) |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 994 | { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 995 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 996 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 997 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 998 | struct drm_mm_node node; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 999 | struct i915_vma *vma; |
| 1000 | void __user *user_data; |
| 1001 | u64 remain, offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1002 | int ret; |
| 1003 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1004 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1005 | if (ret) |
| 1006 | return ret; |
| 1007 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1008 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1009 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1010 | PIN_MAPPABLE | |
| 1011 | PIN_NONFAULT | |
| 1012 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1013 | if (!IS_ERR(vma)) { |
| 1014 | node.start = i915_ggtt_offset(vma); |
| 1015 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1016 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1017 | if (ret) { |
| 1018 | i915_vma_unpin(vma); |
| 1019 | vma = ERR_PTR(ret); |
| 1020 | } |
| 1021 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1022 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1023 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1024 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1025 | goto out_unlock; |
| 1026 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 1030 | if (ret) |
| 1031 | goto out_unpin; |
| 1032 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1033 | mutex_unlock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1034 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1035 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1036 | remain = args->size; |
| 1037 | offset = args->offset; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1038 | |
| 1039 | while (remain > 0) { |
| 1040 | /* Operation in this page |
| 1041 | * |
| 1042 | * page_base = page offset within aperture |
| 1043 | * page_offset = offset within page |
| 1044 | * page_length = bytes to copy for this page |
| 1045 | */ |
| 1046 | u32 page_base = node.start; |
| 1047 | unsigned page_offset = offset_in_page(offset); |
| 1048 | unsigned page_length = PAGE_SIZE - page_offset; |
| 1049 | page_length = remain < page_length ? remain : page_length; |
| 1050 | if (node.allocated) { |
| 1051 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1052 | ggtt->vm.insert_page(&ggtt->vm, |
| 1053 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1054 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1055 | wmb(); |
| 1056 | } else { |
| 1057 | page_base += offset & PAGE_MASK; |
| 1058 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1059 | |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1060 | if (gtt_user_read(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1061 | user_data, page_length)) { |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1062 | ret = -EFAULT; |
| 1063 | break; |
| 1064 | } |
| 1065 | |
| 1066 | remain -= page_length; |
| 1067 | user_data += page_length; |
| 1068 | offset += page_length; |
| 1069 | } |
| 1070 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1071 | mutex_lock(&i915->drm.struct_mutex); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1072 | out_unpin: |
| 1073 | if (node.allocated) { |
| 1074 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1075 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1076 | remove_mappable_node(&node); |
| 1077 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1078 | i915_vma_unpin(vma); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1079 | } |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1080 | out_unlock: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1081 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1082 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 1083 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1084 | return ret; |
| 1085 | } |
| 1086 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1087 | /** |
| 1088 | * Reads data from the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1089 | * @dev: drm device pointer |
| 1090 | * @data: ioctl data blob |
| 1091 | * @file: drm file pointer |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1092 | * |
| 1093 | * On error, the contents of *data are undefined. |
| 1094 | */ |
| 1095 | int |
| 1096 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1097 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1098 | { |
| 1099 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1100 | struct drm_i915_gem_object *obj; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1101 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1102 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1103 | if (args->size == 0) |
| 1104 | return 0; |
| 1105 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1106 | if (!access_ok(u64_to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1107 | args->size)) |
| 1108 | return -EFAULT; |
| 1109 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1110 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1111 | if (!obj) |
| 1112 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1113 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1114 | /* Bounds check source. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1115 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1116 | ret = -EINVAL; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1117 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1120 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 1121 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1122 | ret = i915_gem_object_wait(obj, |
| 1123 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1124 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1125 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1126 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1127 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1128 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1129 | if (ret) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1130 | goto out; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1131 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1132 | ret = i915_gem_shmem_pread(obj, args); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1133 | if (ret == -EFAULT || ret == -ENODEV) |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1134 | ret = i915_gem_gtt_pread(obj, args); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1135 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1136 | i915_gem_object_unpin_pages(obj); |
| 1137 | out: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1138 | i915_gem_object_put(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 1139 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1142 | /* This is the fast write path which cannot handle |
| 1143 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1144 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 1145 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1146 | static inline bool |
| 1147 | ggtt_write(struct io_mapping *mapping, |
| 1148 | loff_t base, int offset, |
| 1149 | char __user *user_data, int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1150 | { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1151 | void __iomem *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1152 | unsigned long unwritten; |
| 1153 | |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 1154 | /* 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] | 1155 | vaddr = io_mapping_map_atomic_wc(mapping, base); |
| 1156 | unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1157 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1158 | io_mapping_unmap_atomic(vaddr); |
| 1159 | if (unwritten) { |
Ville Syrjälä | afe722b | 2017-09-01 20:12:52 +0300 | [diff] [blame] | 1160 | vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE); |
| 1161 | unwritten = copy_from_user((void __force *)vaddr + offset, |
| 1162 | user_data, length); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1163 | io_mapping_unmap(vaddr); |
| 1164 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1165 | |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1166 | return unwritten; |
| 1167 | } |
| 1168 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1169 | /** |
| 1170 | * This is the fast pwrite path, where we copy the data directly from the |
| 1171 | * user into the GTT, uncached. |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1172 | * @obj: i915 GEM object |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1173 | * @args: pwrite arguments structure |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1174 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1175 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1176 | i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj, |
| 1177 | const struct drm_i915_gem_pwrite *args) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1178 | { |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1179 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1180 | struct i915_ggtt *ggtt = &i915->ggtt; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1181 | intel_wakeref_t wakeref; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1182 | struct drm_mm_node node; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1183 | struct i915_vma *vma; |
| 1184 | u64 remain, offset; |
| 1185 | void __user *user_data; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1186 | int ret; |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1187 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1188 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
| 1189 | if (ret) |
| 1190 | return ret; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1191 | |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1192 | if (i915_gem_object_has_struct_page(obj)) { |
| 1193 | /* |
| 1194 | * Avoid waking the device up if we can fallback, as |
| 1195 | * waking/resuming is very slow (worst-case 10-100 ms |
| 1196 | * depending on PCI sleeps and our own resume time). |
| 1197 | * This easily dwarfs any performance advantage from |
| 1198 | * using the cache bypass of indirect GGTT access. |
| 1199 | */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1200 | wakeref = intel_runtime_pm_get_if_in_use(i915); |
| 1201 | if (!wakeref) { |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1202 | ret = -EFAULT; |
| 1203 | goto out_unlock; |
| 1204 | } |
| 1205 | } else { |
| 1206 | /* No backing pages, no fallback, we must force GGTT access */ |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1207 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1210 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
Chris Wilson | a3259ca | 2017-10-09 09:44:00 +0100 | [diff] [blame] | 1211 | PIN_MAPPABLE | |
| 1212 | PIN_NONFAULT | |
| 1213 | PIN_NONBLOCK); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1214 | if (!IS_ERR(vma)) { |
| 1215 | node.start = i915_ggtt_offset(vma); |
| 1216 | node.allocated = false; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 1217 | ret = i915_vma_put_fence(vma); |
Chris Wilson | 1803458 | 2016-08-18 17:16:45 +0100 | [diff] [blame] | 1218 | if (ret) { |
| 1219 | i915_vma_unpin(vma); |
| 1220 | vma = ERR_PTR(ret); |
| 1221 | } |
| 1222 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1223 | if (IS_ERR(vma)) { |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1224 | ret = insert_mappable_node(ggtt, &node, PAGE_SIZE); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1225 | if (ret) |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1226 | goto out_rpm; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1227 | GEM_BUG_ON(!node.allocated); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1228 | } |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1229 | |
| 1230 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 1231 | if (ret) |
| 1232 | goto out_unpin; |
| 1233 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1234 | mutex_unlock(&i915->drm.struct_mutex); |
| 1235 | |
Chris Wilson | b19482d | 2016-08-18 17:16:43 +0100 | [diff] [blame] | 1236 | intel_fb_obj_invalidate(obj, ORIGIN_CPU); |
Paulo Zanoni | 063e4e6 | 2015-02-13 17:23:45 -0200 | [diff] [blame] | 1237 | |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1238 | user_data = u64_to_user_ptr(args->data_ptr); |
| 1239 | offset = args->offset; |
| 1240 | remain = args->size; |
| 1241 | while (remain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1242 | /* Operation in this page |
| 1243 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1244 | * page_base = page offset within aperture |
| 1245 | * page_offset = offset within page |
| 1246 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1247 | */ |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1248 | u32 page_base = node.start; |
Chris Wilson | bb6dc8d | 2016-10-28 13:58:39 +0100 | [diff] [blame] | 1249 | unsigned int page_offset = offset_in_page(offset); |
| 1250 | unsigned int page_length = PAGE_SIZE - page_offset; |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1251 | page_length = remain < page_length ? remain : page_length; |
| 1252 | if (node.allocated) { |
| 1253 | wmb(); /* flush the write before we modify the GGTT */ |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1254 | ggtt->vm.insert_page(&ggtt->vm, |
| 1255 | i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT), |
| 1256 | node.start, I915_CACHE_NONE, 0); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1257 | wmb(); /* flush modifications to the GGTT (insert_page) */ |
| 1258 | } else { |
| 1259 | page_base += offset & PAGE_MASK; |
| 1260 | } |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1261 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1262 | * source page isn't available. Return the error and we'll |
| 1263 | * retry in the slow path. |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1264 | * If the object is non-shmem backed, we retry again with the |
| 1265 | * path that handles page fault. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1266 | */ |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1267 | if (ggtt_write(&ggtt->iomap, page_base, page_offset, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1268 | user_data, page_length)) { |
| 1269 | ret = -EFAULT; |
| 1270 | break; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1271 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1272 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 1273 | remain -= page_length; |
| 1274 | user_data += page_length; |
| 1275 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1276 | } |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1277 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1278 | |
| 1279 | mutex_lock(&i915->drm.struct_mutex); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1280 | out_unpin: |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1281 | if (node.allocated) { |
| 1282 | wmb(); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 1283 | ggtt->vm.clear_range(&ggtt->vm, node.start, node.size); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1284 | remove_mappable_node(&node); |
| 1285 | } else { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1286 | i915_vma_unpin(vma); |
Ankitprasad Sharma | 4f1959e | 2016-06-10 14:23:01 +0530 | [diff] [blame] | 1287 | } |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1288 | out_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1289 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 8bd81815 | 2017-10-19 07:37:33 +0100 | [diff] [blame] | 1290 | out_unlock: |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1291 | mutex_unlock(&i915->drm.struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 1292 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1295 | /* Per-page copy function for the shmem pwrite fastpath. |
| 1296 | * Flushes invalid cachelines before writing to the target if |
| 1297 | * needs_clflush_before is set and flushes out any written cachelines after |
| 1298 | * writing if needs_clflush is set. |
| 1299 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1300 | static int |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1301 | shmem_pwrite(struct page *page, int offset, int len, char __user *user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1302 | bool needs_clflush_before, |
| 1303 | bool needs_clflush_after) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1304 | { |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1305 | char *vaddr; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1306 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1307 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1308 | vaddr = kmap(page); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1309 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1310 | if (needs_clflush_before) |
| 1311 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1312 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1313 | ret = __copy_from_user(vaddr + offset, user_data, len); |
| 1314 | if (!ret && needs_clflush_after) |
| 1315 | drm_clflush_virt_range(vaddr + offset, len); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1316 | |
Chris Wilson | b9d126e | 2019-01-05 12:07:58 +0000 | [diff] [blame] | 1317 | kunmap(page); |
| 1318 | |
| 1319 | return ret ? -EFAULT : 0; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | static int |
| 1323 | i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj, |
| 1324 | const struct drm_i915_gem_pwrite *args) |
| 1325 | { |
| 1326 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 1327 | void __user *user_data; |
| 1328 | u64 remain; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1329 | unsigned int partial_cacheline_write; |
| 1330 | unsigned int needs_clflush; |
| 1331 | unsigned int offset, idx; |
| 1332 | int ret; |
| 1333 | |
| 1334 | ret = mutex_lock_interruptible(&i915->drm.struct_mutex); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1335 | if (ret) |
| 1336 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1337 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1338 | ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush); |
| 1339 | mutex_unlock(&i915->drm.struct_mutex); |
| 1340 | if (ret) |
| 1341 | return ret; |
| 1342 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1343 | /* If we don't overwrite a cacheline completely we need to be |
| 1344 | * careful to have up-to-date data by first clflushing. Don't |
| 1345 | * overcomplicate things and flush the entire patch. |
| 1346 | */ |
| 1347 | partial_cacheline_write = 0; |
| 1348 | if (needs_clflush & CLFLUSH_BEFORE) |
| 1349 | partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1; |
| 1350 | |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1351 | user_data = u64_to_user_ptr(args->data_ptr); |
Chris Wilson | 43394c7 | 2016-08-18 17:16:47 +0100 | [diff] [blame] | 1352 | remain = args->size; |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1353 | offset = offset_in_page(args->offset); |
| 1354 | for (idx = args->offset >> PAGE_SHIFT; remain; idx++) { |
| 1355 | struct page *page = i915_gem_object_get_page(obj, idx); |
Chris Wilson | a5e856a5 | 2018-10-12 15:02:28 +0100 | [diff] [blame] | 1356 | unsigned int length = min_t(u64, remain, PAGE_SIZE - offset); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1357 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1358 | ret = shmem_pwrite(page, offset, length, user_data, |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1359 | (offset | length) & partial_cacheline_write, |
| 1360 | needs_clflush & CLFLUSH_AFTER); |
| 1361 | if (ret) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1362 | break; |
| 1363 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1364 | remain -= length; |
| 1365 | user_data += length; |
| 1366 | offset = 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 1369 | intel_fb_obj_flush(obj, ORIGIN_CPU); |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1370 | i915_gem_obj_finish_shmem_access(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1371 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * Writes data to the object referenced by handle. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1376 | * @dev: drm device |
| 1377 | * @data: ioctl data blob |
| 1378 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1379 | * |
| 1380 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1381 | */ |
| 1382 | int |
| 1383 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1384 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1385 | { |
| 1386 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1387 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1388 | int ret; |
| 1389 | |
| 1390 | if (args->size == 0) |
| 1391 | return 0; |
| 1392 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1393 | if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size)) |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1394 | return -EFAULT; |
| 1395 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1396 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1397 | if (!obj) |
| 1398 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1399 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1400 | /* Bounds check destination. */ |
Matthew Auld | 966d5bf | 2016-12-13 20:32:22 +0000 | [diff] [blame] | 1401 | if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1402 | ret = -EINVAL; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1403 | goto err; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Chris Wilson | f8c1cce | 2018-07-12 19:53:14 +0100 | [diff] [blame] | 1406 | /* Writes not allowed into this read-only object */ |
| 1407 | if (i915_gem_object_is_readonly(obj)) { |
| 1408 | ret = -EINVAL; |
| 1409 | goto err; |
| 1410 | } |
| 1411 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1412 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 1413 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 1414 | ret = -ENODEV; |
| 1415 | if (obj->ops->pwrite) |
| 1416 | ret = obj->ops->pwrite(obj, args); |
| 1417 | if (ret != -ENODEV) |
| 1418 | goto err; |
| 1419 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1420 | ret = i915_gem_object_wait(obj, |
| 1421 | I915_WAIT_INTERRUPTIBLE | |
| 1422 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1423 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1424 | if (ret) |
| 1425 | goto err; |
| 1426 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1427 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1428 | if (ret) |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1429 | goto err; |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1430 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1431 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1432 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1433 | * it would end up going through the fenced access, and we'll get |
| 1434 | * different detiling behavior between reading and writing. |
| 1435 | * pread/pwrite currently are reading and writing from the CPU |
| 1436 | * perspective, requiring manual detiling by the client. |
| 1437 | */ |
Chris Wilson | 6eae005 | 2016-06-20 15:05:52 +0100 | [diff] [blame] | 1438 | if (!i915_gem_object_has_struct_page(obj) || |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1439 | cpu_write_needs_clflush(obj)) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 1440 | /* Note that the gtt paths might fail with non-page-backed user |
| 1441 | * pointers (e.g. gtt mappings when moving data between |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 1442 | * textures). Fallback to the shmem path in that case. |
| 1443 | */ |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1444 | ret = i915_gem_gtt_pwrite_fast(obj, args); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1445 | |
Chris Wilson | d1054ee | 2016-07-16 18:42:36 +0100 | [diff] [blame] | 1446 | if (ret == -EFAULT || ret == -ENOSPC) { |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1447 | if (obj->phys_handle) |
| 1448 | ret = i915_gem_phys_pwrite(obj, args, file); |
Ankitprasad Sharma | b50a537 | 2016-06-10 14:23:03 +0530 | [diff] [blame] | 1449 | else |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1450 | ret = i915_gem_shmem_pwrite(obj, args); |
Chris Wilson | 6a2c423 | 2014-11-04 04:51:40 -0800 | [diff] [blame] | 1451 | } |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 1452 | |
Chris Wilson | fe11562 | 2016-10-28 13:58:40 +0100 | [diff] [blame] | 1453 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1454 | err: |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1455 | i915_gem_object_put(obj); |
Chris Wilson | 258a5ed | 2016-08-05 10:14:16 +0100 | [diff] [blame] | 1456 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1459 | static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj) |
| 1460 | { |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1461 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1462 | struct list_head *list; |
| 1463 | struct i915_vma *vma; |
| 1464 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1465 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
| 1466 | |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1467 | mutex_lock(&i915->ggtt.vm.mutex); |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1468 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1469 | if (!drm_mm_node_allocated(&vma->node)) |
| 1470 | continue; |
| 1471 | |
Chris Wilson | 499197d | 2019-01-28 10:23:52 +0000 | [diff] [blame] | 1472 | list_move_tail(&vma->vm_link, &vma->vm->bound_list); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1473 | } |
Chris Wilson | 09d7e46 | 2019-01-28 10:23:53 +0000 | [diff] [blame] | 1474 | mutex_unlock(&i915->ggtt.vm.mutex); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1475 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1476 | spin_lock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1477 | list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list; |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 1478 | list_move_tail(&obj->mm.link, list); |
| 1479 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1482 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1483 | * Called when user space prepares to use an object with the CPU, either |
| 1484 | * through the mmap ioctl's mapping or a GTT mapping. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1485 | * @dev: drm device |
| 1486 | * @data: ioctl data blob |
| 1487 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1488 | */ |
| 1489 | int |
| 1490 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1491 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1492 | { |
| 1493 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1494 | struct drm_i915_gem_object *obj; |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1495 | u32 read_domains = args->read_domains; |
| 1496 | u32 write_domain = args->write_domain; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1497 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1498 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1499 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1500 | if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1501 | return -EINVAL; |
| 1502 | |
| 1503 | /* Having something in the write domain implies it's in the read |
| 1504 | * domain, and only that read domain. Enforce that in the request. |
| 1505 | */ |
| 1506 | if (write_domain != 0 && read_domains != write_domain) |
| 1507 | return -EINVAL; |
| 1508 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1509 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1510 | if (!obj) |
| 1511 | return -ENOENT; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1512 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1513 | /* Try to flush the object off the GPU without holding the lock. |
| 1514 | * We will repeat the flush holding the lock in the normal manner |
| 1515 | * to catch cases where we are gazumped. |
| 1516 | */ |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1517 | err = i915_gem_object_wait(obj, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1518 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 1519 | I915_WAIT_PRIORITY | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1520 | (write_domain ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1521 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1522 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1523 | goto out; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1524 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1525 | /* |
| 1526 | * Proxy objects do not control access to the backing storage, ergo |
| 1527 | * they cannot be used as a means to manipulate the cache domain |
| 1528 | * tracking for that backing storage. The proxy object is always |
| 1529 | * considered to be outside of any cache domain. |
| 1530 | */ |
| 1531 | if (i915_gem_object_is_proxy(obj)) { |
| 1532 | err = -ENXIO; |
| 1533 | goto out; |
| 1534 | } |
| 1535 | |
| 1536 | /* |
| 1537 | * Flush and acquire obj->pages so that we are coherent through |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1538 | * direct access in memory with previous cached writes through |
| 1539 | * shmemfs and that our cache domain tracking remains valid. |
| 1540 | * For example, if the obj->filp was moved to swap without us |
| 1541 | * being notified and releasing the pages, we would mistakenly |
| 1542 | * continue to assume that the obj remained out of the CPU cached |
| 1543 | * domain. |
| 1544 | */ |
| 1545 | err = i915_gem_object_pin_pages(obj); |
| 1546 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1547 | goto out; |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1548 | |
| 1549 | err = i915_mutex_lock_interruptible(dev); |
| 1550 | if (err) |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1551 | goto out_unpin; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1552 | |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1553 | if (read_domains & I915_GEM_DOMAIN_WC) |
| 1554 | err = i915_gem_object_set_to_wc_domain(obj, write_domain); |
| 1555 | else if (read_domains & I915_GEM_DOMAIN_GTT) |
| 1556 | err = i915_gem_object_set_to_gtt_domain(obj, write_domain); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 1557 | else |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1558 | err = i915_gem_object_set_to_cpu_domain(obj, write_domain); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1559 | |
| 1560 | /* And bump the LRU for this access */ |
| 1561 | i915_gem_object_bump_inactive_ggtt(obj); |
| 1562 | |
| 1563 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1564 | |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1565 | if (write_domain != 0) |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 1566 | intel_fb_obj_invalidate(obj, |
| 1567 | fb_write_origin(obj, write_domain)); |
Daniel Vetter | 031b698 | 2015-06-26 19:35:16 +0200 | [diff] [blame] | 1568 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1569 | out_unpin: |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1570 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1571 | out: |
| 1572 | i915_gem_object_put(obj); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1573 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /** |
| 1577 | * Called when user space has done writes to this buffer |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1578 | * @dev: drm device |
| 1579 | * @data: ioctl data blob |
| 1580 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1581 | */ |
| 1582 | int |
| 1583 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1584 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1585 | { |
| 1586 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1587 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1588 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1589 | obj = i915_gem_object_lookup(file, args->handle); |
Chris Wilson | c21724c | 2016-08-05 10:14:19 +0100 | [diff] [blame] | 1590 | if (!obj) |
| 1591 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1592 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 1593 | /* |
| 1594 | * Proxy objects are barred from CPU access, so there is no |
| 1595 | * need to ban sw_finish as it is a nop. |
| 1596 | */ |
| 1597 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1598 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1599 | i915_gem_object_flush_if_display(obj); |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1600 | i915_gem_object_put(obj); |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 1601 | |
| 1602 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1605 | static inline bool |
| 1606 | __vma_matches(struct vm_area_struct *vma, struct file *filp, |
| 1607 | unsigned long addr, unsigned long size) |
| 1608 | { |
| 1609 | if (vma->vm_file != filp) |
| 1610 | return false; |
| 1611 | |
| 1612 | return vma->vm_start == addr && (vma->vm_end - vma->vm_start) == size; |
| 1613 | } |
| 1614 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1615 | /** |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 1616 | * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address |
| 1617 | * it is mapped to. |
| 1618 | * @dev: drm device |
| 1619 | * @data: ioctl data blob |
| 1620 | * @file: drm file |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1621 | * |
| 1622 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1623 | * imply a ref on the object itself. |
Daniel Vetter | 3436738 | 2014-10-16 12:28:18 +0200 | [diff] [blame] | 1624 | * |
| 1625 | * IMPORTANT: |
| 1626 | * |
| 1627 | * DRM driver writers who look a this function as an example for how to do GEM |
| 1628 | * mmap support, please don't implement mmap support like here. The modern way |
| 1629 | * to implement DRM mmap support is with an mmap offset ioctl (like |
| 1630 | * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly. |
| 1631 | * That way debug tooling like valgrind will understand what's going on, hiding |
| 1632 | * the mmap call in a driver private ioctl will break that. The i915 driver only |
| 1633 | * does cpu mmaps this way because we didn't know better. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1634 | */ |
| 1635 | int |
| 1636 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1637 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1638 | { |
| 1639 | struct drm_i915_gem_mmap *args = data; |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1640 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1641 | unsigned long addr; |
| 1642 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1643 | if (args->flags & ~(I915_MMAP_WC)) |
| 1644 | return -EINVAL; |
| 1645 | |
Borislav Petkov | 568a58e | 2016-03-29 17:42:01 +0200 | [diff] [blame] | 1646 | if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1647 | return -ENODEV; |
| 1648 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1649 | obj = i915_gem_object_lookup(file, args->handle); |
| 1650 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1651 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1652 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1653 | /* prime objects have no backing filp to GEM mmap |
| 1654 | * pages from. |
| 1655 | */ |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1656 | if (!obj->base.filp) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1657 | i915_gem_object_put(obj); |
Tina Zhang | 274b246 | 2017-11-14 10:25:12 +0000 | [diff] [blame] | 1658 | return -ENXIO; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1659 | } |
| 1660 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 1661 | addr = vm_mmap(obj->base.filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1662 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1663 | args->offset); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1664 | if (IS_ERR_VALUE(addr)) |
| 1665 | goto err; |
| 1666 | |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1667 | if (args->flags & I915_MMAP_WC) { |
| 1668 | struct mm_struct *mm = current->mm; |
| 1669 | struct vm_area_struct *vma; |
| 1670 | |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1671 | if (down_write_killable(&mm->mmap_sem)) { |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1672 | i915_gem_object_put(obj); |
Michal Hocko | 80a89a5 | 2016-05-23 16:26:11 -0700 | [diff] [blame] | 1673 | return -EINTR; |
| 1674 | } |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1675 | vma = find_vma(mm, addr); |
Joonas Lahtinen | 5c4604e | 2019-02-07 10:54:53 +0200 | [diff] [blame] | 1676 | if (vma && __vma_matches(vma, obj->base.filp, addr, args->size)) |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1677 | vma->vm_page_prot = |
| 1678 | pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 1679 | else |
| 1680 | addr = -ENOMEM; |
| 1681 | up_write(&mm->mmap_sem); |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1682 | if (IS_ERR_VALUE(addr)) |
| 1683 | goto err; |
Chris Wilson | aeecc96 | 2016-06-17 14:46:39 -0300 | [diff] [blame] | 1684 | |
| 1685 | /* This may race, but that's ok, it only gets set */ |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1686 | WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU); |
Akash Goel | 1816f92 | 2015-01-02 16:29:30 +0530 | [diff] [blame] | 1687 | } |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 1688 | i915_gem_object_put(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1689 | |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 1690 | args->addr_ptr = (u64)addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1691 | |
| 1692 | return 0; |
Joonas Lahtinen | ebfb697 | 2019-02-07 10:54:54 +0200 | [diff] [blame] | 1693 | |
| 1694 | err: |
| 1695 | i915_gem_object_put(obj); |
| 1696 | |
| 1697 | return addr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1700 | static unsigned int tile_row_pages(const struct drm_i915_gem_object *obj) |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1701 | { |
Chris Wilson | 6649a0b | 2017-01-09 16:16:08 +0000 | [diff] [blame] | 1702 | return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT; |
Chris Wilson | 03af84f | 2016-08-18 17:17:01 +0100 | [diff] [blame] | 1703 | } |
| 1704 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1705 | /** |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1706 | * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps |
| 1707 | * |
| 1708 | * A history of the GTT mmap interface: |
| 1709 | * |
| 1710 | * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to |
| 1711 | * aligned and suitable for fencing, and still fit into the available |
| 1712 | * mappable space left by the pinned display objects. A classic problem |
| 1713 | * we called the page-fault-of-doom where we would ping-pong between |
| 1714 | * two objects that could not fit inside the GTT and so the memcpy |
| 1715 | * would page one object in at the expense of the other between every |
| 1716 | * single byte. |
| 1717 | * |
| 1718 | * 1 - Objects can be any size, and have any compatible fencing (X Y, or none |
| 1719 | * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the |
| 1720 | * object is too large for the available space (or simply too large |
| 1721 | * for the mappable aperture!), a view is created instead and faulted |
| 1722 | * into userspace. (This view is aligned and sized appropriately for |
| 1723 | * fenced access.) |
| 1724 | * |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1725 | * 2 - Recognise WC as a separate cache domain so that we can flush the |
| 1726 | * delayed writes via GTT before performing direct access via WC. |
| 1727 | * |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1728 | * Restrictions: |
| 1729 | * |
| 1730 | * * snoopable objects cannot be accessed via the GTT. It can cause machine |
| 1731 | * hangs on some architectures, corruption on others. An attempt to service |
| 1732 | * a GTT page fault from a snoopable object will generate a SIGBUS. |
| 1733 | * |
| 1734 | * * the object must be able to fit into RAM (physical memory, though no |
| 1735 | * limited to the mappable aperture). |
| 1736 | * |
| 1737 | * |
| 1738 | * Caveats: |
| 1739 | * |
| 1740 | * * a new GTT page fault will synchronize rendering from the GPU and flush |
| 1741 | * all data to system memory. Subsequent access will not be synchronized. |
| 1742 | * |
| 1743 | * * all mappings are revoked on runtime device suspend. |
| 1744 | * |
| 1745 | * * there are only 8, 16 or 32 fence registers to share between all users |
| 1746 | * (older machines require fence register for display and blitter access |
| 1747 | * as well). Contention of the fence registers will cause the previous users |
| 1748 | * to be unmapped and any new access will generate new page faults. |
| 1749 | * |
| 1750 | * * running out of memory while servicing a fault may generate a SIGBUS, |
| 1751 | * rather than the expected SIGSEGV. |
| 1752 | */ |
| 1753 | int i915_gem_mmap_gtt_version(void) |
| 1754 | { |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 1755 | return 2; |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1756 | } |
| 1757 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1758 | static inline struct i915_ggtt_view |
Chris Wilson | d899ace | 2018-07-25 16:54:47 +0100 | [diff] [blame] | 1759 | compute_partial_view(const struct drm_i915_gem_object *obj, |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1760 | pgoff_t page_offset, |
| 1761 | unsigned int chunk) |
| 1762 | { |
| 1763 | struct i915_ggtt_view view; |
| 1764 | |
| 1765 | if (i915_gem_object_is_tiled(obj)) |
| 1766 | chunk = roundup(chunk, tile_row_pages(obj)); |
| 1767 | |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1768 | view.type = I915_GGTT_VIEW_PARTIAL; |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1769 | view.partial.offset = rounddown(page_offset, chunk); |
| 1770 | view.partial.size = |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1771 | min_t(unsigned int, chunk, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1772 | (obj->base.size >> PAGE_SHIFT) - view.partial.offset); |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1773 | |
| 1774 | /* If the partial covers the entire object, just create a normal VMA. */ |
| 1775 | if (chunk >= obj->base.size >> PAGE_SHIFT) |
| 1776 | view.type = I915_GGTT_VIEW_NORMAL; |
| 1777 | |
| 1778 | return view; |
| 1779 | } |
| 1780 | |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1781 | /** |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1782 | * i915_gem_fault - fault a page into the GTT |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 1783 | * @vmf: fault info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1784 | * |
| 1785 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1786 | * from userspace. The fault handler takes care of binding the object to |
| 1787 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1788 | * only if needed based on whether the old reg is still valid or the object |
| 1789 | * is tiled) and inserting a new PTE into the faulting process. |
| 1790 | * |
| 1791 | * Note that the faulting process may involve evicting existing objects |
| 1792 | * from the GTT and/or fence registers to make room. So performance may |
| 1793 | * suffer if the GTT working set is large or there are few fence registers |
| 1794 | * left. |
Chris Wilson | 4cc6907 | 2016-08-25 19:05:19 +0100 | [diff] [blame] | 1795 | * |
| 1796 | * The current feature set supported by i915_gem_fault() and thus GTT mmaps |
| 1797 | * 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] | 1798 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1799 | vm_fault_t i915_gem_fault(struct vm_fault *vmf) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1800 | { |
Chris Wilson | 420980c | 2018-06-05 14:57:46 +0100 | [diff] [blame] | 1801 | #define MIN_CHUNK_PAGES (SZ_1M >> PAGE_SHIFT) |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 1802 | struct vm_area_struct *area = vmf->vma; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1803 | 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] | 1804 | struct drm_device *dev = obj->base.dev; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 1805 | struct drm_i915_private *dev_priv = to_i915(dev); |
| 1806 | struct i915_ggtt *ggtt = &dev_priv->ggtt; |
Chris Wilson | aae7c06 | 2018-09-03 09:33:34 +0100 | [diff] [blame] | 1807 | bool write = area->vm_flags & VM_WRITE; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1808 | intel_wakeref_t wakeref; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1809 | struct i915_vma *vma; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1810 | pgoff_t page_offset; |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 1811 | int srcu; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1812 | int ret; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1813 | |
Chris Wilson | 3e977ac | 2018-07-12 19:53:13 +0100 | [diff] [blame] | 1814 | /* Sanity check that we allow writing into this object */ |
| 1815 | if (i915_gem_object_is_readonly(obj) && write) |
| 1816 | return VM_FAULT_SIGBUS; |
| 1817 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1818 | /* We don't use vmf->pgoff since that has the fake offset */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1819 | page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1820 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1821 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1822 | |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1823 | /* 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] | 1824 | * Upon acquiring the lock, we will perform our sanity checks and then |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1825 | * repeat the flush holding the lock in the normal manner to catch cases |
| 1826 | * where we are gazumped. |
| 1827 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 1828 | ret = i915_gem_object_wait(obj, |
| 1829 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 1830 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1831 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1832 | goto err; |
| 1833 | |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1834 | ret = i915_gem_object_pin_pages(obj); |
| 1835 | if (ret) |
| 1836 | goto err; |
| 1837 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1838 | wakeref = intel_runtime_pm_get(dev_priv); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1839 | |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1840 | srcu = i915_reset_trylock(dev_priv); |
| 1841 | if (srcu < 0) { |
| 1842 | ret = srcu; |
| 1843 | goto err_rpm; |
| 1844 | } |
| 1845 | |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1846 | ret = i915_mutex_lock_interruptible(dev); |
| 1847 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1848 | goto err_reset; |
Chris Wilson | 6e4930f | 2014-02-07 18:37:06 -0200 | [diff] [blame] | 1849 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1850 | /* Access to snoopable pages through the GTT is incoherent. */ |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 1851 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) { |
Chris Wilson | ddeff6e | 2014-05-28 16:16:41 +0100 | [diff] [blame] | 1852 | ret = -EFAULT; |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1853 | goto err_unlock; |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1856 | /* Now pin it into the GTT as needed */ |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1857 | vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, |
| 1858 | PIN_MAPPABLE | |
| 1859 | PIN_NONBLOCK | |
| 1860 | PIN_NONFAULT); |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1861 | if (IS_ERR(vma)) { |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1862 | /* Use a partial view if it is bigger than available space */ |
Chris Wilson | 2d4281b | 2017-01-10 09:56:32 +0000 | [diff] [blame] | 1863 | struct i915_ggtt_view view = |
Chris Wilson | 8201c1f | 2017-01-10 09:56:33 +0000 | [diff] [blame] | 1864 | compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES); |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1865 | unsigned int flags; |
Chris Wilson | aa136d9 | 2016-08-18 17:17:03 +0100 | [diff] [blame] | 1866 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1867 | flags = PIN_MAPPABLE; |
| 1868 | if (view.type == I915_GGTT_VIEW_NORMAL) |
| 1869 | flags |= PIN_NONBLOCK; /* avoid warnings for pinned */ |
| 1870 | |
| 1871 | /* |
| 1872 | * Userspace is now writing through an untracked VMA, abandon |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 1873 | * all hope that the hardware is able to track future writes. |
| 1874 | */ |
| 1875 | obj->frontbuffer_ggtt_origin = ORIGIN_CPU; |
| 1876 | |
Chris Wilson | 7e7367d | 2018-06-30 10:05:09 +0100 | [diff] [blame] | 1877 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1878 | if (IS_ERR(vma) && !view.type) { |
| 1879 | flags = PIN_MAPPABLE; |
| 1880 | view.type = I915_GGTT_VIEW_PARTIAL; |
| 1881 | vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, flags); |
| 1882 | } |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1883 | } |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1884 | if (IS_ERR(vma)) { |
| 1885 | ret = PTR_ERR(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1886 | goto err_unlock; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1887 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1888 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1889 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1890 | if (ret) |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1891 | goto err_unpin; |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1892 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1893 | ret = i915_vma_pin_fence(vma); |
| 1894 | if (ret) |
| 1895 | goto err_unpin; |
| 1896 | |
Chris Wilson | b90b91d | 2014-06-10 12:14:40 +0100 | [diff] [blame] | 1897 | /* Finally, remap it using the new GTT offset */ |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1898 | ret = remap_io_mapping(area, |
Chris Wilson | 8bab1193 | 2017-01-14 00:28:25 +0000 | [diff] [blame] | 1899 | area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1900 | (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT, |
Chris Wilson | c58305a | 2016-08-19 16:54:28 +0100 | [diff] [blame] | 1901 | min_t(u64, vma->size, area->vm_end - area->vm_start), |
Matthew Auld | 73ebd50 | 2017-12-11 15:18:20 +0000 | [diff] [blame] | 1902 | &ggtt->iomap); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1903 | if (ret) |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1904 | goto err_fence; |
Chris Wilson | a61007a | 2016-08-18 17:17:02 +0100 | [diff] [blame] | 1905 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1906 | /* Mark as being mmapped into userspace for later revocation */ |
| 1907 | assert_rpm_wakelock_held(dev_priv); |
| 1908 | if (!i915_vma_set_userfault(vma) && !obj->userfault_count++) |
| 1909 | list_add(&obj->userfault_link, &dev_priv->mm.userfault_list); |
| 1910 | GEM_BUG_ON(!obj->userfault_count); |
| 1911 | |
Chris Wilson | 7125397b | 2017-12-06 12:49:14 +0000 | [diff] [blame] | 1912 | i915_vma_set_ggtt_write(vma); |
| 1913 | |
Chris Wilson | aeaaa55 | 2019-02-12 13:08:30 +0000 | [diff] [blame] | 1914 | err_fence: |
| 1915 | i915_vma_unpin_fence(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1916 | err_unpin: |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 1917 | __i915_vma_unpin(vma); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1918 | err_unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1919 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 43a8f68 | 2019-02-21 10:29:19 +0000 | [diff] [blame] | 1920 | err_reset: |
| 1921 | i915_reset_unlock(dev_priv, srcu); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1922 | err_rpm: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1923 | intel_runtime_pm_put(dev_priv, wakeref); |
Chris Wilson | 40e62d5 | 2016-10-28 13:58:41 +0100 | [diff] [blame] | 1924 | i915_gem_object_unpin_pages(obj); |
Chris Wilson | b8f9096 | 2016-08-05 10:14:07 +0100 | [diff] [blame] | 1925 | err: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1926 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1927 | case -EIO: |
Daniel Vetter | 2232f03 | 2014-09-04 09:36:18 +0200 | [diff] [blame] | 1928 | /* |
| 1929 | * We eat errors when the gpu is terminally wedged to avoid |
| 1930 | * userspace unduly crashing (gl has no provisions for mmaps to |
| 1931 | * fail). But any other -EIO isn't ours (e.g. swap in failure) |
| 1932 | * and so needs to be reported. |
| 1933 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 1934 | if (!i915_terminally_wedged(dev_priv)) |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1935 | return VM_FAULT_SIGBUS; |
Gustavo A. R. Silva | f0d759f | 2018-06-28 17:35:41 -0500 | [diff] [blame] | 1936 | /* else: fall through */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1937 | case -EAGAIN: |
Daniel Vetter | 571c608 | 2013-09-12 17:57:28 +0200 | [diff] [blame] | 1938 | /* |
| 1939 | * EAGAIN means the gpu is hung and we'll wait for the error |
| 1940 | * handler to reset everything when re-faulting in |
| 1941 | * i915_mutex_lock_interruptible. |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1942 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1943 | case 0: |
| 1944 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1945 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1946 | case -EBUSY: |
| 1947 | /* |
| 1948 | * EBUSY is ok: this just means that another thread |
| 1949 | * already did the job. |
| 1950 | */ |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1951 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1952 | case -ENOMEM: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1953 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1954 | case -ENOSPC: |
Chris Wilson | 45d6781 | 2014-01-31 11:34:57 +0000 | [diff] [blame] | 1955 | case -EFAULT: |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1956 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1957 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1958 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | 5213701 | 2018-06-06 22:45:20 +0100 | [diff] [blame] | 1959 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1963 | static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj) |
| 1964 | { |
| 1965 | struct i915_vma *vma; |
| 1966 | |
| 1967 | GEM_BUG_ON(!obj->userfault_count); |
| 1968 | |
| 1969 | obj->userfault_count = 0; |
| 1970 | list_del(&obj->userfault_link); |
| 1971 | drm_vma_node_unmap(&obj->base.vma_node, |
| 1972 | obj->base.dev->anon_inode->i_mapping); |
| 1973 | |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 1974 | for_each_ggtt_vma(vma, obj) |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1975 | i915_vma_unset_userfault(vma); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 1976 | } |
| 1977 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1978 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1979 | * i915_gem_release_mmap - remove physical page mappings |
| 1980 | * @obj: obj in question |
| 1981 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1982 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1983 | * relinquish ownership of the pages back to the system. |
| 1984 | * |
| 1985 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1986 | * object through the GTT and then lose the fence register due to |
| 1987 | * resource pressure. Similarly if the object has been moved out of the |
| 1988 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1989 | * mapping will then trigger a page fault on the next user access, allowing |
| 1990 | * fixup by i915_gem_fault(). |
| 1991 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1992 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1993 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1994 | { |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1995 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 1996 | intel_wakeref_t wakeref; |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 1997 | |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 1998 | /* Serialisation between user GTT access and our code depends upon |
| 1999 | * revoking the CPU's PTE whilst the mutex is held. The next user |
| 2000 | * pagefault then has to wait until we release the mutex. |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2001 | * |
| 2002 | * Note that RPM complicates somewhat by adding an additional |
| 2003 | * requirement that operations to the GGTT be made holding the RPM |
| 2004 | * wakeref. |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2005 | */ |
Chris Wilson | 275f039 | 2016-10-24 13:42:14 +0100 | [diff] [blame] | 2006 | lockdep_assert_held(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2007 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2008 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2009 | if (!obj->userfault_count) |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2010 | goto out; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2011 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2012 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 349f2cc | 2016-04-13 17:35:12 +0100 | [diff] [blame] | 2013 | |
| 2014 | /* Ensure that the CPU's PTE are revoked and there are not outstanding |
| 2015 | * memory transactions from userspace before we return. The TLB |
| 2016 | * flushing implied above by changing the PTE above *should* be |
| 2017 | * sufficient, an extra barrier here just provides us with a bit |
| 2018 | * of paranoid documentation about our requirement to serialise |
| 2019 | * memory writes before touching registers / GSM. |
| 2020 | */ |
| 2021 | wmb(); |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 2022 | |
| 2023 | out: |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 2024 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 2025 | } |
| 2026 | |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2027 | void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv) |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2028 | { |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2029 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2030 | int i; |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2031 | |
Chris Wilson | 3594a3e | 2016-10-24 13:42:16 +0100 | [diff] [blame] | 2032 | /* |
| 2033 | * Only called during RPM suspend. All users of the userfault_list |
| 2034 | * must be holding an RPM wakeref to ensure that this can not |
| 2035 | * run concurrently with themselves (and use the struct_mutex for |
| 2036 | * protection between themselves). |
| 2037 | */ |
| 2038 | |
| 2039 | list_for_each_entry_safe(obj, on, |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2040 | &dev_priv->mm.userfault_list, userfault_link) |
| 2041 | __i915_gem_object_release_mmap(obj); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2042 | |
| 2043 | /* The fence will be lost when the device powers down. If any were |
| 2044 | * in use by hardware (i.e. they are pinned), we should not be powering |
| 2045 | * down! All other fences will be reacquired by the user upon waking. |
| 2046 | */ |
| 2047 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 2048 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 2049 | |
Chris Wilson | e0ec3ec | 2017-02-03 12:57:17 +0000 | [diff] [blame] | 2050 | /* Ideally we want to assert that the fence register is not |
| 2051 | * live at this point (i.e. that no piece of code will be |
| 2052 | * trying to write through fence + GTT, as that both violates |
| 2053 | * our tracking of activity and associated locking/barriers, |
| 2054 | * but also is illegal given that the hw is powered down). |
| 2055 | * |
| 2056 | * Previously we used reg->pin_count as a "liveness" indicator. |
| 2057 | * That is not sufficient, and we need a more fine-grained |
| 2058 | * tool if we want to have a sanity check here. |
| 2059 | */ |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2060 | |
| 2061 | if (!reg->vma) |
| 2062 | continue; |
| 2063 | |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 2064 | GEM_BUG_ON(i915_vma_has_userfault(reg->vma)); |
Chris Wilson | 7c108fd | 2016-10-24 13:42:18 +0100 | [diff] [blame] | 2065 | reg->dirty = true; |
| 2066 | } |
Chris Wilson | eedd10f | 2014-06-16 08:57:44 +0100 | [diff] [blame] | 2067 | } |
| 2068 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2069 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 2070 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2071 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2072 | int err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2073 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2074 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2075 | if (likely(!err)) |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2076 | return 0; |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2077 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2078 | /* Attempt to reap some mmap space from dead objects */ |
| 2079 | do { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 2080 | err = i915_gem_wait_for_idle(dev_priv, |
| 2081 | I915_WAIT_INTERRUPTIBLE, |
| 2082 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2083 | if (err) |
| 2084 | break; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2085 | |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2086 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2087 | err = drm_gem_create_mmap_offset(&obj->base); |
Chris Wilson | b42a13d | 2017-01-06 15:22:40 +0000 | [diff] [blame] | 2088 | if (!err) |
| 2089 | break; |
| 2090 | |
| 2091 | } while (flush_delayed_work(&dev_priv->gt.retire_work)); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 2092 | |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2093 | return err; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 2097 | { |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2098 | drm_gem_free_mmap_offset(&obj->base); |
| 2099 | } |
| 2100 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2101 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2102 | i915_gem_mmap_gtt(struct drm_file *file, |
| 2103 | struct drm_device *dev, |
Jani Nikula | 739f3ab | 2019-01-16 11:15:19 +0200 | [diff] [blame] | 2104 | u32 handle, |
| 2105 | u64 *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2106 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2107 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2108 | int ret; |
| 2109 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 2110 | obj = i915_gem_object_lookup(file, handle); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2111 | if (!obj) |
| 2112 | return -ENOENT; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 2113 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 2114 | ret = i915_gem_object_create_mmap_offset(obj); |
Chris Wilson | f3f6184 | 2016-08-05 10:14:14 +0100 | [diff] [blame] | 2115 | if (ret == 0) |
| 2116 | *offset = drm_vma_node_offset_addr(&obj->base.vma_node); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2117 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 2118 | i915_gem_object_put(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 2119 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2122 | /** |
| 2123 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 2124 | * @dev: DRM device |
| 2125 | * @data: GTT mapping ioctl data |
| 2126 | * @file: GEM object info |
| 2127 | * |
| 2128 | * Simply returns the fake offset to userspace so it can mmap it. |
| 2129 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 2130 | * up so we can get faults in the handler above. |
| 2131 | * |
| 2132 | * The fault handler will take care of binding the object into the GTT |
| 2133 | * (since it may have been evicted to make room for something), allocating |
| 2134 | * a fence register, and mapping the appropriate aperture address into |
| 2135 | * userspace. |
| 2136 | */ |
| 2137 | int |
| 2138 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 2139 | struct drm_file *file) |
| 2140 | { |
| 2141 | struct drm_i915_gem_mmap_gtt *args = data; |
| 2142 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 2143 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 2144 | } |
| 2145 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2146 | /* Immediately discard the backing storage */ |
| 2147 | static void |
| 2148 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2149 | { |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2150 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2151 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 2152 | if (obj->base.filp == NULL) |
| 2153 | return; |
| 2154 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2155 | /* Our goal here is to return as much of the memory as |
| 2156 | * is possible back to the system as we are called from OOM. |
| 2157 | * To do this we must instruct the shmfs to drop all of its |
| 2158 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2159 | */ |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2160 | shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2161 | obj->mm.madv = __I915_MADV_PURGED; |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2162 | obj->mm.pages = ERR_PTR(-EFAULT); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2163 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2164 | |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2165 | /* Try to discard unwanted pages */ |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2166 | void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj) |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 2167 | { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2168 | struct address_space *mapping; |
| 2169 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2170 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2171 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2172 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2173 | switch (obj->mm.madv) { |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2174 | case I915_MADV_DONTNEED: |
| 2175 | i915_gem_object_truncate(obj); |
| 2176 | case __I915_MADV_PURGED: |
| 2177 | return; |
| 2178 | } |
| 2179 | |
| 2180 | if (obj->base.filp == NULL) |
| 2181 | return; |
| 2182 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2183 | mapping = obj->base.filp->f_mapping, |
Chris Wilson | 5537252 | 2014-03-25 13:23:06 +0000 | [diff] [blame] | 2184 | invalidate_mapping_pages(mapping, 0, (loff_t)-1); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2187 | /* |
| 2188 | * Move pages to appropriate lru and release the pagevec, decrementing the |
| 2189 | * ref count of those pages. |
| 2190 | */ |
| 2191 | static void check_release_pagevec(struct pagevec *pvec) |
| 2192 | { |
| 2193 | check_move_unevictable_pages(pvec); |
| 2194 | __pagevec_release(pvec); |
| 2195 | cond_resched(); |
| 2196 | } |
| 2197 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2198 | static void |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2199 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj, |
| 2200 | struct sg_table *pages) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2201 | { |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2202 | struct sgt_iter sgt_iter; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2203 | struct pagevec pvec; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2204 | struct page *page; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 2205 | |
Chris Wilson | e5facdf | 2016-12-23 14:57:57 +0000 | [diff] [blame] | 2206 | __i915_gem_object_release_shmem(obj, pages, true); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2207 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2208 | i915_gem_gtt_finish_pages(obj, pages); |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2209 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 2210 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2211 | i915_gem_object_save_bit_17_swizzle(obj, pages); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2212 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2213 | mapping_clear_unevictable(file_inode(obj->base.filp)->i_mapping); |
| 2214 | |
| 2215 | pagevec_init(&pvec); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2216 | for_each_sgt_page(page, sgt_iter, pages) { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2217 | if (obj->mm.dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2218 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2219 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2220 | if (obj->mm.madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2221 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2222 | |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2223 | if (!pagevec_add(&pvec, page)) |
| 2224 | check_release_pagevec(&pvec); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2225 | } |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2226 | if (pagevec_count(&pvec)) |
| 2227 | check_release_pagevec(&pvec); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2228 | obj->mm.dirty = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2229 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2230 | sg_free_table(pages); |
| 2231 | kfree(pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2234 | static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj) |
| 2235 | { |
| 2236 | struct radix_tree_iter iter; |
Ville Syrjälä | c23aa71 | 2017-09-01 20:12:51 +0300 | [diff] [blame] | 2237 | void __rcu **slot; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2238 | |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2239 | rcu_read_lock(); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2240 | radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0) |
| 2241 | radix_tree_delete(&obj->mm.get_page.radix, iter.index); |
Chris Wilson | bea6e98 | 2017-10-26 14:00:31 +0100 | [diff] [blame] | 2242 | rcu_read_unlock(); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2245 | static struct sg_table * |
| 2246 | __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2247 | { |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2248 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2249 | struct sg_table *pages; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2250 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2251 | pages = fetch_and_zero(&obj->mm.pages); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2252 | if (IS_ERR_OR_NULL(pages)) |
| 2253 | return pages; |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 2254 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2255 | spin_lock(&i915->mm.obj_lock); |
| 2256 | list_del(&obj->mm.link); |
| 2257 | spin_unlock(&i915->mm.obj_lock); |
| 2258 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2259 | if (obj->mm.mapping) { |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2260 | void *ptr; |
| 2261 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2262 | ptr = page_mask_bits(obj->mm.mapping); |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2263 | if (is_vmalloc_addr(ptr)) |
| 2264 | vunmap(ptr); |
Chris Wilson | fb8621d | 2016-04-08 12:11:14 +0100 | [diff] [blame] | 2265 | else |
Chris Wilson | 4b30cb2 | 2016-08-18 17:16:42 +0100 | [diff] [blame] | 2266 | kunmap(kmap_to_page(ptr)); |
| 2267 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2268 | obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2269 | } |
| 2270 | |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2271 | __i915_gem_object_reset_page_iter(obj); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2272 | obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 2273 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2274 | return pages; |
| 2275 | } |
| 2276 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2277 | int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj, |
| 2278 | enum i915_mm_subclass subclass) |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2279 | { |
| 2280 | struct sg_table *pages; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2281 | int ret; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2282 | |
| 2283 | if (i915_gem_object_has_pinned_pages(obj)) |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2284 | return -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2285 | |
| 2286 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2287 | |
| 2288 | /* May be called by shrinker from within get_pages() (on another bo) */ |
| 2289 | mutex_lock_nested(&obj->mm.lock, subclass); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2290 | if (unlikely(atomic_read(&obj->mm.pages_pin_count))) { |
| 2291 | ret = -EBUSY; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2292 | goto unlock; |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2293 | } |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* |
| 2296 | * ->put_pages might need to allocate memory for the bit17 swizzle |
| 2297 | * array, hence protect them from being reaped by removing them from gtt |
| 2298 | * lists early. |
| 2299 | */ |
| 2300 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2301 | |
| 2302 | /* |
| 2303 | * XXX Temporary hijinx to avoid updating all backends to handle |
| 2304 | * NULL pages. In the future, when we have more asynchronous |
| 2305 | * get_pages backends we should be better able to handle the |
| 2306 | * cancellation of the async task in a more uniform manner. |
| 2307 | */ |
| 2308 | if (!pages && !i915_gem_object_needs_async_cancel(obj)) |
| 2309 | pages = ERR_PTR(-EINVAL); |
| 2310 | |
Chris Wilson | 4e5462e | 2017-03-07 13:20:31 +0000 | [diff] [blame] | 2311 | if (!IS_ERR(pages)) |
| 2312 | obj->ops->put_pages(obj, pages); |
| 2313 | |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2314 | ret = 0; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2315 | unlock: |
| 2316 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 484d9a8 | 2019-01-15 12:44:42 +0000 | [diff] [blame] | 2317 | |
| 2318 | return ret; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2319 | } |
| 2320 | |
Tvrtko Ursulin | f8e5786 | 2018-09-26 09:03:53 +0100 | [diff] [blame] | 2321 | bool i915_sg_trim(struct sg_table *orig_st) |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2322 | { |
| 2323 | struct sg_table new_st; |
| 2324 | struct scatterlist *sg, *new_sg; |
| 2325 | unsigned int i; |
| 2326 | |
| 2327 | if (orig_st->nents == orig_st->orig_nents) |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2328 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2329 | |
Chris Wilson | 8bfc478f | 2016-12-23 14:57:58 +0000 | [diff] [blame] | 2330 | 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] | 2331 | return false; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2332 | |
| 2333 | new_sg = new_st.sgl; |
| 2334 | for_each_sg(orig_st->sgl, sg, orig_st->nents, i) { |
| 2335 | sg_set_page(new_sg, sg_page(sg), sg->length, 0); |
Matthew Auld | c6d22ab | 2018-09-20 15:27:06 +0100 | [diff] [blame] | 2336 | sg_dma_address(new_sg) = sg_dma_address(sg); |
| 2337 | sg_dma_len(new_sg) = sg_dma_len(sg); |
| 2338 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2339 | new_sg = sg_next(new_sg); |
| 2340 | } |
Chris Wilson | c2dc6cc | 2016-12-19 12:43:46 +0000 | [diff] [blame] | 2341 | 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] | 2342 | |
| 2343 | sg_free_table(orig_st); |
| 2344 | |
| 2345 | *orig_st = new_st; |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 2346 | return true; |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2349 | 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] | 2350 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 2351 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2352 | const unsigned long page_count = obj->base.size / PAGE_SIZE; |
| 2353 | unsigned long i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2354 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2355 | struct sg_table *st; |
| 2356 | struct scatterlist *sg; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2357 | struct sgt_iter sgt_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2358 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2359 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Tvrtko Ursulin | 5602452 | 2017-08-03 10:14:17 +0100 | [diff] [blame] | 2360 | unsigned int max_segment = i915_sg_segment_size(); |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2361 | unsigned int sg_page_sizes; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2362 | struct pagevec pvec; |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2363 | gfp_t noreclaim; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2364 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2365 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2366 | /* |
| 2367 | * Assert that the object is not currently in any GPU domain. As it |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2368 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2369 | * a GPU cache |
| 2370 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 2371 | GEM_BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2372 | GEM_BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2373 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2374 | /* |
| 2375 | * If there's no chance of allocating enough pages for the whole |
| 2376 | * object, bail early. |
| 2377 | */ |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 2378 | if (page_count > totalram_pages()) |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2379 | return -ENOMEM; |
| 2380 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2381 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 2382 | if (st == NULL) |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2383 | return -ENOMEM; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2384 | |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2385 | rebuild_st: |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2386 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2387 | kfree(st); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2388 | return -ENOMEM; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2389 | } |
| 2390 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2391 | /* |
| 2392 | * Get the list of pages out of our struct file. They'll be pinned |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2393 | * at this point until we release them. |
| 2394 | * |
| 2395 | * Fail silently without starting the shrinker |
| 2396 | */ |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 2397 | mapping = obj->base.filp->f_mapping; |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2398 | mapping_set_unevictable(mapping); |
Chris Wilson | 0f6ab55 | 2017-06-09 12:03:48 +0100 | [diff] [blame] | 2399 | noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2400 | noreclaim |= __GFP_NORETRY | __GFP_NOWARN; |
| 2401 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2402 | sg = st->sgl; |
| 2403 | st->nents = 0; |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2404 | sg_page_sizes = 0; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2405 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2406 | const unsigned int shrink[] = { |
| 2407 | I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE, |
| 2408 | 0, |
| 2409 | }, *s = shrink; |
| 2410 | gfp_t gfp = noreclaim; |
| 2411 | |
| 2412 | do { |
Chris Wilson | e6db7f4 | 2018-11-05 17:06:40 +0000 | [diff] [blame] | 2413 | cond_resched(); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2414 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 2415 | if (!IS_ERR(page)) |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2416 | break; |
| 2417 | |
| 2418 | if (!*s) { |
| 2419 | ret = PTR_ERR(page); |
| 2420 | goto err_sg; |
| 2421 | } |
| 2422 | |
Chris Wilson | 912d572 | 2017-09-06 16:19:30 -0700 | [diff] [blame] | 2423 | i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++); |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2424 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2425 | /* |
| 2426 | * We've tried hard to allocate the memory by reaping |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2427 | * our own buffer, now let the real VM do its job and |
| 2428 | * go down in flames if truly OOM. |
Chris Wilson | 24f8e00 | 2017-03-22 11:05:21 +0000 | [diff] [blame] | 2429 | * |
| 2430 | * However, since graphics tend to be disposable, |
| 2431 | * defer the oom here by reporting the ENOMEM back |
| 2432 | * to userspace. |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2433 | */ |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2434 | if (!*s) { |
| 2435 | /* reclaim and warn, but no oom */ |
| 2436 | gfp = mapping_gfp_mask(mapping); |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2437 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2438 | /* |
| 2439 | * Our bo are always dirty and so we require |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2440 | * kswapd to reclaim our pages (direct reclaim |
| 2441 | * does not effectively begin pageout of our |
| 2442 | * buffers on its own). However, direct reclaim |
| 2443 | * only waits for kswapd when under allocation |
| 2444 | * congestion. So as a result __GFP_RECLAIM is |
| 2445 | * unreliable and fails to actually reclaim our |
| 2446 | * dirty pages -- unless you try over and over |
| 2447 | * again with !__GFP_NORETRY. However, we still |
| 2448 | * want to fail this allocation rather than |
| 2449 | * trigger the out-of-memory killer and for |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2450 | * this we want __GFP_RETRY_MAYFAIL. |
Chris Wilson | eaf4180 | 2017-06-09 12:03:47 +0100 | [diff] [blame] | 2451 | */ |
Michal Hocko | dbb3295 | 2017-07-12 14:36:55 -0700 | [diff] [blame] | 2452 | gfp |= __GFP_RETRY_MAYFAIL; |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2453 | } |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 2454 | } while (1); |
| 2455 | |
Chris Wilson | 871dfbd | 2016-10-11 09:20:21 +0100 | [diff] [blame] | 2456 | if (!i || |
| 2457 | sg->length >= max_segment || |
| 2458 | page_to_pfn(page) != last_pfn + 1) { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2459 | if (i) { |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2460 | sg_page_sizes |= sg->length; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2461 | sg = sg_next(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2462 | } |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2463 | st->nents++; |
| 2464 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 2465 | } else { |
| 2466 | sg->length += PAGE_SIZE; |
| 2467 | } |
| 2468 | last_pfn = page_to_pfn(page); |
Daniel Vetter | 3bbbe70 | 2013-10-07 17:15:45 -0300 | [diff] [blame] | 2469 | |
| 2470 | /* Check that the i965g/gm workaround works. */ |
| 2471 | WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2472 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2473 | if (sg) { /* loop terminated early; short sg table */ |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2474 | sg_page_sizes |= sg->length; |
Konrad Rzeszutek Wilk | 426729d | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 2475 | sg_mark_end(sg); |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2476 | } |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 2477 | |
Tvrtko Ursulin | 0c40ce1 | 2016-11-09 15:13:43 +0000 | [diff] [blame] | 2478 | /* Trim unused sg entries to avoid wasting memory. */ |
| 2479 | i915_sg_trim(st); |
| 2480 | |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2481 | ret = i915_gem_gtt_prepare_pages(obj, st); |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2482 | if (ret) { |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2483 | /* |
| 2484 | * DMA remapping failed? One possible cause is that |
Chris Wilson | d766ef5 | 2016-12-19 12:43:45 +0000 | [diff] [blame] | 2485 | * it could not reserve enough large entries, asking |
| 2486 | * for PAGE_SIZE chunks instead may be helpful. |
| 2487 | */ |
| 2488 | if (max_segment > PAGE_SIZE) { |
| 2489 | for_each_sgt_page(page, sgt_iter, st) |
| 2490 | put_page(page); |
| 2491 | sg_free_table(st); |
| 2492 | |
| 2493 | max_segment = PAGE_SIZE; |
| 2494 | goto rebuild_st; |
| 2495 | } else { |
| 2496 | dev_warn(&dev_priv->drm.pdev->dev, |
| 2497 | "Failed to DMA remap %lu pages\n", |
| 2498 | page_count); |
| 2499 | goto err_pages; |
| 2500 | } |
| 2501 | } |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2502 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2503 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2504 | i915_gem_object_do_bit_17_swizzle(obj, st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2505 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2506 | __i915_gem_object_set_pages(obj, st, sg_page_sizes); |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2507 | |
| 2508 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2509 | |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2510 | err_sg: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 2511 | sg_mark_end(sg); |
Chris Wilson | b17993b | 2016-11-14 11:29:30 +0000 | [diff] [blame] | 2512 | err_pages: |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 2513 | mapping_clear_unevictable(mapping); |
| 2514 | pagevec_init(&pvec); |
| 2515 | for_each_sgt_page(page, sgt_iter, st) { |
| 2516 | if (!pagevec_add(&pvec, page)) |
| 2517 | check_release_pagevec(&pvec); |
| 2518 | } |
| 2519 | if (pagevec_count(&pvec)) |
| 2520 | check_release_pagevec(&pvec); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 2521 | sg_free_table(st); |
| 2522 | kfree(st); |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2523 | |
Chris Wilson | e0ff7a7 | 2018-09-03 09:33:36 +0100 | [diff] [blame] | 2524 | /* |
| 2525 | * shmemfs first checks if there is enough memory to allocate the page |
Chris Wilson | 0820baf | 2014-03-25 13:23:03 +0000 | [diff] [blame] | 2526 | * and reports ENOSPC should there be insufficient, along with the usual |
| 2527 | * ENOMEM for a genuine allocation failure. |
| 2528 | * |
| 2529 | * We use ENOSPC in our driver to mean that we have run out of aperture |
| 2530 | * space and so want to translate the error from shmemfs back to our |
| 2531 | * usual understanding of ENOMEM. |
| 2532 | */ |
Imre Deak | e227330 | 2015-07-09 12:59:05 +0300 | [diff] [blame] | 2533 | if (ret == -ENOSPC) |
| 2534 | ret = -ENOMEM; |
| 2535 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2536 | return ret; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj, |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2540 | struct sg_table *pages, |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2541 | unsigned int sg_page_sizes) |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2542 | { |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2543 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 2544 | unsigned long supported = INTEL_INFO(i915)->page_sizes; |
| 2545 | int i; |
| 2546 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2547 | lockdep_assert_held(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2548 | |
| 2549 | obj->mm.get_page.sg_pos = pages->sgl; |
| 2550 | obj->mm.get_page.sg_idx = 0; |
| 2551 | |
| 2552 | obj->mm.pages = pages; |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2553 | |
| 2554 | if (i915_gem_object_is_tiled(obj) && |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2555 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2556 | GEM_BUG_ON(obj->mm.quirked); |
| 2557 | __i915_gem_object_pin_pages(obj); |
| 2558 | obj->mm.quirked = true; |
| 2559 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2560 | |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2561 | GEM_BUG_ON(!sg_page_sizes); |
| 2562 | obj->mm.page_sizes.phys = sg_page_sizes; |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2563 | |
| 2564 | /* |
Matthew Auld | 84e8978 | 2017-10-09 12:00:24 +0100 | [diff] [blame] | 2565 | * Calculate the supported page-sizes which fit into the given |
| 2566 | * sg_page_sizes. This will give us the page-sizes which we may be able |
| 2567 | * to use opportunistically when later inserting into the GTT. For |
| 2568 | * example if phys=2G, then in theory we should be able to use 1G, 2M, |
| 2569 | * 64K or 4K pages, although in practice this will depend on a number of |
| 2570 | * other factors. |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2571 | */ |
| 2572 | obj->mm.page_sizes.sg = 0; |
| 2573 | for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) { |
| 2574 | if (obj->mm.page_sizes.phys & ~0u << i) |
| 2575 | obj->mm.page_sizes.sg |= BIT(i); |
| 2576 | } |
Matthew Auld | a5c08166 | 2017-10-06 23:18:18 +0100 | [diff] [blame] | 2577 | GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg)); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 2578 | |
| 2579 | spin_lock(&i915->mm.obj_lock); |
| 2580 | list_add(&obj->mm.link, &i915->mm.unbound_list); |
| 2581 | spin_unlock(&i915->mm.obj_lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 2585 | { |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2586 | int err; |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2587 | |
| 2588 | if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) { |
| 2589 | DRM_DEBUG("Attempting to obtain a purgeable object\n"); |
| 2590 | return -EFAULT; |
| 2591 | } |
| 2592 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2593 | err = obj->ops->get_pages(obj); |
Matthew Auld | b65a9b9 | 2017-12-18 10:38:55 +0000 | [diff] [blame] | 2594 | GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj)); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2595 | |
Matthew Auld | b91b09e | 2017-10-06 23:18:17 +0100 | [diff] [blame] | 2596 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2597 | } |
| 2598 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2599 | /* Ensure that the associated pages are gathered from the backing storage |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2600 | * 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] | 2601 | * multiple times before they are released by a single call to |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2602 | * i915_gem_object_unpin_pages() - once the pages are no longer referenced |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2603 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 2604 | * or as the object is itself released. |
| 2605 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2606 | int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2607 | { |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2608 | int err; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 2609 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2610 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 2611 | if (err) |
| 2612 | return err; |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 2613 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2614 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2615 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2616 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2617 | err = ____i915_gem_object_get_pages(obj); |
| 2618 | if (err) |
| 2619 | goto unlock; |
| 2620 | |
| 2621 | smp_mb__before_atomic(); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2622 | } |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2623 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2624 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2625 | unlock: |
| 2626 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | 03ac84f | 2016-10-28 13:58:36 +0100 | [diff] [blame] | 2627 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2628 | } |
| 2629 | |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2630 | /* The 'mapping' part of i915_gem_object_pin_map() below */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2631 | static void *i915_gem_object_map(const struct drm_i915_gem_object *obj, |
| 2632 | enum i915_map_type type) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2633 | { |
| 2634 | unsigned long n_pages = obj->base.size >> PAGE_SHIFT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2635 | struct sg_table *sgt = obj->mm.pages; |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2636 | struct sgt_iter sgt_iter; |
| 2637 | struct page *page; |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2638 | struct page *stack_pages[32]; |
| 2639 | struct page **pages = stack_pages; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2640 | unsigned long i = 0; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2641 | pgprot_t pgprot; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2642 | void *addr; |
| 2643 | |
| 2644 | /* A single page can always be kmapped */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2645 | if (n_pages == 1 && type == I915_MAP_WB) |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2646 | return kmap(sg_page(sgt->sgl)); |
| 2647 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2648 | if (n_pages > ARRAY_SIZE(stack_pages)) { |
| 2649 | /* Too big for stack -- allocate temporary array instead */ |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 2650 | pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL); |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2651 | if (!pages) |
| 2652 | return NULL; |
| 2653 | } |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2654 | |
Dave Gordon | 85d1225 | 2016-05-20 11:54:06 +0100 | [diff] [blame] | 2655 | for_each_sgt_page(page, sgt_iter, sgt) |
| 2656 | pages[i++] = page; |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2657 | |
| 2658 | /* Check that we have the expected number of pages */ |
| 2659 | GEM_BUG_ON(i != n_pages); |
| 2660 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2661 | switch (type) { |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2662 | default: |
| 2663 | MISSING_CASE(type); |
| 2664 | /* fallthrough to use PAGE_KERNEL anyway */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2665 | case I915_MAP_WB: |
| 2666 | pgprot = PAGE_KERNEL; |
| 2667 | break; |
| 2668 | case I915_MAP_WC: |
| 2669 | pgprot = pgprot_writecombine(PAGE_KERNEL_IO); |
| 2670 | break; |
| 2671 | } |
| 2672 | addr = vmap(pages, n_pages, 0, pgprot); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2673 | |
Dave Gordon | b338fa4 | 2016-05-20 11:54:05 +0100 | [diff] [blame] | 2674 | if (pages != stack_pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 2675 | kvfree(pages); |
Dave Gordon | dd6034c | 2016-05-20 11:54:04 +0100 | [diff] [blame] | 2676 | |
| 2677 | return addr; |
| 2678 | } |
| 2679 | |
| 2680 | /* get, pin, and map the pages of the object into kernel space */ |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2681 | void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj, |
| 2682 | enum i915_map_type type) |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2683 | { |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2684 | enum i915_map_type has_type; |
| 2685 | bool pinned; |
| 2686 | void *ptr; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2687 | int ret; |
| 2688 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 2689 | if (unlikely(!i915_gem_object_has_struct_page(obj))) |
| 2690 | return ERR_PTR(-ENXIO); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2691 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2692 | ret = mutex_lock_interruptible(&obj->mm.lock); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2693 | if (ret) |
| 2694 | return ERR_PTR(ret); |
| 2695 | |
Chris Wilson | a575c67 | 2017-08-28 11:46:31 +0100 | [diff] [blame] | 2696 | pinned = !(type & I915_MAP_OVERRIDE); |
| 2697 | type &= ~I915_MAP_OVERRIDE; |
| 2698 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2699 | if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) { |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2700 | if (unlikely(!i915_gem_object_has_pages(obj))) { |
Chris Wilson | 88c880b | 2017-09-06 14:52:20 +0100 | [diff] [blame] | 2701 | GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj)); |
| 2702 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2703 | ret = ____i915_gem_object_get_pages(obj); |
| 2704 | if (ret) |
| 2705 | goto err_unlock; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2706 | |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 2707 | smp_mb__before_atomic(); |
| 2708 | } |
| 2709 | atomic_inc(&obj->mm.pages_pin_count); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2710 | pinned = false; |
| 2711 | } |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2712 | GEM_BUG_ON(!i915_gem_object_has_pages(obj)); |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2713 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2714 | ptr = page_unpack_bits(obj->mm.mapping, &has_type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2715 | if (ptr && has_type != type) { |
| 2716 | if (pinned) { |
| 2717 | ret = -EBUSY; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2718 | goto err_unpin; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2719 | } |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2720 | |
| 2721 | if (is_vmalloc_addr(ptr)) |
| 2722 | vunmap(ptr); |
| 2723 | else |
| 2724 | kunmap(kmap_to_page(ptr)); |
| 2725 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 2726 | ptr = obj->mm.mapping = NULL; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2727 | } |
| 2728 | |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2729 | if (!ptr) { |
| 2730 | ptr = i915_gem_object_map(obj, type); |
| 2731 | if (!ptr) { |
| 2732 | ret = -ENOMEM; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2733 | goto err_unpin; |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2734 | } |
| 2735 | |
Chris Wilson | 0ce8178 | 2017-05-17 13:09:59 +0100 | [diff] [blame] | 2736 | obj->mm.mapping = page_pack_bits(ptr, type); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2737 | } |
| 2738 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2739 | out_unlock: |
| 2740 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | d31d7cb | 2016-08-12 12:39:58 +0100 | [diff] [blame] | 2741 | return ptr; |
| 2742 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 2743 | err_unpin: |
| 2744 | atomic_dec(&obj->mm.pages_pin_count); |
| 2745 | err_unlock: |
| 2746 | ptr = ERR_PTR(ret); |
| 2747 | goto out_unlock; |
Chris Wilson | 0a798eb | 2016-04-08 12:11:11 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2750 | static int |
| 2751 | i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj, |
| 2752 | const struct drm_i915_gem_pwrite *arg) |
| 2753 | { |
| 2754 | struct address_space *mapping = obj->base.filp->f_mapping; |
| 2755 | char __user *user_data = u64_to_user_ptr(arg->data_ptr); |
| 2756 | u64 remain, offset; |
| 2757 | unsigned int pg; |
| 2758 | |
| 2759 | /* Before we instantiate/pin the backing store for our use, we |
| 2760 | * can prepopulate the shmemfs filp efficiently using a write into |
| 2761 | * the pagecache. We avoid the penalty of instantiating all the |
| 2762 | * pages, important if the user is just writing to a few and never |
| 2763 | * uses the object on the GPU, and using a direct write into shmemfs |
| 2764 | * allows it to avoid the cost of retrieving a page (either swapin |
| 2765 | * or clearing-before-use) before it is overwritten. |
| 2766 | */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 2767 | if (i915_gem_object_has_pages(obj)) |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2768 | return -ENODEV; |
| 2769 | |
Chris Wilson | a6d65e4 | 2017-10-16 21:27:32 +0100 | [diff] [blame] | 2770 | if (obj->mm.madv != I915_MADV_WILLNEED) |
| 2771 | return -EFAULT; |
| 2772 | |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 2773 | /* Before the pages are instantiated the object is treated as being |
| 2774 | * in the CPU domain. The pages will be clflushed as required before |
| 2775 | * use, and we can freely write into the pages directly. If userspace |
| 2776 | * races pwrite with any other operation; corruption will ensue - |
| 2777 | * that is userspace's prerogative! |
| 2778 | */ |
| 2779 | |
| 2780 | remain = arg->size; |
| 2781 | offset = arg->offset; |
| 2782 | pg = offset_in_page(offset); |
| 2783 | |
| 2784 | do { |
| 2785 | unsigned int len, unwritten; |
| 2786 | struct page *page; |
| 2787 | void *data, *vaddr; |
| 2788 | int err; |
| 2789 | |
| 2790 | len = PAGE_SIZE - pg; |
| 2791 | if (len > remain) |
| 2792 | len = remain; |
| 2793 | |
| 2794 | err = pagecache_write_begin(obj->base.filp, mapping, |
| 2795 | offset, len, 0, |
| 2796 | &page, &data); |
| 2797 | if (err < 0) |
| 2798 | return err; |
| 2799 | |
| 2800 | vaddr = kmap(page); |
| 2801 | unwritten = copy_from_user(vaddr + pg, user_data, len); |
| 2802 | kunmap(page); |
| 2803 | |
| 2804 | err = pagecache_write_end(obj->base.filp, mapping, |
| 2805 | offset, len, len - unwritten, |
| 2806 | page, data); |
| 2807 | if (err < 0) |
| 2808 | return err; |
| 2809 | |
| 2810 | if (unwritten) |
| 2811 | return -EFAULT; |
| 2812 | |
| 2813 | remain -= len; |
| 2814 | user_data += len; |
| 2815 | offset += len; |
| 2816 | pg = 0; |
| 2817 | } while (remain); |
| 2818 | |
| 2819 | return 0; |
| 2820 | } |
| 2821 | |
Chris Wilson | 8547444 | 2019-01-29 18:54:50 +0000 | [diff] [blame] | 2822 | static bool match_ring(struct i915_request *rq) |
| 2823 | { |
| 2824 | struct drm_i915_private *dev_priv = rq->i915; |
| 2825 | u32 ring = I915_READ(RING_START(rq->engine->mmio_base)); |
| 2826 | |
| 2827 | return ring == i915_ggtt_offset(rq->ring->vma); |
| 2828 | } |
| 2829 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2830 | struct i915_request * |
Tvrtko Ursulin | 0bc40be | 2016-03-16 11:00:37 +0000 | [diff] [blame] | 2831 | i915_gem_find_active_request(struct intel_engine_cs *engine) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2832 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2833 | struct i915_request *request, *active = NULL; |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2834 | unsigned long flags; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2835 | |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2836 | /* |
| 2837 | * We are called by the error capture, reset and to dump engine |
| 2838 | * state at random points in time. In particular, note that neither is |
| 2839 | * crucially ordered with an interrupt. After a hang, the GPU is dead |
| 2840 | * and we assume that no more writes can happen (we waited long enough |
| 2841 | * for all writes that were in transaction to be flushed) - adding an |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2842 | * extra delay for a recent interrupt is pointless. Hence, we do |
| 2843 | * not need an engine->irq_seqno_barrier() before the seqno reads. |
Chris Wilson | cc7cc53 | 2018-05-29 14:29:18 +0100 | [diff] [blame] | 2844 | * At all other times, we must assume the GPU is still running, but |
| 2845 | * we only care about the snapshot of this moment. |
Chris Wilson | f69a02c | 2016-07-01 17:23:16 +0100 | [diff] [blame] | 2846 | */ |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2847 | spin_lock_irqsave(&engine->timeline.lock, flags); |
| 2848 | list_for_each_entry(request, &engine->timeline.requests, link) { |
Chris Wilson | 5013eb8 | 2019-01-28 18:18:11 +0000 | [diff] [blame] | 2849 | if (i915_request_completed(request)) |
Chris Wilson | 4db080f | 2013-12-04 11:37:09 +0000 | [diff] [blame] | 2850 | continue; |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2851 | |
Chris Wilson | 8547444 | 2019-01-29 18:54:50 +0000 | [diff] [blame] | 2852 | if (!i915_request_started(request)) |
| 2853 | break; |
| 2854 | |
| 2855 | /* More than one preemptible request may match! */ |
| 2856 | if (!match_ring(request)) |
| 2857 | break; |
| 2858 | |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2859 | active = request; |
| 2860 | break; |
| 2861 | } |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 2862 | spin_unlock_irqrestore(&engine->timeline.lock, flags); |
Chris Wilson | 754c9fd | 2017-02-23 07:44:14 +0000 | [diff] [blame] | 2863 | |
| 2864 | return active; |
Mika Kuoppala | b6b0fac | 2014-01-30 19:04:43 +0200 | [diff] [blame] | 2865 | } |
| 2866 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2867 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2868 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2869 | { |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2870 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2871 | container_of(work, typeof(*dev_priv), gt.retire_work.work); |
Chris Wilson | 91c8a32 | 2016-07-05 10:40:23 +0100 | [diff] [blame] | 2872 | struct drm_device *dev = &dev_priv->drm; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2873 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2874 | /* Come back later if the device is busy... */ |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2875 | if (mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 2876 | i915_retire_requests(dev_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2877 | mutex_unlock(&dev->struct_mutex); |
| 2878 | } |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2879 | |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2880 | /* |
| 2881 | * Keep the retire handler running until we are finally idle. |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2882 | * We do not need to do this test under locking as in the worst-case |
| 2883 | * we queue the retire worker once too often. |
| 2884 | */ |
Chris Wilson | 8892304 | 2018-01-29 14:41:04 +0000 | [diff] [blame] | 2885 | if (READ_ONCE(dev_priv->gt.awake)) |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2886 | queue_delayed_work(dev_priv->wq, |
| 2887 | &dev_priv->gt.retire_work, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2888 | round_jiffies_up_relative(HZ)); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2889 | } |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2890 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2891 | static void shrink_caches(struct drm_i915_private *i915) |
| 2892 | { |
| 2893 | /* |
| 2894 | * kmem_cache_shrink() discards empty slabs and reorders partially |
| 2895 | * filled slabs to prioritise allocating from the mostly full slabs, |
| 2896 | * with the aim of reducing fragmentation. |
| 2897 | */ |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2898 | kmem_cache_shrink(i915->luts); |
| 2899 | kmem_cache_shrink(i915->vmas); |
| 2900 | kmem_cache_shrink(i915->objects); |
Chris Wilson | 32eb6bc | 2019-02-28 10:20:33 +0000 | [diff] [blame^] | 2901 | |
| 2902 | i915_globals_park(); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | struct sleep_rcu_work { |
| 2906 | union { |
| 2907 | struct rcu_head rcu; |
| 2908 | struct work_struct work; |
| 2909 | }; |
| 2910 | struct drm_i915_private *i915; |
| 2911 | unsigned int epoch; |
| 2912 | }; |
| 2913 | |
| 2914 | static inline bool |
| 2915 | same_epoch(struct drm_i915_private *i915, unsigned int epoch) |
| 2916 | { |
| 2917 | /* |
| 2918 | * There is a small chance that the epoch wrapped since we started |
| 2919 | * sleeping. If we assume that epoch is at least a u32, then it will |
| 2920 | * take at least 2^32 * 100ms for it to wrap, or about 326 years. |
| 2921 | */ |
| 2922 | return epoch == READ_ONCE(i915->gt.epoch); |
| 2923 | } |
| 2924 | |
| 2925 | static void __sleep_work(struct work_struct *work) |
| 2926 | { |
| 2927 | struct sleep_rcu_work *s = container_of(work, typeof(*s), work); |
| 2928 | struct drm_i915_private *i915 = s->i915; |
| 2929 | unsigned int epoch = s->epoch; |
| 2930 | |
| 2931 | kfree(s); |
| 2932 | if (same_epoch(i915, epoch)) |
| 2933 | shrink_caches(i915); |
| 2934 | } |
| 2935 | |
| 2936 | static void __sleep_rcu(struct rcu_head *rcu) |
| 2937 | { |
| 2938 | struct sleep_rcu_work *s = container_of(rcu, typeof(*s), rcu); |
| 2939 | struct drm_i915_private *i915 = s->i915; |
| 2940 | |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 2941 | destroy_rcu_head(&s->rcu); |
| 2942 | |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2943 | if (same_epoch(i915, s->epoch)) { |
| 2944 | INIT_WORK(&s->work, __sleep_work); |
| 2945 | queue_work(i915->wq, &s->work); |
| 2946 | } else { |
| 2947 | kfree(s); |
| 2948 | } |
| 2949 | } |
| 2950 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 2951 | static inline bool |
| 2952 | new_requests_since_last_retire(const struct drm_i915_private *i915) |
| 2953 | { |
| 2954 | return (READ_ONCE(i915->gt.active_requests) || |
| 2955 | work_pending(&i915->gt.idle_work.work)); |
| 2956 | } |
| 2957 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2958 | static void assert_kernel_context_is_current(struct drm_i915_private *i915) |
| 2959 | { |
| 2960 | struct intel_engine_cs *engine; |
| 2961 | enum intel_engine_id id; |
| 2962 | |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 2963 | if (i915_reset_failed(i915)) |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2964 | return; |
| 2965 | |
| 2966 | GEM_BUG_ON(i915->gt.active_requests); |
| 2967 | for_each_engine(engine, i915, id) { |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 2968 | GEM_BUG_ON(__i915_active_request_peek(&engine->timeline.last_request)); |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 2969 | GEM_BUG_ON(engine->last_retired_context != |
| 2970 | to_intel_context(i915->kernel_context, engine)); |
| 2971 | } |
| 2972 | } |
| 2973 | |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 2974 | static void |
| 2975 | i915_gem_idle_work_handler(struct work_struct *work) |
| 2976 | { |
| 2977 | struct drm_i915_private *dev_priv = |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2978 | container_of(work, typeof(*dev_priv), gt.idle_work.work); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 2979 | unsigned int epoch = I915_EPOCH_INVALID; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 2980 | bool rearm_hangcheck; |
| 2981 | |
| 2982 | if (!READ_ONCE(dev_priv->gt.awake)) |
| 2983 | return; |
| 2984 | |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 2985 | if (READ_ONCE(dev_priv->gt.active_requests)) |
| 2986 | return; |
| 2987 | |
| 2988 | /* |
| 2989 | * Flush out the last user context, leaving only the pinned |
| 2990 | * kernel context resident. When we are idling on the kernel_context, |
| 2991 | * no more new requests (with a context switch) are emitted and we |
| 2992 | * can finally rest. A consequence is that the idle work handler is |
| 2993 | * always called at least twice before idling (and if the system is |
| 2994 | * idle that implies a round trip through the retire worker). |
| 2995 | */ |
| 2996 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 2997 | i915_gem_switch_to_kernel_context(dev_priv); |
| 2998 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 2999 | |
| 3000 | GEM_TRACE("active_requests=%d (after switch-to-kernel-context)\n", |
| 3001 | READ_ONCE(dev_priv->gt.active_requests)); |
| 3002 | |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3003 | /* |
| 3004 | * Wait for last execlists context complete, but bail out in case a |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3005 | * new request is submitted. As we don't trust the hardware, we |
| 3006 | * continue on if the wait times out. This is necessary to allow |
| 3007 | * the machine to suspend even if the hardware dies, and we will |
| 3008 | * try to recover in resume (after depriving the hardware of power, |
| 3009 | * it may be in a better mmod). |
Imre Deak | 0cb5670 | 2016-11-07 11:20:04 +0200 | [diff] [blame] | 3010 | */ |
Chris Wilson | ffed7bd | 2018-03-01 10:33:38 +0000 | [diff] [blame] | 3011 | __wait_for(if (new_requests_since_last_retire(dev_priv)) return, |
| 3012 | intel_engines_are_idle(dev_priv), |
| 3013 | I915_IDLE_ENGINES_TIMEOUT * 1000, |
| 3014 | 10, 500); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3015 | |
| 3016 | rearm_hangcheck = |
| 3017 | cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work); |
| 3018 | |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3019 | if (!mutex_trylock(&dev_priv->drm.struct_mutex)) { |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3020 | /* Currently busy, come back later */ |
| 3021 | mod_delayed_work(dev_priv->wq, |
| 3022 | &dev_priv->gt.idle_work, |
| 3023 | msecs_to_jiffies(50)); |
| 3024 | goto out_rearm; |
| 3025 | } |
| 3026 | |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3027 | /* |
| 3028 | * New request retired after this work handler started, extend active |
| 3029 | * period until next instance of the work. |
| 3030 | */ |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3031 | if (new_requests_since_last_retire(dev_priv)) |
Imre Deak | 93c97dc | 2016-11-07 11:20:03 +0200 | [diff] [blame] | 3032 | goto out_unlock; |
| 3033 | |
Chris Wilson | e4d2006 | 2018-04-06 16:51:44 +0100 | [diff] [blame] | 3034 | epoch = __i915_gem_park(dev_priv); |
Chris Wilson | ff320d6 | 2017-10-23 22:32:35 +0100 | [diff] [blame] | 3035 | |
Chris Wilson | 1934f5de | 2018-05-31 23:40:57 +0100 | [diff] [blame] | 3036 | assert_kernel_context_is_current(dev_priv); |
| 3037 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3038 | rearm_hangcheck = false; |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3039 | out_unlock: |
Chris Wilson | 5427f20 | 2017-10-23 22:32:34 +0100 | [diff] [blame] | 3040 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3041 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 3042 | out_rearm: |
| 3043 | if (rearm_hangcheck) { |
| 3044 | GEM_BUG_ON(!dev_priv->gt.awake); |
| 3045 | i915_queue_hangcheck(dev_priv); |
Chris Wilson | 35c9418 | 2015-04-07 16:20:37 +0100 | [diff] [blame] | 3046 | } |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3047 | |
| 3048 | /* |
| 3049 | * When we are idle, it is an opportune time to reap our caches. |
| 3050 | * However, we have many objects that utilise RCU and the ordered |
| 3051 | * i915->wq that this work is executing on. To try and flush any |
| 3052 | * pending frees now we are idle, we first wait for an RCU grace |
| 3053 | * period, and then queue a task (that will run last on the wq) to |
| 3054 | * shrink and re-optimize the caches. |
| 3055 | */ |
| 3056 | if (same_epoch(dev_priv, epoch)) { |
| 3057 | struct sleep_rcu_work *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 3058 | if (s) { |
Chris Wilson | a1db9c5 | 2018-11-08 09:21:01 +0000 | [diff] [blame] | 3059 | init_rcu_head(&s->rcu); |
Chris Wilson | 84a1074 | 2018-01-24 11:36:08 +0000 | [diff] [blame] | 3060 | s->i915 = dev_priv; |
| 3061 | s->epoch = epoch; |
| 3062 | call_rcu(&s->rcu, __sleep_rcu); |
| 3063 | } |
| 3064 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3067 | void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file) |
| 3068 | { |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3069 | struct drm_i915_private *i915 = to_i915(gem->dev); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3070 | struct drm_i915_gem_object *obj = to_intel_bo(gem); |
| 3071 | struct drm_i915_file_private *fpriv = file->driver_priv; |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3072 | struct i915_lut_handle *lut, *ln; |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3073 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3074 | mutex_lock(&i915->drm.struct_mutex); |
| 3075 | |
| 3076 | list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) { |
| 3077 | struct i915_gem_context *ctx = lut->ctx; |
| 3078 | struct i915_vma *vma; |
| 3079 | |
Chris Wilson | 432295d | 2017-08-22 12:05:15 +0100 | [diff] [blame] | 3080 | GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF)); |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3081 | if (ctx->file_priv != fpriv) |
| 3082 | continue; |
| 3083 | |
| 3084 | vma = radix_tree_delete(&ctx->handles_vma, lut->handle); |
Chris Wilson | 3ffff01 | 2017-08-22 12:05:17 +0100 | [diff] [blame] | 3085 | GEM_BUG_ON(vma->obj != obj); |
| 3086 | |
| 3087 | /* We allow the process to have multiple handles to the same |
| 3088 | * vma, in the same fd namespace, by virtue of flink/open. |
| 3089 | */ |
| 3090 | GEM_BUG_ON(!vma->open_count); |
| 3091 | if (!--vma->open_count && !i915_vma_is_ggtt(vma)) |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3092 | i915_vma_close(vma); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3093 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3094 | list_del(&lut->obj_link); |
| 3095 | list_del(&lut->ctx_link); |
Chris Wilson | 4ff4b44 | 2017-06-16 15:05:16 +0100 | [diff] [blame] | 3096 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3097 | kmem_cache_free(i915->luts, lut); |
| 3098 | __i915_gem_object_release_unless_active(obj); |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 3099 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 3100 | |
| 3101 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 3102 | } |
| 3103 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3104 | static unsigned long to_wait_timeout(s64 timeout_ns) |
| 3105 | { |
| 3106 | if (timeout_ns < 0) |
| 3107 | return MAX_SCHEDULE_TIMEOUT; |
| 3108 | |
| 3109 | if (timeout_ns == 0) |
| 3110 | return 0; |
| 3111 | |
| 3112 | return nsecs_to_jiffies_timeout(timeout_ns); |
| 3113 | } |
| 3114 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 3115 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3116 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3117 | * @dev: drm device pointer |
| 3118 | * @data: ioctl data blob |
| 3119 | * @file: drm file pointer |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3120 | * |
| 3121 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 3122 | * the timeout parameter. |
| 3123 | * -ETIME: object is still busy after timeout |
| 3124 | * -ERESTARTSYS: signal interrupted the wait |
| 3125 | * -ENONENT: object doesn't exist |
| 3126 | * Also possible, but rare: |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3127 | * -EAGAIN: incomplete, restart syscall |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3128 | * -ENOMEM: damn |
| 3129 | * -ENODEV: Internal IRQ fail |
| 3130 | * -E?: The add request failed |
| 3131 | * |
| 3132 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 3133 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 3134 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 3135 | * without holding struct_mutex the object may become re-busied before this |
| 3136 | * function completes. A similar but shorter * race condition exists in the busy |
| 3137 | * ioctl |
| 3138 | */ |
| 3139 | int |
| 3140 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 3141 | { |
| 3142 | struct drm_i915_gem_wait *args = data; |
| 3143 | struct drm_i915_gem_object *obj; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3144 | ktime_t start; |
| 3145 | long ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3146 | |
Daniel Vetter | 11b5d51 | 2014-09-29 15:31:26 +0200 | [diff] [blame] | 3147 | if (args->flags != 0) |
| 3148 | return -EINVAL; |
| 3149 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 3150 | obj = i915_gem_object_lookup(file, args->bo_handle); |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3151 | if (!obj) |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3152 | return -ENOENT; |
Chris Wilson | 033d549 | 2016-08-05 10:14:17 +0100 | [diff] [blame] | 3153 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3154 | start = ktime_get(); |
| 3155 | |
| 3156 | ret = i915_gem_object_wait(obj, |
Chris Wilson | e9eaf82 | 2018-10-01 15:47:55 +0100 | [diff] [blame] | 3157 | I915_WAIT_INTERRUPTIBLE | |
| 3158 | I915_WAIT_PRIORITY | |
| 3159 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3160 | to_wait_timeout(args->timeout_ns)); |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3161 | |
| 3162 | if (args->timeout_ns > 0) { |
| 3163 | args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 3164 | if (args->timeout_ns < 0) |
| 3165 | args->timeout_ns = 0; |
Chris Wilson | c1d2061 | 2017-02-16 12:54:41 +0000 | [diff] [blame] | 3166 | |
| 3167 | /* |
| 3168 | * Apparently ktime isn't accurate enough and occasionally has a |
| 3169 | * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch |
| 3170 | * things up to make the test happy. We allow up to 1 jiffy. |
| 3171 | * |
| 3172 | * This is a regression from the timespec->ktime conversion. |
| 3173 | */ |
| 3174 | if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns)) |
| 3175 | args->timeout_ns = 0; |
Chris Wilson | b805014 | 2017-08-11 11:57:31 +0100 | [diff] [blame] | 3176 | |
| 3177 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 3178 | if (ret == -ETIME && args->timeout_ns) |
| 3179 | ret = -EAGAIN; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3180 | } |
| 3181 | |
Chris Wilson | f0cd518 | 2016-10-28 13:58:43 +0100 | [diff] [blame] | 3182 | i915_gem_object_put(obj); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3183 | return ret; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 3184 | } |
| 3185 | |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3186 | static int wait_for_engines(struct drm_i915_private *i915) |
| 3187 | { |
Chris Wilson | ee42c00 | 2017-12-11 19:41:34 +0000 | [diff] [blame] | 3188 | if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) { |
Chris Wilson | 59e4b19 | 2017-12-11 19:41:35 +0000 | [diff] [blame] | 3189 | dev_err(i915->drm.dev, |
| 3190 | "Failed to idle engines, declaring wedged!\n"); |
Chris Wilson | 629820f | 2018-03-09 10:11:14 +0000 | [diff] [blame] | 3191 | GEM_TRACE_DUMP(); |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 3192 | i915_gem_set_wedged(i915); |
| 3193 | return -EIO; |
Chris Wilson | 25112b6 | 2017-03-30 15:50:39 +0100 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | return 0; |
| 3197 | } |
| 3198 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3199 | static long |
| 3200 | wait_for_timelines(struct drm_i915_private *i915, |
| 3201 | unsigned int flags, long timeout) |
| 3202 | { |
| 3203 | struct i915_gt_timelines *gt = &i915->gt.timelines; |
| 3204 | struct i915_timeline *tl; |
| 3205 | |
| 3206 | if (!READ_ONCE(i915->gt.active_requests)) |
| 3207 | return timeout; |
| 3208 | |
| 3209 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3210 | list_for_each_entry(tl, >->active_list, link) { |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3211 | struct i915_request *rq; |
| 3212 | |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 3213 | rq = i915_active_request_get_unlocked(&tl->last_request); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3214 | if (!rq) |
| 3215 | continue; |
| 3216 | |
| 3217 | mutex_unlock(>->mutex); |
| 3218 | |
| 3219 | /* |
| 3220 | * "Race-to-idle". |
| 3221 | * |
| 3222 | * Switching to the kernel context is often used a synchronous |
| 3223 | * step prior to idling, e.g. in suspend for flushing all |
| 3224 | * current operations to memory before sleeping. These we |
| 3225 | * want to complete as quickly as possible to avoid prolonged |
| 3226 | * stalls, so allow the gpu to boost to maximum clocks. |
| 3227 | */ |
| 3228 | if (flags & I915_WAIT_FOR_IDLE_BOOST) |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3229 | gen6_rps_boost(rq); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3230 | |
| 3231 | timeout = i915_request_wait(rq, flags, timeout); |
| 3232 | i915_request_put(rq); |
| 3233 | if (timeout < 0) |
| 3234 | return timeout; |
| 3235 | |
| 3236 | /* restart after reacquiring the lock */ |
| 3237 | mutex_lock(>->mutex); |
Chris Wilson | 9407d3b | 2019-01-28 18:18:12 +0000 | [diff] [blame] | 3238 | tl = list_entry(>->active_list, typeof(*tl), link); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3239 | } |
| 3240 | mutex_unlock(>->mutex); |
| 3241 | |
| 3242 | return timeout; |
| 3243 | } |
| 3244 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3245 | int i915_gem_wait_for_idle(struct drm_i915_private *i915, |
| 3246 | unsigned int flags, long timeout) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 3247 | { |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 3248 | GEM_TRACE("flags=%x (%s), timeout=%ld%s\n", |
| 3249 | flags, flags & I915_WAIT_LOCKED ? "locked" : "unlocked", |
| 3250 | timeout, timeout == MAX_SCHEDULE_TIMEOUT ? " (forever)" : ""); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3251 | |
Chris Wilson | 863e9fd | 2017-05-30 13:13:32 +0100 | [diff] [blame] | 3252 | /* If the device is asleep, we have no requests outstanding */ |
| 3253 | if (!READ_ONCE(i915->gt.awake)) |
| 3254 | return 0; |
| 3255 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 3256 | timeout = wait_for_timelines(i915, flags, timeout); |
| 3257 | if (timeout < 0) |
| 3258 | return timeout; |
| 3259 | |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3260 | if (flags & I915_WAIT_LOCKED) { |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3261 | int err; |
Chris Wilson | 9caa34a | 2016-11-11 14:58:08 +0000 | [diff] [blame] | 3262 | |
| 3263 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 3264 | |
Chris Wilson | c1e63f6 | 2018-08-08 11:50:59 +0100 | [diff] [blame] | 3265 | if (GEM_SHOW_DEBUG() && !timeout) { |
| 3266 | /* Presume that timeout was non-zero to begin with! */ |
| 3267 | dev_warn(&i915->drm.pdev->dev, |
| 3268 | "Missed idle-completion interrupt!\n"); |
| 3269 | GEM_TRACE_DUMP(); |
| 3270 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3271 | |
| 3272 | err = wait_for_engines(i915); |
| 3273 | if (err) |
| 3274 | return err; |
| 3275 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3276 | i915_retire_requests(i915); |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 3277 | GEM_BUG_ON(i915->gt.active_requests); |
Chris Wilson | a89d1f9 | 2018-05-02 17:38:39 +0100 | [diff] [blame] | 3278 | } |
Chris Wilson | a61b47f | 2018-06-27 12:53:34 +0100 | [diff] [blame] | 3279 | |
| 3280 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 3281 | } |
| 3282 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3283 | static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj) |
| 3284 | { |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3285 | /* |
| 3286 | * We manually flush the CPU domain so that we can override and |
| 3287 | * force the flush for the display, and perform it asyncrhonously. |
| 3288 | */ |
| 3289 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
| 3290 | if (obj->cache_dirty) |
| 3291 | i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3292 | obj->write_domain = 0; |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj) |
| 3296 | { |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3297 | if (!READ_ONCE(obj->pin_global)) |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3298 | return; |
| 3299 | |
| 3300 | mutex_lock(&obj->base.dev->struct_mutex); |
| 3301 | __i915_gem_object_flush_for_display(obj); |
| 3302 | mutex_unlock(&obj->base.dev->struct_mutex); |
| 3303 | } |
| 3304 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3305 | /** |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3306 | * Moves a single object to the WC read, and possibly write domain. |
| 3307 | * @obj: object to act on |
| 3308 | * @write: ask for write access or read only |
| 3309 | * |
| 3310 | * This function returns when the move is complete, including waiting on |
| 3311 | * flushes to occur. |
| 3312 | */ |
| 3313 | int |
| 3314 | i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write) |
| 3315 | { |
| 3316 | int ret; |
| 3317 | |
| 3318 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3319 | |
| 3320 | ret = i915_gem_object_wait(obj, |
| 3321 | I915_WAIT_INTERRUPTIBLE | |
| 3322 | I915_WAIT_LOCKED | |
| 3323 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3324 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3325 | if (ret) |
| 3326 | return ret; |
| 3327 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3328 | if (obj->write_domain == I915_GEM_DOMAIN_WC) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3329 | return 0; |
| 3330 | |
| 3331 | /* Flush and acquire obj->pages so that we are coherent through |
| 3332 | * direct access in memory with previous cached writes through |
| 3333 | * shmemfs and that our cache domain tracking remains valid. |
| 3334 | * For example, if the obj->filp was moved to swap without us |
| 3335 | * being notified and releasing the pages, we would mistakenly |
| 3336 | * continue to assume that the obj remained out of the CPU cached |
| 3337 | * domain. |
| 3338 | */ |
| 3339 | ret = i915_gem_object_pin_pages(obj); |
| 3340 | if (ret) |
| 3341 | return ret; |
| 3342 | |
| 3343 | flush_write_domain(obj, ~I915_GEM_DOMAIN_WC); |
| 3344 | |
| 3345 | /* Serialise direct access to this object with the barriers for |
| 3346 | * coherent writes from the GPU, by effectively invalidating the |
| 3347 | * WC domain upon first access. |
| 3348 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3349 | if ((obj->read_domains & I915_GEM_DOMAIN_WC) == 0) |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3350 | mb(); |
| 3351 | |
| 3352 | /* It should now be out of any other write domains, and we can update |
| 3353 | * the domain values for our changes. |
| 3354 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3355 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_WC) != 0); |
| 3356 | obj->read_domains |= I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3357 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3358 | obj->read_domains = I915_GEM_DOMAIN_WC; |
| 3359 | obj->write_domain = I915_GEM_DOMAIN_WC; |
Chris Wilson | e22d8e3 | 2017-04-12 12:01:11 +0100 | [diff] [blame] | 3360 | obj->mm.dirty = true; |
| 3361 | } |
| 3362 | |
| 3363 | i915_gem_object_unpin_pages(obj); |
| 3364 | return 0; |
| 3365 | } |
| 3366 | |
| 3367 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3368 | * Moves a single object to the GTT read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3369 | * @obj: object to act on |
| 3370 | * @write: ask for write access or read only |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3371 | * |
| 3372 | * This function returns when the move is complete, including waiting on |
| 3373 | * flushes to occur. |
| 3374 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3375 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3376 | 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] | 3377 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3378 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3379 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3380 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3381 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3382 | ret = i915_gem_object_wait(obj, |
| 3383 | I915_WAIT_INTERRUPTIBLE | |
| 3384 | I915_WAIT_LOCKED | |
| 3385 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3386 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3387 | if (ret) |
| 3388 | return ret; |
| 3389 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3390 | if (obj->write_domain == I915_GEM_DOMAIN_GTT) |
Chris Wilson | c13d87e | 2016-07-20 09:21:15 +0100 | [diff] [blame] | 3391 | return 0; |
| 3392 | |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3393 | /* Flush and acquire obj->pages so that we are coherent through |
| 3394 | * direct access in memory with previous cached writes through |
| 3395 | * shmemfs and that our cache domain tracking remains valid. |
| 3396 | * For example, if the obj->filp was moved to swap without us |
| 3397 | * being notified and releasing the pages, we would mistakenly |
| 3398 | * continue to assume that the obj remained out of the CPU cached |
| 3399 | * domain. |
| 3400 | */ |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3401 | ret = i915_gem_object_pin_pages(obj); |
Chris Wilson | 43566de | 2015-01-02 16:29:29 +0530 | [diff] [blame] | 3402 | if (ret) |
| 3403 | return ret; |
| 3404 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3405 | flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3406 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3407 | /* Serialise direct access to this object with the barriers for |
| 3408 | * coherent writes from the GPU, by effectively invalidating the |
| 3409 | * GTT domain upon first access. |
| 3410 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3411 | if ((obj->read_domains & I915_GEM_DOMAIN_GTT) == 0) |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3412 | mb(); |
| 3413 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3414 | /* It should now be out of any other write domains, and we can update |
| 3415 | * the domain values for our changes. |
| 3416 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3417 | GEM_BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3418 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3419 | if (write) { |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3420 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
| 3421 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3422 | obj->mm.dirty = true; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3423 | } |
| 3424 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 3425 | i915_gem_object_unpin_pages(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3426 | return 0; |
| 3427 | } |
| 3428 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3429 | /** |
| 3430 | * Changes the cache-level of an object across all VMA. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3431 | * @obj: object to act on |
| 3432 | * @cache_level: new cache level to set for the object |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3433 | * |
| 3434 | * After this function returns, the object will be in the new cache-level |
| 3435 | * across all GTT and the contents of the backing storage will be coherent, |
| 3436 | * with respect to the new cache-level. In order to keep the backing storage |
| 3437 | * coherent for all users, we only allow a single cache level to be set |
| 3438 | * globally on the object and prevent it from being changed whilst the |
| 3439 | * hardware is reading from the object. That is if the object is currently |
| 3440 | * on the scanout it will be set to uncached (or equivalent display |
| 3441 | * cache coherency) and all non-MOCS GPU access will also be uncached so |
| 3442 | * that all direct access to the scanout remains coherent. |
| 3443 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3444 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3445 | enum i915_cache_level cache_level) |
| 3446 | { |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3447 | struct i915_vma *vma; |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3448 | int ret; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3449 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3450 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3451 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3452 | if (obj->cache_level == cache_level) |
Chris Wilson | a6a7cc4 | 2016-11-18 21:17:46 +0000 | [diff] [blame] | 3453 | return 0; |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3454 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3455 | /* Inspect the list of currently bound VMA and unbind any that would |
| 3456 | * be invalid given the new cache-level. This is principally to |
| 3457 | * catch the issue of the CS prefetch crossing page boundaries and |
| 3458 | * reading an invalid PTE on older architectures. |
| 3459 | */ |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3460 | restart: |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3461 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3462 | if (!drm_mm_node_allocated(&vma->node)) |
| 3463 | continue; |
| 3464 | |
Chris Wilson | 20dfbde | 2016-08-04 16:32:30 +0100 | [diff] [blame] | 3465 | if (i915_vma_is_pinned(vma)) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3466 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3467 | return -EBUSY; |
| 3468 | } |
| 3469 | |
Chris Wilson | 010e3e6 | 2017-12-06 12:49:13 +0000 | [diff] [blame] | 3470 | if (!i915_vma_is_closed(vma) && |
| 3471 | i915_gem_valid_gtt_space(vma, cache_level)) |
Chris Wilson | aa653a6 | 2016-08-04 07:52:27 +0100 | [diff] [blame] | 3472 | continue; |
| 3473 | |
| 3474 | ret = i915_vma_unbind(vma); |
| 3475 | if (ret) |
| 3476 | return ret; |
| 3477 | |
| 3478 | /* As unbinding may affect other elements in the |
| 3479 | * obj->vma_list (due to side-effects from retiring |
| 3480 | * an active vma), play safe and restart the iterator. |
| 3481 | */ |
| 3482 | goto restart; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3483 | } |
| 3484 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3485 | /* We can reuse the existing drm_mm nodes but need to change the |
| 3486 | * cache-level on the PTE. We could simply unbind them all and |
| 3487 | * rebind with the correct cache-level on next use. However since |
| 3488 | * we already have a valid slot, dma mapping, pages etc, we may as |
| 3489 | * rewrite the PTE in the belief that doing so tramples upon less |
| 3490 | * state and so involves less work. |
| 3491 | */ |
Chris Wilson | 15717de | 2016-08-04 07:52:26 +0100 | [diff] [blame] | 3492 | if (obj->bind_count) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3493 | /* Before we change the PTE, the GPU must not be accessing it. |
| 3494 | * If we wait upon the object, we know that all the bound |
| 3495 | * VMA are no longer active. |
| 3496 | */ |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3497 | ret = i915_gem_object_wait(obj, |
| 3498 | I915_WAIT_INTERRUPTIBLE | |
| 3499 | I915_WAIT_LOCKED | |
| 3500 | I915_WAIT_ALL, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3501 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3502 | if (ret) |
| 3503 | return ret; |
| 3504 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 3505 | if (!HAS_LLC(to_i915(obj->base.dev)) && |
| 3506 | cache_level != I915_CACHE_NONE) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3507 | /* Access to snoopable pages through the GTT is |
| 3508 | * incoherent and on some machines causes a hard |
| 3509 | * lockup. Relinquish the CPU mmaping to force |
| 3510 | * userspace to refault in the pages and we can |
| 3511 | * then double check if the GTT mapping is still |
| 3512 | * valid for that pointer access. |
| 3513 | */ |
| 3514 | i915_gem_release_mmap(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3515 | |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3516 | /* As we no longer need a fence for GTT access, |
| 3517 | * we can relinquish it now (and so prevent having |
| 3518 | * to steal a fence from someone else on the next |
| 3519 | * fence request). Note GPU activity would have |
| 3520 | * dropped the fence as all snoopable access is |
| 3521 | * supposed to be linear. |
| 3522 | */ |
Chris Wilson | e2189dd | 2017-12-07 21:14:07 +0000 | [diff] [blame] | 3523 | for_each_ggtt_vma(vma, obj) { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 3524 | ret = i915_vma_put_fence(vma); |
| 3525 | if (ret) |
| 3526 | return ret; |
| 3527 | } |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3528 | } else { |
| 3529 | /* We either have incoherent backing store and |
| 3530 | * so no GTT access or the architecture is fully |
| 3531 | * coherent. In such cases, existing GTT mmaps |
| 3532 | * ignore the cache bit in the PTE and we can |
| 3533 | * rewrite it without confusing the GPU or having |
| 3534 | * to force userspace to fault back in its mmaps. |
| 3535 | */ |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3536 | } |
| 3537 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3538 | list_for_each_entry(vma, &obj->vma.list, obj_link) { |
Chris Wilson | ef55f92 | 2015-10-09 14:11:27 +0100 | [diff] [blame] | 3539 | if (!drm_mm_node_allocated(&vma->node)) |
| 3540 | continue; |
| 3541 | |
| 3542 | ret = i915_vma_bind(vma, cache_level, PIN_UPDATE); |
| 3543 | if (ret) |
| 3544 | return ret; |
| 3545 | } |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3546 | } |
| 3547 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 3548 | list_for_each_entry(vma, &obj->vma.list, obj_link) |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3549 | vma->node.color = cache_level; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 3550 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3551 | obj->cache_dirty = true; /* Always invalidate stale cachelines */ |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 3552 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3553 | return 0; |
| 3554 | } |
| 3555 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3556 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3557 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3558 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3559 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3560 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3561 | int err = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3562 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3563 | rcu_read_lock(); |
| 3564 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 3565 | if (!obj) { |
| 3566 | err = -ENOENT; |
| 3567 | goto out; |
| 3568 | } |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3569 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3570 | switch (obj->cache_level) { |
| 3571 | case I915_CACHE_LLC: |
| 3572 | case I915_CACHE_L3_LLC: |
| 3573 | args->caching = I915_CACHING_CACHED; |
| 3574 | break; |
| 3575 | |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3576 | case I915_CACHE_WT: |
| 3577 | args->caching = I915_CACHING_DISPLAY; |
| 3578 | break; |
| 3579 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3580 | default: |
| 3581 | args->caching = I915_CACHING_NONE; |
| 3582 | break; |
| 3583 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3584 | out: |
| 3585 | rcu_read_unlock(); |
| 3586 | return err; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3587 | } |
| 3588 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3589 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3590 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3591 | { |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3592 | struct drm_i915_private *i915 = to_i915(dev); |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3593 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3594 | struct drm_i915_gem_object *obj; |
| 3595 | enum i915_cache_level level; |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3596 | int ret = 0; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3597 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3598 | switch (args->caching) { |
| 3599 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3600 | level = I915_CACHE_NONE; |
| 3601 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3602 | case I915_CACHING_CACHED: |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3603 | /* |
| 3604 | * Due to a HW issue on BXT A stepping, GPU stores via a |
| 3605 | * snooped mapping may leave stale data in a corresponding CPU |
| 3606 | * cacheline, whereas normally such cachelines would get |
| 3607 | * invalidated. |
| 3608 | */ |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3609 | if (!HAS_LLC(i915) && !HAS_SNOOP(i915)) |
Imre Deak | e5756c1 | 2015-08-14 18:43:30 +0300 | [diff] [blame] | 3610 | return -ENODEV; |
| 3611 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3612 | level = I915_CACHE_LLC; |
| 3613 | break; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3614 | case I915_CACHING_DISPLAY: |
Chris Wilson | 9c870d0 | 2016-10-24 13:42:15 +0100 | [diff] [blame] | 3615 | level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE; |
Chris Wilson | 4257d3b | 2013-08-08 14:41:11 +0100 | [diff] [blame] | 3616 | break; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3617 | default: |
| 3618 | return -EINVAL; |
| 3619 | } |
| 3620 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3621 | obj = i915_gem_object_lookup(file, args->handle); |
| 3622 | if (!obj) |
| 3623 | return -ENOENT; |
| 3624 | |
Tina Zhang | a03f395 | 2017-11-14 10:25:13 +0000 | [diff] [blame] | 3625 | /* |
| 3626 | * The caching mode of proxy object is handled by its generator, and |
| 3627 | * not allowed to be changed by userspace. |
| 3628 | */ |
| 3629 | if (i915_gem_object_is_proxy(obj)) { |
| 3630 | ret = -ENXIO; |
| 3631 | goto out; |
| 3632 | } |
| 3633 | |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3634 | if (obj->cache_level == level) |
| 3635 | goto out; |
| 3636 | |
| 3637 | ret = i915_gem_object_wait(obj, |
| 3638 | I915_WAIT_INTERRUPTIBLE, |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3639 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3640 | if (ret) |
| 3641 | goto out; |
| 3642 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3643 | ret = i915_mutex_lock_interruptible(dev); |
| 3644 | if (ret) |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3645 | goto out; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3646 | |
| 3647 | ret = i915_gem_object_set_cache_level(obj, level); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3648 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d65415d | 2017-01-19 08:22:10 +0000 | [diff] [blame] | 3649 | |
| 3650 | out: |
| 3651 | i915_gem_object_put(obj); |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3652 | return ret; |
| 3653 | } |
| 3654 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3655 | /* |
Dhinakaran Pandiyan | 07bcd99 | 2018-03-06 19:34:18 -0800 | [diff] [blame] | 3656 | * Prepare buffer for display plane (scanout, cursors, etc). Can be called from |
| 3657 | * an uninterruptible phase (modesetting) and allows any flushes to be pipelined |
| 3658 | * (for pageflips). We only flush the caches while preparing the buffer for |
| 3659 | * display, the callers are responsible for frontbuffer flush. |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3660 | */ |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3661 | struct i915_vma * |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3662 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3663 | u32 alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3664 | const struct i915_ggtt_view *view, |
| 3665 | unsigned int flags) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3666 | { |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3667 | struct i915_vma *vma; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3668 | int ret; |
| 3669 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3670 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3671 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3672 | /* Mark the global pin early so that we account for the |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3673 | * display coherency whilst setting up the cache domains. |
| 3674 | */ |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3675 | obj->pin_global++; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3676 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3677 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3678 | * a result, we make sure that the pinning that is about to occur is |
| 3679 | * done with uncached PTEs. This is lowest common denominator for all |
| 3680 | * chipsets. |
| 3681 | * |
| 3682 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3683 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3684 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3685 | */ |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 3686 | ret = i915_gem_object_set_cache_level(obj, |
Tvrtko Ursulin | 8652744 | 2016-10-13 11:03:00 +0100 | [diff] [blame] | 3687 | HAS_WT(to_i915(obj->base.dev)) ? |
| 3688 | I915_CACHE_WT : I915_CACHE_NONE); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3689 | if (ret) { |
| 3690 | vma = ERR_PTR(ret); |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3691 | goto err_unpin_global; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3692 | } |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3693 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3694 | /* As the user may map the buffer once pinned in the display plane |
| 3695 | * (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] | 3696 | * always use map_and_fenceable for all scanout buffers. However, |
| 3697 | * it may simply be too big to fit into mappable, in which case |
| 3698 | * put it anyway and hope that userspace can cope (but always first |
| 3699 | * try to preserve the existing ABI). |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3700 | */ |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3701 | vma = ERR_PTR(-ENOSPC); |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3702 | if ((flags & PIN_MAPPABLE) == 0 && |
| 3703 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) |
Chris Wilson | 2efb813 | 2016-08-18 17:17:06 +0100 | [diff] [blame] | 3704 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, |
Chris Wilson | 5935485 | 2018-02-20 13:42:06 +0000 | [diff] [blame] | 3705 | flags | |
| 3706 | PIN_MAPPABLE | |
| 3707 | PIN_NONBLOCK); |
| 3708 | if (IS_ERR(vma)) |
Chris Wilson | 767a222 | 2016-11-07 11:01:28 +0000 | [diff] [blame] | 3709 | vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags); |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3710 | if (IS_ERR(vma)) |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3711 | goto err_unpin_global; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3712 | |
Chris Wilson | d8923dc | 2016-08-18 17:17:07 +0100 | [diff] [blame] | 3713 | vma->display_alignment = max_t(u64, vma->display_alignment, alignment); |
| 3714 | |
Chris Wilson | 5a97bcc | 2017-02-22 11:40:46 +0000 | [diff] [blame] | 3715 | __i915_gem_object_flush_for_display(obj); |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3716 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3717 | /* It should now be out of any other write domains, and we can update |
| 3718 | * the domain values for our changes. |
| 3719 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3720 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3721 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3722 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3723 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3724 | err_unpin_global: |
| 3725 | obj->pin_global--; |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3726 | return vma; |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | void |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3730 | i915_gem_object_unpin_from_display_plane(struct i915_vma *vma) |
Chris Wilson | cc98b41 | 2013-08-09 12:25:09 +0100 | [diff] [blame] | 3731 | { |
Chris Wilson | 49d7391 | 2016-11-29 09:50:08 +0000 | [diff] [blame] | 3732 | lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3733 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3734 | if (WARN_ON(vma->obj->pin_global == 0)) |
Tvrtko Ursulin | 8a0c39b | 2015-04-13 11:50:09 +0100 | [diff] [blame] | 3735 | return; |
| 3736 | |
Chris Wilson | bd3d225 | 2017-10-13 21:26:14 +0100 | [diff] [blame] | 3737 | if (--vma->obj->pin_global == 0) |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame] | 3738 | vma->display_alignment = I915_GTT_MIN_ALIGNMENT; |
Tvrtko Ursulin | e661733 | 2015-03-23 11:10:33 +0000 | [diff] [blame] | 3739 | |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3740 | /* Bump the LRU to try and avoid premature eviction whilst flipping */ |
Chris Wilson | befedbb | 2017-01-19 19:26:55 +0000 | [diff] [blame] | 3741 | i915_gem_object_bump_inactive_ggtt(vma->obj); |
Chris Wilson | 383d582 | 2016-08-18 17:17:08 +0100 | [diff] [blame] | 3742 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3743 | i915_vma_unpin(vma); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3744 | } |
| 3745 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3746 | /** |
| 3747 | * Moves a single object to the CPU read, and possibly write domain. |
Tvrtko Ursulin | 14bb2c1 | 2016-06-03 14:02:17 +0100 | [diff] [blame] | 3748 | * @obj: object to act on |
| 3749 | * @write: requesting write or read-only access |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3750 | * |
| 3751 | * This function returns when the move is complete, including waiting on |
| 3752 | * flushes to occur. |
| 3753 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3754 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3755 | 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] | 3756 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3757 | int ret; |
| 3758 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3759 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3760 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3761 | ret = i915_gem_object_wait(obj, |
| 3762 | I915_WAIT_INTERRUPTIBLE | |
| 3763 | I915_WAIT_LOCKED | |
| 3764 | (write ? I915_WAIT_ALL : 0), |
Chris Wilson | 62eb3c2 | 2019-02-13 09:25:04 +0000 | [diff] [blame] | 3765 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3766 | if (ret) |
| 3767 | return ret; |
| 3768 | |
Chris Wilson | ef74921 | 2017-04-12 12:01:10 +0100 | [diff] [blame] | 3769 | flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3770 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3771 | /* Flush the CPU cache if it's still invalid. */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3772 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 3773 | i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC); |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3774 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | /* It should now be out of any other write domains, and we can update |
| 3778 | * the domain values for our changes. |
| 3779 | */ |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 3780 | GEM_BUG_ON(obj->write_domain & ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3781 | |
| 3782 | /* If we're writing through the CPU, then the GPU read domains will |
| 3783 | * need to be invalidated at next use. |
| 3784 | */ |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 3785 | if (write) |
| 3786 | __start_cpu_write(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3787 | |
| 3788 | return 0; |
| 3789 | } |
| 3790 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3791 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3792 | * emitted over 20 msec ago. |
| 3793 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3794 | * Note that if we were to use the current jiffies each time around the loop, |
| 3795 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3796 | * render a frame was over 20ms. |
| 3797 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3798 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3799 | * relatively low latency when blocking on a particular request to finish. |
| 3800 | */ |
| 3801 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3802 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3803 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 3804 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3805 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | d0bc54f | 2015-05-21 21:01:48 +0100 | [diff] [blame] | 3806 | unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3807 | struct i915_request *request, *target = NULL; |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3808 | long ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3809 | |
Chris Wilson | f4457ae | 2016-04-13 17:35:08 +0100 | [diff] [blame] | 3810 | /* ABI: return -EIO if already wedged */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 3811 | ret = i915_terminally_wedged(dev_priv); |
| 3812 | if (ret) |
| 3813 | return ret; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3814 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3815 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3816 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3817 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3818 | break; |
| 3819 | |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 3820 | if (target) { |
| 3821 | list_del(&target->client_link); |
| 3822 | target->file_priv = NULL; |
| 3823 | } |
John Harrison | fcfa423c | 2015-05-29 17:44:12 +0100 | [diff] [blame] | 3824 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3825 | target = request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3826 | } |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3827 | if (target) |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3828 | i915_request_get(target); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3829 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3830 | |
John Harrison | 54fb241 | 2014-11-24 18:49:27 +0000 | [diff] [blame] | 3831 | if (target == NULL) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3832 | return 0; |
| 3833 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3834 | ret = i915_request_wait(target, |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3835 | I915_WAIT_INTERRUPTIBLE, |
| 3836 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3837 | i915_request_put(target); |
John Harrison | ff86588 | 2014-11-24 18:49:28 +0000 | [diff] [blame] | 3838 | |
Chris Wilson | e95433c | 2016-10-28 13:58:27 +0100 | [diff] [blame] | 3839 | return ret < 0 ? ret : 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3840 | } |
| 3841 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3842 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3843 | i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj, |
| 3844 | const struct i915_ggtt_view *view, |
Chris Wilson | 91b2db6 | 2016-08-04 16:32:23 +0100 | [diff] [blame] | 3845 | u64 size, |
Chris Wilson | 2ffffd0 | 2016-08-04 16:32:22 +0100 | [diff] [blame] | 3846 | u64 alignment, |
| 3847 | u64 flags) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3848 | { |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3849 | struct drm_i915_private *dev_priv = to_i915(obj->base.dev); |
Chris Wilson | 82ad644 | 2018-06-05 16:37:58 +0100 | [diff] [blame] | 3850 | struct i915_address_space *vm = &dev_priv->ggtt.vm; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3851 | struct i915_vma *vma; |
| 3852 | int ret; |
Joonas Lahtinen | 72e96d6 | 2016-03-30 16:57:10 +0300 | [diff] [blame] | 3853 | |
Chris Wilson | 4c7d62c | 2016-10-28 13:58:32 +0100 | [diff] [blame] | 3854 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 3855 | |
Chris Wilson | ac87a6fd | 2018-02-20 13:42:05 +0000 | [diff] [blame] | 3856 | if (flags & PIN_MAPPABLE && |
| 3857 | (!view || view->type == I915_GGTT_VIEW_NORMAL)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3858 | /* If the required space is larger than the available |
| 3859 | * aperture, we will not able to find a slot for the |
| 3860 | * object and unbinding the object now will be in |
| 3861 | * vain. Worse, doing so may cause us to ping-pong |
| 3862 | * the object in and out of the Global GTT and |
| 3863 | * waste a lot of cycles under the mutex. |
| 3864 | */ |
| 3865 | if (obj->base.size > dev_priv->ggtt.mappable_end) |
| 3866 | return ERR_PTR(-E2BIG); |
| 3867 | |
| 3868 | /* If NONBLOCK is set the caller is optimistically |
| 3869 | * trying to cache the full object within the mappable |
| 3870 | * aperture, and *must* have a fallback in place for |
| 3871 | * situations where we cannot bind the object. We |
| 3872 | * can be a little more lax here and use the fallback |
| 3873 | * more often to avoid costly migrations of ourselves |
| 3874 | * and other objects within the aperture. |
| 3875 | * |
| 3876 | * Half-the-aperture is used as a simple heuristic. |
| 3877 | * More interesting would to do search for a free |
| 3878 | * block prior to making the commitment to unbind. |
| 3879 | * That caters for the self-harm case, and with a |
| 3880 | * little more heuristics (e.g. NOFAULT, NOEVICT) |
| 3881 | * we could try to minimise harm to others. |
| 3882 | */ |
| 3883 | if (flags & PIN_NONBLOCK && |
| 3884 | obj->base.size > dev_priv->ggtt.mappable_end / 2) |
| 3885 | return ERR_PTR(-ENOSPC); |
| 3886 | } |
| 3887 | |
Chris Wilson | 718659a | 2017-01-16 15:21:28 +0000 | [diff] [blame] | 3888 | vma = i915_vma_instance(obj, vm, view); |
Chengguang Xu | 772b540 | 2019-02-21 10:08:19 +0800 | [diff] [blame] | 3889 | if (IS_ERR(vma)) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3890 | return vma; |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3891 | |
| 3892 | if (i915_vma_misplaced(vma, size, alignment, flags)) { |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3893 | if (flags & PIN_NONBLOCK) { |
| 3894 | if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) |
| 3895 | return ERR_PTR(-ENOSPC); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3896 | |
Chris Wilson | 43ae70d9 | 2017-10-09 09:44:01 +0100 | [diff] [blame] | 3897 | if (flags & PIN_MAPPABLE && |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 3898 | vma->fence_size > dev_priv->ggtt.mappable_end / 2) |
Chris Wilson | ad16d2e | 2016-10-13 09:55:04 +0100 | [diff] [blame] | 3899 | return ERR_PTR(-ENOSPC); |
| 3900 | } |
| 3901 | |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3902 | WARN(i915_vma_is_pinned(vma), |
| 3903 | "bo is already pinned in ggtt with incorrect alignment:" |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3904 | " offset=%08x, req.alignment=%llx," |
| 3905 | " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n", |
| 3906 | i915_ggtt_offset(vma), alignment, |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3907 | !!(flags & PIN_MAPPABLE), |
Chris Wilson | 05a20d0 | 2016-08-18 17:16:55 +0100 | [diff] [blame] | 3908 | i915_vma_is_map_and_fenceable(vma)); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3909 | ret = i915_vma_unbind(vma); |
| 3910 | if (ret) |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3911 | return ERR_PTR(ret); |
Chris Wilson | 59bfa12 | 2016-08-04 16:32:31 +0100 | [diff] [blame] | 3912 | } |
| 3913 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3914 | ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL); |
| 3915 | if (ret) |
| 3916 | return ERR_PTR(ret); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 3917 | |
Chris Wilson | 058d88c | 2016-08-15 10:49:06 +0100 | [diff] [blame] | 3918 | return vma; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3919 | } |
| 3920 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3921 | static __always_inline unsigned int __busy_read_flag(unsigned int id) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3922 | { |
| 3923 | /* Note that we could alias engines in the execbuf API, but |
| 3924 | * that would be very unwise as it prevents userspace from |
| 3925 | * fine control over engine selection. Ahem. |
| 3926 | * |
| 3927 | * This should be something like EXEC_MAX_ENGINE instead of |
| 3928 | * I915_NUM_ENGINES. |
| 3929 | */ |
| 3930 | BUILD_BUG_ON(I915_NUM_ENGINES > 16); |
| 3931 | return 0x10000 << id; |
| 3932 | } |
| 3933 | |
| 3934 | static __always_inline unsigned int __busy_write_id(unsigned int id) |
| 3935 | { |
Chris Wilson | 70cb472 | 2016-08-09 18:08:25 +0100 | [diff] [blame] | 3936 | /* The uABI guarantees an active writer is also amongst the read |
| 3937 | * engines. This would be true if we accessed the activity tracking |
| 3938 | * under the lock, but as we perform the lookup of the object and |
| 3939 | * its activity locklessly we can not guarantee that the last_write |
| 3940 | * being active implies that we have set the same engine flag from |
| 3941 | * last_read - hence we always set both read and write busy for |
| 3942 | * last_write. |
| 3943 | */ |
| 3944 | return id | __busy_read_flag(id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3945 | } |
| 3946 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3947 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3948 | __busy_set_if_active(const struct dma_fence *fence, |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3949 | unsigned int (*flag)(unsigned int id)) |
| 3950 | { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3951 | struct i915_request *rq; |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3952 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3953 | /* We have to check the current hw status of the fence as the uABI |
| 3954 | * guarantees forward progress. We could rely on the idle worker |
| 3955 | * to eventually flush us, but to minimise latency just ask the |
| 3956 | * hardware. |
| 3957 | * |
| 3958 | * Note we only report on the status of native fences. |
| 3959 | */ |
| 3960 | if (!dma_fence_is_i915(fence)) |
Chris Wilson | 1255501 | 2016-08-16 09:50:40 +0100 | [diff] [blame] | 3961 | return 0; |
| 3962 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3963 | /* opencode to_request() in order to avoid const warnings */ |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 3964 | rq = container_of(fence, struct i915_request, fence); |
| 3965 | if (i915_request_completed(rq)) |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3966 | return 0; |
| 3967 | |
Chris Wilson | 1d39f28 | 2017-04-11 13:43:06 +0100 | [diff] [blame] | 3968 | return flag(rq->engine->uabi_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3969 | } |
| 3970 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3971 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3972 | busy_check_reader(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3973 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3974 | return __busy_set_if_active(fence, __busy_read_flag); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3975 | } |
| 3976 | |
Chris Wilson | edf6b76 | 2016-08-09 09:23:33 +0100 | [diff] [blame] | 3977 | static __always_inline unsigned int |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3978 | busy_check_writer(const struct dma_fence *fence) |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3979 | { |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3980 | if (!fence) |
| 3981 | return 0; |
| 3982 | |
| 3983 | return __busy_set_if_active(fence, __busy_write_id); |
Chris Wilson | 3fdc13c | 2016-08-05 10:14:18 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3986 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3987 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3988 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3989 | { |
| 3990 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3991 | struct drm_i915_gem_object *obj; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3992 | struct reservation_object_list *list; |
| 3993 | unsigned int seq; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3994 | int err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3995 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3996 | err = -ENOENT; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 3997 | rcu_read_lock(); |
| 3998 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 3999 | if (!obj) |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4000 | goto out; |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4001 | |
| 4002 | /* A discrepancy here is that we do not report the status of |
| 4003 | * non-i915 fences, i.e. even though we may report the object as idle, |
| 4004 | * a call to set-domain may still stall waiting for foreign rendering. |
| 4005 | * This also means that wait-ioctl may report an object as busy, |
| 4006 | * where busy-ioctl considers it idle. |
| 4007 | * |
| 4008 | * We trade the ability to warn of foreign fences to report on which |
| 4009 | * i915 engines are active for the object. |
| 4010 | * |
| 4011 | * Alternatively, we can trade that extra information on read/write |
| 4012 | * activity with |
| 4013 | * args->busy = |
| 4014 | * !reservation_object_test_signaled_rcu(obj->resv, true); |
| 4015 | * to report the overall busyness. This is what the wait-ioctl does. |
| 4016 | * |
| 4017 | */ |
| 4018 | retry: |
| 4019 | seq = raw_read_seqcount(&obj->resv->seq); |
| 4020 | |
| 4021 | /* Translate the exclusive fence to the READ *and* WRITE engine */ |
| 4022 | args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl)); |
| 4023 | |
| 4024 | /* Translate shared fences to READ set of engines */ |
| 4025 | list = rcu_dereference(obj->resv->fence); |
| 4026 | if (list) { |
| 4027 | unsigned int shared_count = list->shared_count, i; |
| 4028 | |
| 4029 | for (i = 0; i < shared_count; ++i) { |
| 4030 | struct dma_fence *fence = |
| 4031 | rcu_dereference(list->shared[i]); |
| 4032 | |
| 4033 | args->busy |= busy_check_reader(fence); |
| 4034 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4035 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4036 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4037 | if (args->busy && read_seqcount_retry(&obj->resv->seq, seq)) |
| 4038 | goto retry; |
Chris Wilson | 426960b | 2016-01-15 16:51:46 +0000 | [diff] [blame] | 4039 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4040 | err = 0; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4041 | out: |
| 4042 | rcu_read_unlock(); |
| 4043 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | int |
| 4047 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4048 | struct drm_file *file_priv) |
| 4049 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 4050 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4053 | int |
| 4054 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4055 | struct drm_file *file_priv) |
| 4056 | { |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 4057 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4058 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4059 | struct drm_i915_gem_object *obj; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4060 | int err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4061 | |
| 4062 | switch (args->madv) { |
| 4063 | case I915_MADV_DONTNEED: |
| 4064 | case I915_MADV_WILLNEED: |
| 4065 | break; |
| 4066 | default: |
| 4067 | return -EINVAL; |
| 4068 | } |
| 4069 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 4070 | obj = i915_gem_object_lookup(file_priv, args->handle); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4071 | if (!obj) |
| 4072 | return -ENOENT; |
| 4073 | |
| 4074 | err = mutex_lock_interruptible(&obj->mm.lock); |
| 4075 | if (err) |
| 4076 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4077 | |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4078 | if (i915_gem_object_has_pages(obj) && |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 4079 | i915_gem_object_is_tiled(obj) && |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4080 | dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4081 | if (obj->mm.madv == I915_MADV_WILLNEED) { |
| 4082 | GEM_BUG_ON(!obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4083 | __i915_gem_object_unpin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4084 | obj->mm.quirked = false; |
| 4085 | } |
| 4086 | if (args->madv == I915_MADV_WILLNEED) { |
Chris Wilson | 2c3a3f4 | 2016-11-04 10:30:01 +0000 | [diff] [blame] | 4087 | GEM_BUG_ON(obj->mm.quirked); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4088 | __i915_gem_object_pin_pages(obj); |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4089 | obj->mm.quirked = true; |
| 4090 | } |
Daniel Vetter | 656bfa3 | 2014-11-20 09:26:30 +0100 | [diff] [blame] | 4091 | } |
| 4092 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4093 | if (obj->mm.madv != __I915_MADV_PURGED) |
| 4094 | obj->mm.madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4095 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4096 | /* if the object is no longer attached, discard its backing storage */ |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4097 | if (obj->mm.madv == I915_MADV_DONTNEED && |
| 4098 | !i915_gem_object_has_pages(obj)) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4099 | i915_gem_object_truncate(obj); |
| 4100 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4101 | args->retained = obj->mm.madv != __I915_MADV_PURGED; |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4102 | mutex_unlock(&obj->mm.lock); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4103 | |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4104 | out: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 4105 | i915_gem_object_put(obj); |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4106 | return err; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4107 | } |
| 4108 | |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4109 | static void |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4110 | frontbuffer_retire(struct i915_active_request *active, |
| 4111 | struct i915_request *request) |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4112 | { |
| 4113 | struct drm_i915_gem_object *obj = |
| 4114 | container_of(active, typeof(*obj), frontbuffer_write); |
| 4115 | |
Chris Wilson | d59b21e | 2017-02-22 11:40:49 +0000 | [diff] [blame] | 4116 | intel_fb_obj_flush(obj, ORIGIN_CS); |
Chris Wilson | 5b8c8ae | 2016-11-16 19:07:04 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4119 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 4120 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4121 | { |
Chris Wilson | 1233e2d | 2016-10-28 13:58:37 +0100 | [diff] [blame] | 4122 | mutex_init(&obj->mm.lock); |
| 4123 | |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4124 | spin_lock_init(&obj->vma.lock); |
| 4125 | INIT_LIST_HEAD(&obj->vma.list); |
| 4126 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4127 | INIT_LIST_HEAD(&obj->lut_list); |
Chris Wilson | 8d9d574 | 2015-04-07 16:20:38 +0100 | [diff] [blame] | 4128 | INIT_LIST_HEAD(&obj->batch_pool_link); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4129 | |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4130 | init_rcu_head(&obj->rcu); |
| 4131 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4132 | obj->ops = ops; |
| 4133 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4134 | reservation_object_init(&obj->__builtin_resv); |
| 4135 | obj->resv = &obj->__builtin_resv; |
| 4136 | |
Chris Wilson | 5034924 | 2016-08-18 17:17:04 +0100 | [diff] [blame] | 4137 | obj->frontbuffer_ggtt_origin = ORIGIN_GTT; |
Chris Wilson | 21950ee | 2019-02-05 13:00:05 +0000 | [diff] [blame] | 4138 | i915_active_request_init(&obj->frontbuffer_write, |
| 4139 | NULL, frontbuffer_retire); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4140 | |
| 4141 | obj->mm.madv = I915_MADV_WILLNEED; |
| 4142 | INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN); |
| 4143 | mutex_init(&obj->mm.get_page.lock); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 4144 | |
Dave Gordon | f19ec8c | 2016-07-04 11:34:37 +0100 | [diff] [blame] | 4145 | 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] | 4146 | } |
| 4147 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4148 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
Tvrtko Ursulin | 3599a91 | 2016-11-01 14:44:10 +0000 | [diff] [blame] | 4149 | .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE | |
| 4150 | I915_GEM_OBJECT_IS_SHRINKABLE, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4151 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4152 | .get_pages = i915_gem_object_get_pages_gtt, |
| 4153 | .put_pages = i915_gem_object_put_pages_gtt, |
Chris Wilson | 7c55e2c | 2017-03-07 12:03:38 +0000 | [diff] [blame] | 4154 | |
| 4155 | .pwrite = i915_gem_object_pwrite_gtt, |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4156 | }; |
| 4157 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4158 | static int i915_gem_object_create_shmem(struct drm_device *dev, |
| 4159 | struct drm_gem_object *obj, |
| 4160 | size_t size) |
| 4161 | { |
| 4162 | struct drm_i915_private *i915 = to_i915(dev); |
| 4163 | unsigned long flags = VM_NORESERVE; |
| 4164 | struct file *filp; |
| 4165 | |
| 4166 | drm_gem_private_object_init(dev, obj, size); |
| 4167 | |
| 4168 | if (i915->mm.gemfs) |
| 4169 | filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size, |
| 4170 | flags); |
| 4171 | else |
| 4172 | filp = shmem_file_setup("i915", size, flags); |
| 4173 | |
| 4174 | if (IS_ERR(filp)) |
| 4175 | return PTR_ERR(filp); |
| 4176 | |
| 4177 | obj->filp = filp; |
| 4178 | |
| 4179 | return 0; |
| 4180 | } |
| 4181 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4182 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 4183 | i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4184 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4185 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4186 | struct address_space *mapping; |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4187 | unsigned int cache_level; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 4188 | gfp_t mask; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4189 | int ret; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4190 | |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4191 | /* There is a prevalence of the assumption that we fit the object's |
| 4192 | * page count inside a 32bit _signed_ variable. Let's document this and |
| 4193 | * catch if we ever need to fix it. In the meantime, if you do spot |
| 4194 | * such a local variable, please consider fixing! |
| 4195 | */ |
Tvrtko Ursulin | 7a3ee5d | 2017-03-30 17:31:30 +0100 | [diff] [blame] | 4196 | if (size >> PAGE_SHIFT > INT_MAX) |
Chris Wilson | b4bcbe2 | 2016-10-18 13:02:49 +0100 | [diff] [blame] | 4197 | return ERR_PTR(-E2BIG); |
| 4198 | |
| 4199 | if (overflows_type(size, obj->base.size)) |
| 4200 | return ERR_PTR(-E2BIG); |
| 4201 | |
Tvrtko Ursulin | 187685c | 2016-12-01 14:16:36 +0000 | [diff] [blame] | 4202 | obj = i915_gem_object_alloc(dev_priv); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4203 | if (obj == NULL) |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4204 | return ERR_PTR(-ENOMEM); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4205 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 4206 | ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4207 | if (ret) |
| 4208 | goto fail; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4209 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4210 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
Jani Nikula | c0f8683 | 2016-12-07 12:13:04 +0200 | [diff] [blame] | 4211 | if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) { |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4212 | /* 965gm cannot relocate objects above 4GiB. */ |
| 4213 | mask &= ~__GFP_HIGHMEM; |
| 4214 | mask |= __GFP_DMA32; |
| 4215 | } |
| 4216 | |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 4217 | mapping = obj->base.filp->f_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 4218 | mapping_set_gfp_mask(mapping, mask); |
Chris Wilson | 4846bf0 | 2017-06-09 12:03:46 +0100 | [diff] [blame] | 4219 | GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM)); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4220 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 4221 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4222 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 4223 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 4224 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4225 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4226 | if (HAS_LLC(dev_priv)) |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 4227 | /* 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] | 4228 | * cache) for about a 10% performance improvement |
| 4229 | * compared to uncached. Graphics requests other than |
| 4230 | * display scanout are coherent with the CPU in |
| 4231 | * accessing this cache. This means in this mode we |
| 4232 | * don't need to clflush on the CPU side, and on the |
| 4233 | * GPU side we only need to flush internal caches to |
| 4234 | * get data visible to the CPU. |
| 4235 | * |
| 4236 | * However, we maintain the display planes as UC, and so |
| 4237 | * need to rebind when first used as such. |
| 4238 | */ |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4239 | cache_level = I915_CACHE_LLC; |
| 4240 | else |
| 4241 | cache_level = I915_CACHE_NONE; |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 4242 | |
Chris Wilson | b8f55be | 2017-08-11 12:11:16 +0100 | [diff] [blame] | 4243 | i915_gem_object_set_cache_coherency(obj, cache_level); |
Chris Wilson | e27ab73 | 2017-06-15 13:38:49 +0100 | [diff] [blame] | 4244 | |
Daniel Vetter | d861e33 | 2013-07-24 23:25:03 +0200 | [diff] [blame] | 4245 | trace_i915_gem_object_create(obj); |
| 4246 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4247 | return obj; |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4248 | |
| 4249 | fail: |
| 4250 | i915_gem_object_free(obj); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 4251 | return ERR_PTR(ret); |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4254 | static bool discard_backing_storage(struct drm_i915_gem_object *obj) |
| 4255 | { |
| 4256 | /* If we are the last user of the backing storage (be it shmemfs |
| 4257 | * pages or stolen etc), we know that the pages are going to be |
| 4258 | * immediately released. In this case, we can then skip copying |
| 4259 | * back the contents from the GPU. |
| 4260 | */ |
| 4261 | |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 4262 | if (obj->mm.madv != I915_MADV_WILLNEED) |
Chris Wilson | 340fbd8 | 2014-05-22 09:16:52 +0100 | [diff] [blame] | 4263 | return false; |
| 4264 | |
| 4265 | if (obj->base.filp == NULL) |
| 4266 | return true; |
| 4267 | |
| 4268 | /* At first glance, this looks racy, but then again so would be |
| 4269 | * userspace racing mmap against close. However, the first external |
| 4270 | * reference to the filp can only be obtained through the |
| 4271 | * i915_gem_mmap_ioctl() which safeguards us against the user |
| 4272 | * acquiring such a reference whilst we are in the middle of |
| 4273 | * freeing the object. |
| 4274 | */ |
| 4275 | return atomic_long_read(&obj->base.filp->f_count) == 1; |
| 4276 | } |
| 4277 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4278 | static void __i915_gem_free_objects(struct drm_i915_private *i915, |
| 4279 | struct llist_node *freed) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4280 | { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4281 | struct drm_i915_gem_object *obj, *on; |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4282 | intel_wakeref_t wakeref; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4283 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4284 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4285 | llist_for_each_entry_safe(obj, on, freed, freed) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4286 | struct i915_vma *vma, *vn; |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 4287 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4288 | trace_i915_gem_object_destroy(obj); |
| 4289 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4290 | mutex_lock(&i915->drm.struct_mutex); |
| 4291 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4292 | GEM_BUG_ON(i915_gem_object_is_active(obj)); |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4293 | list_for_each_entry_safe(vma, vn, &obj->vma.list, obj_link) { |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4294 | GEM_BUG_ON(i915_vma_is_active(vma)); |
| 4295 | vma->flags &= ~I915_VMA_PIN_MASK; |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 4296 | i915_vma_destroy(vma); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4297 | } |
Chris Wilson | 528cbd1 | 2019-01-28 10:23:54 +0000 | [diff] [blame] | 4298 | GEM_BUG_ON(!list_empty(&obj->vma.list)); |
| 4299 | GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma.tree)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4300 | |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 4301 | /* This serializes freeing with the shrinker. Since the free |
| 4302 | * is delayed, first by RCU then by the workqueue, we want the |
| 4303 | * shrinker to be able to free pages of unreferenced objects, |
| 4304 | * or else we may oom whilst there are plenty of deferred |
| 4305 | * freed objects. |
| 4306 | */ |
| 4307 | if (i915_gem_object_has_pages(obj)) { |
| 4308 | spin_lock(&i915->mm.obj_lock); |
| 4309 | list_del_init(&obj->mm.link); |
| 4310 | spin_unlock(&i915->mm.obj_lock); |
| 4311 | } |
| 4312 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4313 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4314 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4315 | GEM_BUG_ON(obj->bind_count); |
Chris Wilson | a65adaf | 2017-10-09 09:43:57 +0100 | [diff] [blame] | 4316 | GEM_BUG_ON(obj->userfault_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4317 | GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits)); |
Chris Wilson | 67b4804 | 2017-08-22 12:05:16 +0100 | [diff] [blame] | 4318 | GEM_BUG_ON(!list_empty(&obj->lut_list)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4319 | |
| 4320 | if (obj->ops->release) |
| 4321 | obj->ops->release(obj); |
| 4322 | |
| 4323 | if (WARN_ON(i915_gem_object_has_pinned_pages(obj))) |
| 4324 | atomic_set(&obj->mm.pages_pin_count, 0); |
Chris Wilson | 548625e | 2016-11-01 12:11:34 +0000 | [diff] [blame] | 4325 | __i915_gem_object_put_pages(obj, I915_MM_NORMAL); |
Chris Wilson | f1fa4f4 | 2017-10-13 21:26:13 +0100 | [diff] [blame] | 4326 | GEM_BUG_ON(i915_gem_object_has_pages(obj)); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4327 | |
| 4328 | if (obj->base.import_attach) |
| 4329 | drm_prime_gem_destroy(&obj->base, NULL); |
| 4330 | |
Chris Wilson | d07f0e5 | 2016-10-28 13:58:44 +0100 | [diff] [blame] | 4331 | reservation_object_fini(&obj->__builtin_resv); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4332 | drm_gem_object_release(&obj->base); |
| 4333 | i915_gem_info_remove_obj(i915, obj->base.size); |
| 4334 | |
| 4335 | kfree(obj->bit_17); |
| 4336 | i915_gem_object_free(obj); |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4337 | |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4338 | GEM_BUG_ON(!atomic_read(&i915->mm.free_count)); |
| 4339 | atomic_dec(&i915->mm.free_count); |
| 4340 | |
Chris Wilson | cc731f5 | 2017-10-13 21:26:21 +0100 | [diff] [blame] | 4341 | if (on) |
| 4342 | cond_resched(); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4343 | } |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4344 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | static void i915_gem_flush_free_objects(struct drm_i915_private *i915) |
| 4348 | { |
| 4349 | struct llist_node *freed; |
| 4350 | |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4351 | /* Free the oldest, most stale object to keep the free_list short */ |
| 4352 | freed = NULL; |
| 4353 | if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */ |
| 4354 | /* Only one consumer of llist_del_first() allowed */ |
| 4355 | spin_lock(&i915->mm.free_lock); |
| 4356 | freed = llist_del_first(&i915->mm.free_list); |
| 4357 | spin_unlock(&i915->mm.free_lock); |
| 4358 | } |
| 4359 | if (unlikely(freed)) { |
| 4360 | freed->next = NULL; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4361 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 87701b4 | 2017-10-13 21:26:20 +0100 | [diff] [blame] | 4362 | } |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | static void __i915_gem_free_work(struct work_struct *work) |
| 4366 | { |
| 4367 | struct drm_i915_private *i915 = |
| 4368 | container_of(work, struct drm_i915_private, mm.free_work); |
| 4369 | struct llist_node *freed; |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 4370 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4371 | /* |
| 4372 | * All file-owned VMA should have been released by this point through |
Chris Wilson | b1f788c | 2016-08-04 07:52:45 +0100 | [diff] [blame] | 4373 | * i915_gem_close_object(), or earlier by i915_gem_context_close(). |
| 4374 | * However, the object may also be bound into the global GTT (e.g. |
| 4375 | * older GPUs without per-process support, or for direct access through |
| 4376 | * the GTT either for the user or for scanout). Those VMA still need to |
| 4377 | * unbound now. |
| 4378 | */ |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 4379 | |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4380 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4381 | while ((freed = llist_del_all(&i915->mm.free_list))) { |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4382 | spin_unlock(&i915->mm.free_lock); |
| 4383 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4384 | __i915_gem_free_objects(i915, freed); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4385 | if (need_resched()) |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4386 | return; |
| 4387 | |
| 4388 | spin_lock(&i915->mm.free_lock); |
Chris Wilson | 5ad08be | 2017-04-07 11:25:51 +0100 | [diff] [blame] | 4389 | } |
Chris Wilson | f991c49 | 2017-11-06 11:15:08 +0000 | [diff] [blame] | 4390 | spin_unlock(&i915->mm.free_lock); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | static void __i915_gem_free_object_rcu(struct rcu_head *head) |
| 4394 | { |
| 4395 | struct drm_i915_gem_object *obj = |
| 4396 | container_of(head, typeof(*obj), rcu); |
| 4397 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 4398 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4399 | /* |
Chris Wilson | 8811d61 | 2018-11-09 09:03:11 +0000 | [diff] [blame] | 4400 | * We reuse obj->rcu for the freed list, so we had better not treat |
| 4401 | * it like a rcu_head from this point forwards. And we expect all |
| 4402 | * objects to be freed via this path. |
| 4403 | */ |
| 4404 | destroy_rcu_head(&obj->rcu); |
| 4405 | |
| 4406 | /* |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4407 | * Since we require blocking on struct_mutex to unbind the freed |
| 4408 | * object from the GPU before releasing resources back to the |
| 4409 | * system, we can not do that directly from the RCU callback (which may |
| 4410 | * be a softirq context), but must instead then defer that work onto a |
| 4411 | * kthread. We use the RCU callback rather than move the freed object |
| 4412 | * directly onto the work queue so that we can mix between using the |
| 4413 | * worker and performing frees directly from subsequent allocations for |
| 4414 | * crude but effective memory throttling. |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4415 | */ |
| 4416 | if (llist_add(&obj->freed, &i915->mm.free_list)) |
Chris Wilson | beacbd1 | 2018-01-15 12:28:45 +0000 | [diff] [blame] | 4417 | queue_work(i915->wq, &i915->mm.free_work); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
| 4421 | { |
| 4422 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4423 | |
Chris Wilson | bc0629a | 2016-11-01 10:03:17 +0000 | [diff] [blame] | 4424 | if (obj->mm.quirked) |
| 4425 | __i915_gem_object_unpin_pages(obj); |
| 4426 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4427 | if (discard_backing_storage(obj)) |
| 4428 | obj->mm.madv = I915_MADV_DONTNEED; |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 4429 | |
Chris Wilson | 2ef1e72 | 2018-01-15 20:57:59 +0000 | [diff] [blame] | 4430 | /* |
| 4431 | * Before we free the object, make sure any pure RCU-only |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4432 | * read-side critical sections are complete, e.g. |
| 4433 | * i915_gem_busy_ioctl(). For the corresponding synchronized |
| 4434 | * lookup see i915_gem_object_lookup_rcu(). |
| 4435 | */ |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 4436 | atomic_inc(&to_i915(obj->base.dev)->mm.free_count); |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 4437 | call_rcu(&obj->rcu, __i915_gem_free_object_rcu); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4438 | } |
| 4439 | |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4440 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj) |
| 4441 | { |
| 4442 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 4443 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 4444 | if (!i915_gem_object_has_active_reference(obj) && |
| 4445 | i915_gem_object_is_active(obj)) |
Chris Wilson | f8a7fde | 2016-10-28 13:58:29 +0100 | [diff] [blame] | 4446 | i915_gem_object_set_active_reference(obj); |
| 4447 | else |
| 4448 | i915_gem_object_put(obj); |
| 4449 | } |
| 4450 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4451 | void i915_gem_sanitize(struct drm_i915_private *i915) |
| 4452 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4453 | intel_wakeref_t wakeref; |
| 4454 | |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4455 | GEM_TRACE("\n"); |
| 4456 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4457 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4458 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
| 4459 | |
| 4460 | /* |
| 4461 | * As we have just resumed the machine and woken the device up from |
| 4462 | * deep PCI sleep (presumably D3_cold), assume the HW has been reset |
| 4463 | * back to defaults, recovering from whatever wedged state we left it |
| 4464 | * in and so worth trying to use the device once more. |
| 4465 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4466 | if (i915_terminally_wedged(i915)) |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4467 | i915_gem_unset_wedged(i915); |
Chris Wilson | f36325f | 2017-08-26 12:09:34 +0100 | [diff] [blame] | 4468 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4469 | /* |
| 4470 | * If we inherit context state from the BIOS or earlier occupants |
| 4471 | * of the GPU, the GPU may be in an inconsistent state when we |
| 4472 | * try to take over. The only way to remove the earlier state |
| 4473 | * is by resetting. However, resetting on earlier gen is tricky as |
| 4474 | * it may impact the display and we are uncertain about the stability |
Joonas Lahtinen | ea117b8 | 2017-04-28 10:53:38 +0300 | [diff] [blame] | 4475 | * of the reset, so this could be applied to even earlier gen. |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4476 | */ |
Chris Wilson | 55277e1 | 2019-01-03 11:21:04 +0000 | [diff] [blame] | 4477 | intel_engines_sanitize(i915, false); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4478 | |
| 4479 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4480 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | c3160da | 2018-05-31 09:22:45 +0100 | [diff] [blame] | 4481 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4482 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4483 | i915_gem_contexts_lost(i915); |
| 4484 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4487 | int i915_gem_suspend(struct drm_i915_private *i915) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4488 | { |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4489 | intel_wakeref_t wakeref; |
Chris Wilson | dcff85c | 2016-08-05 10:14:11 +0100 | [diff] [blame] | 4490 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4491 | |
Chris Wilson | 09a4c02 | 2018-05-24 09:11:35 +0100 | [diff] [blame] | 4492 | GEM_TRACE("\n"); |
| 4493 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4494 | wakeref = intel_runtime_pm_get(i915); |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4495 | intel_suspend_gt_powersave(i915); |
Chris Wilson | 54b4f68 | 2016-07-21 21:16:19 +0100 | [diff] [blame] | 4496 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4497 | flush_workqueue(i915->wq); |
| 4498 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4499 | mutex_lock(&i915->drm.struct_mutex); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4500 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4501 | /* |
| 4502 | * We have to flush all the executing contexts to main memory so |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4503 | * that they can saved in the hibernation image. To ensure the last |
| 4504 | * context image is coherent, we have to switch away from it. That |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4505 | * leaves the i915->kernel_context still active when |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4506 | * we actually suspend, and its image in memory may not match the GPU |
| 4507 | * state. Fortunately, the kernel_context is disposable and we do |
| 4508 | * not rely on its state. |
| 4509 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4510 | if (!i915_reset_failed(i915)) { |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4511 | ret = i915_gem_switch_to_kernel_context(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4512 | if (ret) |
| 4513 | goto err_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4514 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4515 | ret = i915_gem_wait_for_idle(i915, |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4516 | I915_WAIT_INTERRUPTIBLE | |
Chris Wilson | 0606035 | 2018-05-31 09:22:44 +0100 | [diff] [blame] | 4517 | I915_WAIT_LOCKED | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4518 | I915_WAIT_FOR_IDLE_BOOST, |
| 4519 | MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4520 | if (ret && ret != -EIO) |
| 4521 | goto err_unlock; |
Chris Wilson | f740334 | 2013-09-13 23:57:04 +0100 | [diff] [blame] | 4522 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4523 | assert_kernel_context_is_current(i915); |
Chris Wilson | ecf73eb | 2017-11-30 10:29:51 +0000 | [diff] [blame] | 4524 | } |
Chris Wilson | 01f8f33 | 2018-07-17 09:41:21 +0100 | [diff] [blame] | 4525 | i915_retire_requests(i915); /* ensure we flush after wedging */ |
| 4526 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4527 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4528 | i915_reset_flush(i915); |
Chris Wilson | 45c5f20 | 2013-10-16 11:50:01 +0100 | [diff] [blame] | 4529 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4530 | drain_delayed_work(&i915->gt.retire_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4531 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4532 | /* |
| 4533 | * As the idle_work is rearming if it detects a race, play safe and |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4534 | * repeat the flush until it is definitely idle. |
| 4535 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4536 | drain_delayed_work(&i915->gt.idle_work); |
Chris Wilson | bdeb978 | 2016-12-23 14:57:56 +0000 | [diff] [blame] | 4537 | |
Chris Wilson | eb8d0f5 | 2019-01-25 13:22:28 +0000 | [diff] [blame] | 4538 | intel_uc_suspend(i915); |
| 4539 | |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4540 | /* |
| 4541 | * Assert that we successfully flushed all the work and |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4542 | * reset the GPU back to its idle, low power state. |
| 4543 | */ |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4544 | WARN_ON(i915->gt.awake); |
| 4545 | if (WARN_ON(!intel_engines_are_idle(i915))) |
| 4546 | i915_gem_set_wedged(i915); /* no hope, discard everything */ |
Chris Wilson | bdcf120 | 2014-11-25 11:56:33 +0000 | [diff] [blame] | 4547 | |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4548 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4549 | return 0; |
| 4550 | |
| 4551 | err_unlock: |
Chris Wilson | bf06112 | 2018-07-09 14:02:04 +0100 | [diff] [blame] | 4552 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 538ef96 | 2019-01-14 14:21:18 +0000 | [diff] [blame] | 4553 | intel_runtime_pm_put(i915, wakeref); |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4554 | return ret; |
| 4555 | } |
| 4556 | |
| 4557 | void i915_gem_suspend_late(struct drm_i915_private *i915) |
| 4558 | { |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4559 | struct drm_i915_gem_object *obj; |
| 4560 | struct list_head *phases[] = { |
| 4561 | &i915->mm.unbound_list, |
| 4562 | &i915->mm.bound_list, |
| 4563 | NULL |
| 4564 | }, **phase; |
| 4565 | |
Imre Deak | 1c777c5 | 2016-10-12 17:46:37 +0300 | [diff] [blame] | 4566 | /* |
| 4567 | * Neither the BIOS, ourselves or any other kernel |
| 4568 | * expects the system to be in execlists mode on startup, |
| 4569 | * so we need to reset the GPU back to legacy mode. And the only |
| 4570 | * known way to disable logical contexts is through a GPU reset. |
| 4571 | * |
| 4572 | * So in order to leave the system in a known default configuration, |
| 4573 | * always reset the GPU upon unload and suspend. Afterwards we then |
| 4574 | * clean up the GEM state tracking, flushing off the requests and |
| 4575 | * leaving the system in a known idle state. |
| 4576 | * |
| 4577 | * Note that is of the upmost importance that the GPU is idle and |
| 4578 | * all stray writes are flushed *before* we dismantle the backing |
| 4579 | * storage for the pinned objects. |
| 4580 | * |
| 4581 | * However, since we are uncertain that resetting the GPU on older |
| 4582 | * machines is a good idea, we don't - just in case it leaves the |
| 4583 | * machine in an unusable condition. |
| 4584 | */ |
Chris Wilson | cad9946 | 2017-08-26 12:09:33 +0100 | [diff] [blame] | 4585 | |
Chris Wilson | 9776f47 | 2018-06-01 15:41:24 +0100 | [diff] [blame] | 4586 | mutex_lock(&i915->drm.struct_mutex); |
| 4587 | for (phase = phases; *phase; phase++) { |
| 4588 | list_for_each_entry(obj, *phase, mm.link) |
| 4589 | WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false)); |
| 4590 | } |
| 4591 | mutex_unlock(&i915->drm.struct_mutex); |
| 4592 | |
Chris Wilson | ec92ad0 | 2018-05-31 09:22:46 +0100 | [diff] [blame] | 4593 | intel_uc_sanitize(i915); |
| 4594 | i915_gem_sanitize(i915); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4595 | } |
| 4596 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4597 | void i915_gem_resume(struct drm_i915_private *i915) |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4598 | { |
Chris Wilson | 4dfacb0 | 2018-05-31 09:22:43 +0100 | [diff] [blame] | 4599 | GEM_TRACE("\n"); |
| 4600 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4601 | WARN_ON(i915->gt.awake); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4602 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4603 | mutex_lock(&i915->drm.struct_mutex); |
| 4604 | intel_uncore_forcewake_get(i915, FORCEWAKE_ALL); |
Imre Deak | 31ab49a | 2016-11-07 11:20:05 +0200 | [diff] [blame] | 4605 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4606 | i915_gem_restore_gtt_mappings(i915); |
| 4607 | i915_gem_restore_fences(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4608 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4609 | /* |
| 4610 | * As we didn't flush the kernel context before suspend, we cannot |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4611 | * guarantee that the context image is complete. So let's just reset |
| 4612 | * it and start again. |
| 4613 | */ |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4614 | i915->gt.resume(i915); |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4615 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4616 | if (i915_gem_init_hw(i915)) |
| 4617 | goto err_wedged; |
| 4618 | |
Michal Wajdeczko | 7cfca4a | 2018-03-02 11:15:49 +0000 | [diff] [blame] | 4619 | intel_uc_resume(i915); |
Chris Wilson | 7469c62 | 2017-11-14 13:03:00 +0000 | [diff] [blame] | 4620 | |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4621 | /* Always reload a context for powersaving. */ |
| 4622 | if (i915_gem_switch_to_kernel_context(i915)) |
| 4623 | goto err_wedged; |
| 4624 | |
| 4625 | out_unlock: |
| 4626 | intel_uncore_forcewake_put(i915, FORCEWAKE_ALL); |
| 4627 | mutex_unlock(&i915->drm.struct_mutex); |
| 4628 | return; |
| 4629 | |
| 4630 | err_wedged: |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4631 | if (!i915_reset_failed(i915)) { |
| 4632 | dev_err(i915->drm.dev, |
| 4633 | "Failed to re-initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 4634 | i915_gem_set_wedged(i915); |
| 4635 | } |
Chris Wilson | 37cd330 | 2017-11-12 11:27:38 +0000 | [diff] [blame] | 4636 | goto out_unlock; |
Chris Wilson | 5ab57c7 | 2016-07-15 14:56:20 +0100 | [diff] [blame] | 4637 | } |
| 4638 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4639 | void i915_gem_init_swizzling(struct drm_i915_private *dev_priv) |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4640 | { |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4641 | if (INTEL_GEN(dev_priv) < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4642 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4643 | return; |
| 4644 | |
| 4645 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4646 | DISP_TILE_SURFACE_SWIZZLING); |
| 4647 | |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4648 | if (IS_GEN(dev_priv, 5)) |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4649 | return; |
| 4650 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4651 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4652 | if (IS_GEN(dev_priv, 6)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4653 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4654 | else if (IS_GEN(dev_priv, 7)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4655 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4656 | else if (IS_GEN(dev_priv, 8)) |
Ben Widawsky | 31a5336 | 2013-11-02 21:07:04 -0700 | [diff] [blame] | 4657 | I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4658 | else |
| 4659 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4660 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4661 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4662 | 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] | 4663 | { |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4664 | I915_WRITE(RING_CTL(base), 0); |
| 4665 | I915_WRITE(RING_HEAD(base), 0); |
| 4666 | I915_WRITE(RING_TAIL(base), 0); |
| 4667 | I915_WRITE(RING_START(base), 0); |
| 4668 | } |
| 4669 | |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4670 | static void init_unused_rings(struct drm_i915_private *dev_priv) |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4671 | { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4672 | if (IS_I830(dev_priv)) { |
| 4673 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4674 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4675 | init_unused_ring(dev_priv, SRB1_BASE); |
| 4676 | init_unused_ring(dev_priv, SRB2_BASE); |
| 4677 | init_unused_ring(dev_priv, SRB3_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4678 | } else if (IS_GEN(dev_priv, 2)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4679 | init_unused_ring(dev_priv, SRB0_BASE); |
| 4680 | init_unused_ring(dev_priv, SRB1_BASE); |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 4681 | } else if (IS_GEN(dev_priv, 3)) { |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4682 | init_unused_ring(dev_priv, PRB1_BASE); |
| 4683 | init_unused_ring(dev_priv, PRB2_BASE); |
Ville Syrjälä | 81e7f20 | 2014-08-15 01:21:55 +0300 | [diff] [blame] | 4684 | } |
| 4685 | } |
| 4686 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4687 | static int __i915_gem_restart_engines(void *data) |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4688 | { |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4689 | struct drm_i915_private *i915 = data; |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 4690 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 4691 | enum intel_engine_id id; |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4692 | int err; |
| 4693 | |
| 4694 | for_each_engine(engine, i915, id) { |
| 4695 | err = engine->init_hw(engine); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4696 | if (err) { |
| 4697 | DRM_ERROR("Failed to restart %s (%d)\n", |
| 4698 | engine->name, err); |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4699 | return err; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4700 | } |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
Chris Wilson | 2d5eaad | 2019-02-26 10:24:00 +0000 | [diff] [blame] | 4703 | intel_engines_set_scheduler_caps(i915); |
| 4704 | |
Chris Wilson | 20a8a74 | 2017-02-08 14:30:31 +0000 | [diff] [blame] | 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | int i915_gem_init_hw(struct drm_i915_private *dev_priv) |
| 4709 | { |
Chris Wilson | d200cda | 2016-04-28 09:56:44 +0100 | [diff] [blame] | 4710 | int ret; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4711 | |
Chris Wilson | de867c2 | 2016-10-25 13:16:02 +0100 | [diff] [blame] | 4712 | dev_priv->gt.last_init_time = ktime_get(); |
| 4713 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4714 | /* Double layer security blanket, see i915_gem_init() */ |
| 4715 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 4716 | |
Tvrtko Ursulin | 0031fb9 | 2016-11-04 14:42:44 +0000 | [diff] [blame] | 4717 | if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4718 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4719 | |
Tvrtko Ursulin | 772c2a5 | 2016-10-13 11:03:01 +0100 | [diff] [blame] | 4720 | if (IS_HASWELL(dev_priv)) |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4721 | I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ? |
Ville Syrjälä | 0bf2134 | 2013-11-29 14:56:12 +0200 | [diff] [blame] | 4722 | LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED); |
Rodrigo Vivi | 9435373 | 2013-08-28 16:45:46 -0300 | [diff] [blame] | 4723 | |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4724 | /* Apply the GT workarounds... */ |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 4725 | intel_gt_apply_workarounds(dev_priv); |
Tvrtko Ursulin | 094304b | 2018-12-03 12:50:10 +0000 | [diff] [blame] | 4726 | /* ...and determine whether they are sticking. */ |
| 4727 | intel_gt_verify_workarounds(dev_priv, "init"); |
Oscar Mateo | 59b449d | 2018-04-10 09:12:47 -0700 | [diff] [blame] | 4728 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4729 | i915_gem_init_swizzling(dev_priv); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4730 | |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4731 | /* |
| 4732 | * At least 830 can leave some of the unused rings |
| 4733 | * "active" (ie. head != tail) after resume which |
| 4734 | * will prevent c3 entry. Makes sure all unused rings |
| 4735 | * are totally idle. |
| 4736 | */ |
Tvrtko Ursulin | 50a0bc9 | 2016-10-13 11:02:58 +0100 | [diff] [blame] | 4737 | init_unused_rings(dev_priv); |
Daniel Vetter | d5abdfd | 2014-11-20 09:45:19 +0100 | [diff] [blame] | 4738 | |
Dave Gordon | ed54c1a | 2016-01-19 19:02:54 +0000 | [diff] [blame] | 4739 | BUG_ON(!dev_priv->kernel_context); |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 4740 | ret = i915_terminally_wedged(dev_priv); |
| 4741 | if (ret) |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 4742 | goto out; |
John Harrison | 90638cc | 2015-05-29 17:43:37 +0100 | [diff] [blame] | 4743 | |
Tvrtko Ursulin | c6be607 | 2016-11-16 08:55:31 +0000 | [diff] [blame] | 4744 | ret = i915_ppgtt_init_hw(dev_priv); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4745 | if (ret) { |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4746 | DRM_ERROR("Enabling PPGTT failed (%d)\n", ret); |
John Harrison | 4ad2fd8 | 2015-06-18 13:11:20 +0100 | [diff] [blame] | 4747 | goto out; |
| 4748 | } |
| 4749 | |
Jackie Li | f08e203 | 2018-03-13 17:32:53 -0700 | [diff] [blame] | 4750 | ret = intel_wopcm_init_hw(&dev_priv->wopcm); |
| 4751 | if (ret) { |
| 4752 | DRM_ERROR("Enabling WOPCM failed (%d)\n", ret); |
| 4753 | goto out; |
| 4754 | } |
| 4755 | |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4756 | /* We can't enable contexts until all firmware is loaded */ |
| 4757 | ret = intel_uc_init_hw(dev_priv); |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4758 | if (ret) { |
| 4759 | DRM_ERROR("Enabling uc failed (%d)\n", ret); |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4760 | goto out; |
Chris Wilson | 8177e11 | 2018-02-07 11:15:45 +0000 | [diff] [blame] | 4761 | } |
Michał Winiarski | 9bdc357 | 2017-10-25 18:25:19 +0100 | [diff] [blame] | 4762 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4763 | intel_mocs_init_l3cc_table(dev_priv); |
Peter Antoine | 0ccdacf | 2016-04-13 15:03:25 +0100 | [diff] [blame] | 4764 | |
Chris Wilson | 136109c | 2017-11-02 13:14:30 +0000 | [diff] [blame] | 4765 | /* Only when the HW is re-initialised, can we replay the requests */ |
| 4766 | ret = __i915_gem_restart_engines(dev_priv); |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4767 | if (ret) |
| 4768 | goto cleanup_uc; |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4769 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4770 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4771 | |
| 4772 | return 0; |
Michal Wajdeczko | b96f6eb | 2018-06-05 12:24:43 +0000 | [diff] [blame] | 4773 | |
| 4774 | cleanup_uc: |
| 4775 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 60c0a66 | 2018-07-12 14:48:10 +0200 | [diff] [blame] | 4776 | out: |
| 4777 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 4778 | |
| 4779 | return ret; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4780 | } |
| 4781 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4782 | static int __intel_engines_record_defaults(struct drm_i915_private *i915) |
| 4783 | { |
| 4784 | struct i915_gem_context *ctx; |
| 4785 | struct intel_engine_cs *engine; |
| 4786 | enum intel_engine_id id; |
| 4787 | int err; |
| 4788 | |
| 4789 | /* |
| 4790 | * As we reset the gpu during very early sanitisation, the current |
| 4791 | * register state on the GPU should reflect its defaults values. |
| 4792 | * We load a context onto the hw (with restore-inhibit), then switch |
| 4793 | * over to a second context to save that default register state. We |
| 4794 | * can then prime every new context with that state so they all start |
| 4795 | * from the same default HW values. |
| 4796 | */ |
| 4797 | |
| 4798 | ctx = i915_gem_context_create_kernel(i915, 0); |
| 4799 | if (IS_ERR(ctx)) |
| 4800 | return PTR_ERR(ctx); |
| 4801 | |
| 4802 | for_each_engine(engine, i915, id) { |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4803 | struct i915_request *rq; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4804 | |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 4805 | rq = i915_request_alloc(engine, ctx); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4806 | if (IS_ERR(rq)) { |
| 4807 | err = PTR_ERR(rq); |
| 4808 | goto out_ctx; |
| 4809 | } |
| 4810 | |
Chris Wilson | 3fef5cd | 2017-11-20 10:20:02 +0000 | [diff] [blame] | 4811 | err = 0; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4812 | if (engine->init_context) |
| 4813 | err = engine->init_context(rq); |
| 4814 | |
Chris Wilson | 697b9a8 | 2018-06-12 11:51:35 +0100 | [diff] [blame] | 4815 | i915_request_add(rq); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4816 | if (err) |
| 4817 | goto err_active; |
| 4818 | } |
| 4819 | |
| 4820 | err = i915_gem_switch_to_kernel_context(i915); |
| 4821 | if (err) |
| 4822 | goto err_active; |
| 4823 | |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4824 | if (i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED, HZ / 5)) { |
| 4825 | i915_gem_set_wedged(i915); |
| 4826 | err = -EIO; /* Caller will declare us wedged */ |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4827 | goto err_active; |
Chris Wilson | 2621cef | 2018-07-09 13:20:43 +0100 | [diff] [blame] | 4828 | } |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4829 | |
| 4830 | assert_kernel_context_is_current(i915); |
| 4831 | |
Chris Wilson | 8e1cb32 | 2018-09-20 17:13:43 +0100 | [diff] [blame] | 4832 | /* |
| 4833 | * Immediately park the GPU so that we enable powersaving and |
| 4834 | * treat it as idle. The next time we issue a request, we will |
| 4835 | * unpark and start using the engine->pinned_default_state, otherwise |
| 4836 | * it is in limbo and an early reset may fail. |
| 4837 | */ |
| 4838 | __i915_gem_park(i915); |
| 4839 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4840 | for_each_engine(engine, i915, id) { |
| 4841 | struct i915_vma *state; |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4842 | void *vaddr; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4843 | |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4844 | GEM_BUG_ON(to_intel_context(ctx, engine)->pin_count); |
| 4845 | |
Chris Wilson | ab82a06 | 2018-04-30 14:15:01 +0100 | [diff] [blame] | 4846 | state = to_intel_context(ctx, engine)->state; |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4847 | if (!state) |
| 4848 | continue; |
| 4849 | |
| 4850 | /* |
| 4851 | * As we will hold a reference to the logical state, it will |
| 4852 | * not be torn down with the context, and importantly the |
| 4853 | * object will hold onto its vma (making it possible for a |
| 4854 | * stray GTT write to corrupt our defaults). Unmap the vma |
| 4855 | * from the GTT to prevent such accidents and reclaim the |
| 4856 | * space. |
| 4857 | */ |
| 4858 | err = i915_vma_unbind(state); |
| 4859 | if (err) |
| 4860 | goto err_active; |
| 4861 | |
| 4862 | err = i915_gem_object_set_to_cpu_domain(state->obj, false); |
| 4863 | if (err) |
| 4864 | goto err_active; |
| 4865 | |
| 4866 | engine->default_state = i915_gem_object_get(state->obj); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4867 | |
| 4868 | /* Check we can acquire the image of the context state */ |
| 4869 | vaddr = i915_gem_object_pin_map(engine->default_state, |
Chris Wilson | 666424a | 2018-09-14 13:35:04 +0100 | [diff] [blame] | 4870 | I915_MAP_FORCE_WB); |
Chris Wilson | 37d7c9c | 2018-09-14 13:35:03 +0100 | [diff] [blame] | 4871 | if (IS_ERR(vaddr)) { |
| 4872 | err = PTR_ERR(vaddr); |
| 4873 | goto err_active; |
| 4874 | } |
| 4875 | |
| 4876 | i915_gem_object_unpin_map(engine->default_state); |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) { |
| 4880 | unsigned int found = intel_engines_has_context_isolation(i915); |
| 4881 | |
| 4882 | /* |
| 4883 | * Make sure that classes with multiple engine instances all |
| 4884 | * share the same basic configuration. |
| 4885 | */ |
| 4886 | for_each_engine(engine, i915, id) { |
| 4887 | unsigned int bit = BIT(engine->uabi_class); |
| 4888 | unsigned int expected = engine->default_state ? bit : 0; |
| 4889 | |
| 4890 | if ((found & bit) != expected) { |
| 4891 | DRM_ERROR("mismatching default context state for class %d on engine %s\n", |
| 4892 | engine->uabi_class, engine->name); |
| 4893 | } |
| 4894 | } |
| 4895 | } |
| 4896 | |
| 4897 | out_ctx: |
| 4898 | i915_gem_context_set_closed(ctx); |
| 4899 | i915_gem_context_put(ctx); |
| 4900 | return err; |
| 4901 | |
| 4902 | err_active: |
| 4903 | /* |
| 4904 | * If we have to abandon now, we expect the engines to be idle |
| 4905 | * and ready to be torn-down. First try to flush any remaining |
| 4906 | * request, ensure we are pointing at the kernel context and |
| 4907 | * then remove it. |
| 4908 | */ |
| 4909 | if (WARN_ON(i915_gem_switch_to_kernel_context(i915))) |
| 4910 | goto out_ctx; |
| 4911 | |
Chris Wilson | ec625fb | 2018-07-09 13:20:42 +0100 | [diff] [blame] | 4912 | if (WARN_ON(i915_gem_wait_for_idle(i915, |
| 4913 | I915_WAIT_LOCKED, |
| 4914 | MAX_SCHEDULE_TIMEOUT))) |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 4915 | goto out_ctx; |
| 4916 | |
| 4917 | i915_gem_contexts_lost(i915); |
| 4918 | goto out_ctx; |
| 4919 | } |
| 4920 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 4921 | static int |
| 4922 | i915_gem_init_scratch(struct drm_i915_private *i915, unsigned int size) |
| 4923 | { |
| 4924 | struct drm_i915_gem_object *obj; |
| 4925 | struct i915_vma *vma; |
| 4926 | int ret; |
| 4927 | |
| 4928 | obj = i915_gem_object_create_stolen(i915, size); |
| 4929 | if (!obj) |
| 4930 | obj = i915_gem_object_create_internal(i915, size); |
| 4931 | if (IS_ERR(obj)) { |
| 4932 | DRM_ERROR("Failed to allocate scratch page\n"); |
| 4933 | return PTR_ERR(obj); |
| 4934 | } |
| 4935 | |
| 4936 | vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL); |
| 4937 | if (IS_ERR(vma)) { |
| 4938 | ret = PTR_ERR(vma); |
| 4939 | goto err_unref; |
| 4940 | } |
| 4941 | |
| 4942 | ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH); |
| 4943 | if (ret) |
| 4944 | goto err_unref; |
| 4945 | |
| 4946 | i915->gt.scratch = vma; |
| 4947 | return 0; |
| 4948 | |
| 4949 | err_unref: |
| 4950 | i915_gem_object_put(obj); |
| 4951 | return ret; |
| 4952 | } |
| 4953 | |
| 4954 | static void i915_gem_fini_scratch(struct drm_i915_private *i915) |
| 4955 | { |
| 4956 | i915_vma_unpin_and_release(&i915->gt.scratch, 0); |
| 4957 | } |
| 4958 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 4959 | int i915_gem_init(struct drm_i915_private *dev_priv) |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4960 | { |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4961 | int ret; |
| 4962 | |
Changbin Du | 52b2416 | 2018-05-08 17:07:05 +0800 | [diff] [blame] | 4963 | /* We need to fallback to 4K pages if host doesn't support huge gtt. */ |
| 4964 | if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv)) |
Matthew Auld | da9fe3f3 | 2017-10-06 23:18:31 +0100 | [diff] [blame] | 4965 | mkwrite_device_info(dev_priv)->page_sizes = |
| 4966 | I915_GTT_PAGE_SIZE_4K; |
| 4967 | |
Chris Wilson | 9431282 | 2017-05-03 10:39:18 +0100 | [diff] [blame] | 4968 | dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1); |
Chris Wilson | 57822dc | 2017-02-22 11:40:48 +0000 | [diff] [blame] | 4969 | |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4970 | if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) { |
Chris Wilson | 821ed7d | 2016-09-09 14:11:53 +0100 | [diff] [blame] | 4971 | dev_priv->gt.resume = intel_lr_context_resume; |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 4972 | dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup; |
Chris Wilson | fb5c551 | 2017-11-20 20:55:00 +0000 | [diff] [blame] | 4973 | } else { |
| 4974 | dev_priv->gt.resume = intel_legacy_submission_resume; |
| 4975 | dev_priv->gt.cleanup_engine = intel_engine_cleanup; |
Oscar Mateo | a83014d | 2014-07-24 17:04:21 +0100 | [diff] [blame] | 4976 | } |
| 4977 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 4978 | i915_timelines_init(dev_priv); |
| 4979 | |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4980 | ret = i915_gem_init_userptr(dev_priv); |
| 4981 | if (ret) |
| 4982 | return ret; |
| 4983 | |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 4984 | ret = intel_uc_init_misc(dev_priv); |
Michał Winiarski | 3176ff4 | 2017-12-13 23:13:47 +0100 | [diff] [blame] | 4985 | if (ret) |
| 4986 | return ret; |
| 4987 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 4988 | ret = intel_wopcm_init(&dev_priv->wopcm); |
| 4989 | if (ret) |
| 4990 | goto err_uc_misc; |
| 4991 | |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4992 | /* This is just a security blanket to placate dragons. |
| 4993 | * On some systems, we very sporadically observe that the first TLBs |
| 4994 | * used by the CS may be stale, despite us poking the TLB reset. If |
| 4995 | * we hold the forcewake during initialisation these problems |
| 4996 | * just magically go away. |
| 4997 | */ |
Chris Wilson | ee48700 | 2017-11-22 17:26:21 +0000 | [diff] [blame] | 4998 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 5e4f518 | 2015-02-13 14:35:59 +0000 | [diff] [blame] | 4999 | intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL); |
| 5000 | |
Chris Wilson | f6b9d5c | 2016-08-04 07:52:23 +0100 | [diff] [blame] | 5001 | ret = i915_gem_init_ggtt(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5002 | if (ret) { |
| 5003 | GEM_BUG_ON(ret == -EIO); |
| 5004 | goto err_unlock; |
| 5005 | } |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 5006 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5007 | ret = i915_gem_init_scratch(dev_priv, |
Lucas De Marchi | cf819ef | 2018-12-12 10:10:43 -0800 | [diff] [blame] | 5008 | IS_GEN(dev_priv, 2) ? SZ_256K : PAGE_SIZE); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5009 | if (ret) { |
| 5010 | GEM_BUG_ON(ret == -EIO); |
| 5011 | goto err_ggtt; |
| 5012 | } |
Ben Widawsky | 2fa48d8 | 2013-12-06 14:11:04 -0800 | [diff] [blame] | 5013 | |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5014 | ret = i915_gem_contexts_init(dev_priv); |
| 5015 | if (ret) { |
| 5016 | GEM_BUG_ON(ret == -EIO); |
| 5017 | goto err_scratch; |
| 5018 | } |
| 5019 | |
Tvrtko Ursulin | bf9e842 | 2016-12-01 14:16:38 +0000 | [diff] [blame] | 5020 | ret = intel_engines_init(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5021 | if (ret) { |
| 5022 | GEM_BUG_ON(ret == -EIO); |
| 5023 | goto err_context; |
| 5024 | } |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 5025 | |
Chris Wilson | f58d13d | 2017-11-10 14:26:29 +0000 | [diff] [blame] | 5026 | intel_init_gt_powersave(dev_priv); |
| 5027 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5028 | ret = intel_uc_init(dev_priv); |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5029 | if (ret) |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5030 | goto err_pm; |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5031 | |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5032 | ret = i915_gem_init_hw(dev_priv); |
| 5033 | if (ret) |
| 5034 | goto err_uc_init; |
| 5035 | |
Chris Wilson | cc6a818 | 2017-11-10 14:26:30 +0000 | [diff] [blame] | 5036 | /* |
| 5037 | * Despite its name intel_init_clock_gating applies both display |
| 5038 | * clock gating workarounds; GT mmio workarounds and the occasional |
| 5039 | * GT power context workaround. Worse, sometimes it includes a context |
| 5040 | * register workaround which we need to apply before we record the |
| 5041 | * default HW state for all contexts. |
| 5042 | * |
| 5043 | * FIXME: break up the workarounds and apply them at the right time! |
| 5044 | */ |
| 5045 | intel_init_clock_gating(dev_priv); |
| 5046 | |
Chris Wilson | d2b4b97 | 2017-11-10 14:26:33 +0000 | [diff] [blame] | 5047 | ret = __intel_engines_record_defaults(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5048 | if (ret) |
| 5049 | goto err_init_hw; |
| 5050 | |
| 5051 | if (i915_inject_load_failure()) { |
| 5052 | ret = -ENODEV; |
| 5053 | goto err_init_hw; |
| 5054 | } |
| 5055 | |
| 5056 | if (i915_inject_load_failure()) { |
| 5057 | ret = -EIO; |
| 5058 | goto err_init_hw; |
| 5059 | } |
| 5060 | |
| 5061 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5062 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5063 | |
| 5064 | return 0; |
| 5065 | |
| 5066 | /* |
| 5067 | * Unwinding is complicated by that we want to handle -EIO to mean |
| 5068 | * disable GPU submission but keep KMS alive. We want to mark the |
| 5069 | * HW as irrevisibly wedged, but keep enough state around that the |
| 5070 | * driver doesn't explode during runtime. |
| 5071 | */ |
| 5072 | err_init_hw: |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5073 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5074 | |
| 5075 | WARN_ON(i915_gem_suspend(dev_priv)); |
| 5076 | i915_gem_suspend_late(dev_priv); |
| 5077 | |
Chris Wilson | 8bcf9f7 | 2018-07-10 10:44:20 +0100 | [diff] [blame] | 5078 | i915_gem_drain_workqueue(dev_priv); |
| 5079 | |
Chris Wilson | 8571a05 | 2018-06-06 15:54:41 +0100 | [diff] [blame] | 5080 | mutex_lock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5081 | intel_uc_fini_hw(dev_priv); |
Michał Winiarski | 61b5c15 | 2017-12-13 23:13:48 +0100 | [diff] [blame] | 5082 | err_uc_init: |
| 5083 | intel_uc_fini(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5084 | err_pm: |
| 5085 | if (ret != -EIO) { |
| 5086 | intel_cleanup_gt_powersave(dev_priv); |
| 5087 | i915_gem_cleanup_engines(dev_priv); |
| 5088 | } |
| 5089 | err_context: |
| 5090 | if (ret != -EIO) |
| 5091 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5092 | err_scratch: |
| 5093 | i915_gem_fini_scratch(dev_priv); |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5094 | err_ggtt: |
| 5095 | err_unlock: |
| 5096 | intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL); |
| 5097 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5098 | |
Michal Wajdeczko | f7dc015 | 2018-06-28 14:15:21 +0000 | [diff] [blame] | 5099 | err_uc_misc: |
Sagar Arun Kamble | 70deead | 2018-01-24 21:16:58 +0530 | [diff] [blame] | 5100 | intel_uc_fini_misc(dev_priv); |
Sagar Arun Kamble | da943b5 | 2018-01-10 18:24:16 +0530 | [diff] [blame] | 5101 | |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5102 | if (ret != -EIO) { |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5103 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5104 | i915_timelines_fini(dev_priv); |
| 5105 | } |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5106 | |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5107 | if (ret == -EIO) { |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5108 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5109 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5110 | /* |
| 5111 | * Allow engine initialisation to fail by marking the GPU as |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5112 | * wedged. But we only want to do this where the GPU is angry, |
| 5113 | * for all other failure, such as an allocation failure, bail. |
| 5114 | */ |
Chris Wilson | c41166f | 2019-02-20 14:56:37 +0000 | [diff] [blame] | 5115 | if (!i915_reset_failed(dev_priv)) { |
Chris Wilson | 51c18bf | 2018-06-09 12:10:58 +0100 | [diff] [blame] | 5116 | i915_load_error(dev_priv, |
| 5117 | "Failed to initialize GPU, declaring it wedged!\n"); |
Chris Wilson | 6f74b36 | 2017-10-15 15:37:25 +0100 | [diff] [blame] | 5118 | i915_gem_set_wedged(dev_priv); |
| 5119 | } |
Chris Wilson | 7ed43df | 2018-07-26 09:50:32 +0100 | [diff] [blame] | 5120 | |
| 5121 | /* Minimal basic recovery for KMS */ |
| 5122 | ret = i915_ggtt_enable_hw(dev_priv); |
| 5123 | i915_gem_restore_gtt_mappings(dev_priv); |
| 5124 | i915_gem_restore_fences(dev_priv); |
| 5125 | intel_init_clock_gating(dev_priv); |
| 5126 | |
| 5127 | mutex_unlock(&dev_priv->drm.struct_mutex); |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5128 | } |
| 5129 | |
Chris Wilson | 6ca9a2b | 2017-12-13 13:43:47 +0000 | [diff] [blame] | 5130 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | 6099032 | 2014-04-09 09:19:42 +0100 | [diff] [blame] | 5131 | return ret; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 5132 | } |
| 5133 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5134 | void i915_gem_fini(struct drm_i915_private *dev_priv) |
| 5135 | { |
| 5136 | i915_gem_suspend_late(dev_priv); |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5137 | intel_disable_gt_powersave(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5138 | |
| 5139 | /* Flush any outstanding unpin_work. */ |
| 5140 | i915_gem_drain_workqueue(dev_priv); |
| 5141 | |
| 5142 | mutex_lock(&dev_priv->drm.struct_mutex); |
| 5143 | intel_uc_fini_hw(dev_priv); |
| 5144 | intel_uc_fini(dev_priv); |
| 5145 | i915_gem_cleanup_engines(dev_priv); |
| 5146 | i915_gem_contexts_fini(dev_priv); |
Chris Wilson | 5179749 | 2018-12-04 14:15:16 +0000 | [diff] [blame] | 5147 | i915_gem_fini_scratch(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5148 | mutex_unlock(&dev_priv->drm.struct_mutex); |
| 5149 | |
Tvrtko Ursulin | 25d140f | 2018-12-03 13:33:19 +0000 | [diff] [blame] | 5150 | intel_wa_list_free(&dev_priv->gt_wa_list); |
| 5151 | |
Chris Wilson | 30b71084 | 2018-08-12 23:36:29 +0100 | [diff] [blame] | 5152 | intel_cleanup_gt_powersave(dev_priv); |
| 5153 | |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5154 | intel_uc_fini_misc(dev_priv); |
| 5155 | i915_gem_cleanup_userptr(dev_priv); |
Chris Wilson | 1e34556 | 2019-01-28 10:23:56 +0000 | [diff] [blame] | 5156 | i915_timelines_fini(dev_priv); |
Michal Wajdeczko | 8979187a | 2018-06-04 09:00:32 +0000 | [diff] [blame] | 5157 | |
| 5158 | i915_gem_drain_freed_objects(dev_priv); |
| 5159 | |
| 5160 | WARN_ON(!list_empty(&dev_priv->contexts.list)); |
| 5161 | } |
| 5162 | |
Chris Wilson | 2414551 | 2017-01-24 11:01:35 +0000 | [diff] [blame] | 5163 | void i915_gem_init_mmio(struct drm_i915_private *i915) |
| 5164 | { |
| 5165 | i915_gem_sanitize(i915); |
| 5166 | } |
| 5167 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5168 | void |
Tvrtko Ursulin | cb15d9f | 2016-12-01 14:16:39 +0000 | [diff] [blame] | 5169 | i915_gem_cleanup_engines(struct drm_i915_private *dev_priv) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5170 | { |
Tvrtko Ursulin | e2f8039 | 2016-03-16 11:00:36 +0000 | [diff] [blame] | 5171 | struct intel_engine_cs *engine; |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5172 | enum intel_engine_id id; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5173 | |
Akash Goel | 3b3f165 | 2016-10-13 22:44:48 +0530 | [diff] [blame] | 5174 | for_each_engine(engine, dev_priv, id) |
Tvrtko Ursulin | 117897f | 2016-03-16 11:00:40 +0000 | [diff] [blame] | 5175 | dev_priv->gt.cleanup_engine(engine); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 5176 | } |
| 5177 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5178 | void |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5179 | i915_gem_load_init_fences(struct drm_i915_private *dev_priv) |
| 5180 | { |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5181 | int i; |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5182 | |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5183 | if (INTEL_GEN(dev_priv) >= 7 && !IS_VALLEYVIEW(dev_priv) && |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5184 | !IS_CHERRYVIEW(dev_priv)) |
| 5185 | dev_priv->num_fence_regs = 32; |
Tvrtko Ursulin | c56b89f | 2018-02-09 21:58:46 +0000 | [diff] [blame] | 5186 | else if (INTEL_GEN(dev_priv) >= 4 || |
Jani Nikula | 73f67aa | 2016-12-07 22:48:09 +0200 | [diff] [blame] | 5187 | IS_I945G(dev_priv) || IS_I945GM(dev_priv) || |
| 5188 | IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5189 | dev_priv->num_fence_regs = 16; |
| 5190 | else |
| 5191 | dev_priv->num_fence_regs = 8; |
| 5192 | |
Chris Wilson | c033666 | 2016-05-06 15:40:21 +0100 | [diff] [blame] | 5193 | if (intel_vgpu_active(dev_priv)) |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5194 | dev_priv->num_fence_regs = |
| 5195 | I915_READ(vgtif_reg(avail_rs.fence_num)); |
| 5196 | |
| 5197 | /* Initialize fence registers to zero */ |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 5198 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
| 5199 | struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i]; |
| 5200 | |
| 5201 | fence->i915 = dev_priv; |
| 5202 | fence->id = i; |
| 5203 | list_add_tail(&fence->link, &dev_priv->mm.fence_list); |
| 5204 | } |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5205 | i915_gem_restore_fences(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5206 | |
Tvrtko Ursulin | 4362f4f | 2016-11-16 08:55:33 +0000 | [diff] [blame] | 5207 | i915_gem_detect_bit_6_swizzle(dev_priv); |
Imre Deak | 40ae4e1 | 2016-03-16 14:54:03 +0200 | [diff] [blame] | 5208 | } |
| 5209 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5210 | static void i915_gem_init__mm(struct drm_i915_private *i915) |
| 5211 | { |
| 5212 | spin_lock_init(&i915->mm.object_stat_lock); |
| 5213 | spin_lock_init(&i915->mm.obj_lock); |
| 5214 | spin_lock_init(&i915->mm.free_lock); |
| 5215 | |
| 5216 | init_llist_head(&i915->mm.free_list); |
| 5217 | |
| 5218 | INIT_LIST_HEAD(&i915->mm.unbound_list); |
| 5219 | INIT_LIST_HEAD(&i915->mm.bound_list); |
| 5220 | INIT_LIST_HEAD(&i915->mm.fence_list); |
| 5221 | INIT_LIST_HEAD(&i915->mm.userfault_list); |
| 5222 | |
| 5223 | INIT_WORK(&i915->mm.free_work, __i915_gem_free_work); |
| 5224 | } |
| 5225 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5226 | int i915_gem_init_early(struct drm_i915_private *dev_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5227 | { |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5228 | int err = -ENOMEM; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 5229 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5230 | dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN); |
| 5231 | if (!dev_priv->objects) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5232 | goto err_out; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5233 | |
Tvrtko Ursulin | a933568 | 2016-11-02 15:14:59 +0000 | [diff] [blame] | 5234 | dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN); |
| 5235 | if (!dev_priv->vmas) |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5236 | goto err_objects; |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5237 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5238 | dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0); |
| 5239 | if (!dev_priv->luts) |
| 5240 | goto err_vmas; |
| 5241 | |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5242 | INIT_LIST_HEAD(&dev_priv->gt.active_rings); |
Chris Wilson | 3365e22 | 2018-05-03 20:51:14 +0100 | [diff] [blame] | 5243 | INIT_LIST_HEAD(&dev_priv->gt.closed_vma); |
Chris Wilson | 643b450 | 2018-04-30 14:15:03 +0100 | [diff] [blame] | 5244 | |
Chris Wilson | 9c52d1c | 2017-11-10 23:24:47 +0000 | [diff] [blame] | 5245 | i915_gem_init__mm(dev_priv); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5246 | |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5247 | INIT_DELAYED_WORK(&dev_priv->gt.retire_work, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5248 | i915_gem_retire_work_handler); |
Chris Wilson | 67d97da | 2016-07-04 08:08:31 +0100 | [diff] [blame] | 5249 | INIT_DELAYED_WORK(&dev_priv->gt.idle_work, |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5250 | i915_gem_idle_work_handler); |
Chris Wilson | 1f15b76 | 2016-07-01 17:23:14 +0100 | [diff] [blame] | 5251 | init_waitqueue_head(&dev_priv->gpu_error.wait_queue); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 5252 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 18bb2bc | 2019-01-14 21:04:01 +0000 | [diff] [blame] | 5253 | mutex_init(&dev_priv->gpu_error.wedge_mutex); |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 5254 | init_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5255 | |
Joonas Lahtinen | 6f63340 | 2016-09-01 14:58:21 +0300 | [diff] [blame] | 5256 | atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0); |
| 5257 | |
Chris Wilson | b5add95 | 2016-08-04 16:32:36 +0100 | [diff] [blame] | 5258 | spin_lock_init(&dev_priv->fb_tracking.lock); |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5259 | |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5260 | err = i915_gemfs_init(dev_priv); |
| 5261 | if (err) |
| 5262 | DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err); |
| 5263 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5264 | return 0; |
| 5265 | |
Chris Wilson | 73cb970 | 2016-10-28 13:58:46 +0100 | [diff] [blame] | 5266 | err_vmas: |
| 5267 | kmem_cache_destroy(dev_priv->vmas); |
| 5268 | err_objects: |
| 5269 | kmem_cache_destroy(dev_priv->objects); |
| 5270 | err_out: |
| 5271 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 5272 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5273 | |
Michal Wajdeczko | a0de908 | 2018-03-23 12:34:49 +0000 | [diff] [blame] | 5274 | void i915_gem_cleanup_early(struct drm_i915_private *dev_priv) |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5275 | { |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5276 | i915_gem_drain_freed_objects(dev_priv); |
Chris Wilson | c9c70471 | 2018-02-19 22:06:31 +0000 | [diff] [blame] | 5277 | GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list)); |
| 5278 | GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count)); |
Chris Wilson | c4d4c1c | 2017-02-10 16:35:23 +0000 | [diff] [blame] | 5279 | WARN_ON(dev_priv->mm.object_count); |
Matthew Auld | ea84aa7 | 2016-11-17 21:04:11 +0000 | [diff] [blame] | 5280 | |
Chris Wilson | 2caffbf | 2019-02-08 15:37:03 +0000 | [diff] [blame] | 5281 | cleanup_srcu_struct(&dev_priv->gpu_error.reset_backoff_srcu); |
| 5282 | |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 5283 | kmem_cache_destroy(dev_priv->luts); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5284 | kmem_cache_destroy(dev_priv->vmas); |
| 5285 | kmem_cache_destroy(dev_priv->objects); |
Chris Wilson | 0eafec6 | 2016-08-04 16:32:41 +0100 | [diff] [blame] | 5286 | |
| 5287 | /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */ |
| 5288 | rcu_barrier(); |
Matthew Auld | 465c403 | 2017-10-06 23:18:14 +0100 | [diff] [blame] | 5289 | |
| 5290 | i915_gemfs_fini(dev_priv); |
Imre Deak | d64aa09 | 2016-01-19 15:26:29 +0200 | [diff] [blame] | 5291 | } |
| 5292 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5293 | int i915_gem_freeze(struct drm_i915_private *dev_priv) |
| 5294 | { |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5295 | /* Discard all purgeable objects, let userspace recover those as |
| 5296 | * required after resuming. |
| 5297 | */ |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5298 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5299 | |
Chris Wilson | 6a800ea | 2016-09-21 14:51:07 +0100 | [diff] [blame] | 5300 | return 0; |
| 5301 | } |
| 5302 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5303 | int i915_gem_freeze_late(struct drm_i915_private *i915) |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5304 | { |
| 5305 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5306 | struct list_head *phases[] = { |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5307 | &i915->mm.unbound_list, |
| 5308 | &i915->mm.bound_list, |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5309 | NULL |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5310 | }, **phase; |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5311 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5312 | /* |
| 5313 | * Called just before we write the hibernation image. |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5314 | * |
| 5315 | * We need to update the domain tracking to reflect that the CPU |
| 5316 | * will be accessing all the pages to create and restore from the |
| 5317 | * hibernation, and so upon restoration those pages will be in the |
| 5318 | * CPU domain. |
| 5319 | * |
| 5320 | * To make sure the hibernation image contains the latest state, |
| 5321 | * we update that state just before writing out the image. |
Chris Wilson | 7aab2d5 | 2016-09-09 20:02:18 +0100 | [diff] [blame] | 5322 | * |
| 5323 | * To try and reduce the hibernation image, we manually shrink |
Chris Wilson | d0aa301 | 2017-04-07 11:25:49 +0100 | [diff] [blame] | 5324 | * the objects as well, see i915_gem_freeze() |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5325 | */ |
| 5326 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5327 | i915_gem_shrink(i915, -1UL, NULL, I915_SHRINK_UNBOUND); |
| 5328 | i915_gem_drain_freed_objects(i915); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5329 | |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5330 | mutex_lock(&i915->drm.struct_mutex); |
| 5331 | for (phase = phases; *phase; phase++) { |
| 5332 | list_for_each_entry(obj, *phase, mm.link) |
| 5333 | WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true)); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5334 | } |
Chris Wilson | 95c778d | 2018-06-01 15:41:25 +0100 | [diff] [blame] | 5335 | mutex_unlock(&i915->drm.struct_mutex); |
Chris Wilson | 461fb99 | 2016-05-14 07:26:33 +0100 | [diff] [blame] | 5336 | |
| 5337 | return 0; |
| 5338 | } |
| 5339 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5340 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5341 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5342 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Chris Wilson | e61e0f5 | 2018-02-21 09:56:36 +0000 | [diff] [blame] | 5343 | struct i915_request *request; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5344 | |
| 5345 | /* Clean up our request list when the client is going away, so that |
| 5346 | * later retire_requests won't dereference our soon-to-be-gone |
| 5347 | * file_priv. |
| 5348 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5349 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | c8659ef | 2017-03-02 12:25:25 +0000 | [diff] [blame] | 5350 | list_for_each_entry(request, &file_priv->mm.request_list, client_link) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5351 | request->file_priv = NULL; |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5352 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5353 | } |
| 5354 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5355 | 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] | 5356 | { |
| 5357 | struct drm_i915_file_private *file_priv; |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5358 | int ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5359 | |
Chris Wilson | c4c29d7 | 2016-11-09 10:45:07 +0000 | [diff] [blame] | 5360 | DRM_DEBUG("\n"); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5361 | |
| 5362 | file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); |
| 5363 | if (!file_priv) |
| 5364 | return -ENOMEM; |
| 5365 | |
| 5366 | file->driver_priv = file_priv; |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5367 | file_priv->dev_priv = i915; |
Chris Wilson | ab0e7ff | 2014-02-25 17:11:24 +0200 | [diff] [blame] | 5368 | file_priv->file = file; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5369 | |
| 5370 | spin_lock_init(&file_priv->mm.lock); |
| 5371 | INIT_LIST_HEAD(&file_priv->mm.request_list); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5372 | |
Chris Wilson | c80ff16 | 2016-07-27 09:07:27 +0100 | [diff] [blame] | 5373 | file_priv->bsd_engine = -1; |
Mika Kuoppala | 14921f3 | 2018-06-15 13:44:29 +0300 | [diff] [blame] | 5374 | file_priv->hang_timestamp = jiffies; |
Tvrtko Ursulin | de1add3 | 2016-01-15 15:12:50 +0000 | [diff] [blame] | 5375 | |
Chris Wilson | 829a0af | 2017-06-20 12:05:45 +0100 | [diff] [blame] | 5376 | ret = i915_gem_context_open(i915, file); |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5377 | if (ret) |
| 5378 | kfree(file_priv); |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5379 | |
Ben Widawsky | e422b88 | 2013-12-06 14:10:58 -0800 | [diff] [blame] | 5380 | return ret; |
Chris Wilson | b29c19b | 2013-09-25 17:34:56 +0100 | [diff] [blame] | 5381 | } |
| 5382 | |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5383 | /** |
| 5384 | * i915_gem_track_fb - update frontbuffer tracking |
Geliang Tang | d9072a3 | 2015-09-15 05:58:44 -0700 | [diff] [blame] | 5385 | * @old: current GEM buffer for the frontbuffer slots |
| 5386 | * @new: new GEM buffer for the frontbuffer slots |
| 5387 | * @frontbuffer_bits: bitmask of frontbuffer slots |
Daniel Vetter | b680c37 | 2014-09-19 18:27:27 +0200 | [diff] [blame] | 5388 | * |
| 5389 | * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them |
| 5390 | * from @old and setting them in @new. Both @old and @new can be NULL. |
| 5391 | */ |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5392 | void i915_gem_track_fb(struct drm_i915_gem_object *old, |
| 5393 | struct drm_i915_gem_object *new, |
| 5394 | unsigned frontbuffer_bits) |
| 5395 | { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5396 | /* Control of individual bits within the mask are guarded by |
| 5397 | * the owning plane->mutex, i.e. we can never see concurrent |
| 5398 | * manipulation of individual bits. But since the bitfield as a whole |
| 5399 | * is updated using RMW, we need to use atomics in order to update |
| 5400 | * the bits. |
| 5401 | */ |
| 5402 | BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES > |
Chris Wilson | 74f6e18 | 2018-09-26 11:47:07 +0100 | [diff] [blame] | 5403 | BITS_PER_TYPE(atomic_t)); |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5404 | |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5405 | if (old) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5406 | WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits)); |
| 5407 | atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | if (new) { |
Chris Wilson | faf5bf0 | 2016-08-04 16:32:37 +0100 | [diff] [blame] | 5411 | WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits); |
| 5412 | atomic_or(frontbuffer_bits, &new->frontbuffer_bits); |
Daniel Vetter | a071fa0 | 2014-06-18 23:28:09 +0200 | [diff] [blame] | 5413 | } |
| 5414 | } |
| 5415 | |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5416 | /* Allocate a new GEM object and fill it with the supplied data */ |
| 5417 | struct drm_i915_gem_object * |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5418 | i915_gem_object_create_from_data(struct drm_i915_private *dev_priv, |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5419 | const void *data, size_t size) |
| 5420 | { |
| 5421 | struct drm_i915_gem_object *obj; |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5422 | struct file *file; |
| 5423 | size_t offset; |
| 5424 | int err; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5425 | |
Tvrtko Ursulin | 12d79d7 | 2016-12-01 14:16:37 +0000 | [diff] [blame] | 5426 | obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE)); |
Chris Wilson | fe3db79 | 2016-04-25 13:32:13 +0100 | [diff] [blame] | 5427 | if (IS_ERR(obj)) |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5428 | return obj; |
| 5429 | |
Christian König | c0a51fd | 2018-02-16 13:43:38 +0100 | [diff] [blame] | 5430 | GEM_BUG_ON(obj->write_domain != I915_GEM_DOMAIN_CPU); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5431 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5432 | file = obj->base.filp; |
| 5433 | offset = 0; |
| 5434 | do { |
| 5435 | unsigned int len = min_t(typeof(size), size, PAGE_SIZE); |
| 5436 | struct page *page; |
| 5437 | void *pgdata, *vaddr; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5438 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5439 | err = pagecache_write_begin(file, file->f_mapping, |
| 5440 | offset, len, 0, |
| 5441 | &page, &pgdata); |
| 5442 | if (err < 0) |
| 5443 | goto fail; |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5444 | |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5445 | vaddr = kmap(page); |
| 5446 | memcpy(vaddr, data, len); |
| 5447 | kunmap(page); |
| 5448 | |
| 5449 | err = pagecache_write_end(file, file->f_mapping, |
| 5450 | offset, len, len, |
| 5451 | page, pgdata); |
| 5452 | if (err < 0) |
| 5453 | goto fail; |
| 5454 | |
| 5455 | size -= len; |
| 5456 | data += len; |
| 5457 | offset += len; |
| 5458 | } while (size); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5459 | |
| 5460 | return obj; |
| 5461 | |
| 5462 | fail: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 5463 | i915_gem_object_put(obj); |
Chris Wilson | be062fa | 2017-03-17 19:46:48 +0000 | [diff] [blame] | 5464 | return ERR_PTR(err); |
Dave Gordon | ea70299 | 2015-07-09 19:29:02 +0100 | [diff] [blame] | 5465 | } |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5466 | |
| 5467 | struct scatterlist * |
| 5468 | i915_gem_object_get_sg(struct drm_i915_gem_object *obj, |
| 5469 | unsigned int n, |
| 5470 | unsigned int *offset) |
| 5471 | { |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5472 | struct i915_gem_object_page_iter *iter = &obj->mm.get_page; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5473 | struct scatterlist *sg; |
| 5474 | unsigned int idx, count; |
| 5475 | |
| 5476 | might_sleep(); |
| 5477 | GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5478 | GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj)); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5479 | |
| 5480 | /* As we iterate forward through the sg, we record each entry in a |
| 5481 | * radixtree for quick repeated (backwards) lookups. If we have seen |
| 5482 | * this index previously, we will have an entry for it. |
| 5483 | * |
| 5484 | * Initial lookup is O(N), but this is amortized to O(1) for |
| 5485 | * sequential page access (where each new request is consecutive |
| 5486 | * to the previous one). Repeated lookups are O(lg(obj->base.size)), |
| 5487 | * i.e. O(1) with a large constant! |
| 5488 | */ |
| 5489 | if (n < READ_ONCE(iter->sg_idx)) |
| 5490 | goto lookup; |
| 5491 | |
| 5492 | mutex_lock(&iter->lock); |
| 5493 | |
| 5494 | /* We prefer to reuse the last sg so that repeated lookup of this |
| 5495 | * (or the subsequent) sg are fast - comparing against the last |
| 5496 | * sg is faster than going through the radixtree. |
| 5497 | */ |
| 5498 | |
| 5499 | sg = iter->sg_pos; |
| 5500 | idx = iter->sg_idx; |
| 5501 | count = __sg_page_count(sg); |
| 5502 | |
| 5503 | while (idx + count <= n) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5504 | void *entry; |
| 5505 | unsigned long i; |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5506 | int ret; |
| 5507 | |
| 5508 | /* If we cannot allocate and insert this entry, or the |
| 5509 | * individual pages from this range, cancel updating the |
| 5510 | * sg_idx so that on this lookup we are forced to linearly |
| 5511 | * scan onwards, but on future lookups we will try the |
| 5512 | * insertion again (in which case we need to be careful of |
| 5513 | * the error return reporting that we have already inserted |
| 5514 | * this index). |
| 5515 | */ |
| 5516 | ret = radix_tree_insert(&iter->radix, idx, sg); |
| 5517 | if (ret && ret != -EEXIST) |
| 5518 | goto scan; |
| 5519 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5520 | entry = xa_mk_value(idx); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5521 | for (i = 1; i < count; i++) { |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5522 | ret = radix_tree_insert(&iter->radix, idx + i, entry); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5523 | if (ret && ret != -EEXIST) |
| 5524 | goto scan; |
| 5525 | } |
| 5526 | |
| 5527 | idx += count; |
| 5528 | sg = ____sg_next(sg); |
| 5529 | count = __sg_page_count(sg); |
| 5530 | } |
| 5531 | |
| 5532 | scan: |
| 5533 | iter->sg_pos = sg; |
| 5534 | iter->sg_idx = idx; |
| 5535 | |
| 5536 | mutex_unlock(&iter->lock); |
| 5537 | |
| 5538 | if (unlikely(n < idx)) /* insertion completed by another thread */ |
| 5539 | goto lookup; |
| 5540 | |
| 5541 | /* In case we failed to insert the entry into the radixtree, we need |
| 5542 | * to look beyond the current sg. |
| 5543 | */ |
| 5544 | while (idx + count <= n) { |
| 5545 | idx += count; |
| 5546 | sg = ____sg_next(sg); |
| 5547 | count = __sg_page_count(sg); |
| 5548 | } |
| 5549 | |
| 5550 | *offset = n - idx; |
| 5551 | return sg; |
| 5552 | |
| 5553 | lookup: |
| 5554 | rcu_read_lock(); |
| 5555 | |
| 5556 | sg = radix_tree_lookup(&iter->radix, n); |
| 5557 | GEM_BUG_ON(!sg); |
| 5558 | |
| 5559 | /* If this index is in the middle of multi-page sg entry, |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5560 | * the radix tree will contain a value entry that points |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5561 | * to the start of that range. We will return the pointer to |
| 5562 | * the base page and the offset of this page within the |
| 5563 | * sg entry's range. |
| 5564 | */ |
| 5565 | *offset = 0; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 5566 | if (unlikely(xa_is_value(sg))) { |
| 5567 | unsigned long base = xa_to_value(sg); |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5568 | |
| 5569 | sg = radix_tree_lookup(&iter->radix, base); |
| 5570 | GEM_BUG_ON(!sg); |
| 5571 | |
| 5572 | *offset = n - base; |
| 5573 | } |
| 5574 | |
| 5575 | rcu_read_unlock(); |
| 5576 | |
| 5577 | return sg; |
| 5578 | } |
| 5579 | |
| 5580 | struct page * |
| 5581 | i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n) |
| 5582 | { |
| 5583 | struct scatterlist *sg; |
| 5584 | unsigned int offset; |
| 5585 | |
| 5586 | GEM_BUG_ON(!i915_gem_object_has_struct_page(obj)); |
| 5587 | |
| 5588 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5589 | return nth_page(sg_page(sg), offset); |
| 5590 | } |
| 5591 | |
| 5592 | /* Like i915_gem_object_get_page(), but mark the returned page dirty */ |
| 5593 | struct page * |
| 5594 | i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, |
| 5595 | unsigned int n) |
| 5596 | { |
| 5597 | struct page *page; |
| 5598 | |
| 5599 | page = i915_gem_object_get_page(obj, n); |
Chris Wilson | a4f5ea6 | 2016-10-28 13:58:35 +0100 | [diff] [blame] | 5600 | if (!obj->mm.dirty) |
Chris Wilson | 96d7763 | 2016-10-28 13:58:33 +0100 | [diff] [blame] | 5601 | set_page_dirty(page); |
| 5602 | |
| 5603 | return page; |
| 5604 | } |
| 5605 | |
| 5606 | dma_addr_t |
| 5607 | i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, |
| 5608 | unsigned long n) |
| 5609 | { |
| 5610 | struct scatterlist *sg; |
| 5611 | unsigned int offset; |
| 5612 | |
| 5613 | sg = i915_gem_object_get_sg(obj, n, &offset); |
| 5614 | return sg_dma_address(sg) + (offset << PAGE_SHIFT); |
| 5615 | } |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5616 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5617 | int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align) |
| 5618 | { |
| 5619 | struct sg_table *pages; |
| 5620 | int err; |
| 5621 | |
| 5622 | if (align > obj->base.size) |
| 5623 | return -EINVAL; |
| 5624 | |
| 5625 | if (obj->ops == &i915_gem_phys_ops) |
| 5626 | return 0; |
| 5627 | |
| 5628 | if (obj->ops != &i915_gem_object_ops) |
| 5629 | return -EINVAL; |
| 5630 | |
| 5631 | err = i915_gem_object_unbind(obj); |
| 5632 | if (err) |
| 5633 | return err; |
| 5634 | |
| 5635 | mutex_lock(&obj->mm.lock); |
| 5636 | |
| 5637 | if (obj->mm.madv != I915_MADV_WILLNEED) { |
| 5638 | err = -EFAULT; |
| 5639 | goto err_unlock; |
| 5640 | } |
| 5641 | |
| 5642 | if (obj->mm.quirked) { |
| 5643 | err = -EFAULT; |
| 5644 | goto err_unlock; |
| 5645 | } |
| 5646 | |
| 5647 | if (obj->mm.mapping) { |
| 5648 | err = -EBUSY; |
| 5649 | goto err_unlock; |
| 5650 | } |
| 5651 | |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5652 | pages = __i915_gem_object_unset_pages(obj); |
Chris Wilson | f212381 | 2017-10-16 12:40:37 +0100 | [diff] [blame] | 5653 | |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5654 | obj->ops = &i915_gem_phys_ops; |
| 5655 | |
Chris Wilson | 8fb6a5d | 2017-07-26 19:16:02 +0100 | [diff] [blame] | 5656 | err = ____i915_gem_object_get_pages(obj); |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5657 | if (err) |
| 5658 | goto err_xfer; |
| 5659 | |
| 5660 | /* Perma-pin (until release) the physical set of pages */ |
| 5661 | __i915_gem_object_pin_pages(obj); |
| 5662 | |
| 5663 | if (!IS_ERR_OR_NULL(pages)) |
| 5664 | i915_gem_object_ops.put_pages(obj, pages); |
| 5665 | mutex_unlock(&obj->mm.lock); |
| 5666 | return 0; |
| 5667 | |
| 5668 | err_xfer: |
| 5669 | obj->ops = &i915_gem_object_ops; |
Chris Wilson | acd1c1e | 2018-06-11 08:55:32 +0100 | [diff] [blame] | 5670 | if (!IS_ERR_OR_NULL(pages)) { |
| 5671 | unsigned int sg_page_sizes = i915_sg_page_sizes(pages->sgl); |
| 5672 | |
| 5673 | __i915_gem_object_set_pages(obj, pages, sg_page_sizes); |
| 5674 | } |
Chris Wilson | 8eeb790 | 2017-07-26 19:16:01 +0100 | [diff] [blame] | 5675 | err_unlock: |
| 5676 | mutex_unlock(&obj->mm.lock); |
| 5677 | return err; |
| 5678 | } |
| 5679 | |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5680 | #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) |
| 5681 | #include "selftests/scatterlist.c" |
Chris Wilson | 66d9cb5 | 2017-02-13 17:15:17 +0000 | [diff] [blame] | 5682 | #include "selftests/mock_gem_device.c" |
Chris Wilson | 4465398 | 2017-02-13 17:15:20 +0000 | [diff] [blame] | 5683 | #include "selftests/huge_gem_object.c" |
Matthew Auld | 4049866 | 2017-10-06 23:18:29 +0100 | [diff] [blame] | 5684 | #include "selftests/huge_pages.c" |
Chris Wilson | 8335fd6 | 2017-02-13 17:15:28 +0000 | [diff] [blame] | 5685 | #include "selftests/i915_gem_object.c" |
Chris Wilson | 1705945 | 2017-02-13 17:15:32 +0000 | [diff] [blame] | 5686 | #include "selftests/i915_gem_coherency.c" |
Chris Wilson | 3f51b7e1 | 2018-08-30 14:48:06 +0100 | [diff] [blame] | 5687 | #include "selftests/i915_gem.c" |
Chris Wilson | 935a2f7 | 2017-02-13 17:15:13 +0000 | [diff] [blame] | 5688 | #endif |